204 lines
5.7 KiB
Go
204 lines
5.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitee.com/red-future---jilin-g/common/mongo"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
"order/consts"
|
|
"order/model/entity"
|
|
)
|
|
|
|
type paymentConfig struct{}
|
|
|
|
// PaymentConfig 支付配置数据访问对象
|
|
var PaymentConfig = new(paymentConfig)
|
|
|
|
// Create 创建支付配置
|
|
func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error {
|
|
_, err := mongo.Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection)
|
|
return err
|
|
}
|
|
|
|
// GetByTenantAndMethod 根据租户和支付方式获取配置
|
|
func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payMethod string) (*entity.PaymentConfig, error) {
|
|
var config entity.PaymentConfig
|
|
|
|
filter := bson.M{
|
|
"tenant_id": tenantID,
|
|
"pay_method": payMethod,
|
|
"enabled": true,
|
|
}
|
|
|
|
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
// Update 更新支付配置
|
|
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,
|
|
},
|
|
}
|
|
|
|
_, err := mongo.Update(ctx, filter, update, consts.PaymentConfigCollection)
|
|
return err
|
|
}
|
|
|
|
// GetByID 根据ID获取配置
|
|
func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity.PaymentConfig, error) {
|
|
var config entity.PaymentConfig
|
|
|
|
filter := bson.M{"_id": id}
|
|
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &config, nil
|
|
}
|
|
|
|
// GetByTenantID 根据租户ID获取配置列表
|
|
func (d *paymentConfig) GetByTenantID(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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return configs, nil
|
|
}
|
|
|
|
// Delete 删除支付配置
|
|
func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error {
|
|
filter := bson.M{"_id": id}
|
|
|
|
_, 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
|
|
}
|
|
|
|
type paymentRecord struct{}
|
|
|
|
// PaymentRecord 支付记录数据访问对象
|
|
var PaymentRecord = new(paymentRecord)
|
|
|
|
// Create 创建支付记录
|
|
func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error {
|
|
record.CreatedAt = time.Now().Unix()
|
|
record.UpdatedAt = time.Now().Unix()
|
|
|
|
_, err := mongo.Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection)
|
|
return err
|
|
}
|
|
|
|
// GetByOrderNo 根据订单号获取支付记录
|
|
func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo string) (*entity.PaymentRecord, error) {
|
|
var record entity.PaymentRecord
|
|
|
|
filter := bson.M{
|
|
"tenant_id": tenantID,
|
|
"order_no": orderNo,
|
|
}
|
|
|
|
err := mongo.FindOne(ctx, filter, &record, consts.PaymentRecordCollection)
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return &record, nil
|
|
}
|
|
|
|
// UpdateStatus 更新支付记录状态
|
|
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(),
|
|
}
|
|
|
|
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection)
|
|
return err
|
|
}
|
|
|
|
type refundRecord struct{}
|
|
|
|
// RefundRecord 退款记录数据访问对象
|
|
var RefundRecord = new(refundRecord)
|
|
|
|
// Create 创建退款记录
|
|
func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error {
|
|
record.CreatedAt = time.Now().Unix()
|
|
record.UpdatedAt = time.Now().Unix()
|
|
|
|
_, err := mongo.Insert(ctx, []interface{}{record}, consts.RefundRecordCollection)
|
|
return err
|
|
}
|
|
|
|
// GetByRefundNo 根据退款单号获取退款记录
|
|
func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo string) (*entity.RefundRecord, error) {
|
|
var record entity.RefundRecord
|
|
|
|
filter := bson.M{
|
|
"tenant_id": tenantID,
|
|
"refund_no": refundNo,
|
|
}
|
|
|
|
err := mongo.FindOne(ctx, filter, &record, consts.RefundRecordCollection)
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
return &record, nil
|
|
}
|
|
|
|
// UpdateRefundStatus 更新退款记录状态
|
|
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(),
|
|
}
|
|
|
|
_, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection)
|
|
return err
|
|
}
|