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