User.php 6.0 KB

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