SystemUserLevel.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\models\system;
  3. use app\models\user\UserLevel;
  4. use crmeb\traits\ModelTrait;
  5. use crmeb\basic\BaseModel;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. /**
  10. * TODO 设置会员等级Model
  11. * Class SystemUserLevel
  12. * @package app\models\system
  13. */
  14. class SystemUserLevel extends BaseModel
  15. {
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'system_user_level';
  26. use ModelTrait;
  27. public static function getAddTimeAttr($value)
  28. {
  29. return date('Y-m-d H:i:s', $value);
  30. }
  31. public static function getDiscountAttr($value)
  32. {
  33. return (int)$value;
  34. }
  35. /*
  36. * 获取查询条件
  37. * @param string $alert 别名
  38. * @param object $model 模型
  39. * @return object
  40. * */
  41. public static function setWhere($alert = '', $model = null)
  42. {
  43. $model = $model === null ? new self() : $model;
  44. if ($alert) $model = $model->alias($alert);
  45. $alert = $alert ? $alert . '.' : '';
  46. return $model->where("{$alert}is_show", 1)->where("{$alert}is_del", 0);
  47. }
  48. /*
  49. * 获取某个等级的折扣
  50. * */
  51. public static function getLevelDiscount($id = 0)
  52. {
  53. $model = self::setWhere();
  54. if ($id) $model = $model->where('id', $id);
  55. else $model = $model->order('grade asc');
  56. return $model->value('discount');
  57. }
  58. /**
  59. * 获取用户等级和当前等级
  60. * @param $uid 用户uid
  61. * @param bool $isArray 是否查找任务列表
  62. * @return array|bool|mixed|string|\think\Model|null
  63. * @throws DataNotFoundException
  64. * @throws ModelNotFoundException
  65. * @throws \think\exception\DbException
  66. */
  67. public static function getLevelInfo($uid, $isArray = false)
  68. {
  69. $level = ['id' => 0];
  70. $task = [];
  71. $id = UserLevel::getUserLevel($uid);
  72. if ($id !== false) $level = UserLevel::getUserLevelInfo($id);
  73. $list = self::getLevelListAndGrade($level['id'], $isArray);
  74. if (isset($list[0]) && $isArray) $task = SystemUserTask::getTashList($list[0]['id'], $uid, $level);
  75. if ($isArray) return [$list, $task];
  76. else return $level['id'] && $id !== false ? $level : false;
  77. }
  78. /**
  79. * 获取会员等级级别
  80. * @param $leval_id 等级id
  81. * @return mixed
  82. */
  83. public static function getLevelGrade($leval_id)
  84. {
  85. return self::setWhere()->where('id', $leval_id)->value('grade');
  86. }
  87. /**
  88. * 获取会员等级列表
  89. * @param int $leval_id 用户等级
  90. * @param bool $isArray 是否查找任务列表
  91. * @param string $order
  92. * @return array|\think\Collection
  93. * @throws DataNotFoundException
  94. * @throws ModelNotFoundException
  95. * @throws DbException
  96. */
  97. public static function getLevelListAndGrade($leval_id, $isArray, $order = 'grade asc')
  98. {
  99. $grade = 0;
  100. if (!$leval_id && !$isArray) $leval_id = self::setWhere()->where('grade', self::setWhere()->min('grade'))->order('add_time DESC')->value('id');
  101. $list = self::setWhere()->field('name,discount,image,icon,explain,id,grade')->order($order)->select();
  102. $list = count($list) ? $list->toArray() : [];
  103. foreach ($list as &$item) {
  104. if ($item['id'] == $leval_id)
  105. $grade = $item['grade'];
  106. if ($isArray)
  107. $item['task_list'] = SystemUserTask::getTashList($item['id']);
  108. }
  109. foreach ($list as &$item) {
  110. if ($grade < $item['grade'])
  111. $item['is_clear'] = true;
  112. else
  113. $item['is_clear'] = false;
  114. }
  115. return $list;
  116. }
  117. /**
  118. *
  119. * @param $leval_id
  120. * @param null $list
  121. * @return bool
  122. */
  123. public static function getClear($leval_id, $list = null)
  124. {
  125. $list = $list === null ? self::getLevelListAndGrade($leval_id, false) : $list;
  126. foreach ($list as $item) {
  127. if ($item['id'] == $leval_id) return $item['is_clear'];
  128. }
  129. return false;
  130. }
  131. /**
  132. * 获取当前vipid 的下一个会员id
  133. * @param int $leval_id 当前用户的会员id
  134. * @param $max
  135. * @return int
  136. * @throws DataNotFoundException
  137. * @throws ModelNotFoundException
  138. * @throws DbException
  139. */
  140. public static function getNextLevelId($leval_id, $max)
  141. {
  142. $list = self::getLevelListAndGrade($leval_id, false, 'grade desc');
  143. $grade = 0;
  144. $leveal = [];
  145. foreach ($list as $item) {
  146. if ($item['id'] == $leval_id) $grade = $item['grade'];
  147. }
  148. foreach ($list as $item) {
  149. if ($grade < $item['grade'] && $item['grade'] <= $max) array_push($leveal, $item['id']);
  150. }
  151. return isset($leveal[0]) ? $leveal[0] : 0;
  152. }
  153. /**
  154. * 获取会员等级列表
  155. * @param $uid
  156. * @return array
  157. */
  158. public static function getLevelList($uid)
  159. {
  160. list($list, $task) = self::getLevelInfo($uid, true);
  161. return ['list' => $list, 'task' => $task];
  162. }
  163. }