Group.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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\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. * 组合数据
  22. */
  23. class Group extends BaseController
  24. {
  25. /**
  26. * @var GroupRepository
  27. */
  28. private $repository;
  29. /**
  30. * GroupData constructor.
  31. * @param App $app
  32. * @param GroupRepository $repository
  33. */
  34. public function __construct(App $app, GroupRepository $repository)
  35. {
  36. parent::__construct($app);
  37. $this->repository = $repository;
  38. }
  39. /**
  40. * 获取详情
  41. * @param int $id
  42. * @return mixed
  43. * @author xaboy
  44. */
  45. public function get($id)
  46. {
  47. $data = $this->repository->get($id)->hidden(['create_time', 'sort']);
  48. if (!$data) {
  49. return app('json')->fail('数据组不存在');
  50. } else {
  51. $data->fields = $this->repository->handleFields($data->fields);
  52. return app('json')->success($data);
  53. }
  54. }
  55. /**
  56. * 列表
  57. * @return mixed
  58. * @author xaboy
  59. * @day 2020-04-01
  60. */
  61. public function lst()
  62. {
  63. [$page, $limit] = $this->getPage();
  64. $data = $this->repository->page($page, $limit);
  65. return app('json')->success($data);
  66. }
  67. /**
  68. * 创建组合数据
  69. * @param GroupValidate $validate
  70. * @return mixed
  71. * @author xaboy
  72. * @day 2020-03-27
  73. */
  74. public function create(GroupValidate $validate)
  75. {
  76. $data = $this->request->params(['group_name', 'group_info', 'user_type', 'group_key', ['fields', []], ['sort', 0]]);
  77. $validate->check($data);
  78. if ($this->repository->keyExists($data['group_key']))
  79. return app('json')->fail('数据组key已存在');
  80. $this->repository->create($data);
  81. return app('json')->success('添加成功');
  82. }
  83. /**
  84. * 创建组合数据 数据库
  85. * @return mixed
  86. * @throws FormBuilderException
  87. * @author xaboy
  88. * @day 2020-04-02
  89. */
  90. public function createTable()
  91. {
  92. return app('json')->success(formToData($this->repository->form()));
  93. }
  94. /**
  95. * 更新组合数据 数据库
  96. * @param int $id
  97. * @return mixed
  98. * @throws DataNotFoundException
  99. * @throws DbException
  100. * @throws ModelNotFoundException
  101. * @throws FormBuilderException
  102. * @author xaboy
  103. * @day 2020-04-02
  104. */
  105. public function updateTable($id)
  106. {
  107. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  108. return app('json')->success(formToData($this->repository->updateForm($id)));
  109. }
  110. /**
  111. * 更新组合数据
  112. * @param int $id
  113. * @param GroupValidate $validate
  114. * @return mixed
  115. * @throws DbException
  116. * @author xaboy
  117. * @day 2020-03-27
  118. */
  119. public function update($id, GroupValidate $validate)
  120. {
  121. $data = $this->request->params(['group_name', 'group_info', 'user_type', 'group_key', ['fields', []], ['sort', 0]]);
  122. $validate->check($data);
  123. if (!$this->repository->exists($id))
  124. return app('json')->fail('数据组不存在');
  125. if ($this->repository->keyExists($data['group_key'], $id))
  126. return app('json')->fail('数据组key已存在');
  127. $this->repository->update($id, $data);
  128. return app('json')->success('修改成功');
  129. }
  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. }