|
|
@@ -0,0 +1,164 @@ |
|
|
|
package happy_orchard |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" |
|
|
|
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/happy_orchard/md" |
|
|
|
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache" |
|
|
|
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/implement" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_model.git/src/models" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"time" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
|
func HappyOrchardGiveWater(eg *xorm.Engine, masterId string, uid int, isMultiple int) (err error) { |
|
|
|
newHappyOrchardBasicSettingDb := implement.NewHappyOrchardBasicSettingDb(eg) |
|
|
|
base, _ := newHappyOrchardBasicSettingDb.GetHappyOrchardBasicSetting() |
|
|
|
|
|
|
|
if base.IsOpenSixEnergyBall == 1 { |
|
|
|
var videoRewardSystem *md2.HappyOrchardVideoRewardSystemStruct |
|
|
|
err = json.Unmarshal([]byte(base.VideoRewardSystem), &videoRewardSystem) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if videoRewardSystem.RewardValue == "" || videoRewardSystem.RewardTotalNum == "" || videoRewardSystem.IntervalMinutes == "" || videoRewardSystem.EachRoundHour == "" { |
|
|
|
err = errors.New("视屏奖励机制设置未完全!") |
|
|
|
return |
|
|
|
} |
|
|
|
var rewardSystem []*md2.HappyOrchardVideoRewardSystemStruct |
|
|
|
err = json.Unmarshal([]byte(base.VideoRewardSystem), &rewardSystem) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if len(rewardSystem) == 0 { |
|
|
|
err = errors.New("未设置奖励机制!") |
|
|
|
return |
|
|
|
} |
|
|
|
//增加 one_circles_user_watch_records 记录 |
|
|
|
NewHappyOrchardUserWatchRecordsDb := implement.NewHappyOrchardUserWatchRecordsDb(svc.MasterDb(c)) |
|
|
|
UserWatchRecords, _ := NewHappyOrchardUserWatchRecordsDb.GetHappyOrchardUserWatchRecords(user.Info.Uid) |
|
|
|
now := time.Now() |
|
|
|
if UserWatchRecords == nil { |
|
|
|
residueWatchAdNum := zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) - 1 |
|
|
|
if residueWatchAdNum < 0 { |
|
|
|
residueWatchAdNum = zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) |
|
|
|
} |
|
|
|
_, err2 := eg.Insert(&model.OneCirclesUserWatchRecords{ |
|
|
|
Uid: uid, |
|
|
|
NextWatchAdDate: now.Add(time.Hour * time.Duration(zhios_order_relate_utils.StrToInt(videoRewardSystem.EachRoundHour))), |
|
|
|
ResidueWatchAdNum: zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) - 1, |
|
|
|
CreateAt: now, |
|
|
|
UpdateAt: now, |
|
|
|
}) |
|
|
|
if err2 != nil { |
|
|
|
return err2 |
|
|
|
} |
|
|
|
} else { |
|
|
|
residueWatchAdNum := UserWatchRecords.ResidueWatchAdNum - 1 |
|
|
|
nextWatchAdDate := UserWatchRecords.NextWatchAdDate |
|
|
|
if residueWatchAdNum == 0 { //最后一条广告 |
|
|
|
residueWatchAdNum = zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) |
|
|
|
} |
|
|
|
if residueWatchAdNum == zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum)-1 { //第一条广告 |
|
|
|
nextWatchAdDate = now.Add(time.Hour * time.Duration(zhios_order_relate_utils.StrToInt(videoRewardSystem.EachRoundHour))) |
|
|
|
} |
|
|
|
UserWatchRecords.ResidueWatchAdNum = residueWatchAdNum |
|
|
|
UserWatchRecords.NextWatchAdDate = nextWatchAdDate |
|
|
|
_, err2 := NewHappyOrchardUserWatchRecordsDb.HappyOrchardUserWatchRecordsUpdate(UserWatchRecords.Id, UserWatchRecords, "residue_watch_ad_num") |
|
|
|
if err2 != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
session := eg.NewSession() |
|
|
|
defer func() { |
|
|
|
session.Close() |
|
|
|
if err := recover(); err != nil { |
|
|
|
_ = zhios_order_relate_logx.Error(err) |
|
|
|
} |
|
|
|
}() |
|
|
|
session.Begin() |
|
|
|
amount := videoRewardSystem.RewardValue |
|
|
|
if isMultiple == 1 { |
|
|
|
amount = videoRewardSystem.MultipleRewardValue |
|
|
|
} |
|
|
|
|
|
|
|
if zhios_order_relate_utils.StrToFloat64(amount) > 0 { |
|
|
|
//奖励 |
|
|
|
bools, _ := AddWaterNums(eg, session, uid, 1, amount, "领取水滴球") |
|
|
|
if bools == false { |
|
|
|
session.Rollback() |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
err = session.Commit() |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return errors.New("事务提交失败") |
|
|
|
} |
|
|
|
|
|
|
|
redisKey := fmt.Sprintf(md2.HappyOrchardUserNextWatchAdDate, masterId, uid) |
|
|
|
var watchAdDate string |
|
|
|
if UserWatchRecords.ResidueWatchAdNum == zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) { |
|
|
|
if UserWatchRecords.NextWatchAdDate.Before(time.Now()) { |
|
|
|
watchAdDate = "" |
|
|
|
} else { |
|
|
|
watchAdDate = UserWatchRecords.NextWatchAdDate.Format("2006-01-02 15:04:05") |
|
|
|
} |
|
|
|
} else { |
|
|
|
watchAdDate = time.Now().Add(time.Duration(zhios_order_relate_utils.StrToInt64(videoRewardSystem.IntervalMinutes)) * time.Second).Format("2006-01-02 15:04:05") |
|
|
|
} |
|
|
|
cache.SetEx(redisKey, watchAdDate, 60*60*24) //TODO::默认缓存1小时 |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
func AddWaterNums(eg *xorm.Engine, sess *xorm.Session, uid, kind int, amount, title string) (bool, error) { |
|
|
|
newHappyOrchardUserWatersSess := implement.NewHappyOrchardUserWatersSess(sess) |
|
|
|
watersSess, _ := newHappyOrchardUserWatersSess.GetHappyOrchardUserWatersSess(uid) |
|
|
|
if watersSess == nil { |
|
|
|
newHappyOrchardBasicSettingDb := implement.NewHappyOrchardBasicSettingDb(eg) |
|
|
|
base, _ := newHappyOrchardBasicSettingDb.GetHappyOrchardBasicSetting() |
|
|
|
watersSess = &models.HappyOrchardUserWaters{ |
|
|
|
Uid: uid, |
|
|
|
CreateAt: time.Now().Format("2006-01-02 15:04:05"), |
|
|
|
ReplaceSeedNums: base.ReplaceSeedNums, |
|
|
|
} |
|
|
|
insert, err := sess.Insert(watersSess) |
|
|
|
if insert == 0 || err != nil { |
|
|
|
return false, errors.New("获取水滴失败") |
|
|
|
} |
|
|
|
} |
|
|
|
WaterNums := watersSess.WaterNums |
|
|
|
if kind == 1 { |
|
|
|
watersSess.WaterNums += zhios_order_relate_utils.StrToInt(amount) |
|
|
|
} else { |
|
|
|
watersSess.WaterNums -= zhios_order_relate_utils.StrToInt(amount) |
|
|
|
if watersSess.WaterNums < 0 { |
|
|
|
return false, errors.New("水滴不足") |
|
|
|
} |
|
|
|
} |
|
|
|
watersSess.UpdateAt = time.Now().Format("2006-01-02 15:04:05") |
|
|
|
update, err := sess.Where("id=?", watersSess.Id).Update(watersSess) |
|
|
|
if update == 0 || err != nil { |
|
|
|
return false, errors.New("获取水滴失败") |
|
|
|
} |
|
|
|
log := &models.HappyOrchardUserWatersLog{ |
|
|
|
Uid: uid, |
|
|
|
WaterNums: zhios_order_relate_utils.StrToInt(amount), |
|
|
|
Title: title, |
|
|
|
Kind: kind, |
|
|
|
BeforeWaterNums: WaterNums, |
|
|
|
AfterWaterNums: watersSess.WaterNums, |
|
|
|
CreateAt: time.Now().Format("2006-01-02 15:04:05"), |
|
|
|
UpdateAt: time.Now().Format("2006-01-02 15:04:05"), |
|
|
|
} |
|
|
|
insert, err := sess.Insert(log) |
|
|
|
if insert == 0 || err != nil { |
|
|
|
return false, errors.New("获取水滴失败") |
|
|
|
} |
|
|
|
return true, nil |
|
|
|
} |