QuestionsCategpry.php 4.7 KB

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