123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace app\models\system;
- use app\models\user\UserLevel;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class SystemUserLevel extends BaseModel
- {
-
- protected $pk = 'id';
-
- protected $name = 'system_user_level';
- use ModelTrait;
- public static function getAddTimeAttr($value)
- {
- return date('Y-m-d H:i:s', $value);
- }
- public static function getDiscountAttr($value)
- {
- return (int)$value;
- }
-
- public static function setWhere($alert = '', $model = null)
- {
- $model = $model === null ? new self() : $model;
- if ($alert) $model = $model->alias($alert);
- $alert = $alert ? $alert . '.' : '';
- return $model->where("{$alert}is_show", 1)->where("{$alert}is_del", 0);
- }
-
- public static function getLevelDiscount($id = 0)
- {
- $model = self::setWhere();
- if ($id) $model = $model->where('id', $id);
- else $model = $model->order('grade asc');
- return $model->value('discount');
- }
-
- public static function getLevelInfo($uid, $isArray = false)
- {
- $level = ['id' => 0];
- $task = [];
- $id = UserLevel::getUserLevel($uid);
- if ($id !== false) $level = UserLevel::getUserLevelInfo($id);
- $list = self::getLevelListAndGrade($level['id'], $isArray);
- if (isset($list[0]) && $isArray) $task = SystemUserTask::getTashList($list[0]['id'], $uid, $level);
- if ($isArray) return [$list, $task];
- else return $level['id'] && $id !== false ? $level : false;
- }
-
- public static function getLevelGrade($leval_id)
- {
- return self::setWhere()->where('id', $leval_id)->value('grade');
- }
-
- public static function getLevelListAndGrade($leval_id, $isArray, $order = 'grade asc')
- {
- $grade = 0;
- if (!$leval_id && !$isArray) $leval_id = self::setWhere()->where('grade', self::setWhere()->min('grade'))->order('add_time DESC')->value('id');
- $list = self::setWhere()->field('name,discount,image,icon,explain,id,grade')->order($order)->select();
- $list = count($list) ? $list->toArray() : [];
- foreach ($list as &$item) {
- if ($item['id'] == $leval_id)
- $grade = $item['grade'];
- if ($isArray)
- $item['task_list'] = SystemUserTask::getTashList($item['id']);
- }
- foreach ($list as &$item) {
- if ($grade < $item['grade'])
- $item['is_clear'] = true;
- else
- $item['is_clear'] = false;
- }
- return $list;
- }
-
- public static function getClear($leval_id, $list = null)
- {
- $list = $list === null ? self::getLevelListAndGrade($leval_id, false) : $list;
- foreach ($list as $item) {
- if ($item['id'] == $leval_id) return $item['is_clear'];
- }
- return false;
- }
-
- public static function getNextLevelId($leval_id, $max)
- {
- $list = self::getLevelListAndGrade($leval_id, false, 'grade desc');
- $grade = 0;
- $leveal = [];
- foreach ($list as $item) {
- if ($item['id'] == $leval_id) $grade = $item['grade'];
- }
- foreach ($list as $item) {
- if ($grade < $item['grade'] && $item['grade'] <= $max) array_push($leveal, $item['id']);
- }
- return isset($leveal[0]) ? $leveal[0] : 0;
- }
-
- public static function getLevelList($uid)
- {
- list($list, $task) = self::getLevelInfo($uid, true);
- return ['list' => $list, 'task' => $task];
- }
- }
|