AttachmentCategory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace app\controller\admin\system\attachment;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\system\attachment\AttachmentCategoryRepository;
  5. use app\validate\admin\AttachmentCategoryValidate;
  6. use Exception;
  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. use think\response\Json;
  13. /**
  14. * Class AttachmentCategory
  15. * @package app\controller\admin\system\attachment
  16. * @author zfy
  17. * @day 2020-04-22
  18. */
  19. class AttachmentCategory extends BaseController
  20. {
  21. /**
  22. * @var AttachmentCategoryRepository
  23. */
  24. protected $repository;
  25. /**
  26. * @var int
  27. */
  28. protected $merId;
  29. /**
  30. * AttachmentCategory constructor.
  31. * @param App $app
  32. * @param AttachmentCategoryRepository $repository
  33. */
  34. public function __construct(App $app, AttachmentCategoryRepository $repository)
  35. {
  36. parent::__construct($app);
  37. $this->repository = $repository;
  38. $this->merId = $this->request->merId();
  39. }
  40. /**
  41. * @return mixed
  42. * @throws FormBuilderException
  43. * @author zfy
  44. * @day 2020-04-15
  45. */
  46. public function createForm()
  47. {
  48. return app('json')->success(formToData($this->repository->form($this->merId)));
  49. }
  50. /**
  51. * @param $id
  52. * @return mixed
  53. * @throws DataNotFoundException
  54. * @throws DbException
  55. * @throws FormBuilderException
  56. * @throws ModelNotFoundException
  57. * @author zfy
  58. * @day 2020-04-15
  59. */
  60. public function updateForm($id)
  61. {
  62. if (!$this->repository->merExists($this->merId, $id))
  63. return app('json')->fail('数据不存在');
  64. return app('json')->success(formToData($this->repository->updateForm($this->merId, $id)));
  65. }
  66. /**
  67. * 获取带级别列表
  68. * @return Json
  69. * @throws DataNotFoundException
  70. * @throws DbException
  71. * @throws ModelNotFoundException
  72. * @author 张先生
  73. * @date 2020-03-26
  74. */
  75. public function getFormatList()
  76. {
  77. $result = $this->repository->getFormatList($this->merId);
  78. return app('json')->success($result);
  79. }
  80. /**
  81. * @param AttachmentCategoryValidate $validate
  82. * @return mixed
  83. * @author zfy
  84. * @day 2020-04-15
  85. */
  86. public function create(AttachmentCategoryValidate $validate)
  87. {
  88. $data = $this->checkParam($validate);
  89. if ($data['pid'] && !$this->repository->merExists($this->merId, $data['pid']))
  90. return app('json')->fail('上级分类不存在');
  91. $data['mer_id'] = $this->merId;
  92. $this->repository->create($data);
  93. return app('json')->success('添加成功');
  94. }
  95. /**
  96. * 更新
  97. * @param int $id id
  98. * @param AttachmentCategoryValidate $validate
  99. * @return mixed
  100. * @throws DbException
  101. * @author 张先生
  102. * @date 2020-03-30
  103. */
  104. public function update($id, AttachmentCategoryValidate $validate)
  105. {
  106. $data = $this->checkParam($validate);
  107. if ($data['pid'] && !$this->repository->merExists($this->merId, $data['pid']))
  108. return app('json')->fail('上级分类不存在');
  109. if (!$this->repository->merExists($this->merId, $id))
  110. return app('json')->fail('数据不存在');
  111. $this->repository->update($id, $this->merId, $data);
  112. return app('json')->success('编辑成功');
  113. }
  114. /**
  115. * 添加和修改参数验证
  116. * @param AttachmentCategoryValidate $validate 验证规则
  117. * @return mixed
  118. * @author 张先生
  119. * @date 2020-03-30
  120. */
  121. private function checkParam(AttachmentCategoryValidate $validate)
  122. {
  123. $data = $this->request->params(['pid', 'attachment_category_name', 'attachment_category_enname', 'sort']);
  124. $validate->check($data);
  125. return $data;
  126. }
  127. /**
  128. * 删除单个
  129. * @param int $id
  130. * @return Json
  131. * @throws Exception
  132. * @author 张先生
  133. * @date 2020-03-30
  134. */
  135. public function delete($id)
  136. {
  137. if ($this->repository->merFieldExists($this->merId, 'pid', $id))
  138. return app('json')->fail('存在子级,无法删除');
  139. $this->repository->delete($id, $this->merId);
  140. return app('json')->success();
  141. }
  142. }