SystemUserAgentLevel.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\models\system;
  3. use app\models\user\UserAgentLevel; // 用户代理等级记录表
  4. //use app\models\user\SystemAgentTask; // 代理任务表(关联升级任务)
  5. use crmeb\traits\ModelTrait;
  6. use crmeb\basic\BaseModel;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. /**
  11. * 代理等级设置模型(对应表:system_user_agent_level)
  12. * 管理代理等级配置:名称、返利比例、折扣、有效期等核心规则
  13. * Class SystemUserAgentLevel
  14. * @package app\models\system
  15. */
  16. class SystemUserAgentLevel extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型对应数据表名
  25. * @var string
  26. */
  27. protected $name = 'system_user_agent_level';
  28. use ModelTrait;
  29. /**
  30. * 时间字段格式化(add_time)
  31. * @param int $value 时间戳
  32. * @return string 格式化时间(Y-m-d H:i:s)
  33. */
  34. public function getAddTimeAttr($value)
  35. {
  36. return $value ? date('Y-m-d H:i:s', $value) : '';
  37. }
  38. /**
  39. * 直推返利比例格式化(确保浮点型)
  40. * @param mixed $value 数据库存储值
  41. * @return float 返利比例(百分比)
  42. */
  43. public function getDirectRebatesAttr($value)
  44. {
  45. return (float)$value;
  46. }
  47. /**
  48. * 间接返利比例格式化
  49. * @param mixed $value 数据库存储值
  50. * @return float 返利比例(百分比)
  51. */
  52. public function getIndirectRebatesAttr($value)
  53. {
  54. return (float)$value;
  55. }
  56. /**
  57. * 伞下返利比例格式化
  58. * @param mixed $value 数据库存储值
  59. * @return float 返利比例(百分比)
  60. */
  61. public function getUmbrellaRebatesAttr($value)
  62. {
  63. return (float)$value;
  64. }
  65. /**
  66. * 进货折扣格式化(确保整数)
  67. * @param mixed $value 数据库存储值
  68. * @return int 折扣(百分比)
  69. */
  70. public function getDiscountAttr($value)
  71. {
  72. return (int)$value;
  73. }
  74. /**
  75. * 构建代理等级查询基础条件(显示中、未删除)
  76. * @param string $alias 表别名(联查时使用)
  77. * @param self|null $model 模型实例(支持链式调用)
  78. * @return self
  79. */
  80. public static function setWhere($alias = '', self $model = null)
  81. {
  82. $model = $model ?? new self();
  83. if ($alias) {
  84. $model->alias($alias);
  85. $alias .= '.';
  86. }
  87. // 筛选条件:显示状态(1=显示)、未删除(0=正常)
  88. return $model->where("{$alias}is_show", 1)->where("{$alias}is_del", 0);
  89. }
  90. /**
  91. * 获取指定代理等级的进货折扣
  92. * @param int $id 代理等级ID(0则取最低等级折扣)
  93. * @return int 折扣值(百分比)
  94. */
  95. public static function getAgentDiscount($id = 0)
  96. {
  97. $query = self::setWhere();
  98. $id ? $query->where('id', $id) : $query->order('grade', 'asc');
  99. return $query->value('discount') ?: 0;
  100. }
  101. /**
  102. * 获取指定代理等级的返利比例(按类型区分)
  103. * @param int $id 代理等级ID
  104. * @param string $type 返利类型(direct=直推、indirect=间接、umbrella=伞下)
  105. * @return float 返利比例(百分比)
  106. */
  107. public static function getAgentRebates($id = 0, $type = 'direct')
  108. {
  109. if (!$id) return 0.00;
  110. // PHP7.3 用 switch 替代 match 表达式,实现类型→字段的映射
  111. switch ($type) {
  112. case 'indirect':
  113. $field = 'indirect_rebates';
  114. break;
  115. case 'umbrella':
  116. $field = 'umbrella_rebates';
  117. break;
  118. default:
  119. // 默认返回直推返利(对应原 match 的 default 分支)
  120. $field = 'direct_rebates';
  121. break;
  122. }
  123. // 查询并返回对应返利比例
  124. return self::setWhere()->where('id', $id)->value($field) ?: 0.00;
  125. }
  126. /**
  127. * 获取用户当前代理等级信息(含等级配置+任务)
  128. * @param int $uid 用户ID
  129. * @param bool $isArray 是否返回[等级列表, 任务列表]
  130. * @return array|bool|array|null 等级信息
  131. * @throws DataNotFoundException|ModelNotFoundException|DbException
  132. */
  133. public static function getAgentLevelInfo($uid, $isArray = false)
  134. {
  135. $level = ['id' => 0, 'name' => '普通用户', 'grade' => 0]; // 默认无等级
  136. $task = [];
  137. // 获取用户当前有效代理等级记录
  138. $agentLevelId = UserAgentLevel::getUserAgentLevel($uid);
  139. if ($agentLevelId !== false) {
  140. $level = UserAgentLevel::getUserAgentLevelInfo($agentLevelId) ?: $level;
  141. }
  142. // 获取代理等级列表(含升级标记)
  143. $levelList = self::getAgentLevelListAndGrade($level['id'], $isArray);
  144. // 关联当前等级任务(若需要)
  145. // if ($isArray && isset($levelList[0])) {
  146. // $task = SystemAgentTask::getTaskList($levelList[0]['id'], $uid, $level);
  147. // }
  148. return $isArray ? [$levelList, $task] : ($level['id'] ? $level : false);
  149. }
  150. /**
  151. * 获取代理等级的排序值(grade)
  152. * @param int $id 代理等级ID
  153. * @return int 排序值(数值越大等级越高)
  154. */
  155. public static function getAgentLevelGrade($id)
  156. {
  157. return self::setWhere()->where('id', $id)->value('grade') ?: 0;
  158. }
  159. /**
  160. * 获取代理等级列表(含当前等级标记、可升级状态)
  161. * @param int $currentLevelId 当前等级ID
  162. * @param bool $isArray 是否需要任务列表字段
  163. * @param string $order 排序方式
  164. * @return array 等级列表
  165. * @throws DataNotFoundException|ModelNotFoundException|DbException
  166. */
  167. public static function getAgentLevelListAndGrade($currentLevelId, $isArray, $order = 'grade asc')
  168. {
  169. // 查询所有有效代理等级
  170. $list = self::setWhere()
  171. ->field('id, name, direct_rebates, indirect_rebates, umbrella_rebates, discount, icon, image, explain, grade, is_forever, valid_days')
  172. ->order($order)
  173. ->select()
  174. ->toArray() ?: [];
  175. $currentGrade = $currentLevelId ? self::getAgentLevelGrade($currentLevelId) : 0;
  176. // 处理列表字段:标记当前等级、可升级状态、格式化有效期
  177. foreach ($list as &$item) {
  178. $item['is_current'] = ($item['id'] == $currentLevelId);
  179. $item['is_clear'] = ($currentGrade < $item['grade']); // 是否可升级
  180. $item['valid_text'] = $item['is_forever'] ? '永久有效' : "{$item['valid_days']}天";
  181. // if ($isArray) $item['task_list'] = []; // 预留任务列表字段
  182. }
  183. // 若无当前等级,默认选中最低等级
  184. if (!$currentLevelId && $list) $list[0]['is_current'] = true;
  185. return $list;
  186. }
  187. /**
  188. * 判断目标等级是否可升级(基于当前等级)
  189. * @param int $targetLevelId 目标等级ID
  190. * @param int $currentLevelId 当前等级ID
  191. * @param array|null $levelList 已查询的等级列表(可选)
  192. * @return bool 是否可升级
  193. * @throws DataNotFoundException|ModelNotFoundException|DbException
  194. */
  195. public static function getAgentClear($targetLevelId, $currentLevelId, $levelList = null)
  196. {
  197. $levelList = $levelList ?? self::getAgentLevelListAndGrade($currentLevelId, false);
  198. foreach ($levelList as $item) {
  199. if ($item['id'] == $targetLevelId) return $item['is_clear'];
  200. }
  201. return false;
  202. }
  203. /**
  204. * 获取当前等级的下一个可升级等级ID
  205. * @param int $currentLevelId 当前等级ID
  206. * @param int $maxGrade 最高等级排序限制
  207. * @return int 下一级等级ID(0=无)
  208. * @throws DataNotFoundException|ModelNotFoundException|DbException
  209. */
  210. public static function getNextAgentLevelId($currentLevelId, $maxGrade)
  211. {
  212. $levelList = self::getAgentLevelListAndGrade($currentLevelId, false, 'grade desc');
  213. $currentGrade = self::getAgentLevelGrade($currentLevelId);
  214. $higherLevels = [];
  215. // 筛选:等级高于当前且不超过最高限制
  216. foreach ($levelList as $item) {
  217. if ($item['grade'] > $currentGrade && $item['grade'] <= $maxGrade) {
  218. $higherLevels[] = $item['id'];
  219. }
  220. }
  221. return $higherLevels ? end($higherLevels) : 0;
  222. }
  223. // /**
  224. // * 获取用户代理等级列表+任务列表(前端展示用)
  225. // * @param int $uid 用户ID
  226. // * @return array 格式:['list' => 等级列表, 'task' => 当前任务]
  227. // * @throws DataNotFoundException|ModelNotFoundException|DbException
  228. // */
  229. // public static function getAgentLevelList($uid)
  230. // {
  231. // list($levelList, $task) = self::getAgentLevelInfo($uid, true);
  232. // return compact('levelList', 'task');
  233. // }
  234. }