AttachmentCategory.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 附件分类
  24. */
  25. class AttachmentCategory extends BaseController
  26. {
  27. /**
  28. * @var AttachmentCategoryRepository
  29. */
  30. protected $repository;
  31. /**
  32. * @var int
  33. */
  34. protected $merId;
  35. /**
  36. * AttachmentCategory constructor.
  37. * @param App $app
  38. * @param AttachmentCategoryRepository $repository
  39. */
  40. public function __construct(App $app, AttachmentCategoryRepository $repository)
  41. {
  42. parent::__construct($app);
  43. $this->repository = $repository;
  44. $this->merId = $this->request->merId();
  45. }
  46. /**
  47. * 创建表单
  48. * @return mixed
  49. * @throws FormBuilderException
  50. * @author xaboy
  51. * @day 2020-04-15
  52. */
  53. public function createForm()
  54. {
  55. return app('json')->success(formToData($this->repository->form($this->merId)));
  56. }
  57. /**
  58. * 编辑表单
  59. * @param $id
  60. * @return mixed
  61. * @throws DataNotFoundException
  62. * @throws DbException
  63. * @throws FormBuilderException
  64. * @throws ModelNotFoundException
  65. * @author xaboy
  66. * @day 2020-04-15
  67. */
  68. public function updateForm($id)
  69. {
  70. if (!$this->repository->merExists($this->merId, $id))
  71. return app('json')->fail('数据不存在');
  72. return app('json')->success(formToData($this->repository->updateForm($this->merId, $id)));
  73. }
  74. /**
  75. * 获取带级别列表
  76. * @return Json
  77. * @throws DataNotFoundException
  78. * @throws DbException
  79. * @throws ModelNotFoundException
  80. * @author 张先生
  81. * @date 2020-03-26
  82. */
  83. public function getFormatList()
  84. {
  85. $result = $this->repository->getFormatList($this->merId);
  86. return app('json')->success($result);
  87. }
  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. }