SystemUserLevel.php 5.1 KB

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