User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use think\model\Collection;
  15. use think\model\relation\HasOne;
  16. use think\model\relation\HasMany;
  17. use think\model\relation\BelongsTo;
  18. use app\common\model\user\PointsLog as PointsLogModel;
  19. use think\Paginator;
  20. /**
  21. * 用户模型类
  22. * Class User
  23. * @package app\common\model
  24. */
  25. class User extends BaseModel
  26. {
  27. // 定义表名
  28. protected $name = 'user';
  29. // 定义主键
  30. protected $pk = 'user_id';
  31. // 性别
  32. private $gender = [0 => '未知', 1 => '男', 2 => '女'];
  33. /**
  34. * 获取列表
  35. * @param array $param 查询条件
  36. * @param int $listRows 分页数量
  37. * @return mixed
  38. * @throws \think\db\exception\DbException
  39. */
  40. public function getList(array $param = [], int $listRows = 15)
  41. {
  42. // 筛选条件
  43. $query = $this->getQueryFilter($param);
  44. // 排序条件
  45. $sort = $this->setQuerySort($param);
  46. // 执行查询
  47. $list = $query->alias($this->name)
  48. ->leftJoin('user_info ui','ui.user_id='.$this->name.'.user_id')
  49. ->leftJoin('school_speciality sp','sp.speciality_id=ui.speciality')
  50. ->leftJoin('source_shools s','s.source_shools_id=ui.school_id')
  51. ->field($this->name.'.*,ui.admission_year,s.region_id,s.source_shools_name as school_name,sp.speciality_name')
  52. ->order($sort)
  53. ->paginate($listRows);
  54. // 整理列表数据并返回
  55. return $list;
  56. }
  57. /**
  58. * 设置商品展示的数据
  59. * @param Collection|Paginator $list 商品列表
  60. * @param callable|null $callback 回调函数
  61. * @return mixed
  62. */
  63. protected function setListData($list, callable $callback = null)
  64. {
  65. if ($list->isEmpty()) return $list;
  66. // 遍历商品列表整理数据
  67. foreach ($list as &$item) {
  68. $data = $this->setData($item, $callback);
  69. }
  70. return $list;
  71. }
  72. /**
  73. * 整理数据
  74. * @param Collection|static $info
  75. * @param callable|null $callback
  76. * @return mixed
  77. */
  78. protected function setData($info, callable $callback = null)
  79. {
  80. // 回调函数
  81. is_callable($callback) && call_user_func($callback, $info);
  82. return $info->hidden(array_merge($this->hidden, ['albums']));
  83. }
  84. /**
  85. * 检索查询条件
  86. * @param array $params
  87. * @return \think\db\BaseQuery
  88. */
  89. private function getQueryFilter(array $params)
  90. {
  91. $filter = [];
  92. // 实例化新查询对象
  93. $query = $this->getNewQuery();
  94. // 区
  95. !empty($params['region_id']) && $filter[] = ['s.region_id', '=', "{$params['region_id']}"];
  96. // 市
  97. !empty($params['city_id']) && $filter[] = ['s.city_id', '=', "{$params['city_id']}"];
  98. // 省
  99. !empty($params['province_id']) && $filter[] = ['s.province_id', '=', "{$params['province_id']}"];
  100. // 用户状态
  101. !empty($params['status']) && $filter[] = [$this->name.'.status', '=', "{$params['status']}"];
  102. // 审核状态
  103. !empty($params['ui.status']) && $filter[] = ['ui.status', '=', "{$params['ui.status']}"];
  104. // 生源学校
  105. !empty($params['school_id']) && $filter[] = ['ui.school_id', '=', "{$params['school_id']}"];
  106. // 用户类型
  107. !empty($params['user_type']) && $filter[] = [$this->name.'.user_type', '=', "{$params['user_type']}"];
  108. // 实例化新查询对象
  109. return $query->where($filter)->where(function($query) use ($params){
  110. // 关键词
  111. if(!empty($params['keyword'])){
  112. $query->where('s.source_shools_name','like', "%{$params['keyword']}%")
  113. ->whereOr('sp.speciality_name','like',"%{$params['keyword']}%");
  114. }
  115. // 学长学姐筛选
  116. $admissionYear = isset($params['admission_year'])? intval($params['admission_year']) : -1;
  117. if($admissionYear>0){
  118. $query->where('ui.admission_year','<', $admissionYear)->where('ui.admission_year','>', 0);
  119. }else if(isset($params['admission_year'])){
  120. $query->where('ui.admission_year','>', 0);
  121. }
  122. });
  123. }
  124. /**
  125. * 检索排序条件
  126. * @param array $param
  127. * @return array|string[]
  128. */
  129. private function setQuerySort(array $param = [])
  130. {
  131. $params = $this->setQueryDefaultValue($param, [
  132. 'sortType' => 'all', // 排序类型
  133. $this->name.'.create_time' => false, // 热门排序 (true高到低 false低到高)
  134. ]);
  135. // 排序规则
  136. $sort = [];
  137. if ($params['sortType'] === 'all') {
  138. $sort = [$this->name.'.create_time' => 'desc'];
  139. } elseif ($params['sortType'] === 'view') {
  140. $sort = [$this->name.'.id' => 'desc'];
  141. }
  142. return array_merge($sort, [$this->getPk() => 'desc']);
  143. }
  144. /**
  145. * 关联用户头像表
  146. * @return HasOne
  147. */
  148. public function avatar(): HasOne
  149. {
  150. return $this->hasOne('UploadFile', 'file_id', 'avatar_id')
  151. ->bind(['avatar_url' => 'preview_url']);
  152. }
  153. /**
  154. * 用户资料表
  155. * @return HasOne
  156. */
  157. public function info(): HasOne
  158. {
  159. return $this->hasOne("UserInfo", 'user_id', 'user_id');
  160. }
  161. /**
  162. * 关联会员等级表
  163. * @return BelongsTo
  164. */
  165. public function grade(): BelongsTo
  166. {
  167. $module = self::getCalledModule();
  168. return $this->belongsTo("app\\{$module}\\model\\user\\Grade", 'grade_id');
  169. }
  170. /**
  171. * 关联收货地址表
  172. * @return HasMany
  173. */
  174. public function address(): HasMany
  175. {
  176. return $this->hasMany('UserAddress');
  177. }
  178. /**
  179. * 关联收货地址表 (默认地址)
  180. * @return BelongsTo
  181. */
  182. public function addressDefault(): BelongsTo
  183. {
  184. return $this->belongsTo('UserAddress', 'address_id');
  185. }
  186. /**
  187. * 获取器:显示性别
  188. * @param $value
  189. * @return string
  190. */
  191. public function getGenderAttr($value): string
  192. {
  193. return $this->gender[$value];
  194. }
  195. /**
  196. * 获取用户信息
  197. * @param $where
  198. * @param array $with
  199. * @return static|array|false|null
  200. */
  201. public static function detail($where, array $with = [])
  202. {
  203. $filter = ['is_delete' => 0];
  204. if (is_array($where)) {
  205. $filter = array_merge($filter, $where);
  206. } else {
  207. $filter['user_id'] = (int)$where;
  208. }
  209. return static::get($filter, $with);
  210. }
  211. /**
  212. * 累积用户的实际消费金额
  213. * @param int $userId
  214. * @param float $expendMoney
  215. * @return mixed
  216. */
  217. public static function setIncUserExpend(int $userId, float $expendMoney)
  218. {
  219. return (new static)->setInc($userId, 'expend_money', $expendMoney);
  220. }
  221. /**
  222. * 累积用户可用余额
  223. * @param int $userId
  224. * @param float $money
  225. * @return mixed
  226. */
  227. public static function setIncBalance(int $userId, float $money)
  228. {
  229. return (new static)->setInc($userId, 'balance', $money);
  230. }
  231. /**
  232. * 消减用户可用余额
  233. * @param int $userId
  234. * @param float $money
  235. * @return mixed
  236. */
  237. public static function setDecBalance(int $userId, float $money)
  238. {
  239. return (new static)->setDec([['user_id', '=', $userId]], 'balance', $money);
  240. }
  241. /**
  242. * 指定会员等级下是否存在用户
  243. * @param int $gradeId
  244. * @return bool
  245. */
  246. public static function checkExistByGradeId(int $gradeId): bool
  247. {
  248. $model = new static;
  249. return (bool)$model->where('grade_id', '=', (int)$gradeId)
  250. ->where('is_delete', '=', 0)
  251. ->value($model->getPk());
  252. }
  253. /**
  254. * 指定的手机号是否已存在
  255. * @param string $mobile
  256. * @return bool
  257. */
  258. public static function checkExistByMobile(string $mobile): bool
  259. {
  260. $model = new static;
  261. return (bool)$model->where('mobile', '=', $mobile)
  262. ->where('is_delete', '=', 0)
  263. ->value($model->getPk());
  264. }
  265. /**
  266. * 累积用户总消费金额
  267. * @param int $userId
  268. * @param float $money
  269. * @return mixed
  270. */
  271. public static function setIncPayMoney(int $userId, float $money)
  272. {
  273. return (new static)->setInc($userId, 'pay_money', $money);
  274. }
  275. /**
  276. * 累积用户实际消费的金额 (批量)
  277. * @param array $data
  278. * @return bool
  279. */
  280. public function onBatchIncExpendMoney(array $data): bool
  281. {
  282. foreach ($data as $userId => $expendMoney) {
  283. static::setIncUserExpend($userId, (float)$expendMoney);
  284. }
  285. return true;
  286. }
  287. /**
  288. * 累积用户的可用积分数量 (批量)
  289. * @param array $data
  290. * @return bool
  291. */
  292. public function onBatchIncPoints(array $data): bool
  293. {
  294. foreach ($data as $userId => $value) {
  295. $this->setInc($userId, 'points', $value);
  296. }
  297. return true;
  298. }
  299. /**
  300. * 累积用户的可用积分
  301. * @param int $userId 用户ID
  302. * @param int $points 累计的积分
  303. * @param string $describe
  304. * @return mixed
  305. */
  306. public static function setIncPoints(int $userId, int $points, string $describe)
  307. {
  308. // 新增积分变动明细
  309. PointsLogModel::add([
  310. 'user_id' => $userId,
  311. 'value' => $points,
  312. 'describe' => $describe,
  313. ]);
  314. // 更新用户可用积分
  315. return (new static)->setInc($userId, 'points', $points);
  316. }
  317. }