User.php 3.6 KB

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