User.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\common\model\user;
  3. use app\common\model\BaseModel;
  4. use app\common\model\user\PointsLog as PointsLogModel;
  5. use app\common\model\supplier\User as SupplierUserModel;
  6. /**
  7. * 用户模型
  8. */
  9. class User extends BaseModel
  10. {
  11. protected $pk = 'user_id';
  12. protected $name = 'user';
  13. /**
  14. * 关联会员等级表
  15. */
  16. public function grade()
  17. {
  18. return $this->belongsTo('app\\common\\model\\user\\Grade', 'grade_id', 'grade_id');
  19. }
  20. /**
  21. * 关联收货地址表
  22. */
  23. public function address()
  24. {
  25. return $this->hasMany('app\\common\\model\\user\\UserAddress', 'address_id', 'address_id');
  26. }
  27. /**
  28. * 关联供应商表
  29. */
  30. public function supplierUser()
  31. {
  32. return $this->hasOne('app\\common\\model\\supplier\\User', 'user_id', 'user_id');
  33. }
  34. /**
  35. * 关联收货地址表 (默认地址)
  36. */
  37. public function addressDefault()
  38. {
  39. return $this->belongsTo('app\\common\\model\\user\\UserAddress', 'address_id', 'address_id');
  40. }
  41. /**
  42. * 获取用户信息
  43. */
  44. public static function detail($where)
  45. {
  46. $model = new static;
  47. $filter = ['is_delete' => 0];
  48. if (is_array($where)) {
  49. $filter = array_merge($filter, $where);
  50. } else {
  51. $filter['user_id'] = (int)$where;
  52. }
  53. return $model->where($filter)->with(['address', 'addressDefault', 'grade'])->find();
  54. }
  55. /**
  56. * 获取用户信息
  57. */
  58. public static function detailByUnionid($unionid)
  59. {
  60. $model = new static;
  61. $filter = ['is_delete' => 0];
  62. $filter = array_merge($filter, ['union_id' => $unionid]);
  63. return $model->where($filter)->with(['address', 'addressDefault', 'grade'])->find();
  64. }
  65. /**
  66. * 指定会员等级下是否存在用户
  67. */
  68. public static function checkExistByGradeId($gradeId)
  69. {
  70. $model = new static;
  71. return !!$model->where('grade_id', '=', (int)$gradeId)
  72. ->where('is_delete', '=', 0)
  73. ->value('user_id');
  74. }
  75. /**
  76. * 累积用户总消费金额
  77. */
  78. public function setIncPayMoney($money)
  79. {
  80. return $this->where('user_id', '=', $this['user_id'])->inc('pay_money', $money)->update();
  81. }
  82. /**
  83. * 累积用户实际消费的金额 (批量)
  84. */
  85. public function onBatchIncExpendMoney($data)
  86. {
  87. foreach ($data as $userId => $expendMoney) {
  88. $this->where(['user_id' => $userId])->inc('expend_money', $expendMoney)->update();
  89. event('UserGrade', $userId);
  90. }
  91. return true;
  92. }
  93. /**
  94. * 累积用户的可用积分数量 (批量)
  95. */
  96. public function onBatchIncPoints($data)
  97. {
  98. foreach ($data as $userId => $expendPoints) {
  99. $this->where(['user_id' => $userId])->inc('points', $expendPoints)->update();
  100. }
  101. return true;
  102. }
  103. /**
  104. * 累积用户的可用积分
  105. */
  106. public function setIncPoints($points, $describe)
  107. {
  108. // 新增积分变动明细
  109. PointsLogModel::add([
  110. 'user_id' => $this['user_id'],
  111. 'value' => $points,
  112. 'describe' => $describe,
  113. 'app_id' => $this['app_id'],
  114. ]);
  115. // 更新用户可用积分
  116. $data['points'] = ($this['points'] + $points <= 0) ? 0 : $this['points'] + $points;
  117. // 用户总积分
  118. if ($points > 0) {
  119. $data['total_points'] = $this['total_points'] + $points;
  120. }
  121. $this->where('user_id', '=', $this['user_id'])->update($data);
  122. event('UserGrade', $this['user_id']);
  123. return true;
  124. }
  125. //更新用户类型
  126. public static function updateType($user_id, $user_type)
  127. {
  128. $model = new static;
  129. return $model->where('user_id', '=', $user_id)->update([
  130. 'user_type' => $user_type
  131. ]);
  132. }
  133. /**
  134. * 用户是否成功成为供应商,如果不是则为审核中
  135. * 申请中的不算
  136. */
  137. public static function isSupplier($user_id)
  138. {
  139. return SupplierUserModel::detail([
  140. 'user_id' => $user_id
  141. ]) != null;
  142. }
  143. /**
  144. * 累计邀请书
  145. */
  146. public function setIncInvite($user_id)
  147. {
  148. $this->where('user_id', '=', $user_id)->inc('total_invite')->update();
  149. event('UserGrade', $user_id);
  150. }
  151. }