Group.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\controller\admin\system\groupData;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\system\groupData\GroupRepository;
  5. use app\validate\admin\GroupValidate;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use think\App;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. /**
  12. * Class Group
  13. * @package app\controller\admin\system\groupData
  14. * @author zfy
  15. * @day 2020-03-27
  16. */
  17. class Group extends BaseController
  18. {
  19. /**
  20. * @var GroupRepository
  21. */
  22. private $repository;
  23. /**
  24. * GroupData constructor.
  25. * @param App $app
  26. * @param GroupRepository $repository
  27. */
  28. public function __construct(App $app, GroupRepository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * @param int $id
  35. * @return mixed
  36. * @throws DataNotFoundException
  37. * @throws DbException
  38. * @throws ModelNotFoundException
  39. * @author zfy
  40. * @day 2020-03-27
  41. */
  42. public function get( $id)
  43. {
  44. $data = $this->repository->get($id)->hidden(['create_time', 'sort']);
  45. if (!$data)
  46. return app('json')->fail('数据组不存在');
  47. else
  48. return app('json')->success($data);
  49. }
  50. /**
  51. * @return mixed
  52. * @throws DataNotFoundException
  53. * @throws DbException
  54. * @throws ModelNotFoundException
  55. * @author zfy
  56. * @day 2020-04-01
  57. */
  58. public function lst()
  59. {
  60. [$page, $limit] = $this->getPage();
  61. $data = $this->repository->page($page, $limit);
  62. return app('json')->success($data);
  63. }
  64. /**
  65. * @param GroupValidate $validate
  66. * @return mixed
  67. * @author zfy
  68. * @day 2020-03-27
  69. */
  70. public function create(GroupValidate $validate)
  71. {
  72. $data = $this->request->params(['group_name', 'group_info', 'user_type', 'group_key', ['fields', []], ['sort', 0]]);
  73. $validate->check($data);
  74. if ($this->repository->keyExists($data['group_key']))
  75. return app('json')->fail('数据组key已存在');
  76. $this->repository->create($data);
  77. return app('json')->success('添加成功');
  78. }
  79. /**
  80. * @return mixed
  81. * @throws FormBuilderException
  82. * @author zfy
  83. * @day 2020-04-02
  84. */
  85. public function createTable()
  86. {
  87. return app('json')->success(formToData($this->repository->form()));
  88. }
  89. /**
  90. * @param int $id
  91. * @return mixed
  92. * @throws DataNotFoundException
  93. * @throws DbException
  94. * @throws ModelNotFoundException
  95. * @throws FormBuilderException
  96. * @author zfy
  97. * @day 2020-04-02
  98. */
  99. public function updateTable($id)
  100. {
  101. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  102. return app('json')->success(formToData($this->repository->updateForm($id)));
  103. }
  104. /**
  105. * @param int $id
  106. * @param GroupValidate $validate
  107. * @return mixed
  108. * @throws DbException
  109. * @author zfy
  110. * @day 2020-03-27
  111. */
  112. public function update($id, GroupValidate $validate)
  113. {
  114. $data = $this->request->params(['group_name', 'group_info', 'user_type', 'group_key', ['fields', []], ['sort', 0]]);
  115. $validate->check($data);
  116. if (!$this->repository->exists($id))
  117. return app('json')->fail('数据组不存在');
  118. if ($this->repository->keyExists($data['group_key'], $id))
  119. return app('json')->fail('数据组key已存在');
  120. $this->repository->update($id, $data);
  121. return app('json')->success('修改成功');
  122. }
  123. /**
  124. * @param int $id
  125. * @return mixed
  126. * @throws DbException
  127. * @author zfy
  128. * @day 2020-03-27
  129. */
  130. public function delete($id)
  131. {
  132. if (!$this->repository->exists($id))
  133. return app('json')->fail('数据组不存在');
  134. $this->repository->delete($id);
  135. return app('json')->success('删除成功');
  136. }
  137. }