TestPaperCategory.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\controller\questions;
  12. use app\admin\controller\AuthController;
  13. use service\JsonService as Json;
  14. use app\admin\model\questions\TestPaperCategory as TestPaperCategoryModel;
  15. use app\admin\model\questions\TestPaper;
  16. /**
  17. * 试卷分类
  18. * Class TestPaperCategory
  19. */
  20. class TestPaperCategory extends AuthController
  21. {
  22. public function index($type = 1)
  23. {
  24. $this->assign(['type' => $type]);
  25. return $this->fetch();
  26. }
  27. public function get_category_list($type = 1)
  28. {
  29. $where = parent::getMore([
  30. ['page', 1],
  31. ['limit', 20],
  32. ['tid', 0],
  33. ['title', ''],
  34. ]);
  35. return Json::successful(TestPaperCategoryModel::getAllList($where, $type));
  36. }
  37. /**
  38. * 创建分类
  39. * @param int $id
  40. * @return mixed
  41. * @throws \think\exception\DbException
  42. */
  43. public function create($id = 0, $type = 1)
  44. {
  45. $cate = $id > 0 ? TestPaperCategoryModel::get($id) : [];
  46. $this->assign(['cate' => json_encode($cate), 'id' => $id, 'type' => $type]);
  47. return $this->fetch();
  48. }
  49. public function get_cate_list($type = 1)
  50. {
  51. $category = TestPaperCategoryModel::taskCategoryAll(2, $type);
  52. return Json::successful($category);
  53. }
  54. /**一级分类
  55. * @param int $type
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. */
  60. public function add_cate_list($type = 1)
  61. {
  62. $category = TestPaperCategoryModel::where(['pid' => 0, 'is_del' => 0, 'type' => $type])->select();
  63. $category = count($category) > 0 ? $category->toArray() : [];
  64. $array = [];
  65. $oneCate['id'] = 0;
  66. $oneCate['title'] = '顶级分类';
  67. array_push($array, $oneCate);
  68. foreach ($category as $key => $value) {
  69. array_push($array, $value);
  70. }
  71. return Json::successful($array);
  72. }
  73. /**
  74. * 快速编辑
  75. * @return json
  76. */
  77. public function set_value($field = '', $id = '', $value = '')
  78. {
  79. ($field == '' || $id == '' || $value == '') && Json::fail('缺少参数');
  80. $res = parent::getDataModification('test_paper', $id, $field, $value);
  81. if ($res)
  82. return Json::successful('保存成功');
  83. else
  84. return Json::fail('保存失败');
  85. }
  86. /**
  87. * 新增或者修改
  88. * @return json
  89. */
  90. public function save($id = 0, $type = 1)
  91. {
  92. $post = parent::postMore([
  93. ['title', ''],
  94. ['pid', ''],
  95. ['sort', 0],
  96. ]);
  97. if (!$post['title']) return Json::fail('请输入分类名称');
  98. if ($id) {
  99. $cate = TestPaperCategoryModel::get($id);
  100. if (!$cate['pid'] && $post['pid'] && TestPaperCategoryModel::be(['pid' => $id, 'type' => $type, 'is_del' => 0])) return Json::fail('无法移动有下级的分类');
  101. if (TestPaperCategoryModel::where(['title' => $post['title'], 'is_del' => 0])->where('id', '<>', $id)->count() >= 1) return Json::fail('分类名称已存在');
  102. $res = TestPaperCategoryModel::edit($post, $id);
  103. if ($res)
  104. return Json::successful('修改成功');
  105. else
  106. return Json::fail('修改失败');
  107. } else {
  108. $post['add_time'] = time();
  109. $post['type'] = $type;
  110. $res = TestPaperCategoryModel::set($post);
  111. if ($res)
  112. return Json::successful('添加成功');
  113. else
  114. return Json::fail('添加失败');
  115. }
  116. }
  117. /**
  118. * 删除
  119. * @return json
  120. */
  121. public function delete($id = 0)
  122. {
  123. if (!$id) return Json::fail('缺少参数');
  124. $cate = TestPaperCategoryModel::get($id);
  125. if (!$cate['pid']) {
  126. $count = TestPaperCategoryModel::where('pid', $id)->where('is_del', 0)->count();
  127. if ($count) return Json::fail('暂无法删除,请删除下级分类');
  128. }
  129. if (TestPaper::where('tid', $id)->where('is_del', 0)->count()) return Json::fail('暂无法删除,请先删除试卷');
  130. $res = parent::getDataModification('test_paper', $id, 'is_del', 1);
  131. if ($res)
  132. return Json::successful('删除成功');
  133. else
  134. return Json::fail('删除失败');
  135. }
  136. }