ConfigClassify.php 5.7 KB

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