forked from all_user/stat_center
增加退单管理
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"stat/models/refundOrderMod"
|
||||
)
|
||||
|
||||
// ============ 退款订单管理 ============
|
||||
|
||||
// RefundOrderListReq 退款订单查询。所有过滤条件均为可选,不传表示不限制
|
||||
type RefundOrderListReq struct {
|
||||
AppId *int32 `json:"appId" form:"appId"` // appId
|
||||
UserId *uint64 `json:"userId" form:"userId"` // 用户ID
|
||||
CID *string `json:"cid" form:"cid"` // 渠道id
|
||||
ChannelName *string `json:"channelName" form:"channelName"` // 支付渠道名字
|
||||
DistrictCode *string `json:"districtCode" form:"districtCode"` // 渠道码
|
||||
PageNum int64 `json:"pageNum" form:"pageNum" binding:"required,gte=1"`
|
||||
PageSize int64 `json:"pageSize" form:"pageSize" binding:"required,gte=1,lte=500"`
|
||||
}
|
||||
|
||||
type RefundOrderListResp struct {
|
||||
Total int64 `json:"total"`
|
||||
List refundOrderMod.RefundOrderList `json:"list"`
|
||||
}
|
||||
|
||||
// AddRefundOrderReq 按 cid 回填新增。服务端据此查询 log_recharge 的成功订单并逐条落库
|
||||
type AddRefundOrderReq struct {
|
||||
CID string `json:"cid" binding:"required"` // 渠道id
|
||||
}
|
||||
|
||||
// BatchAddRefundOrderReq 批量按 cid 回填新增
|
||||
type BatchAddRefundOrderReq struct {
|
||||
CIDs []string `json:"cids" binding:"required,min=1,max=200"` // 渠道id列表
|
||||
}
|
||||
|
||||
// AddRefundOrderResp 回填结果
|
||||
type AddRefundOrderResp struct {
|
||||
Matched int64 `json:"matched"` // log_recharge 命中的成功订单数
|
||||
Inserted int64 `json:"inserted"` // 实际新增条数
|
||||
Skipped int64 `json:"skipped"` // 因已存在而跳过的条数
|
||||
Details []RefundCidStat `json:"details"` // 按 cid 分项,单条新增时只有一项
|
||||
}
|
||||
|
||||
type RefundCidStat struct {
|
||||
CID string `json:"cid"`
|
||||
Matched int64 `json:"matched"`
|
||||
Inserted int64 `json:"inserted"`
|
||||
Skipped int64 `json:"skipped"`
|
||||
}
|
||||
|
||||
// DelRefundOrderReq 按 _id 删除
|
||||
type DelRefundOrderReq struct {
|
||||
Id string `json:"id" binding:"required"` // 记录 _id (24位十六进制)
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package refundOrderApi
|
||||
|
||||
import (
|
||||
"stat/api/proto"
|
||||
"stat/common/stdLog"
|
||||
"stat/common/utils"
|
||||
"stat/service/opLogSer"
|
||||
"stat/service/refundOrderSer"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @Tags 退款订单管理
|
||||
// @Summary 退款订单列表
|
||||
// @Description 支持 appId / userId / cid / channelName / districtCode 任意组合过滤
|
||||
// @Router /api/stat/refundOrder/list [POST]
|
||||
// @Param param body proto.RefundOrderListReq true "参数"
|
||||
// @Success 200 {object} proto.RefundOrderListResp "成功"
|
||||
// @Security ApiKeyAuth
|
||||
func List(ctx *gin.Context) {
|
||||
req := &proto.RefundOrderListReq{}
|
||||
if err := ctx.ShouldBindJSON(req); err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
||||
return
|
||||
}
|
||||
resp, err := refundOrderSer.List(ctx, req)
|
||||
if err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.Failure, err.Error())
|
||||
return
|
||||
}
|
||||
stdLog.ServerJSON(ctx, stdLog.Success, resp)
|
||||
}
|
||||
|
||||
// @Tags 退款订单管理
|
||||
// @Summary 新增退款订单
|
||||
// @Description 传入 cid,查询 log_recharge 中该渠道付款成功的订单并逐条回填
|
||||
// @Router /api/stat/refundOrder/add [POST]
|
||||
// @Param param body proto.AddRefundOrderReq true "参数"
|
||||
// @Success 200 {object} proto.AddRefundOrderResp "成功"
|
||||
// @Security ApiKeyAuth
|
||||
func Add(ctx *gin.Context) {
|
||||
account, err := utils.GetAdmin(ctx)
|
||||
if err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
||||
return
|
||||
}
|
||||
req := &proto.AddRefundOrderReq{}
|
||||
if err := ctx.ShouldBindJSON(req); err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
||||
return
|
||||
}
|
||||
resp, err := refundOrderSer.Add(ctx, req)
|
||||
if err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.Failure, err.Error())
|
||||
return
|
||||
}
|
||||
opLogSer.AddOpLog(ctx, account, true, req)
|
||||
stdLog.ServerJSON(ctx, stdLog.Success, resp)
|
||||
}
|
||||
|
||||
// @Tags 退款订单管理
|
||||
// @Summary 批量新增退款订单
|
||||
// @Description 传入 cid 列表,逐个查询 log_recharge 回填
|
||||
// @Router /api/stat/refundOrder/batchAdd [POST]
|
||||
// @Param param body proto.BatchAddRefundOrderReq true "参数"
|
||||
// @Success 200 {object} proto.AddRefundOrderResp "成功"
|
||||
// @Security ApiKeyAuth
|
||||
func BatchAdd(ctx *gin.Context) {
|
||||
account, err := utils.GetAdmin(ctx)
|
||||
if err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
||||
return
|
||||
}
|
||||
req := &proto.BatchAddRefundOrderReq{}
|
||||
if err := ctx.ShouldBindJSON(req); err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
||||
return
|
||||
}
|
||||
resp, err := refundOrderSer.BatchAdd(ctx, req)
|
||||
if err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.Failure, err.Error())
|
||||
return
|
||||
}
|
||||
opLogSer.AddOpLog(ctx, account, true, req)
|
||||
stdLog.ServerJSON(ctx, stdLog.Success, resp)
|
||||
}
|
||||
|
||||
// @Tags 退款订单管理
|
||||
// @Summary 删除退款订单
|
||||
// @Description 按记录 _id 删除
|
||||
// @Router /api/stat/refundOrder/del [POST]
|
||||
// @Param param body proto.DelRefundOrderReq true "参数"
|
||||
// @Success 200 {string} string "成功"
|
||||
// @Security ApiKeyAuth
|
||||
func Del(ctx *gin.Context) {
|
||||
account, err := utils.GetAdmin(ctx)
|
||||
if err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.NoToken, err.Error())
|
||||
return
|
||||
}
|
||||
req := &proto.DelRefundOrderReq{}
|
||||
if err := ctx.ShouldBindJSON(req); err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.ParamError, err.Error())
|
||||
return
|
||||
}
|
||||
if err := refundOrderSer.Del(ctx, req); err != nil {
|
||||
stdLog.ServerJSON(ctx, stdLog.Failure, err.Error())
|
||||
return
|
||||
}
|
||||
opLogSer.AddOpLog(ctx, account, true, req)
|
||||
stdLog.ServerJSON(ctx, stdLog.Success, "")
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -46,6 +46,7 @@ import (
|
||||
"stat/models/rechargechannelmod"
|
||||
"stat/models/rechargegamemod"
|
||||
"stat/models/rechargemod"
|
||||
"stat/models/refundOrderMod"
|
||||
"stat/models/registermod"
|
||||
statrecordmod "stat/models/statrecord"
|
||||
"stat/models/syncgamerecharge"
|
||||
@@ -119,4 +120,5 @@ func InitIndex() {
|
||||
advitiseBindMod.Init(ctx)
|
||||
advitiseClickStatMod.Init(ctx)
|
||||
advitiseDayClickStatMod.Init(ctx)
|
||||
refundOrderMod.Init(ctx)
|
||||
}
|
||||
|
||||
@@ -212,3 +212,13 @@ func UpdateManyRecharge(ctx context.Context, filter bson.M, update bson.M) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindOneRecharge 按条件查询单条充值订单(退款订单管理按 cid 回填数据使用)。
|
||||
// 使用 FindOneEx,查不到时透传 mongo.ErrNoDocuments,由调用方区分"未命中"与"出错"。
|
||||
func FindOneRecharge(ctx context.Context, filter bson.M) (*RechargeMod, error) {
|
||||
var doc RechargeMod
|
||||
if err := db.FindOneEx(ctx, &doc, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package refundOrderMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func Init(ctx context.Context) {
|
||||
db = mongos.New(mongos.MgoManager, TN.RefundOrder)
|
||||
indexes := []mongo.IndexModel{
|
||||
// (appId, orderId) 唯一,从数据库层杜绝重复添加 —— 应用层的"先查后插"
|
||||
// 存在并发竞态,两个请求可能同时通过检查。与 log_recharge 的唯一键对齐。
|
||||
// orderId 为空的历史数据不参与唯一约束,否则多条空值会互相冲突。
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.RefundOrder.AppId, Value: 1},
|
||||
{Key: FN.RefundOrder.OrderId, Value: 1},
|
||||
},
|
||||
Options: options.Index().SetUnique(true).
|
||||
SetPartialFilterExpression(bson.M{FN.RefundOrder.OrderId: bson.M{"$gt": ""}}),
|
||||
},
|
||||
{Keys: bson.D{{Key: FN.RefundOrder.CID, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.RefundOrder.UserId, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.RefundOrder.ChannelName, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.RefundOrder.DistrictCode, Value: 1}}},
|
||||
{Keys: bson.D{{Key: FN.RefundOrder.CreatedAt, Value: -1}}},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(refundOrderMod:Init) CreateIndex failed: %+v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// InsertOneRefundOrder 插入一条。
|
||||
// 必须用 InsertOneTiny: db.InsertOne 会调 addCreatedAt 把 CreatedAt 无条件
|
||||
// 改写成 time.Now(),而本表的 createdAt 存的是原订单创建时间,不能被覆盖。
|
||||
// 命中唯一索引时返回的 error 可用 IsDuplicateKey 判定。
|
||||
func InsertOneRefundOrder(ctx context.Context, doc *RefundOrder) error {
|
||||
if err := db.InsertOneTiny(ctx, doc); err != nil {
|
||||
if !IsDuplicateKey(err) {
|
||||
log.Warn("refundOrderMod InsertOne failed", log.E(err))
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsDuplicateKey 判断是否唯一索引冲突。
|
||||
// 驱动为 v1.4.1,没有 mongo.IsDuplicateKeyError(1.11 才引入),按错误码 11000 判定。
|
||||
func IsDuplicateKey(err error) bool {
|
||||
var we mongo.WriteException
|
||||
if errors.As(err, &we) {
|
||||
for _, e := range we.WriteErrors {
|
||||
if e.Code == 11000 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func FindOneRefundOrder(ctx context.Context, filter bson.M) (*RefundOrder, error) {
|
||||
var doc RefundOrder
|
||||
if err := db.FindOne(ctx, &doc, filter); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
func FindManyRefundOrders(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (RefundOrderList, error) {
|
||||
list := make(RefundOrderList, 0)
|
||||
if err := db.Find(ctx, &list, filter, opts...); err != nil {
|
||||
log.Warn("refundOrderMod Find failed", log.E(err), log.Any("filter", filter))
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func CountRefundOrders(ctx context.Context, filter bson.M) (int64, error) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("refundOrderMod Count failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func DelOneRefundOrder(ctx context.Context, filter bson.M) (int64, error) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("refundOrderMod DeleteOne failed", log.E(err), log.Any("filter", filter))
|
||||
return 0, err
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package refundOrderMod
|
||||
|
||||
type TagRefundOrder struct {
|
||||
ID string
|
||||
AppId string
|
||||
CID string
|
||||
DistrictCode string
|
||||
Money string
|
||||
OrderId string
|
||||
UserId string
|
||||
Type string
|
||||
ChannelName string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
RefundOrder TagRefundOrder
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
RefundOrder string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.RefundOrder.ID = "_id"
|
||||
FN.RefundOrder.AppId = "appId"
|
||||
FN.RefundOrder.CID = "cid"
|
||||
FN.RefundOrder.DistrictCode = "districtCode"
|
||||
FN.RefundOrder.Money = "money"
|
||||
FN.RefundOrder.OrderId = "orderId"
|
||||
FN.RefundOrder.UserId = "userId"
|
||||
FN.RefundOrder.Type = "type"
|
||||
FN.RefundOrder.ChannelName = "channelName"
|
||||
FN.RefundOrder.CreatedAt = "createdAt"
|
||||
|
||||
TN.RefundOrder = CollRefundOrder
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package refundOrderMod
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// 表名
|
||||
const CollRefundOrder = "refund_order"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
// RefundOrder 退款订单
|
||||
// 字段取自 log_recharge 的同名字段,按 cid 回填时逐字段拷贝,是下单时点的快照
|
||||
// @table:refund_order
|
||||
type RefundOrder struct {
|
||||
ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
|
||||
AppId int32 `json:"appId" bson:"appId"` // appId
|
||||
CID string `json:"cid" bson:"cid"` // 渠道id
|
||||
DistrictCode string `json:"districtCode" bson:"districtCode"` // 渠道码
|
||||
Money int64 `json:"money" bson:"money"` // 订单金额
|
||||
OrderId string `json:"orderId" bson:"orderId"` // 流水id
|
||||
UserId uint64 `json:"userId" bson:"userId"` // 用户ID
|
||||
Type string `json:"type" bson:"type"` // 充值方式
|
||||
ChannelName string `json:"channelName" bson:"channelName"` // 支付渠道名字
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 原订单创建时间(取自 log_recharge.createdAt)
|
||||
}
|
||||
|
||||
type RefundOrderList []*RefundOrder
|
||||
@@ -104,6 +104,7 @@ func registerRouter(router *gin.Engine) {
|
||||
appcategoryRouter(rootRouter) // app分类
|
||||
userInviteRouter(rootRouter) // 兼容
|
||||
advitiseV2Router(rootRouter) // 广告管理V2(素材/位置/绑定)
|
||||
refundOrderRouter(rootRouter) // 退款订单管理
|
||||
}
|
||||
|
||||
// 双端下载落地页(无需登陆鉴权)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"stat/api/refundOrderApi"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func refundOrderRouter(router *gin.RouterGroup) {
|
||||
refund := router.Group("/refundOrder")
|
||||
{
|
||||
refund.POST("/list", refundOrderApi.List)
|
||||
refund.POST("/add", refundOrderApi.Add)
|
||||
refund.POST("/batchAdd", refundOrderApi.BatchAdd)
|
||||
refund.POST("/del", refundOrderApi.Del)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package refundOrderSer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"stat/api/proto"
|
||||
"stat/models/rechargemod"
|
||||
"stat/models/refundOrderMod"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 只回填付款成功的订单。status=3 为付款成功,与 models/rechargemod/stat.go 中
|
||||
// 各统计口径一致;付款失败(status=2)的订单不存在退款场景。
|
||||
const rechargeStatusSuccess = 3
|
||||
|
||||
// List 退款订单查询,支持 appId / userId / cid / channelName / districtCode 任意组合
|
||||
func List(ctx context.Context, req *proto.RefundOrderListReq) (*proto.RefundOrderListResp, error) {
|
||||
resp := &proto.RefundOrderListResp{
|
||||
Total: 0,
|
||||
List: make(refundOrderMod.RefundOrderList, 0),
|
||||
}
|
||||
filter := bson.M{}
|
||||
if req.AppId != nil {
|
||||
filter[refundOrderMod.FN.RefundOrder.AppId] = *req.AppId
|
||||
}
|
||||
if req.UserId != nil {
|
||||
filter[refundOrderMod.FN.RefundOrder.UserId] = *req.UserId
|
||||
}
|
||||
if req.CID != nil && *req.CID != "" {
|
||||
filter[refundOrderMod.FN.RefundOrder.CID] = *req.CID
|
||||
}
|
||||
if req.ChannelName != nil && *req.ChannelName != "" {
|
||||
filter[refundOrderMod.FN.RefundOrder.ChannelName] = *req.ChannelName
|
||||
}
|
||||
if req.DistrictCode != nil && *req.DistrictCode != "" {
|
||||
filter[refundOrderMod.FN.RefundOrder.DistrictCode] = *req.DistrictCode
|
||||
}
|
||||
|
||||
total, err := refundOrderMod.CountRefundOrders(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opt := &options.FindOptions{}
|
||||
opt.SetSort(bson.D{{Key: refundOrderMod.FN.RefundOrder.CreatedAt, Value: -1}})
|
||||
opt.SetSkip((req.PageNum - 1) * req.PageSize)
|
||||
opt.SetLimit(req.PageSize)
|
||||
list, err := refundOrderMod.FindManyRefundOrders(ctx, filter, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp.Total = total
|
||||
resp.List = list
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Add 按单个 cid 回填
|
||||
func Add(ctx context.Context, req *proto.AddRefundOrderReq) (*proto.AddRefundOrderResp, error) {
|
||||
return addByCids(ctx, []string{req.CID})
|
||||
}
|
||||
|
||||
// BatchAdd 按多个 cid 批量回填
|
||||
func BatchAdd(ctx context.Context, req *proto.BatchAddRefundOrderReq) (*proto.AddRefundOrderResp, error) {
|
||||
return addByCids(ctx, req.CIDs)
|
||||
}
|
||||
|
||||
// addByCids 逐个 cid 查询 log_recharge 的成功订单,按 (appId, orderId) 判重后落库。
|
||||
// 单个 cid 失败不影响其余 cid,错误直接返回由调用方决定。
|
||||
func addByCids(ctx context.Context, cids []string) (*proto.AddRefundOrderResp, error) {
|
||||
resp := &proto.AddRefundOrderResp{
|
||||
Details: make([]proto.RefundCidStat, 0, len(cids)),
|
||||
}
|
||||
seen := make(map[string]bool, len(cids)) // 同一次请求里 cid 去重
|
||||
for _, cid := range cids {
|
||||
if cid == "" || seen[cid] {
|
||||
continue
|
||||
}
|
||||
seen[cid] = true
|
||||
|
||||
stat, err := addOneCid(ctx, cid)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("回填渠道[%s]失败: %w", cid, err)
|
||||
}
|
||||
resp.Matched += stat.Matched
|
||||
resp.Inserted += stat.Inserted
|
||||
resp.Skipped += stat.Skipped
|
||||
resp.Details = append(resp.Details, *stat)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func addOneCid(ctx context.Context, cid string) (*proto.RefundCidStat, error) {
|
||||
stat := &proto.RefundCidStat{CID: cid}
|
||||
|
||||
// cid 唯一标识一笔订单,只回填付款成功的那条
|
||||
src, err := rechargemod.FindOneRecharge(ctx, bson.M{
|
||||
"cid": cid,
|
||||
"status": rechargeStatusSuccess,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
||||
return stat, nil // 该 cid 没有对应的成功订单,matched 记 0,不算失败
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
stat.Matched = 1
|
||||
|
||||
// 按 (appId, orderId) 判重,已存在则跳过
|
||||
exist, err := refundOrderMod.FindOneRefundOrder(ctx, bson.M{
|
||||
refundOrderMod.FN.RefundOrder.AppId: src.AppId,
|
||||
refundOrderMod.FN.RefundOrder.OrderId: src.OrderId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if exist != nil && !exist.ID.IsZero() {
|
||||
stat.Skipped = 1
|
||||
return stat, nil
|
||||
}
|
||||
|
||||
doc := &refundOrderMod.RefundOrder{
|
||||
AppId: src.AppId,
|
||||
CID: src.CID,
|
||||
DistrictCode: src.DistrictCode,
|
||||
Money: src.Money,
|
||||
OrderId: src.OrderId,
|
||||
UserId: src.UserId,
|
||||
Type: src.Type,
|
||||
ChannelName: src.ChannelName,
|
||||
CreatedAt: src.CreatedAt,
|
||||
}
|
||||
if err := refundOrderMod.InsertOneRefundOrder(ctx, doc); err != nil {
|
||||
// 并发下两个请求可能同时通过上面的判重检查,由唯一索引兜底。
|
||||
// 撞上即视为重复添加,与"重复跳过不报错"的口径一致。
|
||||
if refundOrderMod.IsDuplicateKey(err) {
|
||||
stat.Skipped = 1
|
||||
return stat, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
stat.Inserted = 1
|
||||
return stat, nil
|
||||
}
|
||||
|
||||
// Del 按 _id 删除单条
|
||||
func Del(ctx context.Context, req *proto.DelRefundOrderReq) error {
|
||||
oid, err := primitive.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("id 格式错误: %w", err)
|
||||
}
|
||||
count, err := refundOrderMod.DelOneRefundOrder(ctx, bson.M{
|
||||
refundOrderMod.FN.RefundOrder.ID: oid,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return fmt.Errorf("记录不存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user