ConfigClassify.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\controller\admin\system\config;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\system\config\ConfigClassifyRepository;
  5. use app\common\repositories\system\config\ConfigRepository;
  6. use app\validate\admin\ConfigClassifyValidate;
  7. use FormBuilder\Exception\FormBuilderException;
  8. use think\App;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. /**
  13. * Class ConfigClassify
  14. * @package app\controller\admin\system\config
  15. * @author zfy
  16. * @day 2020-03-27
  17. */
  18. class ConfigClassify extends BaseController
  19. {
  20. /**
  21. * @var ConfigClassifyRepository
  22. */
  23. private $repository;
  24. /**
  25. * ConfigClassify constructor.
  26. * @param App $app
  27. * @param ConfigClassifyRepository $repository
  28. */
  29. public function __construct(App $app, ConfigClassifyRepository $repository)
  30. {
  31. parent::__construct($app);
  32. $this->repository = $repository;
  33. }
  34. /**
  35. * @return mixed
  36. * @throws DataNotFoundException
  37. * @throws DbException
  38. * @throws ModelNotFoundException
  39. * @author zfy
  40. * @day 2020-03-31
  41. */
  42. public function lst()
  43. {
  44. $lst = $this->repository->lst();
  45. $lst['list'] = formatCategory($lst['list']->toArray(), 'config_classify_id');
  46. return app('json')->success($lst);
  47. }
  48. /**
  49. * @param int $pid
  50. * @return mixed
  51. * @throws DataNotFoundException
  52. * @throws DbException
  53. * @throws ModelNotFoundException
  54. * @author zfy
  55. * @day 2020-03-27
  56. */
  57. public function children($pid)
  58. {
  59. return app('json')->success($this->repository->children($pid));
  60. }
  61. /**
  62. * @return mixed
  63. * @throws FormBuilderException
  64. * @author zfy
  65. * @day 2020-03-31
  66. */
  67. public function createTable()
  68. {
  69. $form = $this->repository->createForm();
  70. return app('json')->success(formToData($form));
  71. }
  72. /**
  73. * @param int $id
  74. * @return mixed
  75. * @throws DataNotFoundException
  76. * @throws DbException
  77. * @throws ModelNotFoundException
  78. * @throws FormBuilderException
  79. * @author zfy
  80. * @day 2020-03-31
  81. */
  82. public function updateTable($id)
  83. {
  84. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  85. $form = $this->repository->updateForm($id);
  86. return app('json')->success(formToData($form));
  87. }
  88. /**
  89. * @return mixed
  90. * @author zfy
  91. * @day 2020-03-27
  92. */
  93. public function options()
  94. {
  95. return app('json')->success($this->repository->options());
  96. }
  97. /**
  98. * @param int $id
  99. * @return mixed
  100. * @throws DataNotFoundException
  101. * @throws DbException
  102. * @throws ModelNotFoundException
  103. * @author zfy
  104. * @day 2020-03-27
  105. */
  106. public function get($id)
  107. {
  108. $data = $this->repository->get($id);
  109. if (!$data)
  110. return app('json')->fail('分类不存在');
  111. else
  112. return app('json')->success($data);
  113. }
  114. /**
  115. * @param ConfigClassifyValidate $validate
  116. * @return mixed
  117. * @author zfy
  118. * @day 2020-03-27
  119. */
  120. public function create(ConfigClassifyValidate $validate)
  121. {
  122. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  123. $validate->check($data);
  124. if ($this->repository->keyExists($data['classify_key']))
  125. return app('json')->fail('配置分类key已存在');
  126. if ($data['pid'] && !$this->repository->pidExists($data['pid']))
  127. return app('json')->fail('上级分类不存在');
  128. $this->repository->create($data);
  129. return app('json')->success('添加成功');
  130. }
  131. /**
  132. * @param int $id
  133. * @param ConfigClassifyValidate $validate
  134. * @return mixed
  135. * @throws DbException
  136. * @author zfy
  137. * @day 2020-03-27
  138. */
  139. public function update($id, ConfigClassifyValidate $validate)
  140. {
  141. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  142. $validate->check($data);
  143. if (!$this->repository->exists($id))
  144. return app('json')->fail('分类不存在');
  145. if ($this->repository->keyExists($data['classify_key'], $id))
  146. return app('json')->fail('配置分类key已存在');
  147. if ($data['pid'] && !$this->repository->pidExists($data['pid'], $id))
  148. return app('json')->fail('上级分类不存在');
  149. $this->repository->update($id, $data);
  150. return app('json')->success('修改成功');
  151. }
  152. /**
  153. * @param int $id
  154. * @return mixed
  155. * @throws DbException
  156. * @author zfy
  157. * @day 2020-03-31
  158. */
  159. public function switchStatus($id)
  160. {
  161. $status = $this->request->param('status', 0);
  162. if (!$this->repository->exists($id))
  163. return app('json')->fail('分类不存在');
  164. $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
  165. return app('json')->success('修改成功');
  166. }
  167. /**
  168. * @param int $id
  169. * @param ConfigRepository $configRepository
  170. * @return mixed
  171. * @throws DbException
  172. * @author zfy
  173. * @day 2020-03-27
  174. */
  175. public function delete($id, ConfigRepository $configRepository)
  176. {
  177. if ($this->repository->existsChild($id))
  178. return app('json')->fail('存在子级,无法删除');
  179. if ($configRepository->classifyIdExists($id))
  180. return app('json')->fail('分类下存在配置,无法删除');
  181. $this->repository->delete($id);
  182. return app('json')->success('删除成功');
  183. }
  184. }