蛋蛋星球-制度模式
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

548 lines
20 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. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg"
  10. egg_system_rules "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/cache"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/es"
  13. zhios_order_relate_logx "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils/logx"
  14. es2 "code.fnuoos.com/go_rely_warehouse/zyos_go_es.git/es"
  15. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "github.com/olivere/elastic/v7"
  20. "github.com/shopspring/decimal"
  21. "strings"
  22. "time"
  23. "xorm.io/xorm"
  24. )
  25. // SettlementGiveActivityCoin 结算观看激励广告得到蛋蛋积分
  26. func SettlementGiveActivityCoin(engine *xorm.Engine, user model.User, ecpm string, ch *rabbit.Channel) (rewardValue float64, err error) {
  27. egg_system_rules.FilePutContents("SettlementGiveActivityCoin", egg_system_rules.SerializeStr(user))
  28. uid := user.Id
  29. now := time.Now()
  30. //TODO::拒绝重复异常回调
  31. redisKey := fmt.Sprintf(md2.UserNextWatchAdDate, uid)
  32. nextWatchAdDate, _ := cache.GetString(redisKey)
  33. if nextWatchAdDate != "" {
  34. nextWatchAdDateValue := egg_system_rules.String2Time(nextWatchAdDate)
  35. if nextWatchAdDateValue.After(time.Now()) {
  36. return 0, errors.New("未到可观看时间~~")
  37. }
  38. }
  39. fmt.Println("SettlementGiveActivityCoin:a:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  40. //1、查找 `egg_energy_basic_setting` 基础设置
  41. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  42. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  43. "key": "is_open",
  44. "value": 1,
  45. })
  46. if err != nil {
  47. return
  48. }
  49. if eggEnergyBasicSetting == nil {
  50. err = errors.New("蛋蛋能量设置未开启!")
  51. return
  52. }
  53. var videoRewardSystem *md2.VideoRewardSystemStruct
  54. err = json.Unmarshal([]byte(eggEnergyBasicSetting.VideoRewardSystem), &videoRewardSystem)
  55. if err != nil {
  56. return
  57. }
  58. if eggEnergyBasicSetting.VideoRewardIsOpen == 1 {
  59. if videoRewardSystem.RewardTotalNum == "" || videoRewardSystem.IntervalMinutes == "" || videoRewardSystem.EachRoundHour == "" {
  60. err = errors.New("视屏奖励机制设置未完全!")
  61. return
  62. }
  63. var rewardSystem []*md2.RewardSystemStruct
  64. err = json.Unmarshal([]byte(eggEnergyBasicSetting.RewardSystem), &rewardSystem)
  65. if err != nil {
  66. return
  67. }
  68. if len(rewardSystem) == 0 {
  69. err = errors.New("未设置奖励机制!")
  70. return
  71. }
  72. fmt.Println("SettlementGiveActivityCoin:b:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  73. go dealEcpm(user, ecpm, ch) //TODO::协程处理ecpm的变化
  74. fmt.Println("SettlementGiveActivityCoin:c:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  75. //TODO::奖励多少个活跃积分(根据ecpm, 新用户机制, 蛋蛋分机制)
  76. sysCfgDb := sys_cfg.NewSysCfgDb(engine)
  77. videoRewardUnitPrice := sysCfgDb.SysCfgGetWithDb(enum.VideoRewardUnitPrice)
  78. if videoRewardUnitPrice != "" { //处理ecpm
  79. var coin model.VirtualCoin
  80. _, err = engine.Where("id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).Get(&coin)
  81. if err != nil {
  82. return rewardValue, err
  83. }
  84. activePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
  85. videoRewardUnitPriceValue, _ := decimal.NewFromString(videoRewardUnitPrice)
  86. rewardValue, _ = videoRewardUnitPriceValue.Mul(activePointsCoinExchangeRatioValue).Float64()
  87. } else {
  88. rewardValue = egg_system_rules.StrToFloat64(videoRewardSystem.RewardValue)
  89. }
  90. fmt.Println("SettlementGiveActivityCoin:d:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  91. var newUserRewardRules *md2.NewUserRewardRules
  92. err = json.Unmarshal([]byte(eggEnergyBasicSetting.NewUserIncentiveRules), &newUserRewardRules)
  93. if err != nil {
  94. return
  95. }
  96. userCreateAt := egg_system_rules.String2Time(user.CreateAt)
  97. if int(now.Sub(userCreateAt).Hours()/24) <= newUserRewardRules.ContinueDays { //处理新用户机制
  98. rewardValue *= newUserRewardRules.RewardCoefficient
  99. }
  100. score := 60.00
  101. esIndex := es.GetLatestEffectiveIndexFromAlias(now)
  102. esIndexName := md.EggEnergyUserEggScoreEsAlias + "_" + esIndex
  103. results, err1 := es2.FirstDoc(esIndexName, esIndex+"_"+egg_system_rules.Int64ToStr(user.Id))
  104. if err1 != nil {
  105. if !elastic.IsNotFound(err1) {
  106. return rewardValue, err1
  107. }
  108. }
  109. if !elastic.IsNotFound(err1) {
  110. var doc md.EggEnergyUserEggScoreEs
  111. err = json.Unmarshal(results.Source, &doc)
  112. if err != nil {
  113. return
  114. }
  115. score = doc.ScoreValue
  116. }
  117. fmt.Println("SettlementGiveActivityCoin:e:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  118. eggPointPartitionCoefficientManagementDb := implement.NewEggPointPartitionCoefficientManagementDb(engine)
  119. eggPointPartitionCoefficientManagements, err1 := eggPointPartitionCoefficientManagementDb.EggPointPartitionCoefficientManagementGetAll()
  120. if err1 != nil {
  121. return rewardValue, err1
  122. }
  123. for _, v := range *eggPointPartitionCoefficientManagements { //处理蛋蛋分机制
  124. if egg_system_rules.StrToFloat64(v.StartScore) <= score && egg_system_rules.StrToFloat64(v.EndScore) > score {
  125. rewardValue *= egg_system_rules.StrToFloat64(v.Coefficient)
  126. }
  127. }
  128. //处理圈层奖励
  129. var rewardSystemMap = map[int]*md2.RewardSystemStruct{}
  130. for _, v := range rewardSystem {
  131. rewardSystemMap[v.Level] = v
  132. }
  133. publicPlatoonUserRelationDb := implement.NewPublicPlatoonUserRelationDb(engine)
  134. publicPlatoonUserRelation, err1 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  135. "key": "uid",
  136. "value": uid,
  137. })
  138. if err1 != nil {
  139. return rewardValue, err1
  140. }
  141. var rewardFather []struct {
  142. Uid int64 `json:"uid"` //用户id
  143. RewardValue float64 `json:"reward_value"` //奖励值
  144. StartAt string `json:"start_at"`
  145. EndAt string `json:"end_at"`
  146. SignId int64 `json:"sign_id"`
  147. }
  148. var fatherUids []string
  149. if publicPlatoonUserRelation != nil {
  150. fatherUids = strings.Split(publicPlatoonUserRelation.FatherUid, "-")
  151. }
  152. fmt.Println("SettlementGiveActivityCoin:f:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  153. for k, id := range fatherUids {
  154. tmpOneCirclesPublicPlatoonUserRelation, err11 := publicPlatoonUserRelationDb.PublicPlatoonUserRelationGetOneByParams(map[string]interface{}{
  155. "key": "id",
  156. "value": id,
  157. })
  158. if err11 != nil {
  159. return rewardValue, err11
  160. }
  161. if tmpOneCirclesPublicPlatoonUserRelation == nil {
  162. continue
  163. }
  164. if tmpOneCirclesPublicPlatoonUserRelation.Uid <= 0 {
  165. //待填充位
  166. continue
  167. }
  168. fmt.Println("SettlementGiveActivityCoin:f1:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  169. fatherUid := egg_system_rules.Int64ToStr(tmpOneCirclesPublicPlatoonUserRelation.Uid)
  170. fatherReward := rewardSystemMap[k+1]
  171. //TODO::判断是否活跃
  172. var m model.EggSignIn
  173. has, err33 := engine.Where("uid =?", fatherUid).And("end_time >=?", time.Now().Format("2006-01-02 15:04:05")).Get(&m)
  174. fmt.Println("SettlementGiveActivityCoin:f2:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  175. if err33 != nil {
  176. return rewardValue, err33
  177. }
  178. if !has {
  179. //不活跃不需要奖励
  180. continue
  181. }
  182. //判断是否满足奖励条件
  183. userRelateDb := implement.NewUserRelateDb(engine)
  184. userCount, _, err2 := userRelateDb.SumUserRelateByParentUid(fatherUid)
  185. if err2 != nil {
  186. return rewardValue, err2
  187. }
  188. fmt.Println("SettlementGiveActivityCoin:f3:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  189. if fatherReward != nil && userCount >= egg_system_rules.StrToInt64(fatherReward.RewardCondition) {
  190. fatherRewardValue := rewardValue * (egg_system_rules.StrToFloat64(fatherReward.RewardValue) / 100)
  191. rewardFather = append(rewardFather, struct {
  192. Uid int64 `json:"uid"` //用户id
  193. RewardValue float64 `json:"reward_value"` //奖励值
  194. StartAt string `json:"start_at"`
  195. EndAt string `json:"end_at"`
  196. SignId int64 `json:"sign_id"`
  197. }{
  198. Uid: egg_system_rules.StrToInt64(fatherUid),
  199. RewardValue: fatherRewardValue,
  200. StartAt: m.StartTime,
  201. EndAt: m.EndTime,
  202. SignId: m.Id,
  203. })
  204. }
  205. }
  206. //处理直推奖励
  207. var directPushRewardSetting *md2.DirectPushRewardSettingStruct
  208. err = json.Unmarshal([]byte(eggEnergyBasicSetting.DirectPushReward), &directPushRewardSetting)
  209. if err != nil {
  210. return
  211. }
  212. fatherRewardValue := rewardValue * (egg_system_rules.StrToFloat64(directPushRewardSetting.RewardRateValue) / 100)
  213. var fatherSign model.EggSignIn
  214. if directPushRewardSetting.MemberSelfIsOpenGetTeamReward == "1" {
  215. fmt.Println("user:::::::::::::", user)
  216. has, err44 := engine.Where("uid =?", user.ParentUid).And("end_time >=?", time.Now().Format("2006-01-02 15:04:05")).Get(&fatherSign)
  217. if err44 != nil {
  218. return rewardValue, err44
  219. }
  220. if !has {
  221. //不活跃不需要奖励
  222. fatherRewardValue = 0
  223. }
  224. }
  225. fmt.Println("SettlementGiveActivityCoin:g:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  226. session := engine.NewSession()
  227. defer func() {
  228. session.Close()
  229. if err := recover(); err != nil {
  230. _ = zhios_order_relate_logx.Error(err)
  231. }
  232. }()
  233. session.Begin()
  234. //给相应的用户加上"个人"活跃积分
  235. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  236. Kind: "add",
  237. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardPersonalActiveCoin),
  238. TransferType: int(enum.EggEnergyWatchAdRewardPersonalActiveCoin),
  239. CoinId: eggEnergyBasicSetting.PersonEggPointsCoinId,
  240. Uid: uid,
  241. Amount: rewardValue,
  242. })
  243. if err != nil {
  244. _ = session.Rollback()
  245. fmt.Println("err:::::2222", err)
  246. return rewardValue, err
  247. }
  248. fmt.Println("SettlementGiveActivityCoin:i:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  249. //给相应的用户上级加上"团队"活跃积分
  250. if fatherRewardValue > 0 {
  251. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  252. Kind: "add",
  253. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardFansActiveCoin),
  254. TransferType: int(enum.EggEnergyWatchAdRewardFansActiveCoin),
  255. CoinId: eggEnergyBasicSetting.TeamEggPointsCoinId,
  256. Uid: user.ParentUid,
  257. Amount: fatherRewardValue,
  258. })
  259. if err != nil {
  260. _ = session.Rollback()
  261. fmt.Println("err:::::2222", err)
  262. return rewardValue, err
  263. }
  264. }
  265. fmt.Println("SettlementGiveActivityCoin:j:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  266. //给相应的用户加上"团队"活跃积分
  267. for _, vv := range rewardFather {
  268. if vv.RewardValue > 0 {
  269. //TODO::推入rabbitmq 异步处理
  270. ch.Publish(md2.EggEnergyExchange, md.DealUserVirtualCoinReq{
  271. Kind: "add",
  272. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggEnergyWatchAdRewardTeamActiveCoin),
  273. TransferType: int(enum.EggEnergyWatchAdRewardTeamActiveCoin),
  274. CoinId: eggEnergyBasicSetting.TeamEggPointsCoinId,
  275. Uid: vv.Uid,
  276. Amount: vv.RewardValue,
  277. }, md2.EggEnergyRoutKeyForSettlementPublicGiveActivityCoin)
  278. ch.Publish(md2.EggEnergyExchange, md2.EggEnergyTeamAssistanceReq{
  279. AssistanceUid: uid,
  280. Uid: vv.Uid,
  281. AssistanceValue: vv.RewardValue,
  282. SignStart: vv.StartAt,
  283. SignEnd: vv.EndAt,
  284. SignId: vv.SignId,
  285. }, md2.EggEnergyTeamAssistance)
  286. }
  287. }
  288. fmt.Println("SettlementGiveActivityCoin:k:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  289. err = session.Commit()
  290. if err != nil {
  291. _ = session.Rollback()
  292. return rewardValue, errors.New("事务提交失败")
  293. }
  294. }
  295. //增加 egg_user_watch_records 记录
  296. eggEnergyUserWatchRecordsDb := implement.NewEggEnergyUserWatchRecordsDb(engine)
  297. eggEnergyUserWatchRecords, err1 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsGetOneByParams(map[string]interface{}{
  298. "key": "uid",
  299. "value": uid,
  300. })
  301. if err1 != nil {
  302. return rewardValue, err1
  303. }
  304. if eggEnergyUserWatchRecords == nil {
  305. residueWatchAdNum := egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) - 1
  306. if residueWatchAdNum < 0 {
  307. residueWatchAdNum = egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)
  308. }
  309. eggEnergyUserWatchRecords = &model.EggEnergyUserWatchRecords{
  310. Uid: uid,
  311. NextWatchAdDate: now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt(videoRewardSystem.EachRoundHour))),
  312. ResidueWatchAdNum: egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) - 1,
  313. CreateAt: now.Format("2006-01-02 15:04:05"),
  314. UpdateAt: now.Format("2006-01-02 15:04:05"),
  315. }
  316. _, err2 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsInsert(eggEnergyUserWatchRecords)
  317. if err2 != nil {
  318. return rewardValue, err2
  319. }
  320. } else {
  321. residueWatchAdNum := eggEnergyUserWatchRecords.ResidueWatchAdNum - 1
  322. nextWatchAdDate := eggEnergyUserWatchRecords.NextWatchAdDate
  323. if residueWatchAdNum == 0 { //最后一条广告
  324. residueWatchAdNum = egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)
  325. }
  326. if residueWatchAdNum == egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum)-1 { //第一条广告
  327. nextWatchAdDate = now.Add(time.Hour * time.Duration(egg_system_rules.StrToInt(videoRewardSystem.EachRoundHour)))
  328. }
  329. eggEnergyUserWatchRecords.ResidueWatchAdNum = residueWatchAdNum
  330. eggEnergyUserWatchRecords.NextWatchAdDate = nextWatchAdDate
  331. _, err2 := eggEnergyUserWatchRecordsDb.EggEnergyUserWatchRecordsUpdate(eggEnergyUserWatchRecords.Id, eggEnergyUserWatchRecords, "residue_watch_ad_num", "next_watch_ad_date")
  332. if err2 != nil {
  333. return rewardValue, err2
  334. }
  335. }
  336. fmt.Println("SettlementGiveActivityCoin:l:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  337. var watchAdDate string
  338. if eggEnergyUserWatchRecords.ResidueWatchAdNum == egg_system_rules.StrToInt(videoRewardSystem.RewardTotalNum) {
  339. if eggEnergyUserWatchRecords.NextWatchAdDate.Before(time.Now()) {
  340. watchAdDate = ""
  341. } else {
  342. watchAdDate = eggEnergyUserWatchRecords.NextWatchAdDate.Format("2006-01-02 15:04:05")
  343. }
  344. } else {
  345. watchAdDate = time.Now().Add(time.Duration(egg_system_rules.StrToInt64(videoRewardSystem.IntervalMinutes)) * time.Second).Format("2006-01-02 15:04:05")
  346. }
  347. cache.SetEx(redisKey, watchAdDate, 60*60*24) //TODO::默认缓存1小时
  348. fmt.Println("SettlementGiveActivityCoin:m:", egg_system_rules.Int64ToStr(user.Id)+":"+ecpm+":", time.Since(now))
  349. return
  350. }
  351. func dealEcpm(user model.User, ecpm string, ch *rabbit.Channel) {
  352. //查找缓存中的ecpm值和当前的ecpm是否一致
  353. key := fmt.Sprintf(md.UserEcpmCacheKey, user.Id)
  354. ecpmCacheValue, _ := cache.GetString(key)
  355. if ecpmCacheValue == "" || ecpmCacheValue != ecpm {
  356. //更新缓存值
  357. cache.SetEx(key, ecpm, md.UserEcpmCacheTime)
  358. //TODO::推入rabbitmq 异步处理
  359. ch.Publish(md2.EggEnergyExchange, md.DealUserEcpmReq{
  360. Uid: user.Id,
  361. Ecpm: ecpm,
  362. }, md2.EggEnergyRoutKeyForDealUserEcpm)
  363. }
  364. }
  365. func ActivityCoinToAlipayRealName(engine *xorm.Engine, uid int64, amount string) (err error) {
  366. session := engine.NewSession()
  367. defer func() {
  368. session.Close()
  369. if err := recover(); err != nil {
  370. _ = zhios_order_relate_logx.Error(err)
  371. }
  372. }()
  373. session.Begin()
  374. err = rule.DealUserWallet(session, md.DealUserWalletReq{
  375. Direction: "sub",
  376. Kind: int(enum.UserToAlipayRealName),
  377. Title: enum.UserToAlipayRealName.String(),
  378. Uid: uid,
  379. Amount: egg_system_rules.StrToFloat64(amount),
  380. })
  381. if err != nil {
  382. session.Rollback()
  383. return err
  384. }
  385. return session.Commit()
  386. amountValue, _ := decimal.NewFromString(amount)
  387. //1、查找 `egg_energy_basic_setting` 基础设置
  388. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  389. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  390. "key": "is_open",
  391. "value": 1,
  392. })
  393. if err != nil {
  394. return
  395. }
  396. if eggEnergyBasicSetting == nil {
  397. err = errors.New("蛋蛋能量设置未开启!")
  398. return
  399. }
  400. var coin model.VirtualCoin
  401. _, err = engine.Where("id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).Get(&coin)
  402. if err != nil {
  403. return
  404. }
  405. personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
  406. //2、判断个人蛋蛋积分是否足够
  407. var personUserVirtualAmount model.UserVirtualAmount
  408. _, err = engine.Where("coin_id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).And("uid =?", uid).Get(&personUserVirtualAmount)
  409. if err != nil {
  410. fmt.Println("err:::::1111", err)
  411. return
  412. }
  413. personUserVirtualAmountValue, _ := decimal.NewFromString(personUserVirtualAmount.Amount)
  414. needVirtualAmountValue := amountValue.Mul(personActivePointsCoinExchangeRatioValue)
  415. needVirtualAmount, _ := amountValue.Mul(personActivePointsCoinExchangeRatioValue).Float64()
  416. if personUserVirtualAmountValue.LessThan(needVirtualAmountValue) {
  417. err = errors.New("当前蛋蛋积分不足!")
  418. return
  419. }
  420. //3、扣除对应个人蛋蛋积分
  421. session = engine.NewSession()
  422. defer func() {
  423. session.Close()
  424. if err := recover(); err != nil {
  425. _ = zhios_order_relate_logx.Error(err)
  426. }
  427. }()
  428. session.Begin()
  429. //给相应的用户加上"个人"活跃积分
  430. err = rule.DealUserVirtualCoin(session, md.DealUserVirtualCoinReq{
  431. Kind: "sub",
  432. Title: enum.UserVirtualAmountFlowTransferType.String(enum.EggPointCoinToAliPayRealName),
  433. TransferType: int(enum.EggPointCoinToAliPayRealName),
  434. CoinId: eggEnergyBasicSetting.PersonEggPointsCoinId,
  435. Uid: uid,
  436. Amount: needVirtualAmount,
  437. })
  438. if err != nil {
  439. _ = session.Rollback()
  440. fmt.Println("err:::::2222", err)
  441. return err
  442. }
  443. err = session.Commit()
  444. if err != nil {
  445. _ = session.Rollback()
  446. return errors.New("事务提交失败")
  447. }
  448. return
  449. }
  450. func CheckActivityCoinToAlipayRealName(engine *xorm.Engine, uid int64, amount string) (err error, isCan bool) {
  451. amountValue, _ := decimal.NewFromString(amount)
  452. //1、查找 `egg_energy_basic_setting` 基础设置
  453. eggEnergyBasicSettingDb := implement.NewEggEnergyBasicSettingDb(engine)
  454. eggEnergyBasicSetting, err := eggEnergyBasicSettingDb.EggEnergyBasicSettingGetOneByParams(map[string]interface{}{
  455. "key": "is_open",
  456. "value": 1,
  457. })
  458. if err != nil {
  459. return
  460. }
  461. if eggEnergyBasicSetting == nil {
  462. err = errors.New("蛋蛋能量设置未开启!")
  463. return
  464. }
  465. var coin model.VirtualCoin
  466. _, err = engine.Where("id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).Get(&coin)
  467. if err != nil {
  468. return
  469. }
  470. personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
  471. //2、判断个人蛋蛋积分是否足够
  472. var personUserVirtualAmount model.UserVirtualAmount
  473. _, err = engine.Where("coin_id = ?", eggEnergyBasicSetting.PersonEggPointsCoinId).And("uid =?", uid).Get(&personUserVirtualAmount)
  474. if err != nil {
  475. fmt.Println("err:::::1111", err)
  476. return
  477. }
  478. personUserVirtualAmountValue, _ := decimal.NewFromString(personUserVirtualAmount.Amount)
  479. needVirtualAmountValue := amountValue.Mul(personActivePointsCoinExchangeRatioValue)
  480. if personUserVirtualAmountValue.GreaterThan(needVirtualAmountValue) {
  481. isCan = true
  482. }
  483. return
  484. }
  485. // CalcUserContinuousDailyActivityDays 计算用户连续活跃天数
  486. func CalcUserContinuousDailyActivityDays(engine *xorm.Engine, uid int, startDate string, endDate string) (err error, days int, isContinuousDailyActivity bool) {
  487. startAt, err := time.ParseInLocation("2006-01-02", startDate, time.Local) //起始时间
  488. endAt, err := time.ParseInLocation("2006-01-02", endDate, time.Local) //起始时间
  489. var list []model.EggEnergyUserActivity
  490. err = engine.Where("date >= ?", startAt.Format("2006-01-02")).
  491. And("date < ?", endAt.Format("2006-01-02")).
  492. And("uid =?", uid).Find(&list)
  493. if err != nil {
  494. fmt.Println("err:::::1111", err)
  495. return
  496. }
  497. days = len(list)
  498. diffDays := egg_system_rules.GetDiffDays(endAt, startAt) //相差天数
  499. if days == diffDays {
  500. isContinuousDailyActivity = true
  501. }
  502. return
  503. }