修改奖励类型

This commit is contained in:
2025-06-24 19:53:39 +08:00
parent e28c44ecbb
commit 1c0b9a2d93
42 changed files with 666 additions and 1060 deletions

View File

@ -17,6 +17,7 @@ type sReward struct{}
func init() {
service.RegisterReward(New())
service.RegisterReward(New())
}
func New() service.IReward {
@ -77,10 +78,7 @@ func (s *sReward) Create(ctx context.Context, in *model.RewardCreateIn) (out *mo
id, err := dao.Rewards.Ctx(ctx).Data(do.Rewards{
RewardTypeId: in.RewardTypeId,
Name: in.Name,
Description: in.Description,
Source: in.Source,
StoreId: in.StoreId,
Value: in.Value,
Status: in.Status,
ValidFrom: in.ValidFrom,
ValidTo: in.ValidTo,
@ -98,7 +96,7 @@ func (s *sReward) Create(ctx context.Context, in *model.RewardCreateIn) (out *mo
func (s *sReward) Update(ctx context.Context, in *model.RewardUpdateIn) (out *model.RewardUpdateOut, err error) {
// 查询原始记录,确保存在,并获取 source 与 store_id 用于权限校验
data, err := dao.Rewards.Ctx(ctx).
Fields(dao.Rewards.Columns().Id, dao.Rewards.Columns().Source, dao.Rewards.Columns().StoreId).
Fields(dao.Rewards.Columns().Id, dao.Rewards.Columns().StoreId).
Where(do.Rewards{Id: in.Id}).One()
if err != nil {
return nil, ecode.Fail.Sub("查询奖励失败")
@ -107,11 +105,6 @@ func (s *sReward) Update(ctx context.Context, in *model.RewardUpdateIn) (out *mo
return nil, ecode.Params.Sub("奖励不存在")
}
// 系统奖励source=1只能由管理员修改
if data[dao.Rewards.Columns().Source].Int() == 1 && in.OperatorRole != consts.AdminRoleCode {
return nil, ecode.Params.Sub("只有管理员可以修改系统奖励")
}
storeId := data[dao.Rewards.Columns().StoreId].Int64()
// 权限校验(管理员跳过)
@ -161,14 +154,12 @@ func (s *sReward) Update(ctx context.Context, in *model.RewardUpdateIn) (out *mo
_, err = dao.Rewards.Ctx(ctx).
Where(do.Rewards{Id: in.Id}).
Data(do.Rewards{
Name: in.Name,
Description: in.Description,
Value: in.Value,
Status: in.Status,
ValidFrom: in.ValidFrom,
ValidTo: in.ValidTo,
ExpireType: in.ExpireType,
ExpireDays: in.ExpireDays,
Name: in.Name,
Status: in.Status,
ValidFrom: in.ValidFrom,
ValidTo: in.ValidTo,
ExpireType: in.ExpireType,
ExpireDays: in.ExpireDays,
}).OmitEmptyData().Update()
if err != nil {
@ -245,123 +236,13 @@ func (s *sReward) Delete(ctx context.Context, in *model.RewardDeleteIn) (out *mo
// List 奖励列表
func (s *sReward) List(ctx context.Context, in *model.RewardListIn) (out *model.RewardListOut, err error) {
var list []model.Reward
orm := dao.Rewards.Ctx(ctx)
rewardCols := dao.Rewards.Columns()
rewardTypeCols := dao.RewardTypes.Columns()
// ==== 权限校验 ====
switch in.OperatorRole {
case consts.AdminRoleCode:
// 系统管理员只能查询 source = 1 的奖励
orm = orm.Where(fmt.Sprintf("%s.%s = ?", dao.Rewards.Table(), rewardCols.Source), 1)
case consts.MerchantRoleCode, consts.StoreRoleCode:
// 合并商户和门店角色权限校验
var exist bool
if in.OperatorRole == consts.MerchantRoleCode {
exist, err = dao.MerchantAdmins.Ctx(ctx).
Where(do.MerchantAdmins{Id: in.OperatorId}).
LeftJoin(
dao.Stores.Table(),
fmt.Sprintf("%s.%s = %s.%s",
dao.MerchantAdmins.Table(), dao.MerchantAdmins.Columns().MerchantId,
dao.Stores.Table(), dao.Stores.Columns().MerchantId,
),
).
Where(fmt.Sprintf("%s.%s = ?", dao.Stores.Table(), dao.Stores.Columns().Id), in.StoreId).
Exist()
} else {
exist, err = dao.StoreAdmins.Ctx(ctx).
Where(do.StoreAdmins{Id: in.OperatorId}).
LeftJoin(
dao.Stores.Table(),
fmt.Sprintf("%s.%s = %s.%s",
dao.StoreAdmins.Table(), dao.StoreAdmins.Columns().StoreId,
dao.Stores.Table(), dao.Stores.Columns().Id,
),
).
Where(fmt.Sprintf("%s.%s = ?", dao.Stores.Table(), dao.Stores.Columns().Id), in.StoreId).
Exist()
}
if err != nil {
return nil, ecode.Fail.Sub("检查操作者权限异常")
}
if !exist {
return nil, ecode.Params.Sub("无门店权限")
}
// 商户和门店角色查询 source = 1 或 (source = 2 且 store_id = in.StoreId)
orm = orm.Where(
fmt.Sprintf(
"(%s.%s = ? OR (%s.%s = ? AND %s.%s = ?))",
dao.Rewards.Table(), rewardCols.Source,
dao.Rewards.Table(), rewardCols.Source,
dao.Rewards.Table(), rewardCols.StoreId,
),
1, 2, in.StoreId,
)
default:
return nil, ecode.Params.Sub("无效的角色")
}
// ==== 其他查询条件 ====
if in.Status != 0 {
orm = orm.Where(fmt.Sprintf("%s.%s = ?", dao.Rewards.Table(), rewardCols.Status), in.Status)
}
if in.RewardTypeId != 0 {
// 确保 reward_type_id 过滤独立应用
orm = orm.Where(fmt.Sprintf("%s.%s = ?", dao.Rewards.Table(), rewardCols.RewardTypeId), in.RewardTypeId)
}
if in.Name != "" {
orm = orm.WhereLike(rewardCols.Name, "%"+in.Name+"%")
}
// ==== 总数统计 ====
total, err := orm.Count()
if err != nil {
return nil, err
}
// ==== 分页查询 + 联表字段 ====
err = orm.Page(in.Page, in.Size).
LeftJoin(
dao.RewardTypes.Table(),
fmt.Sprintf("%s.%s = %s.%s",
dao.Rewards.Table(), rewardCols.RewardTypeId,
dao.RewardTypes.Table(), rewardTypeCols.Id,
),
).
Fields(fmt.Sprintf(
"%s.*, %s.%s AS reward_type_name",
dao.Rewards.Table(),
dao.RewardTypes.Table(), rewardTypeCols.Name,
)).
OrderDesc(rewardCols.CreatedAt).
Scan(&list)
if err != nil {
return nil, err
}
return &model.RewardListOut{
List: list,
Total: total,
}, nil
return nil, nil
}
// GetLift 领取奖励
func (s *sReward) GetLift(ctx context.Context, in *model.GetRewardIn) (out *model.GetRewardOut, err error) {
// 遍历奖励类型列表
//for _, v := range in.RewradTypeId {
// if v > 0 {
// // 发背包+兑换
//
// }
// if v > 0 {
// // 直接发背包
// }
//}
activity, err := gamelife.GetGamelifeClient(ctx).RequestActivity(ctx, &model.QQNetbarActivityIn{PopenId: in.PopenId, ServiceName: consts.GetGift, GiftParam: model.GiftParam{
TaskId: in.TaskId,
AreaId: in.AreaId,