SystemUserLevel.php 6.9 KB

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