User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 $data
  68. * @return false|int
  69. */
  70. public function edit($data)
  71. {
  72. if (!$this->validateForm($data, 'edit')) {
  73. return false;
  74. }
  75. return $this->allowField(true)->save($data) !== false;
  76. }
  77. /**
  78. * 表单验证
  79. * @param $data
  80. * @param string $scene
  81. * @return bool
  82. */
  83. private function validateForm($data, $scene = 'edit')
  84. {
  85. if ($scene === 'edit') {
  86. // 需要判断等级权重是否已存在
  87. $checkId = self::where(['mobile'=> $data['mobile']])->whereNotIn('user_id',[$data['user_id']])->value('user_id');
  88. if ($data['mobile'] && $checkId && $data['user_id'] != $checkId) {
  89. $this->error = '用户手机号已存在';
  90. return false;
  91. }
  92. }
  93. return true;
  94. }
  95. /**
  96. * 累积用户的实际消费金额
  97. * @param $userId
  98. * @param $expendMoney
  99. * @return int|true
  100. * @throws \think\Exception
  101. */
  102. public function setIncUserExpend($userId, $expendMoney)
  103. {
  104. return $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney);
  105. }
  106. /**
  107. * 指定会员等级下是否存在用户
  108. * @param $gradeId
  109. * @return bool
  110. */
  111. public static function checkExistByGradeId($gradeId)
  112. {
  113. $model = new static;
  114. return !!$model->where('grade_id', '=', (int)$gradeId)
  115. ->where('is_delete', '=', 0)
  116. ->value('user_id');
  117. }
  118. /**
  119. * 累积用户总消费金额
  120. * @param $money
  121. * @return int|true
  122. * @throws \think\Exception
  123. */
  124. public function setIncPayMoney($money)
  125. {
  126. return $this->setInc('pay_money', $money);
  127. }
  128. /**
  129. * 累积用户升级总消费金额
  130. * @param $money
  131. * @return int|true
  132. * @throws \think\Exception
  133. */
  134. public function setIncUpgradePayMoney($money)
  135. {
  136. return $this->setInc('expend_upgrade_money', $money);
  137. }
  138. /**
  139. * 累积用户实际消费的金额 (批量)
  140. * @param $data
  141. * @return array|false
  142. * @throws \Exception
  143. */
  144. public function onBatchIncExpendMoney($data)
  145. {
  146. foreach ($data as $userId => $expendMoney) {
  147. $this->where(['user_id' => $userId])->setInc('expend_money', $expendMoney);
  148. }
  149. return true;
  150. }
  151. /**
  152. * 累积用户实际升级消费的金额 (批量)
  153. * @param $data
  154. * @return array|false
  155. * @throws \Exception
  156. */
  157. public function onBatchIncUpgradeMoney($data)
  158. {
  159. foreach ($data as $userId => $money) {
  160. if($money>0){
  161. $this->where(['user_id' => $userId])->setInc('expend_upgrade_money', $money);
  162. }
  163. }
  164. return true;
  165. }
  166. /**
  167. * 累积用户的可用积分数量 (批量)
  168. * @param $data
  169. * @return array|false
  170. * @throws \Exception
  171. */
  172. public function onBatchIncPoints($data)
  173. {
  174. foreach ($data as $userId => $expendMoney) {
  175. $this->where(['user_id' => $userId])->setInc('points', $expendMoney);
  176. }
  177. return true;
  178. }
  179. /**
  180. * 累积用户的可用积分
  181. * @param $points
  182. * @param $describe
  183. * @return int|true
  184. * @throws \think\Exception
  185. */
  186. public function setIncPoints($points, $describe)
  187. {
  188. // 新增积分变动明细
  189. PointsLogModel::add([
  190. 'user_id' => $this['user_id'],
  191. 'value' => $points,
  192. 'describe' => $describe,
  193. ]);
  194. // 更新用户可用积分
  195. return $this->setInc('points', $points);
  196. }
  197. }