QuestionsCategpry.php 4.8 KB

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