Merge remote-tracking branch 'origin/master'
This commit is contained in:
@ -686,132 +686,76 @@ func (s *sTask) GetTaskList(ctx context.Context, in *model.GetTaskListV2In) (out
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取任务奖励列表失败")
|
||||
}
|
||||
|
||||
if int(v.UserTimes) >= v.TargetTimes && v.Status == 1 {
|
||||
completedTime := gtime.Now()
|
||||
// 判断当前用户完成情况,已完成根据任务、用户,任务类型检查是否存在用户任务记录
|
||||
orm := dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: v.TaskID})
|
||||
if v.GameTaskConfig.TimeType == 1 {
|
||||
// 每日任务
|
||||
start := gtime.Now().StartOfDay()
|
||||
end := gtime.Now().EndOfDay()
|
||||
orm = orm.WhereBetween(dao.UserTasks.Columns().CreatedAt, start, end)
|
||||
}
|
||||
value, err := orm.Fields(dao.UserTasks.Columns().Id).Value()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取用户任务失败")
|
||||
}
|
||||
|
||||
if value.IsEmpty() {
|
||||
// 不存在创建用户任务完成记录,同时组装用户可领取奖励数据,
|
||||
glog.Info(ctx, "用户不存在创建用户任务完成记录,同时组装用户可领取奖励数据")
|
||||
if err = dao.UserTaskRewards.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
storeId, err := dao.Stores.Ctx(ctx).Fields(dao.Stores.Columns().Id).Where(do.Stores{NetbarAccount: in.NetBarAccount}).Value()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("获取门店信息失败")
|
||||
}
|
||||
if storeId.IsEmpty() {
|
||||
return ecode.Fail.Sub("获取门店信息失败")
|
||||
}
|
||||
serialNumber, err := snowid.GetSnowClient().GenerateSerialNumber()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("生成流水号异常")
|
||||
}
|
||||
id, err := dao.UserTasks.Ctx(ctx).Data(do.UserTasks{
|
||||
UserId: in.UserId,
|
||||
TaskId: v.TaskID,
|
||||
StoreId: storeId.Int(),
|
||||
Status: 3,
|
||||
SerialNumber: serialNumber,
|
||||
TaskName: v.Title,
|
||||
GameId: in.Gid,
|
||||
TaskType: v.GameTaskConfig.TimeType,
|
||||
}).InsertAndGetId()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("创建用户任务记录失败")
|
||||
}
|
||||
result.TaskList[i].UserTaskId = id
|
||||
insertData := make([]do.UserTaskRewards, 0)
|
||||
// 首先拼装平台奖励
|
||||
for _, prize := range v.PrizeList {
|
||||
for _, goods := range prize.GoodsList {
|
||||
if goods.GoodsType == 37 {
|
||||
continue
|
||||
}
|
||||
insertData = append(insertData, do.UserTaskRewards{
|
||||
UserTaskId: id,
|
||||
RewardName: goods.GoodsName,
|
||||
Status: consts.RewardPendingStatus,
|
||||
IssueQuantity: goods.Num,
|
||||
Source: 1,
|
||||
RewardTypeId: goods.GoodsType,
|
||||
})
|
||||
orm := dao.UserTasks.Ctx(ctx).Where(do.UserTasks{UserId: in.UserId, TaskId: v.TaskID})
|
||||
if v.GameTaskConfig.TimeType == 1 {
|
||||
// 每日任务
|
||||
start := gtime.Now().StartOfDay()
|
||||
end := gtime.Now().EndOfDay()
|
||||
orm = orm.WhereBetween(dao.UserTasks.Columns().CreatedAt, start, end)
|
||||
}
|
||||
one, err := orm.Fields(dao.UserTasks.Columns().Id, dao.UserTasks.Columns().UserTimes).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("获取用户任务失败")
|
||||
}
|
||||
if one.IsEmpty() || one["id"].IsEmpty() {
|
||||
result.TaskList[i].Status = 1
|
||||
} else {
|
||||
if v.Status == 2 {
|
||||
if v.UserTimes-one["user_times"].Int64() >= v.TargetTimes {
|
||||
completeTime := gtime.Now()
|
||||
// 判断是否完成任务,修改奖励下发记录状态为 2
|
||||
if err := dao.UserTasks.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
if _, err := dao.UserTasks.Ctx(ctx).WherePri(one["id"].Int64()).Data(do.UserTasks{Status: 3}).Update(); err != nil {
|
||||
return ecode.Fail.Sub("修改用户任务状态失败")
|
||||
}
|
||||
}
|
||||
// 拼装门店奖励数据
|
||||
if len(result.TaskList[i].Rewards) > 0 {
|
||||
for _, reward := range result.TaskList[i].Rewards {
|
||||
var quantity uint64
|
||||
quantity, err = CalculateNetfeeRewardQuantity(ctx, in.UserId, in.StoreId, &reward, completedTime)
|
||||
if err != nil {
|
||||
quantity = reward.GrantQuantity
|
||||
}
|
||||
in := do.UserTaskRewards{
|
||||
RewardId: reward.Id,
|
||||
UserTaskId: id,
|
||||
RewardName: reward.Name,
|
||||
Status: consts.RewardPendingStatus,
|
||||
IssueQuantity: quantity,
|
||||
Source: 2,
|
||||
RewardTypeId: reward.RewardTypeId,
|
||||
}
|
||||
if reward.RewardTypeId == 37 {
|
||||
in.Source = 1
|
||||
}
|
||||
insertData = append(insertData, in)
|
||||
}
|
||||
}
|
||||
if _, err = dao.UserTaskRewards.Ctx(ctx).Data(insertData).Insert(); err != nil {
|
||||
return ecode.Fail.Sub("创建用户任务奖励记录失败")
|
||||
}
|
||||
return
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
result.TaskList[i].UserTaskId = value.Int64()
|
||||
// 存在该任务记录,则判断该任务记录对应的奖励是否都已领取,需要修改任务状态
|
||||
glog.Info(ctx, "用户存在该任务记录,则判断该任务记录对应的奖励是否都已领取,需要修改任务状态")
|
||||
if v.Status != 1 && v.Status != 2 {
|
||||
count, err := dao.UserTaskRewards.Ctx(ctx).Where(do.UserTaskRewards{UserTaskId: value.Int()}).WhereIn(dao.UserTasks.Columns().Status, []int{2, 3, 5}).Count()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询用户门店任务奖励失败")
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
result.TaskList[i].Status = 2
|
||||
} else {
|
||||
result.TaskList[i].Status = 3
|
||||
all, err := dao.UserTaskRewards.Ctx(ctx).Where(do.UserTaskRewards{UserTaskId: one["id"].Int64()}).Fields(dao.UserTaskRewards.Columns().Id, dao.UserTaskRewards.Columns().RewardId).All()
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("获取用户任务奖励失败")
|
||||
}
|
||||
for _, record := range all {
|
||||
updateData := do.UserTaskRewards{Status: consts.RewardPendingStatus}
|
||||
rewardTypeCode, err2 := dao.Rewards.Ctx(ctx).WherePri(record["reward_id"].Int64()).
|
||||
InnerJoin(
|
||||
dao.RewardTypes.Table(),
|
||||
fmt.Sprintf("%s.%s = %s.%s",
|
||||
dao.RewardTypes.Table(), dao.RewardTypes.Columns().Id,
|
||||
dao.Rewards.Table(), dao.Rewards.Columns().RewardTypeId,
|
||||
),
|
||||
).Fields(dao.RewardTypes.Columns().Code).Value()
|
||||
if err2 != nil {
|
||||
return ecode.Fail.Sub("获取用户任务奖励失败")
|
||||
}
|
||||
if rewardTypeCode.String() == consts.NetfeeCode {
|
||||
if quantity, err := CalculateNetfeeRewardQuantity(ctx, in.UserId, in.StoreId, record["reward_id"].Int64(), completeTime); err == nil && quantity != 0 {
|
||||
updateData.IssueQuantity = quantity
|
||||
}
|
||||
}
|
||||
if _, err := dao.UserTaskRewards.Ctx(ctx).Data(updateData).Update(); err != nil {
|
||||
return ecode.Fail.Sub("修改用户任务奖励失败")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.TaskList[i].Status = 2
|
||||
} else {
|
||||
result.TaskList[i].Status = 1
|
||||
}
|
||||
if err = dao.UserTaskRewards.Transaction(ctx, func(ctx context.Context, tx gdb.TX) (err error) {
|
||||
_, err = dao.UserTasks.Ctx(ctx).WherePri(value.Int64()).Where(dao.UserTasks.Columns().Status).WhereNot(dao.UserTasks.Columns().Status, 2).Update(do.UserTasks{
|
||||
Status: 3,
|
||||
})
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("修改用户任务状态失败")
|
||||
}
|
||||
_, err = dao.UserTaskRewards.Ctx(ctx).Where(do.UserTaskRewards{UserTaskId: value.Int()}).Where(dao.UserTaskRewards.Columns().Status, 1).Update(do.UserTaskRewards{
|
||||
Status: 2,
|
||||
})
|
||||
if err != nil {
|
||||
return ecode.Fail.Sub("修改用户任务奖励状态失败")
|
||||
}
|
||||
return
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
result.TaskList[i].UserTimes -= one["user_times"].Int64()
|
||||
} else if v.Status == 3 {
|
||||
// 查询该用户任务记录奖励都已发放
|
||||
count, err := dao.UserTaskRewards.Ctx(ctx).Where(do.UserTaskRewards{UserTaskId: one["id"].Int64()}).Where(dao.UserTaskRewards.Columns().Status, []int{2, 3, 5}).Count()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查询用户门店任务奖励失败")
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
result.TaskList[i].Status = 2
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
out.PageIdx = result.PageIdx
|
||||
@ -926,19 +870,44 @@ func (s *sTask) SyncTaskFromGamelife(ctx context.Context) (out *model.SyncTaskOu
|
||||
//delete(taskMap, "PrizeList")
|
||||
//delete(taskMap, "prize_list")
|
||||
delete(taskMap, "reward")
|
||||
|
||||
_, err = dao.Tasks.Ctx(ctx).Data(do.Tasks{
|
||||
TaskId: task.TaskID,
|
||||
StoreId: store["id"].Int(),
|
||||
GameId: game["game_id"].Int(),
|
||||
NetbarAcconut: store["netbar_account"].String(),
|
||||
Task: gconv.Bytes(taskMap),
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
if err := dao.Tasks.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
_, err = dao.Tasks.Ctx(ctx).Data(do.Tasks{
|
||||
TaskId: task.TaskID,
|
||||
StoreId: store["id"].Int(),
|
||||
GameId: game["game_id"].Int(),
|
||||
NetbarAcconut: store["netbar_account"].String(),
|
||||
Task: gconv.Bytes(taskMap),
|
||||
}).Insert()
|
||||
for _, prize := range task.PrizeList {
|
||||
for _, goods := range prize.GoodsList {
|
||||
if goods.GoodsType == 37 {
|
||||
continue
|
||||
}
|
||||
if id, err := dao.Rewards.Ctx(ctx).Data(do.Rewards{
|
||||
ImageUrl: consts.PlatformRewards[goods.GoodsType],
|
||||
Name: goods.GoodsName,
|
||||
RewardTypeId: goods.GoodsType,
|
||||
GameId: game["game_id"].Int(),
|
||||
Status: 1,
|
||||
GrantQuantity: goods.Num,
|
||||
Source: 1,
|
||||
}).InsertAndGetId(); err == nil {
|
||||
if _, err := dao.TaskRewards.Ctx(ctx).Data(do.TaskRewards{
|
||||
TaskId: task.TaskID,
|
||||
RewardId: id,
|
||||
}).Insert(); err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
glog.Errorf(ctx, "插入任务失败,task_id=%s,网吧=%s,游戏=%s,错误=%v",
|
||||
task.TaskID, store["netbar_account"].String(), game["game_name"].String(), err)
|
||||
continue
|
||||
}
|
||||
|
||||
insertCount++
|
||||
}
|
||||
|
||||
@ -951,21 +920,7 @@ func (s *sTask) SyncTaskFromGamelife(ctx context.Context) (out *model.SyncTaskOu
|
||||
wg.Wait()
|
||||
return
|
||||
}
|
||||
func CalculateNetfeeRewardQuantity(ctx context.Context, userId int64, storeId int64, reward *model.SimpleReward, completedTime *gtime.Time) (uint64, error) {
|
||||
const rewardTypeCode = consts.NetfeeCode
|
||||
|
||||
// 判断是否是门店网费奖励
|
||||
exist, err := dao.RewardTypes.Ctx(ctx).
|
||||
WherePri(reward.RewardTypeId).
|
||||
Where(do.RewardTypes{Code: rewardTypeCode}).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return 0, ecode.Fail.Sub("获取奖励类型失败")
|
||||
}
|
||||
if !exist {
|
||||
// 不是网费奖励,返回当前奖励默认值
|
||||
return reward.GrantQuantity, nil
|
||||
}
|
||||
func CalculateNetfeeRewardQuantity(ctx context.Context, userId int64, storeId, rewardId int64, completedTime *gtime.Time) (uint64, error) {
|
||||
|
||||
// 获取当前小时 & 星期几(0=周日)
|
||||
hour := completedTime.Hour()
|
||||
@ -992,7 +947,7 @@ func CalculateNetfeeRewardQuantity(ctx context.Context, userId int64, storeId in
|
||||
return 0, ecode.Fail.Sub("获取会员等级失败")
|
||||
}
|
||||
if levelId.IsEmpty() {
|
||||
return reward.GrantQuantity, nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 获取区域ID
|
||||
@ -1004,7 +959,7 @@ func CalculateNetfeeRewardQuantity(ctx context.Context, userId int64, storeId in
|
||||
return 0, ecode.Fail.Sub("获取区域失败")
|
||||
}
|
||||
if areaId.IsEmpty() {
|
||||
return reward.GrantQuantity, nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 获取门店该区域、等级、奖励配置
|
||||
@ -1013,7 +968,7 @@ func CalculateNetfeeRewardQuantity(ctx context.Context, userId int64, storeId in
|
||||
StoreId: storeId,
|
||||
AreaId: areaId.Int(),
|
||||
MemberLevelId: levelId.Int(),
|
||||
RewardId: reward.Id,
|
||||
RewardId: rewardId,
|
||||
}).
|
||||
Fields(dao.StoreNetfeeAreaLevel.Columns().PriceData).
|
||||
Value()
|
||||
@ -1023,7 +978,7 @@ func CalculateNetfeeRewardQuantity(ctx context.Context, userId int64, storeId in
|
||||
|
||||
// 若配置为空,返回默认值
|
||||
if priceDataStr.IsEmpty() {
|
||||
return reward.GrantQuantity, nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// 解析 priceData
|
||||
|
||||
Reference in New Issue
Block a user