Agent.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\common\model\commission;
  3. use app\common\model\User;
  4. use app\common\Enum\AgentType;
  5. use app\common\library\BcMath;
  6. use app\common\model\commission\Level;
  7. use app\common\model\commission\Reward;
  8. use think\Model;
  9. class Agent extends Model
  10. {
  11. protected $pk = 'user_id';
  12. protected $name = 'shop_commission_agent';
  13. protected $type = [
  14. 'become_time' => 'timestamp',
  15. 'child_agent_level_1' => 'json',
  16. 'child_agent_level_all' => 'json',
  17. ];
  18. protected $append = [
  19. 'status_text',
  20. 'agent_type_text',
  21. 'pending_reward',
  22. 'commission_balance'
  23. ];
  24. // 分销商状态 AGENT_STATUS
  25. const AGENT_STATUS_NORMAL = 'normal'; // 正常
  26. const AGENT_STATUS_PENDING = 'pending'; // 审核中 不分佣、不打款、没有团队信息
  27. const AGENT_STATUS_FREEZE = 'freeze'; // 冻结 正常记录分佣、不打款,记录业绩和团队信息 冻结解除后立即打款
  28. const AGENT_STATUS_FORBIDDEN = 'forbidden'; // 禁用 不分佣、不记录业绩和团队信息
  29. const AGENT_STATUS_NEEDINFO = 'needinfo'; // 需要完善表单资料 临时状态
  30. const AGENT_STATUS_REJECT = 'reject'; // 审核驳回, 重新修改 临时状态
  31. const AGENT_STATUS_NULL = NULL; // 未满足成为分销商条件
  32. // 分销商升级锁 UPGRADE_LOCK
  33. const UPGRADE_LOCK_OPEN = 1; // 禁止分销商升级
  34. const UPGRADE_LOCK_CLOSE = 0; // 允许分销商升级
  35. public function statusList()
  36. {
  37. return [
  38. self::AGENT_STATUS_NORMAL => '正常',
  39. self::AGENT_STATUS_PENDING => '审核中',
  40. self::AGENT_STATUS_FREEZE => '冻结',
  41. self::AGENT_STATUS_FORBIDDEN => '禁用',
  42. self::AGENT_STATUS_REJECT => '拒绝'
  43. ];
  44. }
  45. /**
  46. * 获取状态文本
  47. */
  48. public function getStatusTextAttr($value, $data)
  49. {
  50. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  51. $statusList = $this->statusList();
  52. return isset($statusList[$value]) ? $statusList[$value] : '正常';
  53. }
  54. /**
  55. * 获取代理商类型文本
  56. */
  57. public function getAgentTypeTextAttr($value, $data)
  58. {
  59. $value = $value ? $value : (isset($data['agent_type']) ? $data['agent_type'] : 'normal');
  60. return AgentType::getTypeText($value);
  61. }
  62. /**
  63. * 可用分销商
  64. */
  65. public function scopeAvaliable($query)
  66. {
  67. return $query->where('status', 'in', [self::AGENT_STATUS_NORMAL, self::AGENT_STATUS_FREEZE]);
  68. }
  69. public function user()
  70. {
  71. return $this->belongsTo(User::class, 'user_id', 'id')->field('id, nickname, avatar, mobile, total_consume, parent_user_id');
  72. }
  73. public function levelInfo()
  74. {
  75. return $this->belongsTo(Level::class, 'level', 'level')->field(['level', 'name', 'image', 'commission_rules']);
  76. }
  77. // 添加蛇形命名的关联方法别名
  78. public function level_info()
  79. {
  80. return $this->levelInfo();
  81. }
  82. public function getPendingRewardAttr($value, $data)
  83. {
  84. $amount = Reward::pending()->where('agent_id', $data['user_id'])->sum('commission');
  85. // 使用BcMath工具类格式化金额,确保精度
  86. return BcMath::format($amount);
  87. }
  88. /**
  89. * 获取佣金余额(累计收益 - 已提现金额)
  90. */
  91. public function getCommissionBalanceAttr($value, $data)
  92. {
  93. $totalIncome = $data['total_income'] ?? '0.00';
  94. $withdrawnAmount = $data['withdrawn_amount'] ?? '0.00';
  95. // 使用BcMath工具类计算余额,并确保不为负数
  96. $balance = BcMath::sub($totalIncome, $withdrawnAmount);
  97. return BcMath::max($balance);
  98. }
  99. public function levelStatusInfo()
  100. {
  101. return $this->belongsTo(Level::class, 'level_status', 'level');
  102. }
  103. // 添加蛇形命名的关联方法别名
  104. public function level_status_info()
  105. {
  106. return $this->levelStatusInfo();
  107. }
  108. public function upgradeLevel()
  109. {
  110. return $this->belongsTo(Level::class, 'level_status', 'level');
  111. }
  112. // 添加蛇形命名的关联方法别名
  113. public function upgrade_level()
  114. {
  115. return $this->upgradeLevel();
  116. }
  117. }