User.php 2.7 KB

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