118 lines
3.2 KiB
Go
118 lines
3.2 KiB
Go
package agent
|
||
|
||
import (
|
||
"context"
|
||
"ycc-server/app/main/model"
|
||
"ycc-server/common/ctxdata"
|
||
"ycc-server/common/xerr"
|
||
|
||
"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 GetWhitelistListLogic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
func NewGetWhitelistListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhitelistListLogic {
|
||
return &GetWhitelistListLogic{
|
||
Logger: logx.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (l *GetWhitelistListLogic) GetWhitelistList(req *types.GetWhitelistListReq) (resp *types.GetWhitelistListResp, err error) {
|
||
userID, err := ctxdata.GetUidFromCtx(l.ctx)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
|
||
}
|
||
|
||
// 1. 验证是否为代理(查询白名单列表需要是代理)
|
||
_, 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. 构建查询条件
|
||
// 注意:系统会自动处理del_state,不需要手动添加
|
||
builder := l.svcCtx.UserFeatureWhitelistModel.SelectBuilder().
|
||
Where("user_id = ?", userID)
|
||
|
||
// 如果指定了身份证号,添加筛选条件
|
||
if req.IdCard != "" {
|
||
builder = builder.Where("id_card = ?", req.IdCard)
|
||
}
|
||
|
||
// 3. 分页查询
|
||
total, err := l.svcCtx.UserFeatureWhitelistModel.FindCount(l.ctx, builder, "id")
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询总数失败, %v", err)
|
||
}
|
||
|
||
whitelists, err := l.svcCtx.UserFeatureWhitelistModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "create_time DESC")
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询白名单列表失败, %v", err)
|
||
}
|
||
|
||
// 4. 组装响应
|
||
list := make([]types.WhitelistItem, 0, len(whitelists))
|
||
for _, w := range whitelists {
|
||
statusText := "生效"
|
||
if w.Status == 2 {
|
||
statusText = "已失效"
|
||
}
|
||
|
||
list = append(list, types.WhitelistItem{
|
||
Id: w.Id,
|
||
IdCard: w.IdCard,
|
||
FeatureId: w.FeatureId,
|
||
FeatureApiId: w.FeatureApiId,
|
||
FeatureName: "", // 需要从feature表查询,或者冗余存储
|
||
Amount: w.Amount,
|
||
Status: w.Status,
|
||
StatusText: statusText,
|
||
CreateTime: w.CreateTime.Format("2006-01-02 15:04:05"),
|
||
})
|
||
}
|
||
|
||
// 5. 查询feature名称(批量查询优化)
|
||
if len(list) > 0 {
|
||
featureIds := make([]string, 0, len(list))
|
||
featureMap := make(map[string]string) // feature_id -> feature_name
|
||
for _, item := range list {
|
||
featureIds = append(featureIds, item.FeatureId)
|
||
}
|
||
|
||
// 批量查询feature
|
||
for _, featureId := range featureIds {
|
||
feature, err := l.svcCtx.FeatureModel.FindOne(l.ctx, featureId)
|
||
if err == nil {
|
||
featureMap[featureId] = feature.Name
|
||
}
|
||
}
|
||
|
||
// 填充feature名称
|
||
for i := range list {
|
||
if name, ok := featureMap[list[i].FeatureId]; ok {
|
||
list[i].FeatureName = name
|
||
}
|
||
}
|
||
}
|
||
|
||
return &types.GetWhitelistListResp{
|
||
Total: total,
|
||
List: list,
|
||
}, nil
|
||
}
|
||
|