app-cloudep-product-service/pkg/repository/kyc.go

198 lines
5.0 KiB
Go
Raw Permalink Normal View History

2025-03-31 13:24:40 +00:00
package repository
import (
2025-04-06 02:08:46 +00:00
"context"
"errors"
"time"
2025-03-31 13:24:40 +00:00
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/entity"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
mgo "code.30cm.net/digimon/library-go/mongo"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/mon"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
2025-04-04 06:39:01 +00:00
"go.mongodb.org/mongo-driver/mongo"
2025-03-31 13:24:40 +00:00
"go.mongodb.org/mongo-driver/mongo/options"
)
type KYCRepositoryParam struct {
Conf *mgo.Conf
CacheConf cache.CacheConf
DBOpts []mon.Option
CacheOpts []cache.Option
}
type KYCRepository struct {
DB mgo.DocumentDBWithCacheUseCase
}
func NewKYCRepository(param KYCRepositoryParam) repository.KYCRepository {
e := entity.KYC{}
documentDB, err := mgo.MustDocumentDBWithCache(
param.Conf,
e.CollectionName(),
param.CacheConf,
param.DBOpts,
param.CacheOpts,
)
if err != nil {
panic(err)
}
return &KYCRepository{
DB: documentDB,
}
}
func (repo *KYCRepository) Create(ctx context.Context, data *entity.KYC) error {
if data.ID.IsZero() {
now := time.Now().UTC().UnixNano()
data.ID = primitive.NewObjectID()
data.CreatedAt = now
data.UpdatedAt = now
}
_, err := repo.DB.GetClient().InsertOne(ctx, data)
return err
}
func (repo *KYCRepository) FindLatestByUID(ctx context.Context, uid string) (*entity.KYC, error) {
filter := bson.M{"uid": uid}
var result entity.KYC
2025-04-06 02:08:46 +00:00
opts := options.FindOne().SetSort(bson.D{{Key: "created_at", Value: -1}}) // 用 SetSort 加入排序
2025-03-31 13:24:40 +00:00
err := repo.DB.GetClient().FindOne(ctx, &result, filter, opts)
if err != nil {
2025-04-04 06:39:01 +00:00
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, ErrNotFound
}
2025-04-06 02:08:46 +00:00
2025-03-31 13:24:40 +00:00
return nil, err
}
2025-04-06 02:08:46 +00:00
2025-03-31 13:24:40 +00:00
return &result, nil
}
func (repo *KYCRepository) FindByID(ctx context.Context, id string) (*entity.KYC, error) {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
filter := bson.M{"_id": oid}
var result entity.KYC
err = repo.DB.GetClient().FindOne(ctx, &result, filter)
if err != nil {
return nil, err
}
2025-04-06 02:08:46 +00:00
2025-03-31 13:24:40 +00:00
return &result, nil
}
func (repo *KYCRepository) List(ctx context.Context, params repository.KYCQueryParams) ([]*entity.KYC, int64, error) {
filter := bson.M{}
if params.UID != nil {
filter["uid"] = *params.UID
}
if params.Country != nil {
filter["country_region"] = *params.Country
}
if params.Status != nil {
filter["status"] = *params.Status
}
2025-04-03 07:32:14 +00:00
// 設置排序選項
opts := options.Find().SetSkip((params.PageIndex - 1) * params.PageSize).SetLimit(params.PageSize)
2025-04-10 07:59:30 +00:00
// if params.SortByDate {
2025-04-08 09:20:34 +00:00
// opts.SetSort(bson.E{Key: "created_at", Value: -1})
2025-04-10 07:59:30 +00:00
// } else {
2025-04-08 09:20:34 +00:00
// opts.SetSort(bson.D{{Key: "updated_at", Value: -1}})
2025-04-10 07:59:30 +00:00
// }
2025-04-03 07:32:14 +00:00
// 查詢符合條件的總數
total, err := repo.DB.GetClient().CountDocuments(ctx, filter)
if err != nil {
return nil, 0, err
}
// 執行查詢並獲取結果
2025-03-31 13:24:40 +00:00
var results []*entity.KYC
2025-04-03 07:32:14 +00:00
err = repo.DB.GetClient().Find(ctx, &results, filter, opts)
2025-03-31 13:24:40 +00:00
if err != nil {
return nil, 0, err
}
2025-04-03 07:32:14 +00:00
2025-03-31 13:24:40 +00:00
return results, total, nil
}
func (repo *KYCRepository) UpdateStatus(ctx context.Context, id string, status string, reason string) error {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
filter := bson.M{"_id": oid}
update := bson.M{
"$set": bson.M{
"status": status,
"reject_reason": reason,
"updated_at": time.Now().UTC().UnixNano(),
},
}
_, err = repo.DB.GetClient().UpdateOne(ctx, filter, update)
2025-04-06 02:08:46 +00:00
2025-03-31 13:24:40 +00:00
return err
}
func (repo *KYCRepository) UpdateKYCInfo(ctx context.Context, id string, update *repository.KYCUpdateParams) error {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
setFields := bson.M{"updated_at": time.Now().UTC().UnixNano()}
if update.Name != nil {
setFields["name"] = *update.Name
}
if update.Identification != nil {
setFields["identification"] = *update.Identification
}
if update.IdentificationType != nil {
setFields["identification_type"] = *update.IdentificationType
}
if update.Address != nil {
setFields["address"] = *update.Address
}
if update.PostalCode != nil {
setFields["postal_code"] = *update.PostalCode
}
if update.IDFrontImage != nil {
setFields["id_front_image"] = *update.IDFrontImage
}
if update.IDBackImage != nil {
setFields["id_back_image"] = *update.IDBackImage
}
if update.BankStatementImg != nil {
setFields["bank_statement_img"] = *update.BankStatementImg
}
if update.BankCode != nil {
setFields["bank_code"] = *update.BankCode
}
if update.BankName != nil {
setFields["bank_name"] = *update.BankName
}
if update.BranchCode != nil {
setFields["branch_code"] = *update.BranchCode
}
if update.BranchName != nil {
setFields["branch_name"] = *update.BranchName
}
if update.BankAccount != nil {
setFields["bank_account"] = *update.BankAccount
}
filter := bson.M{"_id": oid, "status": "PENDING"} // 僅允許更新尚未審核的
updateDoc := bson.M{"$set": setFields}
_, err = repo.DB.GetClient().UpdateOne(ctx, filter, updateDoc)
2025-04-06 02:08:46 +00:00
2025-03-31 13:24:40 +00:00
return err
}