Agent.php 4.5 KB

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