SystemUserLevel.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. @file_put_contents('quanju.txt', $list[0]."-第二步\r\n", 8);
  75. if (isset($list[0]) && $isArray){
  76. @file_put_contents('quanju.txt', "-第三步\r\n", 8);
  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. foreach ($list as &$item) {
  114. if ($grade < $item['grade'])
  115. $item['is_clear'] = true;
  116. else
  117. $item['is_clear'] = false;
  118. }
  119. return $list;
  120. }
  121. /**
  122. *
  123. * @param $leval_id
  124. * @param null $list
  125. * @return bool
  126. */
  127. public static function getClear($leval_id, $list = null)
  128. {
  129. $list = $list === null ? self::getLevelListAndGrade($leval_id, false) : $list;
  130. foreach ($list as $item) {
  131. if ($item['id'] == $leval_id) return $item['is_clear'];
  132. }
  133. return false;
  134. }
  135. /**
  136. * 获取当前vipid 的下一个会员id
  137. * @param int $leval_id 当前用户的会员id
  138. * @param $max
  139. * @return int
  140. * @throws DataNotFoundException
  141. * @throws ModelNotFoundException
  142. * @throws DbException
  143. */
  144. public static function getNextLevelId($leval_id, $max)
  145. {
  146. $list = self::getLevelListAndGrade($leval_id, false, 'grade desc');
  147. $grade = 0;
  148. $leveal = [];
  149. foreach ($list as $item) {
  150. if ($item['id'] == $leval_id) $grade = $item['grade'];
  151. }
  152. foreach ($list as $item) {
  153. if ($grade < $item['grade'] && $item['grade'] <= $max) array_push($leveal, $item['id']);
  154. }
  155. return isset($leveal[0]) ? $leveal[0] : 0;
  156. }
  157. /**
  158. * 获取会员等级列表
  159. * @param $uid
  160. * @return array
  161. */
  162. public static function getLevelList($uid)
  163. {
  164. @file_put_contents('quanju.txt', "-第一步\r\n", 8);
  165. list($list, $task) = self::getLevelInfo($uid, true);
  166. return ['list' => $list, 'task' => $task];
  167. }
  168. }