User.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\common\model\dealer;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 分销商用户模型
  6. * Class Apply
  7. * @package app\common\model\dealer
  8. */
  9. class User extends BaseModel
  10. {
  11. protected $name = 'dealer_user';
  12. /**
  13. * 关联会员记录表
  14. * @return \think\model\relation\BelongsTo
  15. */
  16. public function user()
  17. {
  18. return $this->belongsTo('app\common\model\User');
  19. }
  20. /**
  21. * 关联推荐人表
  22. * @return \think\model\relation\BelongsTo
  23. */
  24. public function referee()
  25. {
  26. return $this->belongsTo('app\common\model\User', 'referee_id')
  27. ->field(['user_id', 'nickName']);
  28. }
  29. /**
  30. * 获取分销商用户信息
  31. * @param $user_id
  32. * @return null|static
  33. * @throws \think\exception\DbException
  34. */
  35. public static function detail($user_id)
  36. {
  37. return self::get($user_id, ['user', 'referee']);
  38. }
  39. /**
  40. * 是否为分销商
  41. * @param $user_id
  42. * @return bool
  43. * @throws \think\exception\DbException
  44. */
  45. public static function isDealerUser($user_id)
  46. {
  47. $dealer = self::detail($user_id);
  48. return !!$dealer && !$dealer['is_delete'];
  49. }
  50. /**
  51. * 新增分销商用户记录
  52. * @param $user_id
  53. * @param $data
  54. * @return false|int
  55. * @throws \think\exception\DbException
  56. */
  57. public static function add($user_id, $data)
  58. {
  59. $model = static::detail($user_id) ?: new static;
  60. return $model->save(array_merge([
  61. 'user_id' => $user_id,
  62. 'is_delete' => 0,
  63. 'wxapp_id' => $model::$wxapp_id
  64. ], $data));
  65. }
  66. /**
  67. * 发放分销商佣金
  68. * @param $user_id
  69. * @param $money
  70. * @return bool
  71. * @throws \think\Exception
  72. * @throws \think\exception\DbException
  73. */
  74. public static function grantMoney($user_id, $money, $type=4, $remark='')
  75. {
  76. // 分销商详情
  77. $model = static::detail($user_id);
  78. if (!$model || $model['is_delete']) {
  79. return false;
  80. }
  81. // 累积分销商可提现佣金
  82. $model->setInc('money', $money);
  83. // 记录分销商资金明细
  84. Capital::add([
  85. 'user_id' => $user_id,
  86. 'type' => $type,
  87. 'flow_type' => 10,
  88. 'money' => $money,
  89. 'describe' => $remark? $remark : '订单佣金结算',
  90. 'wxapp_id' => $model['wxapp_id'],
  91. ]);
  92. return true;
  93. }
  94. }