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

108 lines
3.1 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils"
  5. "applet/app/utils/cache"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "fmt"
  9. "time"
  10. )
  11. func AutoCountTeamAssistance() {
  12. // 1.查签到表,统计今天0点后开始签到的用户
  13. now := time.Now()
  14. nowStr := now.Format("2006-01-02 15:04:05")
  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. var teamAssistance []model.EggEnergyTeamAssistance
  24. // 2. 查询这些用户的下级
  25. for _, signIn := range signIns {
  26. relateDb := implement.NewUserRelateDb(db.Db)
  27. relates, err := relateDb.FindUserRelateByParentUid(signIn.Uid, 0)
  28. if err != nil {
  29. fmt.Println(err)
  30. return
  31. }
  32. // 3. 判断这些下级助力次数
  33. var uids []int64
  34. for _, relate := range *relates {
  35. uids = append(uids, relate.Uid)
  36. }
  37. typePrefix := "incentive_eggSmash"
  38. types := fmt.Sprintf("type LIKE '%s'", typePrefix+"%")
  39. assistedNum, err := db.Db.Where("create_at > ?", signIn.CreateAt).
  40. And(types).In("uid", uids).
  41. GroupBy("uid").Count(&model.AdvertisingCallback{})
  42. if err != nil {
  43. fmt.Println(err)
  44. return
  45. }
  46. if assistedNum > 0 {
  47. // 保存助力信息 数据库
  48. var assistedUsers []model.AdvertisingCallback
  49. err := db.Db.Select("uid").Where("create_at > ?", signIn.CreateAt).
  50. And(types).In("uid", uids).
  51. GroupBy("uid").Find(&assistedUsers)
  52. assistedUserDetails := make([]model.EggEnergyAssistanceDetail, len(assistedUsers))
  53. for i, assistedUser := range assistedUsers {
  54. m := model.EggEnergyAssistanceDetail{
  55. Uid: signIn.Uid,
  56. AssistedUid: int64(assistedUser.Uid),
  57. SignId: signIn.Id,
  58. CreatedAt: nowStr,
  59. UpdatedAt: nowStr,
  60. }
  61. assistedUserDetails[i] = m
  62. }
  63. // 保存助力信息到 redis
  64. cacheKey := fmt.Sprintf("EggEnergyTeamAssistance:%d", signIn.Uid)
  65. targetTime, err1 := time.Parse("2006-01-02 15:04:05", signIn.EndTime)
  66. if err1 != nil {
  67. fmt.Println("解析时间错误:", err1)
  68. return
  69. }
  70. diff := targetTime.Sub(time.Now()).Seconds()
  71. _, err = cache.SetEx(cacheKey, nil, int(diff))
  72. if err != nil {
  73. return
  74. }
  75. for _, assistedUser := range assistedUsers {
  76. cache.SetBit(cacheKey, int64(assistedUser.Uid), 1)
  77. }
  78. assistedTimes, err := db.Db.Where("create_at > ?", signIn.CreateAt).
  79. And(types).In("uid", uids).Count(&model.AdvertisingCallback{})
  80. if err != nil {
  81. fmt.Println(err)
  82. return
  83. }
  84. m := model.EggEnergyTeamAssistance{
  85. Uid: signIn.Uid,
  86. AssistedNum: int(assistedNum),
  87. AssistedTimes: int(assistedTimes),
  88. RewardScore: utils.Float64ToStr(float64(assistedTimes) * 187.5),
  89. SignId: signIn.Id,
  90. Date: date,
  91. CreateAt: nowStr,
  92. UpdateAt: nowStr,
  93. }
  94. teamAssistance = append(teamAssistance, m)
  95. }
  96. }
  97. insert, err := db.Db.Insert(&teamAssistance)
  98. if err != nil {
  99. fmt.Println(err)
  100. return
  101. }
  102. fmt.Println("insert ", insert)
  103. }