2025-11-27 13:09:54 +08:00
|
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"ycc-server/app/main/model"
|
|
|
|
|
|
"ycc-server/common/ctxdata"
|
|
|
|
|
|
"ycc-server/common/globalkey"
|
|
|
|
|
|
"ycc-server/common/xerr"
|
2025-12-02 19:57:10 +08:00
|
|
|
|
"ycc-server/pkg/lzkit/crypto"
|
2025-11-27 13:09:54 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
|
|
|
|
"ycc-server/app/main/api/internal/svc"
|
|
|
|
|
|
"ycc-server/app/main/api/internal/types"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type GetRebateListLogic struct {
|
|
|
|
|
|
logx.Logger
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewGetRebateListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRebateListLogic {
|
|
|
|
|
|
return &GetRebateListLogic{
|
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *GetRebateListLogic) GetRebateList(req *types.GetRebateListReq) (resp *types.GetRebateListResp, err error) {
|
|
|
|
|
|
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 获取代理信息
|
|
|
|
|
|
agent, err := l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("您不是代理"), "")
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败, %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 构建查询条件
|
|
|
|
|
|
builder := l.svcCtx.AgentRebateModel.SelectBuilder().
|
2025-12-02 19:57:10 +08:00
|
|
|
|
Where("agent_id = ? AND del_state = ?", agent.Id, globalkey.DelStateNo)
|
|
|
|
|
|
|
|
|
|
|
|
// 如果指定了返佣类型,则按类型筛选(1, 2, 3)
|
|
|
|
|
|
if req.RebateType != nil {
|
|
|
|
|
|
builder = builder.Where("rebate_type = ?", *req.RebateType)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
builder = builder.OrderBy("create_time DESC")
|
2025-11-27 13:09:54 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 分页查询
|
|
|
|
|
|
page := req.Page
|
|
|
|
|
|
if page <= 0 {
|
|
|
|
|
|
page = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
pageSize := req.PageSize
|
|
|
|
|
|
if pageSize <= 0 {
|
|
|
|
|
|
pageSize = 20
|
|
|
|
|
|
}
|
|
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 查询总数
|
2025-12-02 19:57:10 +08:00
|
|
|
|
total, err := l.svcCtx.AgentRebateModel.FindCount(l.ctx, builder, "id")
|
2025-11-27 13:09:54 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询返佣总数失败, %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 查询列表
|
|
|
|
|
|
builder = builder.Limit(uint64(pageSize)).Offset(uint64(offset))
|
|
|
|
|
|
rebates, err := l.svcCtx.AgentRebateModel.FindAll(l.ctx, builder, "")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询返佣列表失败, %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 6. 组装响应
|
|
|
|
|
|
var list []types.RebateItem
|
|
|
|
|
|
for _, rebate := range rebates {
|
2025-12-02 19:57:10 +08:00
|
|
|
|
// 查询订单号
|
|
|
|
|
|
orderNo := ""
|
2025-12-09 18:55:28 +08:00
|
|
|
|
if rebate.OrderId != "" {
|
2025-12-02 19:57:10 +08:00
|
|
|
|
order, err := l.svcCtx.OrderModel.FindOne(l.ctx, rebate.OrderId)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
orderNo = order.OrderNo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询来源代理手机号和等级
|
|
|
|
|
|
sourceAgentMobile := ""
|
|
|
|
|
|
sourceAgentLevel := int64(0)
|
2025-12-09 18:55:28 +08:00
|
|
|
|
if rebate.SourceAgentId != "" {
|
2025-12-02 19:57:10 +08:00
|
|
|
|
sourceAgent, err := l.svcCtx.AgentModel.FindOne(l.ctx, rebate.SourceAgentId)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
if sourceAgent.Mobile != "" {
|
|
|
|
|
|
decrypted, err := crypto.DecryptMobile(sourceAgent.Mobile, l.svcCtx.Config.Encrypt.SecretKey)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
sourceAgentMobile = decrypted
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
sourceAgentLevel = sourceAgent.Level
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-27 13:09:54 +08:00
|
|
|
|
list = append(list, types.RebateItem{
|
2025-12-02 19:57:10 +08:00
|
|
|
|
Id: rebate.Id,
|
|
|
|
|
|
SourceAgentId: rebate.SourceAgentId,
|
|
|
|
|
|
SourceAgentMobile: sourceAgentMobile,
|
|
|
|
|
|
SourceAgentLevel: sourceAgentLevel,
|
|
|
|
|
|
OrderId: rebate.OrderId,
|
|
|
|
|
|
OrderNo: orderNo,
|
|
|
|
|
|
RebateType: rebate.RebateType,
|
|
|
|
|
|
Amount: rebate.RebateAmount,
|
|
|
|
|
|
CreateTime: rebate.CreateTime.Format("2006-01-02 15:04:05"),
|
2025-11-27 13:09:54 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &types.GetRebateListResp{
|
|
|
|
|
|
Total: total,
|
|
|
|
|
|
List: list,
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|