蛋蛋星球 后台端
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.

svc_team_assistance_count.go 5.4 KiB

4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
4 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  7. "fmt"
  8. "github.com/shopspring/decimal"
  9. "strings"
  10. "time"
  11. )
  12. func AutoCountTeamAssistance() {
  13. // 1.查签到表,统计今天0点后开始签到的用户
  14. now := time.Now()
  15. date := now.Format("2006-01-02")
  16. todayStr := fmt.Sprintf("%s %s", date, "00:00:00")
  17. var signIns []model.EggSignIn
  18. err := db.Db.Where("start_time > ?", todayStr).Find(&signIns)
  19. if err != nil {
  20. fmt.Println(err)
  21. return
  22. }
  23. detailDb := implement.NewEggEnergyAssistanceDetailDb(db.Db)
  24. assistanceDb := implement.NewEggEnergyTeamAssistanceDb(db.Db)
  25. var teamAssistance []model.EggEnergyTeamAssistance
  26. for _, signIn := range signIns {
  27. fmt.Println("uid>>>>>>", signIn.Uid)
  28. // 2. 查询这些用户的下级
  29. sql := fmt.Sprintf("SELECT uid FROM `public_platoon_user_relation` WHERE father_uid1 = %d OR father_uid2= %d OR father_uid3= %d OR father_uid4= %d OR father_uid5= %d OR father_uid6= %d OR father_uid7= %d OR father_uid8= %d OR father_uid9= %d", signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid, signIn.Uid)
  30. nativeString, _ := db.QueryNativeString(db.Db, sql)
  31. if nativeString != nil {
  32. var userIds []string
  33. for _, v := range nativeString {
  34. userIds = append(userIds, v["uid"])
  35. }
  36. // 3、统计流水表得出助力次数
  37. userIdStr := strings.Join(userIds, ",")
  38. sql1 := fmt.Sprintf("select * from `user_virtual_coin_flow` where uid in (%s) and transfer_type = 2 and create_at > '%s' group by 'uid'", userIdStr, todayStr)
  39. fmt.Println("sql>>>>>", sql1)
  40. results, _ := db.QueryNativeString(db.Db, sql1)
  41. if results != nil {
  42. var rewardScore float64
  43. for _, vv := range results {
  44. amountValue, _ := decimal.NewFromString(vv["amount"])
  45. rateValue := decimal.NewFromFloat(0.1)
  46. rewardScoreValue, _ := amountValue.Mul(rateValue).RoundFloor(8).Float64()
  47. rewardScore += rewardScoreValue
  48. //插入助力数据
  49. detail := model.EggEnergyAssistanceDetail{
  50. Uid: signIn.Uid,
  51. AssistedUid: utils.StrToInt64(vv["uid"]),
  52. SignId: signIn.Id,
  53. CreatedAt: vv["create_at"],
  54. UpdatedAt: vv["create_at"],
  55. }
  56. _, err = detailDb.AssistanceDetailInsert(&detail)
  57. if err != nil {
  58. fmt.Println("err1111>>>>>>>>>>>>>>>>>", err.Error())
  59. return
  60. }
  61. }
  62. //4、更新助力数据
  63. assistance, _ := assistanceDb.EggEnergyTeamAssistanceGetOneByParams(map[string]interface{}{
  64. "key": "sign_id",
  65. "value": signIn.Id,
  66. })
  67. if assistance != nil {
  68. assistance.AssistedNum = len(results)
  69. assistance.RewardScore = utils.Float64ToStr(rewardScore)
  70. _, err = assistanceDb.EggEnergyTeamAssistanceUpdate(assistance.Id, assistance, "assisted_num", "reward_score")
  71. if err != nil {
  72. fmt.Println("err2222>>>>>>>>>>>>>>>>>", err.Error())
  73. return
  74. }
  75. } else {
  76. m := model.EggEnergyTeamAssistance{
  77. Uid: signIn.Uid,
  78. AssistedNum: len(results),
  79. AssistedTimes: 0,
  80. RewardScore: utils.Float64ToStr(rewardScore),
  81. Date: date,
  82. SignId: signIn.Id,
  83. CreateAt: now.Format("2006-01-02 15:04:05"),
  84. UpdateAt: now.Format("2006-01-02 15:04:05"),
  85. }
  86. _, err = assistanceDb.EggEnergyTeamAssistanceInsert(&m)
  87. if err != nil {
  88. fmt.Println("err3333>>>>>>>>>>>>>>>>>", err.Error())
  89. return
  90. }
  91. }
  92. fmt.Println(results)
  93. }
  94. }
  95. //if assistedNum > 0 {
  96. // // 保存助力信息 数据库
  97. // var assistedUsers []model.AdvertisingCallback
  98. // err := db.Db.Select("uid").Where("create_at > ?", signIn.CreateAt).
  99. // And(types).In("uid", uids).
  100. // GroupBy("uid").Find(&assistedUsers)
  101. // assistedUserDetails := make([]model.EggEnergyAssistanceDetail, len(assistedUsers))
  102. // for i, assistedUser := range assistedUsers {
  103. // m := model.EggEnergyAssistanceDetail{
  104. // Uid: signIn.Uid,
  105. // AssistedUid: int64(assistedUser.Uid),
  106. // SignId: signIn.Id,
  107. // CreatedAt: nowStr,
  108. // UpdatedAt: nowStr,
  109. // }
  110. // assistedUserDetails[i] = m
  111. // }
  112. // // 保存助力信息到 redis
  113. // cacheKey := fmt.Sprintf("EggEnergyTeamAssistance:%d", signIn.Uid)
  114. // targetTime, err1 := time.Parse("2006-01-02 15:04:05", signIn.EndTime)
  115. // if err1 != nil {
  116. // fmt.Println("解析时间错误:", err1)
  117. // return
  118. // }
  119. // diff := targetTime.Sub(time.Now()).Seconds()
  120. // _, err = cache.SetEx(cacheKey, nil, int(diff))
  121. // if err != nil {
  122. // return
  123. // }
  124. // for _, assistedUser := range assistedUsers {
  125. // cache.SetBit(cacheKey, int64(assistedUser.Uid), 1)
  126. // }
  127. //
  128. // assistedTimes, err := db.Db.Where("create_at > ?", signIn.CreateAt).
  129. // And(types).In("uid", uids).Count(&model.AdvertisingCallback{})
  130. // if err != nil {
  131. // fmt.Println(err)
  132. // return
  133. // }
  134. // m := model.EggEnergyTeamAssistance{
  135. // Uid: signIn.Uid,
  136. // AssistedNum: int(assistedNum),
  137. // AssistedTimes: int(assistedTimes),
  138. // RewardScore: utils.Float64ToStr(float64(assistedTimes) * 187.5),
  139. // SignId: signIn.Id,
  140. // Date: date,
  141. // CreateAt: nowStr,
  142. // UpdateAt: nowStr,
  143. // }
  144. // teamAssistance = append(teamAssistance, m)
  145. //}
  146. }
  147. insert, err := db.Db.Insert(&teamAssistance)
  148. if err != nil {
  149. fmt.Println(err)
  150. return
  151. }
  152. fmt.Println("insert ", insert)
  153. }