蛋蛋星球-制度模式
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

358 satır
13 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. "github.com/shopspring/decimal"
  17. "strings"
  18. "time"
  19. "xorm.io/xorm"
  20. )
  21. // SettlementGiveActivityCoin 结算观看激励广告得到蛋蛋积分
  22. func SettlementGiveActivityCoin(engine *xorm.Engine, uid int64, ch *rabbit.Channel) (err error) {
  23. //1、查找 `egg_energy_basic_setting` 基础设置
  24. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  25. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  26. "key": "is_open",
  27. "value": 1,
  28. })
  29. if err != nil {
  30. return
  31. }
  32. if eggEnergyBasicSetting == nil {
  33. err = errors.New("蛋蛋能量设置未开启!")
  34. return
  35. }
  36. if eggEnergyBasicSetting.VideoRewardIsOpen == 1 {
  37. var videoRewardSystem *md2.VideoRewardSystemStruct
  38. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VideoRewardSystem), &videoRewardSystem)
  39. if err != nil {
  40. return
  41. }
  42. if videoRewardSystem.RewardValue == "" || videoRewardSystem.RewardTotalNum == "" || videoRewardSystem.IntervalMinutes == "" || videoRewardSystem.EachRoundHour == "" {
  43. err = errors.New("视屏奖励机制设置未完全!")
  44. return
  45. }
  46. var rewardSystem []*md2.RewardSystemStruct
  47. err = json.Unmarshal([]byte(eggEnergyBasicSetting.RewardSystem), &rewardSystem)
  48. if err != nil {
  49. return
  50. }
  51. if len(rewardSystem) == 0 {
  52. err = errors.New("未设置奖励机制!")
  53. return
  54. }
  55. rewardValue := egg_system_rules.StrToFloat64(videoRewardSystem.RewardValue) //奖励多少个活跃积分
  56. var rewardSystemMap = map[int]*md2.RewardSystemStruct{}
  57. for _, v := range rewardSystem {
  58. rewardSystemMap[v.Level] = v
  59. }
  60. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  61. publicPlatoonUserRelation, err1 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  62. "key": "uid",
  63. "value": uid,
  64. })
  65. if err1 != nil {
  66. return err1
  67. }
  68. var rewardFather []struct {
  69. Uid int64 `json:"uid"` //用户id
  70. RewardValue float64 `json:"reward_value"` //奖励值
  71. }
  72. var fatherUids []string
  73. if publicPlatoonUserRelation != nil {
  74. fatherUids = strings.Split(publicPlatoonUserRelation.FatherUid, "-")
  75. }
  76. for k, id := range fatherUids {
  77. tmpOneCirclesPublicPlatoonUserRelation, err11 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  78. "key": "id",
  79. "value": id,
  80. })
  81. if err11 != nil {
  82. return err11
  83. }
  84. if tmpOneCirclesPublicPlatoonUserRelation == nil {
  85. continue
  86. }
  87. if tmpOneCirclesPublicPlatoonUserRelation.Uid <= 0 {
  88. //待填充位
  89. continue
  90. }
  91. fatherUid := egg_system_rules.Int64ToStr(tmpOneCirclesPublicPlatoonUserRelation.Uid)
  92. fatherReward := rewardSystemMap[k+1]
  93. //TODO::判断是否活跃
  94. var m model.EggSignIn
  95. has, err33 := engine.Where("uid =?", fatherUid).And("end_time >=?", time.Now().Format("2006-01-02 15:04:05")).Get(&m)
  96. if err33 != nil {
  97. return err33
  98. }
  99. if !has {
  100. //不活跃不需要奖励
  101. continue
  102. }
  103. //判断是否满足奖励条件
  104. userRelateDb := implement.NewUserRelateDb(engine)
  105. userCount, _, err2 := userRelateDb.SumUserRelateByParentUid(fatherUid)
  106. if err2 != nil {
  107. return err2
  108. }
  109. if fatherReward != nil && userCount >= egg_system_rules.StrToInt64(fatherReward.RewardCondition) {
  110. fatherRewardValue := rewardValue * (egg_system_rules.StrToFloat64(fatherReward.RewardValue) / 100)
  111. rewardFather = append(rewardFather, struct {
  112. Uid int64 `json:"uid"` //用户id
  113. RewardValue float64 `json:"reward_value"` //奖励值
  114. }{
  115. Uid: egg_system_rules.StrToInt64(fatherUid),
  116. RewardValue: fatherRewardValue,
  117. })
  118. }
  119. }
  120. //增加 egg_user_watch_records 记录
  121. eggEnergyUserWatchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(engine)
  122. eggEnergyUserWatchRecords, err1 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  123. "key": "uid",
  124. "value": uid,
  125. })
  126. if err1 != nil {
  127. return err1
  128. }
  129. now := time.Now()
  130. if eggEnergyUserWatchRecords == nil {
  131. residueWatchAdNum := egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) - 1
  132. if residueWatchAdNum < 0 {
  133. residueWatchAdNum = egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)
  134. }
  135. _, err2 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsInsert(&model.EggEnergyUserWatchRecords{
  136. Uid: uid,
  137. NextWatchAdDate: now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt(videoRewardSystem.EachRoundHour))),
  138. ResidueWatchAdNum: egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) - 1,
  139. CreateAt: now.Format("2006-01-02 15:04:05"),
  140. UpdateAt: now.Format("2006-01-02 15:04:05"),
  141. })
  142. if err2 != nil {
  143. return err2
  144. }
  145. } else {
  146. residueWatchAdNum := eggEnergyUserWatchRecords.ResidueWatchAdNum - 1
  147. nextWatchAdDate := eggEnergyUserWatchRecords.NextWatchAdDate
  148. if residueWatchAdNum == 0 { //最后一条广告
  149. residueWatchAdNum = egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)
  150. }
  151. if residueWatchAdNum == egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)-1 { //第一条广告
  152. nextWatchAdDate = now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt(videoRewardSystem.EachRoundHour)))
  153. }
  154. eggEnergyUserWatchRecords.ResidueWatchAdNum = residueWatchAdNum
  155. eggEnergyUserWatchRecords.NextWatchAdDate = nextWatchAdDate
  156. _, err2 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsUpdate(eggEnergyUserWatchRecords.Id, eggEnergyUserWatchRecords, "residue_watch_ad_num", "next_watch_ad_date")
  157. if err2 != nil {
  158. return err2
  159. }
  160. }
  161. session := engine.NewSession()
  162. defer func() {
  163. session.Close()
  164. if err := recover(); err != nil {
  165. _ = zhios_order_relate_logx.Error(err)
  166. }
  167. }()
  168. session.Begin()
  169. //给相应的用户加上"个人"活跃积分
  170. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  171. Kind: "add",
  172. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardPersonalActiveCoin),
  173. TransferType: int(enum.EggEnergyWatchAdRewardPersonalActiveCoin),
  174. CoinId: eggEnergyBasicSetting.PersonEggPointsCoinId,
  175. Uid: uid,
  176. Amount: rewardValue,
  177. })
  178. if err != nil {
  179. _ = session.Rollback()
  180. fmt.Println("err:::::2222", err)
  181. return err
  182. }
  183. //给相应的用户加上"团队"活跃积分
  184. for _, vv := range rewardFather {
  185. //TODO::推入rabbitmq 异步处理
  186. ch.Publish(md2.EggEnergyExchange, md.DealUserVirtualCoinReq{
  187. Kind: "add",
  188. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardTeamActiveCoin),
  189. TransferType: int(enum.EggEnergyWatchAdRewardTeamActiveCoin),
  190. CoinId: eggEnergyBasicSetting.TeamEggPointsCoinId,
  191. Uid: vv.Uid,
  192. Amount: vv.RewardValue,
  193. }, md2.EggEnergyRoutKeyForSettlementPublicGiveActivityCoin)
  194. }
  195. err = session.Commit()
  196. if err != nil {
  197. _ = session.Rollback()
  198. return errors.New("事务提交失败")
  199. }
  200. redisKey := fmt.Sprintf(md2.UserNextWatchAdDate, uid)
  201. var watchAdDate string
  202. if eggEnergyUserWatchRecords.ResidueWatchAdNum == egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) {
  203. if eggEnergyUserWatchRecords.NextWatchAdDate.Before(time.Now()) {
  204. watchAdDate = ""
  205. } else {
  206. watchAdDate = eggEnergyUserWatchRecords.NextWatchAdDate.Format("2006-01-02 15:04:05")
  207. }
  208. } else {
  209. watchAdDate = time.Now().Add(time.Duration(egg_system_rules.StrToInt64(videoRewardSystem.IntervalMinutes)) * time.Second).Format("2006-01-02 15:04:05")
  210. }
  211. cache.SetEx(redisKey, watchAdDate, 60*60*24) //TODO::默认缓存1小时
  212. }
  213. return
  214. }
  215. func ActivityCoinToAlipayRealName(engine *xorm.Engine, uid int64, amount string) (err error) {
  216. amountValue, _ := decimal.NewFromString(amount)
  217. //1、查找 `egg_energy_basic_setting` 基础设置
  218. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  219. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  220. "key": "is_open",
  221. "value": 1,
  222. })
  223. if err != nil {
  224. return
  225. }
  226. if eggEnergyBasicSetting == nil {
  227. err = errors.New("蛋蛋能量设置未开启!")
  228. return
  229. }
  230. var coin model.VirtualCoin
  231. _, err = engine.Where("id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).Get(&coin)
  232. if err != nil {
  233. return
  234. }
  235. personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
  236. //2、判断个人蛋蛋积分是否足够
  237. var personUserVirtualAmount model.UserVirtualAmount
  238. _, err = engine.Where("coin_id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).And("uid =?", uid).Get(&personUserVirtualAmount)
  239. if err != nil {
  240. fmt.Println("err:::::1111", err)
  241. return
  242. }
  243. personUserVirtualAmountValue, _ := decimal.NewFromString(personUserVirtualAmount.Amount)
  244. needVirtualAmountValue := amountValue.Mul(personActivePointsCoinExchangeRatioValue)
  245. needVirtualAmount, _ := amountValue.Mul(personActivePointsCoinExchangeRatioValue).Float64()
  246. if personUserVirtualAmountValue.LessThan(needVirtualAmountValue) {
  247. err = errors.New("当前蛋蛋积分不足!")
  248. return
  249. }
  250. //3、扣除对应个人蛋蛋积分
  251. session := engine.NewSession()
  252. defer func() {
  253. session.Close()
  254. if err := recover(); err != nil {
  255. _ = zhios_order_relate_logx.Error(err)
  256. }
  257. }()
  258. session.Begin()
  259. //给相应的用户加上"个人"活跃积分
  260. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  261. Kind: "sub",
  262. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggPointCoinToAliPayRealName),
  263. TransferType: int(enum.EggPointCoinToAliPayRealName),
  264. CoinId: eggEnergyBasicSetting.PersonEggPointsCoinId,
  265. Uid: uid,
  266. Amount: needVirtualAmount,
  267. })
  268. if err != nil {
  269. _ = session.Rollback()
  270. fmt.Println("err:::::2222", err)
  271. return err
  272. }
  273. err = session.Commit()
  274. if err != nil {
  275. _ = session.Rollback()
  276. return errors.New("事务提交失败")
  277. }
  278. return
  279. }
  280. func CheckActivityCoinToAlipayRealName(engine *xorm.Engine, uid int64, amount string) (err error, isCan bool) {
  281. amountValue, _ := decimal.NewFromString(amount)
  282. //1、查找 `egg_energy_basic_setting` 基础设置
  283. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  284. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  285. "key": "is_open",
  286. "value": 1,
  287. })
  288. if err != nil {
  289. return
  290. }
  291. if eggEnergyBasicSetting == nil {
  292. err = errors.New("蛋蛋能量设置未开启!")
  293. return
  294. }
  295. var coin model.VirtualCoin
  296. _, err = engine.Where("id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).Get(&coin)
  297. if err != nil {
  298. return
  299. }
  300. personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
  301. //2、判断个人蛋蛋积分是否足够
  302. var personUserVirtualAmount model.UserVirtualAmount
  303. _, err = engine.Where("coin_id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).And("uid =?", uid).Get(&personUserVirtualAmount)
  304. if err != nil {
  305. fmt.Println("err:::::1111", err)
  306. return
  307. }
  308. personUserVirtualAmountValue, _ := decimal.NewFromString(personUserVirtualAmount.Amount)
  309. needVirtualAmountValue := amountValue.Mul(personActivePointsCoinExchangeRatioValue)
  310. if personUserVirtualAmountValue.GreaterThan(needVirtualAmountValue) {
  311. isCan = true
  312. }
  313. return
  314. }
  315. // CalcUserContinuousDailyActivityDays 计算用户连续活跃天数
  316. func CalcUserContinuousDailyActivityDays(engine *xorm.Engine, uid int, startDate string, endDate string) (err error, days int, isContinuousDailyActivity bool) {
  317. startAt, err := time.ParseInLocation("2006-01-02", startDate, time.Local) //起始时间
  318. endAt, err := time.ParseInLocation("2006-01-02", endDate, time.Local) //起始时间
  319. var list []model.EggEnergyUserActivity
  320. err = engine.Where("date >= ?", startAt.Format("2006-01-02")).
  321. And("date < ?", endAt.Format("2006-01-02")).
  322. And("uid =?", uid).Find(&list)
  323. if err != nil {
  324. fmt.Println("err:::::1111", err)
  325. return
  326. }
  327. days = len(list)
  328. diffDays := egg_system_rules.GetDiffDays(endAt, startAt) //相差天数
  329. if days == diffDays {
  330. isContinuousDailyActivity = true
  331. }
  332. return
  333. }