User.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 BelongsTo
  43. */
  44. public function grade(): BelongsTo
  45. {
  46. $module = self::getCalledModule();
  47. return $this->belongsTo("app\\{$module}\\model\\user\\Grade", 'grade_id');
  48. }
  49. /**
  50. * 关联收货地址表
  51. * @return HasMany
  52. */
  53. public function address(): HasMany
  54. {
  55. return $this->hasMany('UserAddress');
  56. }
  57. /**
  58. * 关联收货地址表 (默认地址)
  59. * @return BelongsTo
  60. */
  61. public function addressDefault(): BelongsTo
  62. {
  63. return $this->belongsTo('UserAddress', 'address_id');
  64. }
  65. /**
  66. * 获取器:显示性别
  67. * @param $value
  68. * @return string
  69. */
  70. public function getGenderAttr($value): string
  71. {
  72. return $this->gender[$value];
  73. }
  74. /**
  75. * 获取用户信息
  76. * @param $where
  77. * @param array $with
  78. * @return static|array|null
  79. */
  80. public static function detail($where, array $with = [])
  81. {
  82. $filter = ['is_delete' => 0];
  83. if (is_array($where)) {
  84. $filter = array_merge($filter, $where);
  85. } else {
  86. $filter['user_id'] = (int)$where;
  87. }
  88. return static::get($filter, $with);
  89. }
  90. /**
  91. * 累积用户的实际消费金额
  92. * @param int $userId
  93. * @param float $expendMoney
  94. * @return mixed
  95. */
  96. public static function setIncUserExpend(int $userId, float $expendMoney)
  97. {
  98. return (new static)->setInc($userId, 'expend_money', $expendMoney);
  99. }
  100. /**
  101. * 累积用户可用余额
  102. * @param int $userId
  103. * @param float $money
  104. * @return mixed
  105. */
  106. public static function setIncBalance(int $userId, float $money)
  107. {
  108. return (new static)->setInc($userId, 'balance', $money);
  109. }
  110. /**
  111. * 消减用户可用余额
  112. * @param int $userId
  113. * @param float $money
  114. * @return mixed
  115. */
  116. public static function setDecBalance(int $userId, float $money)
  117. {
  118. return (new static)->setDec([['user_id', '=', $userId]], 'balance', $money);
  119. }
  120. /**
  121. * 指定会员等级下是否存在用户
  122. * @param int $gradeId
  123. * @return bool
  124. */
  125. public static function checkExistByGradeId(int $gradeId): bool
  126. {
  127. $model = new static;
  128. return (bool)$model->where('grade_id', '=', (int)$gradeId)
  129. ->where('is_delete', '=', 0)
  130. ->value($model->getPk());
  131. }
  132. /**
  133. * 指定的手机号是否已存在
  134. * @param string $mobile
  135. * @return bool
  136. */
  137. public static function checkExistByMobile(string $mobile): bool
  138. {
  139. $model = new static;
  140. return (bool)$model->where('mobile', '=', $mobile)
  141. ->where('is_delete', '=', 0)
  142. ->value($model->getPk());
  143. }
  144. /**
  145. * 累积用户总消费金额
  146. * @param int $userId
  147. * @param float $money
  148. * @return mixed
  149. */
  150. public static function setIncPayMoney(int $userId, float $money)
  151. {
  152. return (new static)->setInc($userId, 'pay_money', $money);
  153. }
  154. /**
  155. * 累积用户实际消费的金额 (批量)
  156. * @param array $data
  157. * @return bool
  158. */
  159. public function onBatchIncExpendMoney(array $data): bool
  160. {
  161. foreach ($data as $userId => $expendMoney) {
  162. static::setIncUserExpend($userId, (float)$expendMoney);
  163. }
  164. return true;
  165. }
  166. /**
  167. * 累积用户的可用积分数量 (批量)
  168. * @param array $data
  169. * @return bool
  170. */
  171. public function onBatchIncPoints(array $data): bool
  172. {
  173. foreach ($data as $userId => $value) {
  174. $this->setInc($userId, 'points', $value);
  175. }
  176. return true;
  177. }
  178. /**
  179. * 累积用户的可用积分
  180. * @param int $userId 用户ID
  181. * @param int $points 累计的积分
  182. * @param string $describe 积分描述
  183. * @param int|null $storeId
  184. * @return mixed
  185. */
  186. public static function setIncPoints(int $userId, int $points, string $describe, ?int $storeId = null)
  187. {
  188. // 新增积分变动明细
  189. PointsLogModel::add([
  190. 'user_id' => $userId,
  191. 'value' => $points,
  192. 'describe' => $describe,
  193. 'store_id' => $storeId
  194. ]);
  195. // 更新用户可用积分
  196. return (new static)->setInc($userId, 'points', $points);
  197. }
  198. }