Agent.php 3.9 KB

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