QuestionsCategpry.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\questions;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use service\UtilService as Util;
  15. use app\admin\model\questions\Questions as QuestionsModel;
  16. /**
  17. * 试题分类 Model
  18. * Class QuestionsCategpry
  19. * @package app\admin\model\questions
  20. */
  21. class QuestionsCategpry extends ModelBasic
  22. {
  23. use ModelTrait;
  24. /**
  25. * 全部试题分类
  26. */
  27. public static function taskCategoryAll($type = 0)
  28. {
  29. $model = self::where(['is_del' => 0, 'mer_id' => 0]);
  30. if ($type == 1) {
  31. $model = $model->where('pid', 0);
  32. }
  33. $list = $model->order('sort desc,add_time desc')->select();
  34. $list = count($list) > 0 ? $list->toArray() : [];
  35. $list = Util::sortListTier($list);
  36. return $list;
  37. }
  38. /**
  39. * 试题分类列表
  40. */
  41. public static function getAllList($where)
  42. {
  43. $data = self::setWhere($where)->column('id,pid');
  44. $list = [];
  45. foreach ($data as $ket => $item) {
  46. $cate = self::where('id', $ket)->find();
  47. if ($cate) {
  48. $cate = $cate->toArray();
  49. if ($item > 0) {
  50. $cate['sum'] = QuestionsModel::where(['is_del' => 0, 'mer_id' => 0])->where('pid', $ket)->count();
  51. } else {
  52. $pids = self::categoryId($ket);
  53. $cate['sum'] = QuestionsModel::where(['is_del' => 0, 'mer_id' => 0])->where('pid', 'in', $pids)->count();
  54. }
  55. array_push($list, $cate);
  56. unset($cate);
  57. }
  58. if ($item > 0 && !array_key_exists($item, $data)) {
  59. $cate = self::where('id', $item)->find();
  60. if ($cate) {
  61. $cate = $cate->toArray();
  62. $pids = self::categoryId($item);
  63. $cate['sum'] = QuestionsModel::where(['is_del' => 0, 'mer_id' => 0])->where('pid', 'in', $pids)->count();
  64. array_push($list, $cate);
  65. }
  66. }
  67. }
  68. return $list;
  69. }
  70. public static function setWhere($where)
  71. {
  72. $model = self::order('sort desc,add_time desc')->where(['is_del' => 0, 'mer_id' => 0]);
  73. if ($where['pid']) $model = $model->where('id', $where['pid']);
  74. if ($where['title'] != '') $model = $model->where('title', 'like', "%$where[title]%");
  75. return $model;
  76. }
  77. /**获取一个分类下的所有分类ID
  78. * @param int $pid
  79. */
  80. public static function categoryId($pid = 0)
  81. {
  82. $data = self::where(['is_del' => 0, 'mer_id' => 0])->where('pid', $pid)->column('id');
  83. array_push($data, $pid);
  84. return $data;
  85. }
  86. }