User.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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\relation\HasOne;
  15. use think\model\relation\HasMany;
  16. use think\model\relation\BelongsTo;
  17. use app\common\model\user\PointsLog as PointsLogModel;
  18. /**
  19. * 用户模型类
  20. * Class User
  21. * @package app\common\model
  22. */
  23. class User extends BaseModel
  24. {
  25. // 定义表名
  26. protected $name = 'user';
  27. // 定义主键
  28. protected $pk = 'user_id';
  29. // 性别
  30. private $gender = [0 => '未知', 1 => '男', 2 => '女'];
  31. /**
  32. * 关联用户头像表
  33. * @return HasOne
  34. */
  35. public function avatar(): HasOne
  36. {
  37. return $this->hasOne('UploadFile', 'file_id', 'avatar_id')
  38. ->bind(['avatar_url' => 'preview_url']);
  39. }
  40. /**
  41. * 用户资料表
  42. * @return HasOne
  43. */
  44. public function info(): HasOne
  45. {
  46. return $this->hasOne("UserInfo", 'user_id', 'user_id');
  47. }
  48. /**
  49. * 关联会员等级表
  50. * @return BelongsTo
  51. */
  52. public function grade(): BelongsTo
  53. {
  54. $module = self::getCalledModule();
  55. return $this->belongsTo("app\\{$module}\\model\\user\\Grade", 'grade_id');
  56. }
  57. /**
  58. * 关联收货地址表
  59. * @return HasMany
  60. */
  61. public function address(): HasMany
  62. {
  63. return $this->hasMany('UserAddress');
  64. }
  65. /**
  66. * 关联收货地址表 (默认地址)
  67. * @return BelongsTo
  68. */
  69. public function addressDefault(): BelongsTo
  70. {
  71. return $this->belongsTo('UserAddress', 'address_id');
  72. }
  73. /**
  74. * 获取器:显示性别
  75. * @param $value
  76. * @return string
  77. */
  78. public function getGenderAttr($value): string
  79. {
  80. return $this->gender[$value];
  81. }
  82. /**
  83. * 获取用户信息
  84. * @param $where
  85. * @param array $with
  86. * @return static|array|false|null
  87. */
  88. public static function detail($where, array $with = [])
  89. {
  90. $filter = ['is_delete' => 0];
  91. if (is_array($where)) {
  92. $filter = array_merge($filter, $where);
  93. } else {
  94. $filter['user_id'] = (int)$where;
  95. }
  96. return static::get($filter, $with);
  97. }
  98. /**
  99. * 累积用户的实际消费金额
  100. * @param int $userId
  101. * @param float $expendMoney
  102. * @return mixed
  103. */
  104. public static function setIncUserExpend(int $userId, float $expendMoney)
  105. {
  106. return (new static)->setInc($userId, 'expend_money', $expendMoney);
  107. }
  108. /**
  109. * 累积用户可用余额
  110. * @param int $userId
  111. * @param float $money
  112. * @return mixed
  113. */
  114. public static function setIncBalance(int $userId, float $money)
  115. {
  116. return (new static)->setInc($userId, 'balance', $money);
  117. }
  118. /**
  119. * 消减用户可用余额
  120. * @param int $userId
  121. * @param float $money
  122. * @return mixed
  123. */
  124. public static function setDecBalance(int $userId, float $money)
  125. {
  126. return (new static)->setDec([['user_id', '=', $userId]], 'balance', $money);
  127. }
  128. /**
  129. * 指定会员等级下是否存在用户
  130. * @param int $gradeId
  131. * @return bool
  132. */
  133. public static function checkExistByGradeId(int $gradeId): bool
  134. {
  135. $model = new static;
  136. return (bool)$model->where('grade_id', '=', (int)$gradeId)
  137. ->where('is_delete', '=', 0)
  138. ->value($model->getPk());
  139. }
  140. /**
  141. * 指定的手机号是否已存在
  142. * @param string $mobile
  143. * @return bool
  144. */
  145. public static function checkExistByMobile(string $mobile): bool
  146. {
  147. $model = new static;
  148. return (bool)$model->where('mobile', '=', $mobile)
  149. ->where('is_delete', '=', 0)
  150. ->value($model->getPk());
  151. }
  152. /**
  153. * 累积用户总消费金额
  154. * @param int $userId
  155. * @param float $money
  156. * @return mixed
  157. */
  158. public static function setIncPayMoney(int $userId, float $money)
  159. {
  160. return (new static)->setInc($userId, 'pay_money', $money);
  161. }
  162. /**
  163. * 累积用户实际消费的金额 (批量)
  164. * @param array $data
  165. * @return bool
  166. */
  167. public function onBatchIncExpendMoney(array $data): bool
  168. {
  169. foreach ($data as $userId => $expendMoney) {
  170. static::setIncUserExpend($userId, (float)$expendMoney);
  171. }
  172. return true;
  173. }
  174. /**
  175. * 累积用户的可用积分数量 (批量)
  176. * @param array $data
  177. * @return bool
  178. */
  179. public function onBatchIncPoints(array $data): bool
  180. {
  181. foreach ($data as $userId => $value) {
  182. $this->setInc($userId, 'points', $value);
  183. }
  184. return true;
  185. }
  186. /**
  187. * 累积用户的可用积分
  188. * @param int $userId 用户ID
  189. * @param int $points 累计的积分
  190. * @param string $describe
  191. * @return mixed
  192. */
  193. public static function setIncPoints(int $userId, int $points, string $describe)
  194. {
  195. // 新增积分变动明细
  196. PointsLogModel::add([
  197. 'user_id' => $userId,
  198. 'value' => $points,
  199. 'describe' => $describe,
  200. ]);
  201. // 更新用户可用积分
  202. return (new static)->setInc($userId, 'points', $points);
  203. }
  204. }