CateModel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\admin\model\exam;
  3. use addons\exam\model\BaseModel;
  4. use think\Config;
  5. use think\Db;
  6. use traits\model\SoftDelete;
  7. class CateModel extends BaseModel
  8. {
  9. use SoftDelete;
  10. // 表名
  11. protected $name = 'exam_cate';
  12. // 自动写入时间戳字段
  13. protected $autoWriteTimestamp = false;
  14. // 定义时间戳字段名
  15. protected $createTime = false;
  16. protected $updateTime = false;
  17. protected $deleteTime = 'deletetime';
  18. // 追加属性
  19. protected $append
  20. = [
  21. 'kind_text',
  22. 'level_text'
  23. ];
  24. protected static function init()
  25. {
  26. // self::beforeWrite(function ($row) {
  27. // if (!$row['parent_id']) {
  28. // $row['level'] = 1;
  29. // } else {
  30. // $parent = self::get($row['parent_id']);
  31. //
  32. // if ($parent['level'] == 1) {
  33. // $row['level'] = 2;
  34. // } else {
  35. // $row['level'] = 3;
  36. // }
  37. // }
  38. // }, true);
  39. }
  40. public function getKindList()
  41. {
  42. return [
  43. 'QUESTION' => __('Question'),
  44. 'ROOM' => __('Room'),
  45. // 'COURSE' => __('Course'),
  46. // 'PAPER' => __('Paper'),
  47. ];
  48. }
  49. public function getLevelList()
  50. {
  51. return ['1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3')];
  52. }
  53. public function getKindTextAttr($value, $data)
  54. {
  55. $value = $value ? $value : ($data['kind'] ?? '');
  56. $list = $this->getKindList();
  57. return $list[$value] ?? '';
  58. }
  59. public function getLevelTextAttr($value, $data)
  60. {
  61. $value = $value ? $value : ($data['level'] ?? '');
  62. $list = $this->getLevelList();
  63. return $list[$value] ?? '';
  64. }
  65. /**
  66. * 组装筛选三级分类格式
  67. * @param string $kind
  68. * @return array[]
  69. */
  70. public static function threeLevel(string $kind)
  71. {
  72. $data = self::all(function ($query) use ($kind) {
  73. $query->where('status', '1')->where('kind', $kind)->order('sort desc');
  74. });
  75. $result = [
  76. [
  77. 'name' => '全部',
  78. 'value' => 'all',
  79. 'submenu' => [
  80. 'name' => '全部',
  81. 'value' => 'all'
  82. ]
  83. ]
  84. ];
  85. $level1 = [];
  86. $level2 = [];
  87. $level3 = [];
  88. // 分级
  89. foreach ($data as $item) {
  90. switch ($item['level']) {
  91. case 1:
  92. array_push($level1, $item);
  93. break;
  94. case 2:
  95. array_push($level2, $item);
  96. break;
  97. case 3:
  98. array_push($level3, $item);
  99. break;
  100. }
  101. }
  102. // 先组装level2和level3的数据,即level1的子级
  103. $level1_sub_menu = [];
  104. foreach ($level2 as $item2) {
  105. $sub_menu = [];
  106. foreach ($level3 as $item3) {
  107. if ($item3['parent_id'] == $item2['id']) {
  108. $sub_menu[] = [
  109. 'name' => $item3['name'],
  110. 'value' => $item3['id'],
  111. ];
  112. }
  113. }
  114. $level1_sub_menu[] = [
  115. 'name' => $item2['name'],
  116. 'value' => $item2['id'],
  117. 'submenu' => $sub_menu,
  118. 'parent_id' => $item2['parent_id']
  119. ];
  120. }
  121. // 装载level1
  122. foreach ($level1 as $item) {
  123. $level1_menu = [
  124. 'name' => $item['name'],
  125. 'value' => $item['id'],
  126. ];
  127. foreach ($level1_sub_menu as $sub_menu) {
  128. if ($sub_menu['parent_id'] == $item['id']) {
  129. $level1_menu['submenu'][] = $sub_menu;
  130. }
  131. }
  132. $result[] = $level1_menu;
  133. }
  134. return $result;
  135. }
  136. /**
  137. * 获取子分类id
  138. * @param $cate_id
  139. * @return array
  140. */
  141. public static function getChildId($cate_id)
  142. {
  143. if (!$cate_id || is_nan($cate_id)) {
  144. return [];
  145. }
  146. $prefix = Config::get('database.prefix');
  147. $table_name = "{$prefix}exam_cate";
  148. $ids = Db::query("
  149. SELECT id FROM {$table_name} WHERE parent_id = $cate_id
  150. UNION
  151. SELECT id FROM {$table_name} WHERE parent_id IN (SELECT id FROM {$table_name} WHERE parent_id = $cate_id)
  152. ");
  153. return $ids ? array_column($ids, 'id') : [];
  154. }
  155. /**
  156. * 组装三级分类格式
  157. * @param string $kind
  158. * @return array[]
  159. */
  160. public static function threeLevel2(string $kind)
  161. {
  162. $data = self::all(function ($query) use ($kind) {
  163. $query->where('status', '1')->where('kind', $kind)->order('sort desc');
  164. });
  165. $result = [];
  166. $level1 = [];
  167. $level2 = [];
  168. $level3 = [];
  169. // 分级
  170. foreach ($data as $item) {
  171. switch ($item['level']) {
  172. case 1:
  173. array_push($level1, $item);
  174. break;
  175. case 2:
  176. array_push($level2, $item);
  177. break;
  178. case 3:
  179. array_push($level3, $item);
  180. break;
  181. }
  182. }
  183. // 先组装level2和level3的数据,即level1的子级
  184. $level1_sub_menu = [];
  185. foreach ($level2 as $item2) {
  186. $children = [];
  187. foreach ($level3 as $item3) {
  188. if ($item3['parent_id'] == $item2['id']) {
  189. $children[] = [
  190. 'text' => $item3['name'],
  191. 'value' => $item3['id'],
  192. ];
  193. }
  194. }
  195. $level1_sub_menu[] = [
  196. 'text' => $item2['name'],
  197. 'value' => $item2['id'],
  198. 'children' => $children,
  199. 'parent_id' => $item2['parent_id']
  200. ];
  201. }
  202. // 装载level1
  203. foreach ($level1 as $item) {
  204. $level1_menu = [
  205. 'text' => $item['name'],
  206. 'value' => $item['id'],
  207. ];
  208. foreach ($level1_sub_menu as $children) {
  209. if ($children['parent_id'] == $item['id']) {
  210. $level1_menu['children'][] = $children;
  211. }
  212. }
  213. $result[] = $level1_menu;
  214. }
  215. return $result;
  216. }
  217. /**
  218. * 获取顶级分类
  219. * @param $cate_id
  220. * @return self
  221. */
  222. public static function getTop($cate_id)
  223. {
  224. if (!$cate_id || !is_numeric($cate_id)) {
  225. return null;
  226. }
  227. $cate = self::get($cate_id);
  228. if (!$cate) {
  229. return null;
  230. }
  231. if (!$cate['parent_id']) {
  232. return $cate;
  233. }
  234. return self::getTop($cate['parent_id']);
  235. }
  236. }