蛋蛋星球-制度模式
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

121 lignes
3.2 KiB

  1. package rule
  2. import (
  3. db "code.fnuoos.com/EggPlanet/egg_models.git/src"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  5. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  6. utils "code.fnuoos.com/EggPlanet/egg_system_rules.git/utils"
  7. "errors"
  8. "fmt"
  9. "xorm.io/xorm"
  10. )
  11. /*
  12. 会员升级
  13. uid 用户id
  14. lv 要升级的等级
  15. */
  16. func UserUpgradeTask(eg *xorm.Engine, uid, lv int) ([]map[string]string, error) {
  17. taskList := make([]map[string]string, 0)
  18. //获取任务
  19. NewUserLevelTaskDb := implement.NewUserLevelTaskDb(eg)
  20. levelTask, _ := NewUserLevelTaskDb.UserLevelTaskFindByParams(map[string]interface{}{
  21. "key": "level_id",
  22. "value": lv,
  23. })
  24. if len(levelTask) == 0 {
  25. return taskList, errors.New("任务条件未设置")
  26. }
  27. NewUserLevelDb := implement.NewUserLevelDb(eg)
  28. level, _ := NewUserLevelDb.UserLevelByID(lv)
  29. if level == nil {
  30. return taskList, errors.New("等级不存在")
  31. }
  32. chooseNum := 0
  33. mustTaskErr := 0
  34. for _, v := range levelTask {
  35. isSuccess := 0
  36. var count int64 = 0
  37. switch v.TaskType {
  38. case 1: //直推会员人数
  39. count = ExtendUserCount(eg, uid)
  40. if count >= int64(v.FinishCount) {
  41. isSuccess = 1
  42. }
  43. break
  44. case 2: //直推会员活跃天数
  45. count = ExtendUserCountWithDay(eg, uid, v.WithinDays, v.ActiveDays)
  46. if count >= int64(v.FinishCount) {
  47. isSuccess = 1
  48. }
  49. break
  50. case 3: //直推会员等级
  51. count = ExtendUserCountWithLv(eg, uid, v.TaskTypeLevelId)
  52. if count >= int64(v.FinishCount) {
  53. isSuccess = 1
  54. }
  55. break
  56. }
  57. tmp := map[string]string{
  58. "id": utils.IntToStr(v.Id),
  59. "count": utils.Int64ToStr(count),
  60. "is_success": utils.IntToStr(isSuccess),
  61. }
  62. taskList = append(taskList, tmp)
  63. if isSuccess == 1 || v.IsMustTask == 0 { //记录选做任务数量
  64. chooseNum++
  65. }
  66. if isSuccess == 0 && v.IsMustTask == 1 { //必做任务未完成
  67. mustTaskErr = 1
  68. }
  69. }
  70. if mustTaskErr == 1 {
  71. return taskList, errors.New("必做任务未完成")
  72. }
  73. if level.ChoosableNum > chooseNum { //选做任务未完成
  74. return taskList, errors.New("选择任务未完成")
  75. }
  76. return taskList, nil
  77. }
  78. // 直推人数
  79. func ExtendUserCount(eg *xorm.Engine, uid int) int64 {
  80. count, _ := eg.Where("parent_uid=?", uid).Count(&model.User{})
  81. return count
  82. }
  83. // 直推人数达到对应等级
  84. func ExtendUserCountWithLv(eg *xorm.Engine, uid, lv int) int64 {
  85. count, _ := eg.Where("parent_uid=? and level=?", uid, lv).Count(&model.User{})
  86. return count
  87. }
  88. // X天直推X个用户活跃X天
  89. func ExtendUserCountWithDay(eg *xorm.Engine, uid, day, activityDay int) int64 {
  90. var user []model.User
  91. today := utils.GetTimeRange("today")
  92. times := today["start"] - int64(day*86400)
  93. eg.Where("parent_uid=? and create_at>=?", uid, times).Find(&user)
  94. var num int64 = 0
  95. if len(user) > 0 {
  96. uids := ""
  97. for _, v := range user {
  98. if uids == "" {
  99. uids += utils.Int64ToStr(v.Id)
  100. } else {
  101. uids += "," + utils.Int64ToStr(v.Id)
  102. }
  103. }
  104. sql := `select count(*) as count from egg_energy_user_activity where uid in(%s) `
  105. sql = fmt.Sprintf(sql, uids)
  106. nativeString, _ := db.QueryNativeString(eg, sql)
  107. for _, v := range nativeString {
  108. if utils.StrToInt(v["count"]) >= activityDay {
  109. num++
  110. }
  111. }
  112. }
  113. return num
  114. }