User.php 12 KB

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