123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?php
- namespace app\admin\model\exam;
- use addons\exam\model\BaseModel;
- use think\Config;
- use think\Db;
- use traits\model\SoftDelete;
- class CateModel extends BaseModel
- {
- use SoftDelete;
- // 表名
- protected $name = 'exam_cate';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = false;
- // 定义时间戳字段名
- protected $createTime = false;
- protected $updateTime = false;
- protected $deleteTime = 'deletetime';
- // 追加属性
- protected $append
- = [
- 'kind_text',
- 'level_text'
- ];
- protected static function init()
- {
- // self::beforeWrite(function ($row) {
- // if (!$row['parent_id']) {
- // $row['level'] = 1;
- // } else {
- // $parent = self::get($row['parent_id']);
- //
- // if ($parent['level'] == 1) {
- // $row['level'] = 2;
- // } else {
- // $row['level'] = 3;
- // }
- // }
- // }, true);
- }
- public function getKindList()
- {
- return [
- 'QUESTION' => __('Question'),
- 'ROOM' => __('Room'),
- // 'COURSE' => __('Course'),
- // 'PAPER' => __('Paper'),
- ];
- }
- public function getLevelList()
- {
- return ['1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3')];
- }
- public function getKindTextAttr($value, $data)
- {
- $value = $value ? $value : ($data['kind'] ?? '');
- $list = $this->getKindList();
- return $list[$value] ?? '';
- }
- public function getLevelTextAttr($value, $data)
- {
- $value = $value ? $value : ($data['level'] ?? '');
- $list = $this->getLevelList();
- return $list[$value] ?? '';
- }
- /**
- * 组装筛选三级分类格式
- * @param string $kind
- * @return array[]
- */
- public static function threeLevel(string $kind)
- {
- $data = self::all(function ($query) use ($kind) {
- $query->where('status', '1')->where('kind', $kind)->order('sort desc');
- });
- $result = [
- [
- 'name' => '全部',
- 'value' => 'all',
- 'submenu' => [
- 'name' => '全部',
- 'value' => 'all'
- ]
- ]
- ];
- $level1 = [];
- $level2 = [];
- $level3 = [];
- // 分级
- foreach ($data as $item) {
- switch ($item['level']) {
- case 1:
- array_push($level1, $item);
- break;
- case 2:
- array_push($level2, $item);
- break;
- case 3:
- array_push($level3, $item);
- break;
- }
- }
- // 先组装level2和level3的数据,即level1的子级
- $level1_sub_menu = [];
- foreach ($level2 as $item2) {
- $sub_menu = [];
- foreach ($level3 as $item3) {
- if ($item3['parent_id'] == $item2['id']) {
- $sub_menu[] = [
- 'name' => $item3['name'],
- 'value' => $item3['id'],
- ];
- }
- }
- $level1_sub_menu[] = [
- 'name' => $item2['name'],
- 'value' => $item2['id'],
- 'submenu' => $sub_menu,
- 'parent_id' => $item2['parent_id']
- ];
- }
- // 装载level1
- foreach ($level1 as $item) {
- $level1_menu = [
- 'name' => $item['name'],
- 'value' => $item['id'],
- ];
- foreach ($level1_sub_menu as $sub_menu) {
- if ($sub_menu['parent_id'] == $item['id']) {
- $level1_menu['submenu'][] = $sub_menu;
- }
- }
- $result[] = $level1_menu;
- }
- return $result;
- }
- /**
- * 获取子分类id
- * @param $cate_id
- * @return array
- */
- public static function getChildId($cate_id)
- {
- if (!$cate_id || is_nan($cate_id)) {
- return [];
- }
- $prefix = Config::get('database.prefix');
- $table_name = "{$prefix}exam_cate";
- $ids = Db::query("
- SELECT id FROM {$table_name} WHERE parent_id = $cate_id
- UNION
- SELECT id FROM {$table_name} WHERE parent_id IN (SELECT id FROM {$table_name} WHERE parent_id = $cate_id)
- ");
- return $ids ? array_column($ids, 'id') : [];
- }
- /**
- * 组装三级分类格式
- * @param string $kind
- * @return array[]
- */
- public static function threeLevel2(string $kind)
- {
- $data = self::all(function ($query) use ($kind) {
- $query->where('status', '1')->where('kind', $kind)->order('sort desc');
- });
- $result = [];
- $level1 = [];
- $level2 = [];
- $level3 = [];
- // 分级
- foreach ($data as $item) {
- switch ($item['level']) {
- case 1:
- array_push($level1, $item);
- break;
- case 2:
- array_push($level2, $item);
- break;
- case 3:
- array_push($level3, $item);
- break;
- }
- }
- // 先组装level2和level3的数据,即level1的子级
- $level1_sub_menu = [];
- foreach ($level2 as $item2) {
- $children = [];
- foreach ($level3 as $item3) {
- if ($item3['parent_id'] == $item2['id']) {
- $children[] = [
- 'text' => $item3['name'],
- 'value' => $item3['id'],
- ];
- }
- }
- $level1_sub_menu[] = [
- 'text' => $item2['name'],
- 'value' => $item2['id'],
- 'children' => $children,
- 'parent_id' => $item2['parent_id']
- ];
- }
- // 装载level1
- foreach ($level1 as $item) {
- $level1_menu = [
- 'text' => $item['name'],
- 'value' => $item['id'],
- ];
- foreach ($level1_sub_menu as $children) {
- if ($children['parent_id'] == $item['id']) {
- $level1_menu['children'][] = $children;
- }
- }
- $result[] = $level1_menu;
- }
- return $result;
- }
- /**
- * 获取顶级分类
- * @param $cate_id
- * @return self
- */
- public static function getTop($cate_id)
- {
- if (!$cate_id || !is_numeric($cate_id)) {
- return null;
- }
- $cate = self::get($cate_id);
- if (!$cate) {
- return null;
- }
- if (!$cate['parent_id']) {
- return $cate;
- }
- return self::getTop($cate['parent_id']);
- }
- }
|