AttachmentCategory.php 5.0 KB

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