User.php 5.2 KB

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