SystemUserLevel.php 6.0 KB

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