Group.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-27
  7. *
  8. *
  9. */
  10. namespace app\controller\merchant\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. use think\facade\Db;
  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('数据组不存在1');
  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. // $where['mer_id']=$this->request->merId();
  71. $where['user_type']=1;
  72. $list = Db::name('system_group')->where($where)->page($page, $limit)->hidden(['fields', 'sort'])->order('sort desc,group_id desc')->select();
  73. $count = Db::name('system_group')->where($where)->count();
  74. $data= compact('count', 'list');
  75. return app('json')->success($data);
  76. }
  77. /**
  78. * @param GroupValidate $validate
  79. * @return mixed
  80. * @author xaboy
  81. * @day 2020-03-27
  82. */
  83. public function create(GroupValidate $validate)
  84. {
  85. $data = $this->request->params(['group_name', 'group_info', 'user_type', 'group_key', ['fields', []], ['sort', 0]]);
  86. $validate->check($data);
  87. if ($this->repository->keyExists($data['group_key']))
  88. return app('json')->fail('数据组key已存在');
  89. $this->repository->create($data);
  90. return app('json')->success('添加成功');
  91. }
  92. /**
  93. * @return mixed
  94. * @throws FormBuilderException
  95. * @author xaboy
  96. * @day 2020-04-02
  97. */
  98. public function createTable()
  99. {
  100. return app('json')->success(formToData($this->repository->form()));
  101. }
  102. /**
  103. * @param int $id
  104. * @return mixed
  105. * @throws DataNotFoundException
  106. * @throws DbException
  107. * @throws ModelNotFoundException
  108. * @throws FormBuilderException
  109. * @author xaboy
  110. * @day 2020-04-02
  111. */
  112. public function updateTable($id)
  113. {
  114. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  115. return app('json')->success(formToData($this->repository->updateForm($id)));
  116. }
  117. /**
  118. * @param int $id
  119. * @param GroupValidate $validate
  120. * @return mixed
  121. * @throws DbException
  122. * @author xaboy
  123. * @day 2020-03-27
  124. */
  125. public function update($id, GroupValidate $validate)
  126. {
  127. $data = $this->request->params(['group_name', 'group_info', 'user_type', 'group_key', ['fields', []], ['sort', 0]]);
  128. $validate->check($data);
  129. if (!$this->repository->exists($id))
  130. return app('json')->fail('数据组不存在');
  131. if ($this->repository->keyExists($data['group_key'], $id))
  132. return app('json')->fail('数据组key已存在');
  133. $this->repository->update($id, $data);
  134. return app('json')->success('修改成功');
  135. }
  136. /**
  137. * @param int $id
  138. * @return mixed
  139. * @throws DbException
  140. * @author xaboy
  141. * @day 2020-03-27
  142. */
  143. public function delete($id)
  144. {
  145. if (!$this->repository->exists($id))
  146. return app('json')->fail('数据组不存在');
  147. $this->repository->delete($id);
  148. return app('json')->success('删除成功');
  149. }
  150. }