SystemUserLevel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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){
  73. $level = UserLevel::getUserLevelInfo($id);
  74. }
  75. $list = self::getLevelListAndGrade($level['id'], $isArray);
  76. if (isset($list[0]) && $isArray){
  77. $task = SystemUserTask::getTashList($list[0]['id'], $uid, $level);
  78. }
  79. if ($isArray) return [$list, $task];
  80. else return $level['id'] && $id !== false ? $level : false;
  81. }
  82. /**
  83. * 获取会员等级级别
  84. * @param $leval_id 等级id
  85. * @return mixed
  86. */
  87. public static function getLevelGrade($leval_id)
  88. {
  89. return self::setWhere()->where('id', $leval_id)->value('grade');
  90. }
  91. /**
  92. * 获取会员等级列表
  93. * @param int $leval_id 用户等级
  94. * @param bool $isArray 是否查找任务列表
  95. * @param string $order
  96. * @return array|\think\Collection
  97. * @throws DataNotFoundException
  98. * @throws ModelNotFoundException
  99. * @throws DbException
  100. */
  101. public static function getLevelListAndGrade($leval_id, $isArray, $order = 'grade asc')
  102. {
  103. $grade = 0;
  104. if (!$leval_id && !$isArray) $leval_id = self::setWhere()->where('grade', self::setWhere()->min('grade'))->order('add_time DESC')->value('id');
  105. $list = self::setWhere()->field('name,discount,image,icon,explain,id,grade')->order($order)->select();
  106. $list = count($list) ? $list->toArray() : [];
  107. foreach ($list as &$item) {
  108. if ($item['id'] == $leval_id)
  109. $grade = $item['grade'];
  110. if ($isArray){
  111. $item['task_list'] = SystemUserTask::getTashList($item['id']);
  112. }
  113. }
  114. foreach ($list as &$item) {
  115. if ($grade < $item['grade'])
  116. $item['is_clear'] = true;
  117. else
  118. $item['is_clear'] = false;
  119. }
  120. return $list;
  121. }
  122. /**
  123. *
  124. * @param $leval_id
  125. * @param null $list
  126. * @return bool
  127. */
  128. public static function getClear($leval_id, $list = null)
  129. {
  130. $list = $list === null ? self::getLevelListAndGrade($leval_id, false) : $list;
  131. foreach ($list as $item) {
  132. if ($item['id'] == $leval_id) return $item['is_clear'];
  133. }
  134. return false;
  135. }
  136. /**
  137. * 获取当前vipid 的下一个会员id
  138. * @param int $leval_id 当前用户的会员id
  139. * @param $max
  140. * @return int
  141. * @throws DataNotFoundException
  142. * @throws ModelNotFoundException
  143. * @throws DbException
  144. */
  145. public static function getNextLevelId($leval_id, $max)
  146. {
  147. $list = self::getLevelListAndGrade($leval_id, false, 'grade desc');
  148. $grade = 0;
  149. $leveal = [];
  150. foreach ($list as $item) {
  151. if ($item['id'] == $leval_id) $grade = $item['grade'];
  152. }
  153. foreach ($list as $item) {
  154. if ($grade < $item['grade'] && $item['grade'] <= $max) array_push($leveal, $item['id']);
  155. }
  156. return isset($leveal[0]) ? $leveal[0] : 0;
  157. }
  158. /**
  159. * 获取会员等级列表
  160. * @param $uid
  161. * @return array
  162. */
  163. public static function getLevelList($uid)
  164. {
  165. list($list, $task) = self::getLevelInfo($uid, true);
  166. return ['list' => $list, 'task' => $task];
  167. }
  168. }