初始化项目

This commit is contained in:
2025-12-10 13:51:09 +08:00
parent 3c55577df8
commit 0486f468d6
19 changed files with 1078 additions and 1010 deletions

View File

@@ -2,228 +2,202 @@ package dao
import (
"context"
"errors"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"order/consts"
"order/model/entity"
)
// PaymentConfigDao 支付配置数据访问对象
type paymentConfig struct{}
type PaymentConfigDao struct {
collection *mongo.Collection
}
// NewPaymentConfigDao 创建支付配置DAO实例
func NewPaymentConfigDao(collection *mongo.Collection) *PaymentConfigDao {
return &PaymentConfigDao{
collection: collection,
}
}
// PaymentConfig 支付配置数据访问对象
var PaymentConfig = new(paymentConfig)
// Create 创建支付配置
func (d *PaymentConfigDao) Create(ctx context.Context, config *entity.PaymentConfig) error {
if d.collection == nil {
return errors.New("collection not initialized")
}
config.ID = primitive.NewObjectID()
_, err := d.collection.InsertOne(ctx, config)
func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error {
_, err := mongo.Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection)
return err
}
// GetByTenantAndMethod 根据租户和支付方式获取配置
func (d *PaymentConfigDao) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) {
if d.collection == nil {
return nil, errors.New("collection not initialized")
}
func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) {
var config entity.PaymentConfig
err := d.collection.FindOne(ctx, bson.M{
filter := bson.M{
"tenant_id": tenantID,
"pay_method": payMethod,
"enabled": true,
}).Decode(&config)
}
if err == mongo.ErrNoDocuments {
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
if err != nil {
return nil, nil
}
return &config, err
return &config, nil
}
// Update 更新支付配置
func (d *PaymentConfigDao) Update(ctx context.Context, id string, update bson.M) error {
if d.collection == nil {
return errors.New("collection not initialized")
func (d *paymentConfig) Update(ctx context.Context, config *entity.PaymentConfig) error {
filter := bson.M{"_id": config.ID}
update := bson.M{
"$set": bson.M{
"tenant_id": config.TenantID,
"pay_method": config.PayMethod,
"config_name": config.ConfigName,
"description": config.Description,
"enabled": config.Enabled,
"app_id": config.AppID,
"mch_id": config.MchID,
"api_key": config.APIKey,
"notify_url": config.NotifyURL,
"return_url": config.ReturnURL,
"cert_path": config.CertPath,
"key_path": config.KeyPath,
"app_private_key": config.AppPrivateKey,
"alipay_public_key": config.AlipayPublicKey,
"sandbox": config.Sandbox,
"gateway_url": config.GatewayURL,
"support_native": config.SupportNative,
"support_jsapi": config.SupportJSAPI,
"support_app": config.SupportAPP,
"support_h5": config.SupportH5,
},
}
objectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
_, err := mongo.Update(ctx, filter, update, consts.PaymentConfigCollection)
return err
}
// ListByTenant 查询租户的支付配置列表
func (d *PaymentConfigDao) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
if d.collection == nil {
return nil, errors.New("collection not initialized")
}
// GetByID 根据ID获取配置
func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity.PaymentConfig, error) {
var config entity.PaymentConfig
filter := bson.M{"tenant_id": tenantID}
cursor, err := d.collection.Find(ctx, filter)
filter := bson.M{"_id": id}
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
return &config, nil
}
// GetByTenantID 根据租户ID获取配置列表
func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
var configs []entity.PaymentConfig
if err := cursor.All(ctx, &configs); err != nil {
filter := bson.M{"tenant_id": tenantID}
err := mongo.Find(ctx, filter, &configs, consts.PaymentConfigCollection)
if err != nil {
return nil, err
}
return configs, nil
}
// PaymentRecordDao 支付记录数据访问对象
// Delete 删除支付配置
func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error {
filter := bson.M{"_id": id}
type PaymentRecordDao struct {
collection *mongo.Collection
_, err := mongo.Delete(ctx, filter, consts.PaymentConfigCollection)
return err
}
func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) {
var configs []entity.PaymentConfig
filter := bson.M{"tenant_id": tenantID}
err := mongo.Find(ctx, filter, &configs, consts.PaymentConfigCollection)
return configs, err
}
// NewPaymentRecordDao 创建支付记录DAO实例
func NewPaymentRecordDao(collection *mongo.Collection) *PaymentRecordDao {
return &PaymentRecordDao{
collection: collection,
}
}
type paymentRecord struct{}
// PaymentRecord 支付记录数据访问对象
var PaymentRecord = new(paymentRecord)
// Create 创建支付记录
func (d *PaymentRecordDao) Create(ctx context.Context, record *entity.PaymentRecord) error {
if d.collection == nil {
return errors.New("collection not initialized")
}
record.ID = primitive.NewObjectID()
func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error {
record.CreatedAt = time.Now().Unix()
record.UpdatedAt = time.Now().Unix()
_, err := d.collection.InsertOne(ctx, record)
_, err := mongo.Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection)
return err
}
// GetByOrderNo 根据订单号获取支付记录
func (d *PaymentRecordDao) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) {
if d.collection == nil {
return nil, errors.New("collection not initialized")
}
func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) {
var record entity.PaymentRecord
err := d.collection.FindOne(ctx, bson.M{
filter := bson.M{
"tenant_id": tenantID,
"order_no": orderNo,
}).Decode(&record)
}
if err == mongo.ErrNoDocuments {
err := mongo.FindOne(ctx, filter, &record, consts.PaymentRecordCollection)
if err != nil {
return nil, nil
}
return &record, err
return &record, nil
}
// UpdateStatus 更新支付记录状态
func (d *PaymentRecordDao) UpdateStatus(ctx context.Context, id string, status, transactionID, tradeNo string) error {
if d.collection == nil {
return errors.New("collection not initialized")
}
objectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
func (d *paymentRecord) UpdateStatus(ctx context.Context, id string, status, transactionID, tradeNo string) error {
filter := bson.M{"_id": id}
update := bson.M{
"status": status,
"transaction_id": transactionID,
"trade_no": tradeNo,
"updated_at": time.Now().Unix(),
"updated_at": time.Now(),
}
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection)
return err
}
// RefundRecordDao 退款记录数据访问对象
type refundRecord struct{}
type RefundRecordDao struct {
collection *mongo.Collection
}
// NewRefundRecordDao 创建退款记录DAO实例
func NewRefundRecordDao(collection *mongo.Collection) *RefundRecordDao {
return &RefundRecordDao{
collection: collection,
}
}
// RefundRecord 退款记录数据访问对象
var RefundRecord = new(refundRecord)
// Create 创建退款记录
func (d *RefundRecordDao) Create(ctx context.Context, record *entity.RefundRecord) error {
if d.collection == nil {
return errors.New("collection not initialized")
}
record.ID = primitive.NewObjectID()
func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error {
record.CreatedAt = time.Now().Unix()
record.UpdatedAt = time.Now().Unix()
_, err := d.collection.InsertOne(ctx, record)
_, err := mongo.Insert(ctx, []interface{}{record}, consts.RefundRecordCollection)
return err
}
// GetByRefundNo 根据退款单号获取退款记录
func (d *RefundRecordDao) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) {
if d.collection == nil {
return nil, errors.New("collection not initialized")
}
func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) {
var record entity.RefundRecord
err := d.collection.FindOne(ctx, bson.M{
filter := bson.M{
"tenant_id": tenantID,
"refund_no": refundNo,
}).Decode(&record)
}
if err == mongo.ErrNoDocuments {
err := mongo.FindOne(ctx, filter, &record, consts.RefundRecordCollection)
if err != nil {
return nil, nil
}
return &record, err
return &record, nil
}
// UpdateRefundStatus 更新退款记录状态
func (d *RefundRecordDao) UpdateRefundStatus(ctx context.Context, id, status, refundID string) error {
if d.collection == nil {
return errors.New("collection not initialized")
}
objectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
func (d *refundRecord) UpdateRefundStatus(ctx context.Context, id, status, refundID string) error {
filter := bson.M{"_id": id}
update := bson.M{
"status": status,
"refund_id": refundID,
"updated_at": time.Now().Unix(),
"updated_at": time.Now(),
}
_, err = d.collection.UpdateOne(ctx, bson.M{"_id": objectID}, bson.M{"$set": update})
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection)
return err
}