SystemUserLevel.php 5.4 KB

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