User.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\common\model\plus\agent;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 分销商用户模型
  6. */
  7. class User extends BaseModel
  8. {
  9. protected $name = 'agent_user';
  10. protected $pk = 'user_id';
  11. /**
  12. * 关联会员记录表
  13. * @return \think\model\relation\BelongsTo
  14. */
  15. public function user()
  16. {
  17. return $this->belongsTo('app\\common\\model\\user\\User');
  18. }
  19. /**
  20. * 关联推荐人表
  21. * @return \think\model\relation\BelongsTo
  22. */
  23. public function referee()
  24. {
  25. return $this->belongsTo('app\\common\\model\\user\\User', 'referee_id', 'user_id');
  26. }
  27. /**
  28. * 详情
  29. */
  30. public static function detail($user_id, $with = ['user', 'referee'])
  31. {
  32. return (new static())->with($with)->find($user_id);
  33. }
  34. /**
  35. * 是否为分销商
  36. */
  37. public static function isAgentUser($user_id)
  38. {
  39. $agent = self::detail($user_id);
  40. return !!$agent && !$agent['is_delete'];
  41. }
  42. /**
  43. * 新增分销商用户记录
  44. * @param $user_id
  45. * @param $data
  46. * @return bool
  47. */
  48. public static function add($user_id, $data)
  49. {
  50. $model = static::detail($user_id) ?: new static;
  51. return $model->save(array_merge([
  52. 'user_id' => $user_id,
  53. 'is_delete' => 0,
  54. 'app_id' => $model::$app_id
  55. ], $data));
  56. }
  57. /**
  58. * 发放分销商佣金
  59. * @param $user_id
  60. * @param $money
  61. * @return bool
  62. */
  63. public static function grantMoney($user_id, $money)
  64. {
  65. // 分销商详情
  66. $model = static::detail($user_id);
  67. if (!$model || $model['is_delete']) {
  68. return false;
  69. }
  70. // 累积分销商可提现佣金
  71. $model->where('user_id', '=', $user_id)->inc('money', $money)->update();
  72. // 记录分销商资金明细
  73. Capital::add([
  74. 'user_id' => $user_id,
  75. 'flow_type' => 10,
  76. 'money' => $money,
  77. 'describe' => '订单佣金结算',
  78. 'app_id' => $model['app_id'],
  79. ]);
  80. return true;
  81. }
  82. }