User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\common\model;
  10. use app\common\model\user\PointsLog as PointsLogModel;
  11. /**
  12. * 用户模型类
  13. * Class User
  14. * @package app\common\model
  15. */
  16. class User extends BaseModel
  17. {
  18. protected $name = 'user';
  19. // 性别
  20. private $gender = ['未知', '男', '女'];
  21. /**
  22. * 关联会员等级表
  23. * @return \think\model\relation\BelongsTo
  24. */
  25. public function grade()
  26. {
  27. $module = self::getCalledModule() ?: 'common';
  28. return $this->belongsTo("app\\{$module}\\model\\user\\Grade");
  29. }
  30. /**
  31. * 关联收货地址表
  32. * @return \think\model\relation\HasMany
  33. */
  34. public function address()
  35. {
  36. return $this->hasMany('UserAddress');
  37. }
  38. /**
  39. * 关联收货地址表 (默认地址)
  40. * @return \think\model\relation\BelongsTo
  41. */
  42. public function addressDefault()
  43. {
  44. return $this->belongsTo('UserAddress', 'address_id');
  45. }
  46. /**
  47. * 显示性别
  48. * @param $value
  49. * @return mixed
  50. */
  51. public function getGenderAttr($value)
  52. {
  53. return $this->gender[$value];
  54. }
  55. /**
  56. * 获取用户信息
  57. * @param $where
  58. * @param $with
  59. * @return null|static
  60. * @throws \think\exception\DbException
  61. */
  62. public static function detail($where, $with = ['address', 'addressDefault'])
  63. {
  64. $filter = ['is_delete' => 0];
  65. if (is_array($where)) {
  66. $filter = array_merge($filter, $where);
  67. } else {
  68. $filter['user_id'] = (int)$where;
  69. }
  70. return static::get($filter, $with);
  71. }
  72. /**
  73. * 累积用户的实际消费金额
  74. * @param $userId
  75. * @param $expendMoney
  76. * @return int|true
  77. * @throws \think\Exception
  78. */
  79. public function setIncUserExpend($userId, $expendMoney)
  80. {
  81. return $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney);
  82. }
  83. /**
  84. * 指定会员等级下是否存在用户
  85. * @param $gradeId
  86. * @return bool
  87. */
  88. public static function checkExistByGradeId($gradeId)
  89. {
  90. $model = new static;
  91. return !!$model->where('grade_id', '=', (int)$gradeId)
  92. ->where('is_delete', '=', 0)
  93. ->value('user_id');
  94. }
  95. /**
  96. * 累积用户总消费金额
  97. * @param $money
  98. * @return int|true
  99. * @throws \think\Exception
  100. */
  101. public function setIncPayMoney($money)
  102. {
  103. return $this->setInc('pay_money', $money);
  104. }
  105. /**
  106. * 累积用户实际消费的金额 (批量)
  107. * @param $data
  108. * @return array|false
  109. * @throws \Exception
  110. */
  111. public function onBatchIncExpendMoney($data)
  112. {
  113. foreach ($data as $userId => $expendMoney) {
  114. $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney);
  115. }
  116. return true;
  117. }
  118. /**
  119. * 累积用户的可用积分数量 (批量)
  120. * @param $data
  121. * @return array|false
  122. * @throws \Exception
  123. */
  124. public function onBatchIncPoints($data)
  125. {
  126. foreach ($data as $userId => $expendMoney) {
  127. $this->where(['user_id' => $userId])->setInc('points', $expendMoney);
  128. }
  129. return true;
  130. }
  131. /**
  132. * 累积用户的可用积分
  133. * @param $points
  134. * @param $describe
  135. * @return int|true
  136. * @throws \think\Exception
  137. */
  138. public function setIncPoints($points, $describe)
  139. {
  140. // 新增积分变动明细
  141. PointsLogModel::add([
  142. 'user_id' => $this['user_id'],
  143. 'value' => $points,
  144. 'describe' => $describe,
  145. ]);
  146. // 更新用户可用积分
  147. return $this->setInc('points', $points);
  148. }
  149. }