SystemUserLevel.php 4.8 KB

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