86 lines
2.6 KiB
Go
86 lines
2.6 KiB
Go
package mysqldao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/global"
|
|
"git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/internal/mysqlmodel"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// RefundDao 退款单数据访问
|
|
type RefundDao struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewRefundDao 创建退款 DAO
|
|
func NewRefundDao() *RefundDao {
|
|
return &RefundDao{db: global.MysqlEngine}
|
|
}
|
|
|
|
// RefundListParams 退款单分页列表筛选参数。
|
|
type RefundListParams struct {
|
|
Status int8 // 退款状态(WithStatus 为 true 时生效)
|
|
WithStatus bool // 是否按状态筛选(退款状态 0=待处理 是合法值,需显式区分「不过滤」)
|
|
OrderNo string // 订单号(精确)
|
|
Page int
|
|
Size int
|
|
}
|
|
|
|
// RefundList 分页查询退款单
|
|
func (d *RefundDao) RefundList(ctx context.Context, p RefundListParams) ([]mysqlmodel.RefundFlowLog, int64, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.RefundList")
|
|
defer span.End()
|
|
|
|
var list []mysqlmodel.RefundFlowLog
|
|
var total int64
|
|
q := d.db.WithContext(ctx).Model(&mysqlmodel.RefundFlowLog{})
|
|
if p.WithStatus {
|
|
q = q.Where("status = ?", p.Status)
|
|
}
|
|
if p.OrderNo != "" {
|
|
q = q.Where("order_no = ?", p.OrderNo)
|
|
}
|
|
if err := q.Count(&total).Error; err != nil {
|
|
span.RecordError(err)
|
|
return nil, 0, err
|
|
}
|
|
if err := q.Offset((p.Page - 1) * p.Size).Limit(p.Size).Order("id DESC").Find(&list).Error; err != nil {
|
|
span.RecordError(err)
|
|
return nil, 0, err
|
|
}
|
|
return list, total, nil
|
|
}
|
|
|
|
// RefundGetByRefundNo 按退款单号查询
|
|
func (d *RefundDao) RefundGetByRefundNo(ctx context.Context, refundNo string) (*mysqlmodel.RefundFlowLog, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.RefundGetByRefundNo")
|
|
defer span.End()
|
|
|
|
var refund mysqlmodel.RefundFlowLog
|
|
if err := d.db.WithContext(ctx).Where("refund_no = ?", refundNo).First(&refund).Error; err != nil {
|
|
span.RecordError(err)
|
|
return nil, err
|
|
}
|
|
return &refund, nil
|
|
}
|
|
|
|
// RefundTransition 条件更新退款状态(仅 from 状态可流转到 to,行锁防并发重复审核)。
|
|
func (d *RefundDao) RefundTransition(ctx context.Context, refundNo string, from, to int8, failReason string) (bool, error) {
|
|
ctx, span := global.Tracer.Start(ctx, "mysqldao.RefundTransition")
|
|
defer span.End()
|
|
|
|
updates := map[string]any{"status": to}
|
|
if failReason != "" {
|
|
updates["fail_reason"] = failReason
|
|
}
|
|
res := d.db.WithContext(ctx).Model(&mysqlmodel.RefundFlowLog{}).
|
|
Where("refund_no = ? AND status = ?", refundNo, from).
|
|
Updates(updates)
|
|
if res.Error != nil {
|
|
span.RecordError(res.Error)
|
|
return false, res.Error
|
|
}
|
|
return res.RowsAffected > 0, nil
|
|
}
|