蛋蛋星球-制度模式
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.

164 lines
4.0 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. "time"
  10. "xorm.io/xorm"
  11. )
  12. func UserUpgradeInsert(eg *xorm.Engine, uid int) error {
  13. userDb := implement.NewUserDb(eg)
  14. user, _ := userDb.GetUser(int64(uid))
  15. level := user.Level
  16. isNext := 0
  17. levelDb := implement.NewUserLevelDb(eg)
  18. levels, _ := levelDb.UserLevelAllByAsc()
  19. for _, v1 := range levels {
  20. if isNext == 1 {
  21. level = v1.Id
  22. isNext = 2
  23. }
  24. if v1.Id == level && isNext == 0 {
  25. isNext = 1
  26. }
  27. }
  28. if level <= user.Level {
  29. return errors.New("等级相同")
  30. }
  31. task, err := UserUpgradeTask(eg, int(user.Id), level)
  32. if err != nil {
  33. return err
  34. }
  35. //升级
  36. oldLevel := user.Level
  37. user.Level = level
  38. _, err = eg.Where("id=?", user.Id).Cols("level").Update(user)
  39. if err != nil {
  40. return err
  41. }
  42. var tmp = model.UserUpgradeLevel{
  43. Uid: int(user.Id),
  44. Level: level,
  45. OldLv: oldLevel,
  46. CreateTime: time.Now(),
  47. Task: utils.SerializeStr(task),
  48. }
  49. eg.Insert(&tmp)
  50. return nil
  51. }
  52. /*
  53. 会员升级
  54. uid 用户id
  55. lv 要升级的等级
  56. */
  57. func UserUpgradeTask(eg *xorm.Engine, uid, lv int) ([]map[string]string, error) {
  58. taskList := make([]map[string]string, 0)
  59. //获取任务
  60. NewUserLevelTaskDb := implement.NewUserLevelTaskDb(eg)
  61. levelTask, _ := NewUserLevelTaskDb.UserLevelTaskFindByParams(map[string]interface{}{
  62. "key": "level_id",
  63. "value": lv,
  64. })
  65. if len(levelTask) == 0 {
  66. return taskList, errors.New("任务条件未设置")
  67. }
  68. NewUserLevelDb := implement.NewUserLevelDb(eg)
  69. level, _ := NewUserLevelDb.UserLevelByID(lv)
  70. if level == nil {
  71. return taskList, errors.New("等级不存在")
  72. }
  73. chooseNum := 0
  74. mustTaskErr := 0
  75. for _, v := range levelTask {
  76. isSuccess := 0
  77. var count int64 = 0
  78. switch v.TaskType {
  79. case 1: //直推会员人数
  80. count = ExtendUserCount(eg, uid)
  81. if count >= int64(v.FinishCount) {
  82. isSuccess = 1
  83. }
  84. break
  85. case 2: //直推会员活跃天数
  86. count = ExtendUserCountWithDay(eg, uid, v.WithinDays, v.ActiveDays)
  87. if count >= int64(v.FinishCount) {
  88. isSuccess = 1
  89. }
  90. break
  91. case 3: //直推会员等级
  92. count = ExtendUserCountWithLv(eg, uid, v.TaskTypeLevelId)
  93. if count >= int64(v.FinishCount) {
  94. isSuccess = 1
  95. }
  96. break
  97. }
  98. tmp := map[string]string{
  99. "id": utils.IntToStr(v.Id),
  100. "count": utils.Int64ToStr(count),
  101. "is_success": utils.IntToStr(isSuccess),
  102. }
  103. taskList = append(taskList, tmp)
  104. if isSuccess == 1 && v.IsMustTask == 0 { //记录选做任务数量
  105. chooseNum++
  106. }
  107. if isSuccess == 0 && v.IsMustTask == 1 { //必做任务未完成
  108. mustTaskErr = 1
  109. }
  110. }
  111. if mustTaskErr == 1 {
  112. return taskList, errors.New("必做任务未完成")
  113. }
  114. if chooseNum < level.ChoosableNum { //选做任务未完成
  115. return taskList, errors.New("选择任务未完成")
  116. }
  117. return taskList, nil
  118. }
  119. // 直推人数
  120. func ExtendUserCount(eg *xorm.Engine, uid int) int64 {
  121. count, _ := eg.Where("parent_uid=?", uid).Count(&model.User{})
  122. return count
  123. }
  124. // 直推人数达到对应等级
  125. func ExtendUserCountWithLv(eg *xorm.Engine, uid, lv int) int64 {
  126. count, _ := eg.Where("parent_uid=? and level=?", uid, lv).Count(&model.User{})
  127. return count
  128. }
  129. // X天直推X个用户活跃X天
  130. func ExtendUserCountWithDay(eg *xorm.Engine, uid, day, activityDay int) int64 {
  131. var user []model.User
  132. today := utils.GetTimeRange("today")
  133. times := today["start"] - int64(day*86400)
  134. eg.Where("parent_uid=? and create_at>=?", uid, times).Find(&user)
  135. var num int64 = 0
  136. if len(user) > 0 {
  137. uids := ""
  138. for _, v := range user {
  139. if uids == "" {
  140. uids += utils.Int64ToStr(v.Id)
  141. } else {
  142. uids += "," + utils.Int64ToStr(v.Id)
  143. }
  144. }
  145. sql := `select count(*) as count from egg_energy_user_activity where uid in(%s) `
  146. sql = fmt.Sprintf(sql, uids)
  147. nativeString, _ := db.QueryNativeString(eg, sql)
  148. for _, v := range nativeString {
  149. if utils.StrToInt(v["count"]) >= activityDay {
  150. num++
  151. }
  152. }
  153. }
  154. return num
  155. }