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

183 lines
4.8 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. case 4: //直推会员活跃
  98. count = ExtendUserCountActivity(eg, uid, v.WithinDays, v.ActiveNum, v.FinishCount)
  99. if count >= int64(v.FinishCount) {
  100. isSuccess = 1
  101. }
  102. break
  103. }
  104. tmp := map[string]string{
  105. "id": utils.IntToStr(v.Id),
  106. "count": utils.Int64ToStr(count),
  107. "is_success": utils.IntToStr(isSuccess),
  108. }
  109. taskList = append(taskList, tmp)
  110. if isSuccess == 1 && v.IsMustTask == 0 { //记录选做任务数量
  111. chooseNum++
  112. }
  113. if isSuccess == 0 && v.IsMustTask == 1 { //必做任务未完成
  114. mustTaskErr = 1
  115. }
  116. }
  117. if mustTaskErr == 1 {
  118. return taskList, errors.New("必做任务未完成")
  119. }
  120. if chooseNum < level.ChoosableNum { //选做任务未完成
  121. return taskList, errors.New("选择任务未完成")
  122. }
  123. return taskList, nil
  124. }
  125. // 直推人数
  126. func ExtendUserCount(eg *xorm.Engine, uid int) int64 {
  127. count, _ := eg.Where("parent_uid=? and state=1", uid).Count(&model.User{})
  128. return count
  129. }
  130. // 直推人数达到对应等级
  131. func ExtendUserCountWithLv(eg *xorm.Engine, uid, lv int) int64 {
  132. count, _ := eg.Where("parent_uid=? and level=? and state=1", uid, lv).Count(&model.User{})
  133. return count
  134. }
  135. // X天直推X个用户活跃X天
  136. func ExtendUserCountWithDay(eg *xorm.Engine, uid, day, activityDay int) int64 {
  137. var user []model.User
  138. today := utils.GetTimeRange("today")
  139. times := today["end"] - int64(day*86400)
  140. eg.Where("parent_uid=? and create_at>=? and state=1", uid, times).Find(&user)
  141. var num int64 = 0
  142. if len(user) > 0 {
  143. uids := ""
  144. for _, v := range user {
  145. if uids == "" {
  146. uids += utils.Int64ToStr(v.Id)
  147. } else {
  148. uids += "," + utils.Int64ToStr(v.Id)
  149. }
  150. }
  151. sql := `select count(*) as count from egg_energy_user_activity where uid in(%s) `
  152. sql = fmt.Sprintf(sql, uids)
  153. nativeString, _ := db.QueryNativeString(eg, sql)
  154. for _, v := range nativeString {
  155. if utils.StrToInt(v["count"]) >= activityDay {
  156. num++
  157. }
  158. }
  159. }
  160. return num
  161. }
  162. // 直推人数
  163. func ExtendUserCountActivity(eg *xorm.Engine, uid, day, activityNum, finishCount int) int64 {
  164. today := utils.GetTimeRange("today")
  165. times := today["end"] - int64(day*86400)
  166. sql := `SELECT COUNT(*) as count,u.id FROM user u
  167. LEFT JOIN egg_sign_in esi on esi.uid=u.id
  168. WHERE esi.start_time>='%s' and u.parent_uid=%d and u.state=1 GROUP BY u.id HAVING count>=%d LIMIT %d`
  169. sql = fmt.Sprintf(sql, time.Unix(times, 0).Format("2006-01-02 15:04:05"), uid, activityNum, finishCount)
  170. nativeString, _ := db.QueryNativeString(eg, sql)
  171. return int64(len(nativeString))
  172. }