| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace app\models\system;
- use app\models\user\UserAgentLevel; // 用户代理等级记录表
- //use app\models\user\SystemAgentTask; // 代理任务表(关联升级任务)
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * 代理等级设置模型(对应表:system_user_agent_level)
- * 管理代理等级配置:名称、返利比例、折扣、有效期等核心规则
- * Class SystemUserAgentLevel
- * @package app\models\system
- */
- class SystemUserAgentLevel extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型对应数据表名
- * @var string
- */
- protected $name = 'system_user_agent_level';
- use ModelTrait;
- /**
- * 时间字段格式化(add_time)
- * @param int $value 时间戳
- * @return string 格式化时间(Y-m-d H:i:s)
- */
- public function getAddTimeAttr($value)
- {
- return $value ? date('Y-m-d H:i:s', $value) : '';
- }
- /**
- * 直推返利比例格式化(确保浮点型)
- * @param mixed $value 数据库存储值
- * @return float 返利比例(百分比)
- */
- public function getDirectRebatesAttr($value)
- {
- return (float)$value;
- }
- /**
- * 间接返利比例格式化
- * @param mixed $value 数据库存储值
- * @return float 返利比例(百分比)
- */
- public function getIndirectRebatesAttr($value)
- {
- return (float)$value;
- }
- /**
- * 伞下返利比例格式化
- * @param mixed $value 数据库存储值
- * @return float 返利比例(百分比)
- */
- public function getUmbrellaRebatesAttr($value)
- {
- return (float)$value;
- }
- /**
- * 进货折扣格式化(确保整数)
- * @param mixed $value 数据库存储值
- * @return int 折扣(百分比)
- */
- public function getDiscountAttr($value)
- {
- return (int)$value;
- }
- /**
- * 构建代理等级查询基础条件(显示中、未删除)
- * @param string $alias 表别名(联查时使用)
- * @param self|null $model 模型实例(支持链式调用)
- * @return self
- */
- public static function setWhere($alias = '', self $model = null)
- {
- $model = $model ?? new self();
- if ($alias) {
- $model->alias($alias);
- $alias .= '.';
- }
- // 筛选条件:显示状态(1=显示)、未删除(0=正常)
- return $model->where("{$alias}is_show", 1)->where("{$alias}is_del", 0);
- }
- /**
- * 获取指定代理等级的进货折扣
- * @param int $id 代理等级ID(0则取最低等级折扣)
- * @return int 折扣值(百分比)
- */
- public static function getAgentDiscount($id = 0)
- {
- $query = self::setWhere();
- $id ? $query->where('id', $id) : $query->order('grade', 'asc');
- return $query->value('discount') ?: 0;
- }
- /**
- * 获取指定代理等级的返利比例(按类型区分)
- * @param int $id 代理等级ID
- * @param string $type 返利类型(direct=直推、indirect=间接、umbrella=伞下)
- * @return float 返利比例(百分比)
- */
- public static function getAgentRebates($id = 0, $type = 'direct')
- {
- if (!$id) return 0.00;
- // PHP7.3 用 switch 替代 match 表达式,实现类型→字段的映射
- switch ($type) {
- case 'indirect':
- $field = 'indirect_rebates';
- break;
- case 'umbrella':
- $field = 'umbrella_rebates';
- break;
- default:
- // 默认返回直推返利(对应原 match 的 default 分支)
- $field = 'direct_rebates';
- break;
- }
- // 查询并返回对应返利比例
- return self::setWhere()->where('id', $id)->value($field) ?: 0.00;
- }
- /**
- * 获取用户当前代理等级信息(含等级配置+任务)
- * @param int $uid 用户ID
- * @param bool $isArray 是否返回[等级列表, 任务列表]
- * @return array|bool|array|null 等级信息
- * @throws DataNotFoundException|ModelNotFoundException|DbException
- */
- public static function getAgentLevelInfo($uid, $isArray = false)
- {
- $level = ['id' => 0, 'name' => '普通用户', 'grade' => 0]; // 默认无等级
- $task = [];
- // 获取用户当前有效代理等级记录
- $agentLevelId = UserAgentLevel::getUserAgentLevel($uid);
- if ($agentLevelId !== false) {
- $level = UserAgentLevel::getUserAgentLevelInfo($agentLevelId) ?: $level;
- }
- // 获取代理等级列表(含升级标记)
- $levelList = self::getAgentLevelListAndGrade($level['id'], $isArray);
- // 关联当前等级任务(若需要)
- // if ($isArray && isset($levelList[0])) {
- // $task = SystemAgentTask::getTaskList($levelList[0]['id'], $uid, $level);
- // }
- return $isArray ? [$levelList, $task] : ($level['id'] ? $level : false);
- }
- /**
- * 获取代理等级的排序值(grade)
- * @param int $id 代理等级ID
- * @return int 排序值(数值越大等级越高)
- */
- public static function getAgentLevelGrade($id)
- {
- return self::setWhere()->where('id', $id)->value('grade') ?: 0;
- }
- /**
- * 获取代理等级列表(含当前等级标记、可升级状态)
- * @param int $currentLevelId 当前等级ID
- * @param bool $isArray 是否需要任务列表字段
- * @param string $order 排序方式
- * @return array 等级列表
- * @throws DataNotFoundException|ModelNotFoundException|DbException
- */
- public static function getAgentLevelListAndGrade($currentLevelId, $isArray, $order = 'grade asc')
- {
- // 查询所有有效代理等级
- $list = self::setWhere()
- ->field('id, name, direct_rebates, indirect_rebates, umbrella_rebates, discount, icon, image, explain, grade, is_forever, valid_days')
- ->order($order)
- ->select()
- ->toArray() ?: [];
- $currentGrade = $currentLevelId ? self::getAgentLevelGrade($currentLevelId) : 0;
- // 处理列表字段:标记当前等级、可升级状态、格式化有效期
- foreach ($list as &$item) {
- $item['is_current'] = ($item['id'] == $currentLevelId);
- $item['is_clear'] = ($currentGrade < $item['grade']); // 是否可升级
- $item['valid_text'] = $item['is_forever'] ? '永久有效' : "{$item['valid_days']}天";
- // if ($isArray) $item['task_list'] = []; // 预留任务列表字段
- }
- // 若无当前等级,默认选中最低等级
- if (!$currentLevelId && $list) $list[0]['is_current'] = true;
- return $list;
- }
- /**
- * 判断目标等级是否可升级(基于当前等级)
- * @param int $targetLevelId 目标等级ID
- * @param int $currentLevelId 当前等级ID
- * @param array|null $levelList 已查询的等级列表(可选)
- * @return bool 是否可升级
- * @throws DataNotFoundException|ModelNotFoundException|DbException
- */
- public static function getAgentClear($targetLevelId, $currentLevelId, $levelList = null)
- {
- $levelList = $levelList ?? self::getAgentLevelListAndGrade($currentLevelId, false);
- foreach ($levelList as $item) {
- if ($item['id'] == $targetLevelId) return $item['is_clear'];
- }
- return false;
- }
- /**
- * 获取当前等级的下一个可升级等级ID
- * @param int $currentLevelId 当前等级ID
- * @param int $maxGrade 最高等级排序限制
- * @return int 下一级等级ID(0=无)
- * @throws DataNotFoundException|ModelNotFoundException|DbException
- */
- public static function getNextAgentLevelId($currentLevelId, $maxGrade)
- {
- $levelList = self::getAgentLevelListAndGrade($currentLevelId, false, 'grade desc');
- $currentGrade = self::getAgentLevelGrade($currentLevelId);
- $higherLevels = [];
- // 筛选:等级高于当前且不超过最高限制
- foreach ($levelList as $item) {
- if ($item['grade'] > $currentGrade && $item['grade'] <= $maxGrade) {
- $higherLevels[] = $item['id'];
- }
- }
- return $higherLevels ? end($higherLevels) : 0;
- }
- // /**
- // * 获取用户代理等级列表+任务列表(前端展示用)
- // * @param int $uid 用户ID
- // * @return array 格式:['list' => 等级列表, 'task' => 当前任务]
- // * @throws DataNotFoundException|ModelNotFoundException|DbException
- // */
- // public static function getAgentLevelList($uid)
- // {
- // list($levelList, $task) = self::getAgentLevelInfo($uid, true);
- // return compact('levelList', 'task');
- // }
- }
|