|
@@ -1,138 +1,149 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace app\common\model\commission;
|
|
|
-
|
|
|
-use app\common\model\User;
|
|
|
-use app\common\Enum\AgentType;
|
|
|
-use app\common\library\BcMath;
|
|
|
-use app\common\model\commission\Level;
|
|
|
-use app\common\model\commission\Reward;
|
|
|
-use think\Model;
|
|
|
-class Agent extends Model
|
|
|
-{
|
|
|
- protected $pk = 'user_id';
|
|
|
-
|
|
|
- protected $name = 'shop_commission_agent';
|
|
|
-
|
|
|
- protected $type = [
|
|
|
- 'become_time' => 'timestamp',
|
|
|
- 'child_agent_level_1' => 'json',
|
|
|
- 'child_agent_level_all' => 'json',
|
|
|
- ];
|
|
|
- protected $append = [
|
|
|
- 'status_text',
|
|
|
- 'agent_type_text',
|
|
|
- 'pending_reward',
|
|
|
- 'commission_balance'
|
|
|
- ];
|
|
|
-
|
|
|
- // 分销商状态 AGENT_STATUS
|
|
|
- const AGENT_STATUS_NORMAL = 'normal'; // 正常
|
|
|
- const AGENT_STATUS_PENDING = 'pending'; // 审核中 不分佣、不打款、没有团队信息
|
|
|
- const AGENT_STATUS_FREEZE = 'freeze'; // 冻结 正常记录分佣、不打款,记录业绩和团队信息 冻结解除后立即打款
|
|
|
- const AGENT_STATUS_FORBIDDEN = 'forbidden'; // 禁用 不分佣、不记录业绩和团队信息
|
|
|
- const AGENT_STATUS_NEEDINFO = 'needinfo'; // 需要完善表单资料 临时状态
|
|
|
- const AGENT_STATUS_REJECT = 'reject'; // 审核驳回, 重新修改 临时状态
|
|
|
- const AGENT_STATUS_NULL = NULL; // 未满足成为分销商条件
|
|
|
-
|
|
|
-
|
|
|
- // 分销商升级锁 UPGRADE_LOCK
|
|
|
- const UPGRADE_LOCK_OPEN = 1; // 禁止分销商升级
|
|
|
- const UPGRADE_LOCK_CLOSE = 0; // 允许分销商升级
|
|
|
-
|
|
|
- public function statusList()
|
|
|
- {
|
|
|
- return [
|
|
|
- self::AGENT_STATUS_NORMAL => '正常',
|
|
|
- self::AGENT_STATUS_PENDING => '审核中',
|
|
|
- self::AGENT_STATUS_FREEZE => '冻结',
|
|
|
- self::AGENT_STATUS_FORBIDDEN => '禁用',
|
|
|
- self::AGENT_STATUS_REJECT => '拒绝'
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取状态文本
|
|
|
- */
|
|
|
- public function getStatusTextAttr($value, $data)
|
|
|
- {
|
|
|
- $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
|
|
|
- $statusList = $this->statusList();
|
|
|
- return isset($statusList[$value]) ? $statusList[$value] : '正常';
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取代理商类型文本
|
|
|
- */
|
|
|
- public function getAgentTypeTextAttr($value, $data)
|
|
|
- {
|
|
|
- $value = $value ? $value : (isset($data['agent_type']) ? $data['agent_type'] : 'normal');
|
|
|
- return AgentType::getTypeText($value);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 可用分销商
|
|
|
- */
|
|
|
- public function scopeAvaliable($query)
|
|
|
- {
|
|
|
- return $query->where('status', 'in', [self::AGENT_STATUS_NORMAL, self::AGENT_STATUS_FREEZE]);
|
|
|
- }
|
|
|
-
|
|
|
- public function user()
|
|
|
- {
|
|
|
- return $this->belongsTo(User::class, 'user_id', 'id')->field('id, nickname, avatar, mobile, total_consume, parent_user_id');
|
|
|
- }
|
|
|
-
|
|
|
- public function levelInfo()
|
|
|
- {
|
|
|
- return $this->belongsTo(Level::class, 'level', 'level')->field(['level', 'name', 'image', 'commission_rules']);
|
|
|
- }
|
|
|
-
|
|
|
- // 添加蛇形命名的关联方法别名
|
|
|
- public function level_info()
|
|
|
- {
|
|
|
- return $this->levelInfo();
|
|
|
- }
|
|
|
-
|
|
|
- public function getPendingRewardAttr($value, $data)
|
|
|
- {
|
|
|
- $amount = Reward::pending()->where('agent_id', $data['user_id'])->sum('commission');
|
|
|
- // 使用BcMath工具类格式化金额,确保精度
|
|
|
- return BcMath::format($amount);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取佣金余额(累计收益 - 已提现金额)
|
|
|
- */
|
|
|
- public function getCommissionBalanceAttr($value, $data)
|
|
|
- {
|
|
|
- $totalIncome = $data['total_income'] ?? '0.00';
|
|
|
- $withdrawnAmount = $data['withdrawn_amount'] ?? '0.00';
|
|
|
-
|
|
|
- // 使用BcMath工具类计算余额,并确保不为负数
|
|
|
- $balance = BcMath::sub($totalIncome, $withdrawnAmount);
|
|
|
- return BcMath::max($balance);
|
|
|
- }
|
|
|
-
|
|
|
- public function levelStatusInfo()
|
|
|
- {
|
|
|
- return $this->belongsTo(Level::class, 'level_status', 'level');
|
|
|
- }
|
|
|
-
|
|
|
- // 添加蛇形命名的关联方法别名
|
|
|
- public function level_status_info()
|
|
|
- {
|
|
|
- return $this->levelStatusInfo();
|
|
|
- }
|
|
|
-
|
|
|
- public function upgradeLevel()
|
|
|
- {
|
|
|
- return $this->belongsTo(Level::class, 'level_status', 'level');
|
|
|
- }
|
|
|
-
|
|
|
- // 添加蛇形命名的关联方法别名
|
|
|
- public function upgrade_level()
|
|
|
- {
|
|
|
- return $this->upgradeLevel();
|
|
|
- }
|
|
|
-}
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\common\model\commission;
|
|
|
+
|
|
|
+use app\common\model\User;
|
|
|
+use app\common\Enum\AgentType;
|
|
|
+use app\common\library\BcMath;
|
|
|
+use app\common\model\commission\Level;
|
|
|
+use app\common\model\commission\Reward;
|
|
|
+use think\Model;
|
|
|
+class Agent extends Model
|
|
|
+{
|
|
|
+ protected $pk = 'user_id';
|
|
|
+
|
|
|
+ protected $name = 'shop_commission_agent';
|
|
|
+
|
|
|
+ protected $type = [
|
|
|
+ 'become_time' => 'timestamp',
|
|
|
+ 'expire_time' => 'timestamp',
|
|
|
+ 'child_agent_level_1' => 'json',
|
|
|
+ 'child_agent_level_all' => 'json',
|
|
|
+ ];
|
|
|
+ protected $append = [
|
|
|
+ 'status_text',
|
|
|
+ 'agent_type_text',
|
|
|
+ 'pending_reward',
|
|
|
+ 'commission_balance'
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 分销商状态 AGENT_STATUS
|
|
|
+ const AGENT_STATUS_NORMAL = 'normal'; // 正常
|
|
|
+ const AGENT_STATUS_PENDING = 'pending'; // 审核中 不分佣、不打款、没有团队信息
|
|
|
+ const AGENT_STATUS_FREEZE = 'freeze'; // 冻结 正常记录分佣、不打款,记录业绩和团队信息 冻结解除后立即打款
|
|
|
+ const AGENT_STATUS_FORBIDDEN = 'forbidden'; // 禁用 不分佣、不记录业绩和团队信息
|
|
|
+ const AGENT_STATUS_NEEDINFO = 'needinfo'; // 需要完善表单资料 临时状态
|
|
|
+ const AGENT_STATUS_REJECT = 'reject'; // 审核驳回, 重新修改 临时状态
|
|
|
+ const AGENT_STATUS_NULL = NULL; // 未满足成为分销商条件
|
|
|
+
|
|
|
+
|
|
|
+ // 分销商升级锁 UPGRADE_LOCK
|
|
|
+ const UPGRADE_LOCK_OPEN = 1; // 禁止分销商升级
|
|
|
+ const UPGRADE_LOCK_CLOSE = 0; // 允许分销商升级
|
|
|
+
|
|
|
+ public function statusList()
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ self::AGENT_STATUS_NORMAL => '正常',
|
|
|
+ self::AGENT_STATUS_PENDING => '审核中',
|
|
|
+ self::AGENT_STATUS_FREEZE => '冻结',
|
|
|
+ self::AGENT_STATUS_FORBIDDEN => '禁用',
|
|
|
+ self::AGENT_STATUS_REJECT => '拒绝'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // public function getExpireTimeAttr($value, $data)
|
|
|
+ // {
|
|
|
+ // // 时间格式
|
|
|
+ // if ($value) {
|
|
|
+ // return date('Y-m-d H:i:s', $value);
|
|
|
+ // }
|
|
|
+ // return '';
|
|
|
+
|
|
|
+ // }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取状态文本
|
|
|
+ */
|
|
|
+ public function getStatusTextAttr($value, $data)
|
|
|
+ {
|
|
|
+ $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
|
|
|
+ $statusList = $this->statusList();
|
|
|
+ return isset($statusList[$value]) ? $statusList[$value] : '正常';
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取代理商类型文本
|
|
|
+ */
|
|
|
+ public function getAgentTypeTextAttr($value, $data)
|
|
|
+ {
|
|
|
+ $value = $value ? $value : (isset($data['agent_type']) ? $data['agent_type'] : 'normal');
|
|
|
+ return AgentType::getTypeText($value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 可用分销商
|
|
|
+ */
|
|
|
+ public function scopeAvaliable($query)
|
|
|
+ {
|
|
|
+ return $query->where('status', 'in', [self::AGENT_STATUS_NORMAL, self::AGENT_STATUS_FREEZE]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function user()
|
|
|
+ {
|
|
|
+ return $this->belongsTo(User::class, 'user_id', 'id')->field('id, nickname, avatar, mobile, total_consume, parent_user_id');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function levelInfo()
|
|
|
+ {
|
|
|
+ return $this->belongsTo(Level::class, 'level', 'level')->field(['level', 'name', 'image', 'commission_rules']);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加蛇形命名的关联方法别名
|
|
|
+ public function level_info()
|
|
|
+ {
|
|
|
+ return $this->levelInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getPendingRewardAttr($value, $data)
|
|
|
+ {
|
|
|
+ $amount = Reward::pending()->where('agent_id', $data['user_id'])->sum('commission');
|
|
|
+ // 使用BcMath工具类格式化金额,确保精度
|
|
|
+ return BcMath::format($amount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取佣金余额(累计收益 - 已提现金额)
|
|
|
+ */
|
|
|
+ public function getCommissionBalanceAttr($value, $data)
|
|
|
+ {
|
|
|
+ $totalIncome = $data['total_income'] ?? '0.00';
|
|
|
+ $withdrawnAmount = $data['withdrawn_amount'] ?? '0.00';
|
|
|
+
|
|
|
+ // 使用BcMath工具类计算余额,并确保不为负数
|
|
|
+ $balance = BcMath::sub($totalIncome, $withdrawnAmount);
|
|
|
+ return BcMath::max($balance);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function levelStatusInfo()
|
|
|
+ {
|
|
|
+ return $this->belongsTo(Level::class, 'level_status', 'level');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加蛇形命名的关联方法别名
|
|
|
+ public function level_status_info()
|
|
|
+ {
|
|
|
+ return $this->levelStatusInfo();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function upgradeLevel()
|
|
|
+ {
|
|
|
+ return $this->belongsTo(Level::class, 'level_status', 'level');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加蛇形命名的关联方法别名
|
|
|
+ public function upgrade_level()
|
|
|
+ {
|
|
|
+ return $this->upgradeLevel();
|
|
|
+ }
|
|
|
+}
|