User.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <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. return $query->where($filter)->where(function($query) use ($params){
  98. // 关键词
  99. if(!empty($params['keyword'])){
  100. $query->where('s.source_shools_name','like', "%{$params['keyword']}%")
  101. ->whereOr('sp.speciality_name','like',"%{$params['keyword']}%");
  102. }
  103. $admissionYear = isset($params['admission_year'])? intval($params['admission_year']) : -1;
  104. if($admissionYear>0){
  105. $query->where('ui.admission_year','<', $admissionYear)->where('ui.admission_year','>', 0);
  106. }else if(isset($params['admission_year'])){
  107. $query->where('ui.admission_year','>', 0);
  108. }
  109. /*$userId = isset($params['user_id'])? $params['user_id'] : 0;
  110. if($userId>0){
  111. $query->whereNotIn($this->name.'.user_id', [$userId]);
  112. }*/
  113. });
  114. }
  115. /**
  116. * 检索排序条件
  117. * @param array $param
  118. * @return array|string[]
  119. */
  120. private function setQuerySort(array $param = [])
  121. {
  122. $params = $this->setQueryDefaultValue($param, [
  123. 'sortType' => 'all', // 排序类型
  124. $this->name.'.create_time' => false, // 热门排序 (true高到低 false低到高)
  125. ]);
  126. // 排序规则
  127. $sort = [];
  128. if ($params['sortType'] === 'all') {
  129. $sort = [$this->name.'.create_time' => 'desc'];
  130. } elseif ($params['sortType'] === 'view') {
  131. $sort = [$this->name.'.id' => 'desc'];
  132. }
  133. return array_merge($sort, [$this->getPk() => 'desc']);
  134. }
  135. /**
  136. * 关联用户头像表
  137. * @return HasOne
  138. */
  139. public function avatar(): HasOne
  140. {
  141. return $this->hasOne('UploadFile', 'file_id', 'avatar_id')
  142. ->bind(['avatar_url' => 'preview_url']);
  143. }
  144. /**
  145. * 用户资料表
  146. * @return HasOne
  147. */
  148. public function info(): HasOne
  149. {
  150. return $this->hasOne("UserInfo", 'user_id', 'user_id');
  151. }
  152. /**
  153. * 关联会员等级表
  154. * @return BelongsTo
  155. */
  156. public function grade(): BelongsTo
  157. {
  158. $module = self::getCalledModule();
  159. return $this->belongsTo("app\\{$module}\\model\\user\\Grade", 'grade_id');
  160. }
  161. /**
  162. * 关联收货地址表
  163. * @return HasMany
  164. */
  165. public function address(): HasMany
  166. {
  167. return $this->hasMany('UserAddress');
  168. }
  169. /**
  170. * 关联收货地址表 (默认地址)
  171. * @return BelongsTo
  172. */
  173. public function addressDefault(): BelongsTo
  174. {
  175. return $this->belongsTo('UserAddress', 'address_id');
  176. }
  177. /**
  178. * 获取器:显示性别
  179. * @param $value
  180. * @return string
  181. */
  182. public function getGenderAttr($value): string
  183. {
  184. return $this->gender[$value];
  185. }
  186. /**
  187. * 获取用户信息
  188. * @param $where
  189. * @param array $with
  190. * @return static|array|false|null
  191. */
  192. public static function detail($where, array $with = [])
  193. {
  194. $filter = ['is_delete' => 0];
  195. if (is_array($where)) {
  196. $filter = array_merge($filter, $where);
  197. } else {
  198. $filter['user_id'] = (int)$where;
  199. }
  200. return static::get($filter, $with);
  201. }
  202. /**
  203. * 累积用户的实际消费金额
  204. * @param int $userId
  205. * @param float $expendMoney
  206. * @return mixed
  207. */
  208. public static function setIncUserExpend(int $userId, float $expendMoney)
  209. {
  210. return (new static)->setInc($userId, 'expend_money', $expendMoney);
  211. }
  212. /**
  213. * 累积用户可用余额
  214. * @param int $userId
  215. * @param float $money
  216. * @return mixed
  217. */
  218. public static function setIncBalance(int $userId, float $money)
  219. {
  220. return (new static)->setInc($userId, 'balance', $money);
  221. }
  222. /**
  223. * 消减用户可用余额
  224. * @param int $userId
  225. * @param float $money
  226. * @return mixed
  227. */
  228. public static function setDecBalance(int $userId, float $money)
  229. {
  230. return (new static)->setDec([['user_id', '=', $userId]], 'balance', $money);
  231. }
  232. /**
  233. * 指定会员等级下是否存在用户
  234. * @param int $gradeId
  235. * @return bool
  236. */
  237. public static function checkExistByGradeId(int $gradeId): bool
  238. {
  239. $model = new static;
  240. return (bool)$model->where('grade_id', '=', (int)$gradeId)
  241. ->where('is_delete', '=', 0)
  242. ->value($model->getPk());
  243. }
  244. /**
  245. * 指定的手机号是否已存在
  246. * @param string $mobile
  247. * @return bool
  248. */
  249. public static function checkExistByMobile(string $mobile): bool
  250. {
  251. $model = new static;
  252. return (bool)$model->where('mobile', '=', $mobile)
  253. ->where('is_delete', '=', 0)
  254. ->value($model->getPk());
  255. }
  256. /**
  257. * 累积用户总消费金额
  258. * @param int $userId
  259. * @param float $money
  260. * @return mixed
  261. */
  262. public static function setIncPayMoney(int $userId, float $money)
  263. {
  264. return (new static)->setInc($userId, 'pay_money', $money);
  265. }
  266. /**
  267. * 累积用户实际消费的金额 (批量)
  268. * @param array $data
  269. * @return bool
  270. */
  271. public function onBatchIncExpendMoney(array $data): bool
  272. {
  273. foreach ($data as $userId => $expendMoney) {
  274. static::setIncUserExpend($userId, (float)$expendMoney);
  275. }
  276. return true;
  277. }
  278. /**
  279. * 累积用户的可用积分数量 (批量)
  280. * @param array $data
  281. * @return bool
  282. */
  283. public function onBatchIncPoints(array $data): bool
  284. {
  285. foreach ($data as $userId => $value) {
  286. $this->setInc($userId, 'points', $value);
  287. }
  288. return true;
  289. }
  290. /**
  291. * 累积用户的可用积分
  292. * @param int $userId 用户ID
  293. * @param int $points 累计的积分
  294. * @param string $describe
  295. * @return mixed
  296. */
  297. public static function setIncPoints(int $userId, int $points, string $describe)
  298. {
  299. // 新增积分变动明细
  300. PointsLogModel::add([
  301. 'user_id' => $userId,
  302. 'value' => $points,
  303. 'describe' => $describe,
  304. ]);
  305. // 更新用户可用积分
  306. return (new static)->setInc($userId, 'points', $points);
  307. }
  308. }