User.php 3.6 KB

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