蛋蛋星球-制度模式
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

245 řádky
9.0 KiB

  1. package egg_energy
  2. import (
  3. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  5. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  6. "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  8. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  9. egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache"
  11. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
  12. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  13. "encoding/json"
  14. "errors"
  15. "fmt"
  16. "strings"
  17. "time"
  18. "xorm.io/xorm"
  19. )
  20. // SettlementGiveActivityCoin 结算观看激励广告得到蛋蛋积分
  21. func SettlementGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit.Channel) (err error) {
  22. //1、查找 `egg_energy_basic_setting` 基础设置
  23. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  24. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  25. "key": "is_open",
  26. "value": 1,
  27. })
  28. if err != nil {
  29. return
  30. }
  31. if eggEnergyBasicSetting == nil {
  32. err = errors.New("蛋蛋能量设置未开启!")
  33. return
  34. }
  35. if eggEnergyBasicSetting.VideoRewardIsOpen == 1 {
  36. var videoRewardSystem *md2.VideoRewardSystemStruct
  37. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VideoRewardSystem), &videoRewardSystem)
  38. if err != nil {
  39. return
  40. }
  41. if videoRewardSystem.RewardValue == "" || videoRewardSystem.RewardTotalNum == "" || videoRewardSystem.IntervalMinutes == "" || videoRewardSystem.EachRoundHour == "" {
  42. err = errors.New("视屏奖励机制设置未完全!")
  43. return
  44. }
  45. var rewardSystem []*md2.RewardSystemStruct
  46. err = json.Unmarshal([]byte(eggEnergyBasicSetting.RewardSystem), &rewardSystem)
  47. if err != nil {
  48. return
  49. }
  50. if len(rewardSystem) == 0 {
  51. err = errors.New("未设置奖励机制!")
  52. return
  53. }
  54. rewardValue := egg_system_rules.StrToFloat64(videoRewardSystem.RewardValue) //奖励多少个活跃积分
  55. var rewardSystemMap = map[int]*md2.RewardSystemStruct{}
  56. for _, v := range rewardSystem {
  57. rewardSystemMap[v.Level] = v
  58. }
  59. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  60. publicPlatoonUserRelation, err1 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  61. "key": "uid",
  62. "value": uid,
  63. })
  64. if err1 != nil {
  65. return err1
  66. }
  67. var rewardFather []struct {
  68. Uid int64 `json:"uid"` //用户id
  69. RewardValue float64 `json:"reward_value"` //奖励值
  70. }
  71. var fatherUids []string
  72. if publicPlatoonUserRelation != nil {
  73. fatherUids = strings.Split(publicPlatoonUserRelation.FatherUid, "-")
  74. }
  75. for k, id := range fatherUids {
  76. tmpOneCirclesPublicPlatoonUserRelation, err11 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  77. "key": "id",
  78. "value": id,
  79. })
  80. if err11 != nil {
  81. return err11
  82. }
  83. if tmpOneCirclesPublicPlatoonUserRelation == nil {
  84. continue
  85. }
  86. if tmpOneCirclesPublicPlatoonUserRelation.Uid <= 0 {
  87. //待填充位
  88. continue
  89. }
  90. fatherUid := egg_system_rules.Int64ToStr(tmpOneCirclesPublicPlatoonUserRelation.Uid)
  91. fatherReward := rewardSystemMap[k+1]
  92. //TODO::判断是否活跃
  93. var m model.EggSignIn
  94. has, err33 := engine.Where("uid =?", fatherUid).And("end_time >=?", time.Now().Format("2006-01-02 15:04:05")).Get(&m)
  95. if err33 != nil {
  96. return err33
  97. }
  98. if !has {
  99. //不活跃不需要奖励
  100. continue
  101. }
  102. //判断是否满足奖励条件
  103. userRelateDb := implement.NewUserRelateDb(engine)
  104. userCount, _, err2 := userRelateDb.SumUserRelateByParentUid(fatherUid)
  105. if err2 != nil {
  106. return err2
  107. }
  108. if fatherReward != nil && userCount >= egg_system_rules.StrToInt64(fatherReward.RewardCondition) {
  109. fatherRewardValue := rewardValue * (egg_system_rules.StrToFloat64(fatherReward.RewardValue) / 100)
  110. rewardFather = append(rewardFather, struct {
  111. Uid int64 `json:"uid"` //用户id
  112. RewardValue float64 `json:"reward_value"` //奖励值
  113. }{
  114. Uid: egg_system_rules.StrToInt64(fatherUid),
  115. RewardValue: fatherRewardValue,
  116. })
  117. }
  118. }
  119. //增加 egg_user_watch_records 记录
  120. eggEnergyUserWatchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(engine)
  121. eggEnergyUserWatchRecords, err1 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  122. "key": "uid",
  123. "value": uid,
  124. })
  125. if err1 != nil {
  126. return err1
  127. }
  128. now := time.Now()
  129. if eggEnergyUserWatchRecords == nil {
  130. residueWatchAdNum := egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) - 1
  131. if residueWatchAdNum < 0 {
  132. residueWatchAdNum = egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)
  133. }
  134. _, err2 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsInsert(&model.EggEnergyUserWatchRecords{
  135. Uid: uid,
  136. NextWatchAdDate: now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt(videoRewardSystem.EachRoundHour))),
  137. ResidueWatchAdNum: egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) - 1,
  138. CreateAt: now,
  139. UpdateAt: now,
  140. })
  141. if err2 != nil {
  142. return err2
  143. }
  144. } else {
  145. residueWatchAdNum := eggEnergyUserWatchRecords.ResidueWatchAdNum - 1
  146. nextWatchAdDate := eggEnergyUserWatchRecords.NextWatchAdDate
  147. if residueWatchAdNum == 0 { //最后一条广告
  148. residueWatchAdNum = egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)
  149. }
  150. if residueWatchAdNum == egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)-1 { //第一条广告
  151. nextWatchAdDate = now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt(videoRewardSystem.EachRoundHour)))
  152. }
  153. eggEnergyUserWatchRecords.ResidueWatchAdNum = residueWatchAdNum
  154. eggEnergyUserWatchRecords.NextWatchAdDate = nextWatchAdDate
  155. _, err2 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsUpdate(eggEnergyUserWatchRecords.Id, eggEnergyUserWatchRecords, "residue_watch_ad_num", "next_watch_ad_date")
  156. if err2 != nil {
  157. return err2
  158. }
  159. }
  160. session := engine.NewSession()
  161. defer func() {
  162. session.Close()
  163. if err := recover(); err != nil {
  164. _ = zhios_order_relate_logx.Error(err)
  165. }
  166. }()
  167. session.Begin()
  168. //给相应的用户加上"个人"活跃积分
  169. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  170. Kind: "add",
  171. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardPersonalActiveCoin),
  172. TransferType: int(enum.EggEnergyWatchAdRewardPersonalActiveCoin),
  173. CoinId: eggEnergyBasicSetting.PersonEggPointsCoinId,
  174. Uid: uid,
  175. Amount: rewardValue,
  176. })
  177. if err != nil {
  178. _ = session.Rollback()
  179. fmt.Println("err:::::2222", err)
  180. return err
  181. }
  182. //给相应的用户加上"团队"活跃积分
  183. for _, vv := range rewardFather {
  184. //TODO::推入rabbitmq 异步处理
  185. ch.Publish(md2.EggEnergyExchange, md.DealUserVirtualCoinReq{
  186. Kind: "add",
  187. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardTeamActiveCoin),
  188. TransferType: int(enum.EggEnergyWatchAdRewardTeamActiveCoin),
  189. CoinId: eggEnergyBasicSetting.TeamEggPointsCoinId,
  190. Uid: vv.Uid,
  191. Amount: vv.RewardValue,
  192. }, md2.EggEnergyRoutKeyForSettlementPublicGiveActivityCoin)
  193. }
  194. err = session.Commit()
  195. if err != nil {
  196. _ = session.Rollback()
  197. return errors.New("事务提交失败")
  198. }
  199. redisKey := fmt.Sprintf(md2.UserNextWatchAdDate, uid)
  200. var watchAdDate string
  201. if eggEnergyUserWatchRecords.ResidueWatchAdNum == egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) {
  202. if eggEnergyUserWatchRecords.NextWatchAdDate.Before(time.Now()) {
  203. watchAdDate = ""
  204. } else {
  205. watchAdDate = eggEnergyUserWatchRecords.NextWatchAdDate.Format("2006-01-02 15:04:05")
  206. }
  207. } else {
  208. watchAdDate = time.Now().Add(time.Duration(egg_system_rules.StrToInt64(videoRewardSystem.IntervalMinutes)) * time.Second).Format("2006-01-02 15:04:05")
  209. }
  210. cache.SetEx(redisKey, watchAdDate, 60*60*24) //TODO::默认缓存1小时
  211. }
  212. return
  213. }
  214. // CalcUserContinuousDailyActivityDays 计算用户连续活跃天数
  215. func CalcUserContinuousDailyActivityDays(engine *xorm.Engine, uid int, startDate string, endDate string) (err error, days int, isContinuousDailyActivity bool) {
  216. startAt, err := time.ParseInLocation("2006-01-02", startDate, time.Local) //起始时间
  217. endAt, err := time.ParseInLocation("2006-01-02", endDate, time.Local) //起始时间
  218. var list []model.EggEnergyUserActivity
  219. err = engine.Where("date >= ?", startAt.Format("2006-01-02")).
  220. And("date < ?", endAt.Format("2006-01-02")).
  221. And("uid =?", uid).Find(&list)
  222. if err != nil {
  223. fmt.Println("err:::::1111", err)
  224. return
  225. }
  226. days = len(list)
  227. diffDays := egg_system_rules.GetDiffDays(endAt, startAt) //相差天数
  228. if days == diffDays {
  229. isContinuousDailyActivity = true
  230. }
  231. return
  232. }