SystemUserAgentLevel.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * 代理等级设置模型(对应表:system_user_agent_level)
  4. * 作用:管理代理等级的基础配置(名称、返利比例、有效期等)
  5. * @author: yourname
  6. * @day: 2025/10
  7. */
  8. namespace app\admin\model\system;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. class SystemUserAgentLevel extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型关联的数据表名
  20. * @var string
  21. */
  22. protected $name = 'system_user_agent_level';
  23. use ModelTrait;
  24. /**
  25. * add_time 字段修改器:新增时自动填充当前时间戳
  26. * @return int
  27. */
  28. public static function setAddTimeAttr()
  29. {
  30. return time();
  31. }
  32. /**
  33. * add_time 字段获取器:时间戳转格式化时间
  34. * @param int $value 时间戳
  35. * @return string 格式化时间(Y-m-d H:i:s)
  36. */
  37. public static function getAddTimeAttr($value)
  38. {
  39. return $value ? date('Y-m-d H:i:s', $value) : '';
  40. }
  41. /**
  42. * 构建代理等级查询条件(过滤删除、支持显示状态/名称搜索)
  43. * @param array $where 查询条件(如is_show、name等)
  44. * @param string $alias 表别名(联查时用)
  45. * @param SystemAgentLevel|null $model 模型实例(支持链式调用)
  46. * @return SystemAgentLevel
  47. */
  48. public static function setWhere($where, $alias = '', $model = null)
  49. {
  50. $model = $model === null ? new self() : $model;
  51. // 处理表别名
  52. if ($alias) {
  53. $model = $model->alias($alias);
  54. $alias .= '.';
  55. }
  56. // 基础条件:未删除
  57. $model = $model->where("{$alias}is_del", 0);
  58. // 筛选:显示状态(1=显示,0=隐藏)
  59. if (isset($where['is_show']) && $where['is_show'] !== '') {
  60. $model = $model->where("{$alias}is_show", $where['is_show']);
  61. }
  62. // 搜索:代理等级名称(模糊匹配)
  63. if (isset($where['name']) && $where['name']) {
  64. $model = $model->where("{$alias}name", 'LIKE', "%{$where['name']}%");
  65. }
  66. return $model;
  67. }
  68. /**
  69. * 获取代理等级列表(带分页、排序)
  70. * @param array $where 分页及筛选条件(page、limit、is_show、name等)
  71. * @return array 格式:['data' => 列表数据, 'count' => 总数]
  72. */
  73. public static function getSystemAgentList($where)
  74. {
  75. // 分页查询:按等级排序(grade越大等级越高)
  76. $data = self::setWhere($where)
  77. ->order('grade desc')
  78. ->page((int)$where['page'], (int)$where['limit'])
  79. ->select();
  80. $data = $data ? $data->toArray() : [];
  81. // 补充格式化字段(如有效期文本)
  82. foreach ($data as &$item) {
  83. $item['is_forever_text'] = $item['is_forever'] ? '永久有效' : '限时有效';
  84. $item['valid_days_text'] = $item['is_forever'] ? '—' : "{$item['valid_days']}天";
  85. }
  86. // 统计符合条件的总数
  87. $count = self::setWhere($where)->count();
  88. return compact('data', 'count');
  89. }
  90. /**
  91. * 根据等级ID获取单个代理等级配置详情
  92. * @param int $id 代理等级ID
  93. * @return array|null 等级详情(含格式化字段)
  94. */
  95. public static function getAgentLevelConfig($id)
  96. {
  97. $level = self::setWhere([])->where('id', $id)->find();
  98. if (!$level) return null;
  99. $level = $level->toArray();
  100. // 格式化显示字段
  101. $level['is_forever_text'] = $level['is_forever'] ? '永久有效' : '限时有效';
  102. $level['valid_days_text'] = $level['is_forever'] ? '—' : "{$level['valid_days']}天";
  103. $level['rebates_text'] = "直推:{$level['direct_rebates']}% | 间接:{$level['indirect_rebates']}% | 伞下:{$level['umbrella_rebates']}%";
  104. return $level;
  105. }
  106. }