User.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * @var array
  15. */
  16. protected $type = [
  17. 'first_num' => 'integer',
  18. 'second_num' => 'integer',
  19. 'third_num' => 'integer',
  20. ];
  21. /**
  22. * 关联会员记录表
  23. * @return \think\model\relation\BelongsTo
  24. */
  25. public function user()
  26. {
  27. return $this->belongsTo('app\common\model\User');
  28. }
  29. /**
  30. * 关联推荐人表
  31. * @return \think\model\relation\BelongsTo
  32. */
  33. public function referee()
  34. {
  35. return $this->belongsTo('app\common\model\User', 'referee_id')
  36. ->field(['user_id', 'nickName']);
  37. }
  38. /**
  39. * 获取分销商用户信息
  40. * @param $userId
  41. * @param array $with
  42. * @return static|null
  43. * @throws \think\exception\DbException
  44. */
  45. public static function detail($userId, $with = ['user', 'referee'])
  46. {
  47. return self::get($userId, $with);
  48. }
  49. /**
  50. * 是否为分销商
  51. * @param $userId
  52. * @return bool
  53. */
  54. public static function isDealerUser($userId)
  55. {
  56. return !!(new static)->where('user_id', '=', $userId)
  57. ->where('is_delete', '=', 0)
  58. ->value('user_id');
  59. }
  60. /**
  61. * 新增分销商用户记录
  62. * @param $user_id
  63. * @param $data
  64. * @return false|int
  65. * @throws \think\exception\DbException
  66. */
  67. public static function add($user_id, $data)
  68. {
  69. $model = static::detail($user_id) ?: new static;
  70. return $model->save(array_merge([
  71. 'user_id' => $user_id,
  72. 'is_delete' => 0,
  73. 'wxapp_id' => $model::$wxapp_id
  74. ], $data));
  75. }
  76. /**
  77. * 发放分销商佣金
  78. * @param $user_id
  79. * @param $money
  80. * @return bool
  81. * @throws \think\Exception
  82. * @throws \think\exception\DbException
  83. */
  84. public static function grantMoney($user_id, $money)
  85. {
  86. // 分销商详情
  87. $model = static::detail($user_id);
  88. if (!$model || $model['is_delete']) {
  89. return false;
  90. }
  91. // 累积分销商可提现佣金
  92. $model->setInc('money', $money);
  93. // 记录分销商资金明细
  94. Capital::add([
  95. 'user_id' => $user_id,
  96. 'flow_type' => 10,
  97. 'money' => $money,
  98. 'describe' => '订单佣金结算',
  99. 'wxapp_id' => $model['wxapp_id'],
  100. ]);
  101. return true;
  102. }
  103. }