Group.php 4.1 KB

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