User.php 5.9 KB

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