User.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\common\model\plus\agent;
  3. use app\common\enum\user\grade\ChangeTypeEnum;
  4. use app\common\model\BaseModel;
  5. use app\common\model\plus\agent\GradeLog as GradeLogModel;
  6. /**
  7. * 分销商用户模型
  8. */
  9. class User extends BaseModel
  10. {
  11. protected $name = 'agent_user';
  12. protected $pk = 'user_id';
  13. /**
  14. * 关联会员记录表
  15. * @return \think\model\relation\BelongsTo
  16. */
  17. public function user()
  18. {
  19. return $this->belongsTo('app\\common\\model\\user\\User');
  20. }
  21. /**
  22. * 关联推荐人表
  23. * @return \think\model\relation\BelongsTo
  24. */
  25. public function referee()
  26. {
  27. return $this->belongsTo('app\\common\\model\\user\\User', 'referee_id', 'user_id');
  28. }
  29. /**
  30. * 详情
  31. */
  32. public static function detail($user_id, $with = ['user', 'referee'])
  33. {
  34. return (new static())->with($with)->find($user_id);
  35. }
  36. /**
  37. * 是否为分销商
  38. * @param $user_id
  39. * @return bool
  40. */
  41. public static function isAgentUser($user_id)
  42. {
  43. $agent = self::detail($user_id);
  44. return !!$agent && !$agent['is_delete'];
  45. }
  46. /**
  47. * 新增分销商用户记录
  48. * @param $user_id
  49. * @param $data
  50. * @return bool
  51. */
  52. public static function add($user_id, $data)
  53. {
  54. $model = static::detail($user_id) ?: new static;
  55. $model->save(array_merge([
  56. 'user_id' => $user_id,
  57. 'is_delete' => 0,
  58. 'app_id' => $model::$app_id
  59. ], $data));
  60. event('AgentUserGrade', $model['referee_id']);
  61. return true;
  62. }
  63. /**
  64. * 发放分销商佣金
  65. * @param $user_id
  66. * @param $money
  67. * @return bool
  68. */
  69. public static function grantMoney($user_id, $money)
  70. {
  71. // 分销商详情
  72. $model = static::detail($user_id);
  73. if (!$model || $model['is_delete']) {
  74. return false;
  75. }
  76. // 累积分销商可提现佣金
  77. $model->where('user_id', '=', $user_id)->inc('money', $money)->update();
  78. // 记录分销商资金明细
  79. Capital::add([
  80. 'user_id' => $user_id,
  81. 'flow_type' => 10,
  82. 'money' => $money,
  83. 'describe' => '订单佣金结算',
  84. 'app_id' => $model['app_id'],
  85. ]);
  86. return true;
  87. }
  88. /**
  89. * 累计分销商成员数量
  90. */
  91. public static function setMemberInc($agent_id, $level)
  92. {
  93. $fields = [1 => 'first_num', 2 => 'second_num', 3 => 'third_num'];
  94. $model = static::detail($agent_id);
  95. return $model->where('user_id', '=', $agent_id)->inc($fields[$level])->update();
  96. }
  97. /**
  98. * 批量设置会员等级
  99. */
  100. public function upgradeGrade($user, $upgradeGrade)
  101. {
  102. // 更新会员等级的数据
  103. $this->where('user_id', '=', $user['user_id'])
  104. ->update([
  105. 'grade_id' => $upgradeGrade['grade_id']
  106. ]);
  107. (new GradeLogModel)->save([
  108. 'old_grade_id' => $user['grade_id'],
  109. 'new_grade_id' => $upgradeGrade['grade_id'],
  110. 'change_type' => ChangeTypeEnum::AUTO_UPGRADE,
  111. 'user_id' => $user['user_id'],
  112. 'app_id' => $user['app_id']
  113. ]);
  114. return true;
  115. }
  116. /**
  117. * 详情
  118. */
  119. public static function agentCount($referee_id)
  120. {
  121. return (new static())->where('referee_id', '=', $referee_id)->count();
  122. }
  123. }