|
|
@@ -0,0 +1,265 @@ |
|
|
|
package egg_energy |
|
|
|
|
|
|
|
import ( |
|
|
|
"code.fnuoos.com/EggPlanet/egg_models.git/src/implement" |
|
|
|
"code.fnuoos.com/EggPlanet/egg_models.git/src/model" |
|
|
|
zhios_order_relate_utils "code.fnuoos.com/EggPlanet/egg_models.git/utils" |
|
|
|
"code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" |
|
|
|
md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md" |
|
|
|
"code.fnuoos.com/EggPlanet/egg_system_rules.git/rule" |
|
|
|
"code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" |
|
|
|
"code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache" |
|
|
|
zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx" |
|
|
|
"code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
"xorm.io/xorm" |
|
|
|
) |
|
|
|
|
|
|
|
// HomePageWatchOverAd 观看完广告 |
|
|
|
func HomePageWatchOverAd(engine *xorm.Engine, req md.HomePageWatchOverAdReq) error { |
|
|
|
redisKey := fmt.Sprintf(md.UserNextWatchAdDate, req.UID) |
|
|
|
nextWatchAdDate, _ := cache.GetString(redisKey) |
|
|
|
if nextWatchAdDate != "" { |
|
|
|
nextWatchAdDateValue := zhios_order_relate_utils.String2Time(nextWatchAdDate) |
|
|
|
if nextWatchAdDateValue.After(time.Now()) { |
|
|
|
return errors.New("非观看广告时间") |
|
|
|
} |
|
|
|
} |
|
|
|
ch, err := rabbit.Cfg.Pool.GetChannel() |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
defer ch.Release() |
|
|
|
|
|
|
|
err = settlementPublicGiveActivityCoin(engine, req.UID, ch) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func settlementPublicGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit.Channel) error { |
|
|
|
//1、查找 `basic_setting` 基础设置 |
|
|
|
settingDb := implement.NewEggEnergyBasicSettingDb(engine) |
|
|
|
setting, err := settingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{ |
|
|
|
"key": "is_open", |
|
|
|
"value": 1, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
if setting.VideoRewardIsOpen == 1 { |
|
|
|
var videoRewardSystem *md.VideoRewardSystemStruct |
|
|
|
err = json.Unmarshal([]byte(setting.VideoRewardSystem), &videoRewardSystem) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if videoRewardSystem.RewardValue == "" || videoRewardSystem.RewardTotalNum == "" || videoRewardSystem.IntervalMinutes == "" || videoRewardSystem.EachRoundHour == "" { |
|
|
|
err = errors.New("视屏奖励机制设置未完全!") |
|
|
|
return err |
|
|
|
} |
|
|
|
var rewardSystem []*md.RewardSystemStruct |
|
|
|
err = json.Unmarshal([]byte(setting.RewardSystem), &rewardSystem) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if len(rewardSystem) == 0 { |
|
|
|
err = errors.New("未设置奖励机制!") |
|
|
|
return err |
|
|
|
} |
|
|
|
rewardValue := zhios_order_relate_utils.StrToFloat64(videoRewardSystem.RewardValue) //奖励多少个活跃积分 |
|
|
|
var rewardSystemMap = map[int]*md.RewardSystemStruct{} |
|
|
|
for _, v := range rewardSystem { |
|
|
|
rewardSystemMap[v.Level] = v |
|
|
|
} |
|
|
|
publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine) |
|
|
|
publicPlatoonUserRelation, err1 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{ |
|
|
|
"key": "uid", |
|
|
|
"value": uid, |
|
|
|
}) |
|
|
|
if err1 != nil { |
|
|
|
return err1 |
|
|
|
} |
|
|
|
|
|
|
|
var rewardFather []struct { |
|
|
|
Uid int `json:"uid"` //用户id |
|
|
|
RewardValue float64 `json:"reward_value"` //奖励值 |
|
|
|
} |
|
|
|
|
|
|
|
var fatherUids []string |
|
|
|
if publicPlatoonUserRelation != nil { |
|
|
|
fatherUids = strings.Split(publicPlatoonUserRelation.FatherUid, "-") |
|
|
|
} |
|
|
|
signInDb := implement.NewEggSignInDb(engine) |
|
|
|
userRelateDb := implement.NewUserRelateDb(engine) |
|
|
|
for k, id := range fatherUids { |
|
|
|
tempRelation, err2 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{ |
|
|
|
"key": "id", |
|
|
|
"value": id, |
|
|
|
}) |
|
|
|
if err2 != nil { |
|
|
|
return err2 |
|
|
|
} |
|
|
|
|
|
|
|
if tempRelation == nil { |
|
|
|
continue |
|
|
|
} |
|
|
|
if tempRelation.Uid <= 0 { |
|
|
|
//待填充位 |
|
|
|
continue |
|
|
|
} |
|
|
|
|
|
|
|
fatherUid := zhios_order_relate_utils.Int64ToStr(tempRelation.Uid) |
|
|
|
fatherReward := rewardSystemMap[k+1] |
|
|
|
//TODO::判断是否活跃 |
|
|
|
has, _, err := signInDb.EggSignINGetOneByTimeAndUid("", time.Now().Format("2006-01-02 15:04:05"), zhios_order_relate_utils.StrToInt64(fatherUid), 1) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
if !has { |
|
|
|
//不活跃不需要奖励 |
|
|
|
continue |
|
|
|
} |
|
|
|
//判断是否满足奖励条件 |
|
|
|
userCount, _, err := userRelateDb.SumUserRelateByParentUid(fatherUid) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
if fatherReward != nil && userCount >= zhios_order_relate_utils.StrToInt64(fatherReward.RewardCondition) { |
|
|
|
fatherRewardValue := rewardValue * (zhios_order_relate_utils.StrToFloat64(fatherReward.RewardValue) / 100) |
|
|
|
rewardFather = append(rewardFather, struct { |
|
|
|
Uid int `json:"uid"` //用户id |
|
|
|
RewardValue float64 `json:"reward_value"` //奖励值 |
|
|
|
}{ |
|
|
|
Uid: zhios_order_relate_utils.StrToInt(fatherUid), |
|
|
|
RewardValue: fatherRewardValue, |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//增加 circles_user_watch_records 记录 |
|
|
|
watchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(engine) |
|
|
|
watchRecord, err := watchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{ |
|
|
|
"key": "uid", |
|
|
|
"value": uid, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
now := time.Now() |
|
|
|
if watchRecord == nil { |
|
|
|
residueWatchAdNum := zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) - 1 |
|
|
|
if residueWatchAdNum < 0 { |
|
|
|
residueWatchAdNum = zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) |
|
|
|
} |
|
|
|
tempM := model.EggEnergyUserWatchRecords{ |
|
|
|
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.Format("2006-01-02 15:04:05"), |
|
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
} |
|
|
|
_, err := watchRecordsDb.EggEnergyUserWatchRecordsInsert(&tempM) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} else { |
|
|
|
residueWatchAdNum := watchRecord.ResidueWatchAdNum - 1 |
|
|
|
nextWatchAdDate := watchRecord.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))) |
|
|
|
} |
|
|
|
watchRecord.ResidueWatchAdNum = residueWatchAdNum |
|
|
|
watchRecord.NextWatchAdDate = nextWatchAdDate |
|
|
|
_, err := watchRecordsDb.EggEnergyUserWatchRecordsUpdate(watchRecord.Id, watchRecord, "residue_watch_ad_num", "next_watch_ad_date") |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
session := engine.NewSession() |
|
|
|
defer func() { |
|
|
|
session.Close() |
|
|
|
if err := recover(); err != nil { |
|
|
|
_ = zhios_order_relate_logx.Error(err) |
|
|
|
} |
|
|
|
}() |
|
|
|
session.Begin() |
|
|
|
|
|
|
|
//给相应的用户加上"个人"活跃积分 |
|
|
|
err = rule.DealUserVirtualCoin(session, md2.DealUserVirtualCoinReq{ |
|
|
|
Kind: "add", |
|
|
|
Title: enum.EggEnergyWatchAdRewardPersonalActiveCoin.String(), |
|
|
|
TransferType: int(enum.EggEnergyWatchAdRewardPersonalActiveCoin), |
|
|
|
CoinId: setting.PersonEggPointsCoinId, |
|
|
|
Uid: uid, |
|
|
|
Amount: rewardValue, |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
fmt.Println("after watch over ad to deal user virtual coin err:::::2222", err) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//给相应的用户加上"团队"活跃积分 |
|
|
|
for _, vv := range rewardFather { |
|
|
|
//TODO::推入rabbitmq 异步处理 |
|
|
|
ch.Publish(md.EggEnergyExchange, md.EggEnergyStructForDealUserVirtualCoinData{ |
|
|
|
Kind: "add", |
|
|
|
Title: enum.EggEnergyWatchAdRewardTeamActiveCoin.String(), |
|
|
|
TransferType: int(enum.EggEnergyWatchAdRewardTeamActiveCoin), |
|
|
|
CoinId: setting.TeamEggPointsCoinId, |
|
|
|
Uid: int64(vv.Uid), |
|
|
|
Amount: vv.RewardValue, |
|
|
|
}, md.EggEnergyRoutKeyForDealUserVirtualCoinData) |
|
|
|
//err = DealUserCoin(session, md.DealUserCoinReq{ |
|
|
|
// Kind: "add", |
|
|
|
// Mid: masterId, |
|
|
|
// Title: md.OneCirclesWatchAdRewardTeamActiveCoin, |
|
|
|
// TransferType: md.OneCirclesWatchAdRewardTeamActiveCoinForUserVirtualCoinFlow, |
|
|
|
// OrdId: "", |
|
|
|
// CoinId: oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId, |
|
|
|
// Uid: vv.Uid, |
|
|
|
// Amount: vv.RewardValue, |
|
|
|
//}) |
|
|
|
//if err != nil { |
|
|
|
// _ = session.Rollback() |
|
|
|
// fmt.Println("err:::::2222", err) |
|
|
|
// return err |
|
|
|
//} |
|
|
|
} |
|
|
|
err = session.Commit() |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return errors.New("事务提交失败") |
|
|
|
} |
|
|
|
if watchRecord == nil { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
redisKey := fmt.Sprintf(md.UserNextWatchAdDate, uid) |
|
|
|
var watchAdDate string |
|
|
|
if watchRecord.ResidueWatchAdNum == zhios_order_relate_utils.StrToInt(videoRewardSystem.RewardTotalNum) { |
|
|
|
if watchRecord.NextWatchAdDate.Before(time.Now()) { |
|
|
|
watchAdDate = "" |
|
|
|
} else { |
|
|
|
watchAdDate = watchRecord.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") |
|
|
|
//watchAdDate = oneCirclesUserWatchRecords.NextWatchAdDate.Format("2006-01-02 15:04:05") |
|
|
|
} |
|
|
|
cache.SetEx(redisKey, watchAdDate, 60*60*24) //TODO::默认缓存1小时 |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |