GroupData.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\GroupDataRepository;
  13. use app\common\repositories\system\groupData\GroupRepository;
  14. use app\validate\admin\GroupDataValidate;
  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 GroupData
  22. * @package app\controller\admin\system\groupData
  23. * @author xaboy
  24. * @day 2020-03-27
  25. */
  26. class GroupData extends BaseController
  27. {
  28. /**
  29. * @var GroupDataRepository
  30. */
  31. protected $repository;
  32. /**
  33. * GroupData constructor.
  34. * @param App $app
  35. * @param GroupDataRepository $repository
  36. */
  37. public function __construct(App $app, GroupDataRepository $repository)
  38. {
  39. parent::__construct($app);
  40. $this->repository = $repository;
  41. }
  42. /**
  43. * @param int $groupId
  44. * @return mixed
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. * @author xaboy
  49. * @day 2020-03-30
  50. */
  51. public function lst($groupId)
  52. {
  53. [$page, $limit] = $this->getPage();
  54. $lst = $this->repository->getGroupDataLst($this->request->merId(), $groupId, $page, $limit);
  55. return app('json')->success($lst);
  56. }
  57. /**
  58. * @param int $groupId
  59. * @return mixed
  60. * @throws FormBuilderException
  61. * @author xaboy
  62. * @day 2020-04-02
  63. */
  64. public function createTable($groupId)
  65. {
  66. if (!app()->make(GroupRepository::class)->exists($groupId))
  67. return app('json')->fail('组合数据不存在!');
  68. return app('json')->success(formToData($this->repository->form($groupId)));
  69. }
  70. /**
  71. * @param $id
  72. * @return mixed
  73. * @throws DbException
  74. * @author xaboy
  75. * @day 2020-05-13
  76. */
  77. public function changeStatus($id)
  78. {
  79. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  80. if (!$this->repository->merExists($this->request->merId(), $id))
  81. return app('json')->fail('数据不存在');
  82. $this->repository->update($id, compact('status'));
  83. return app('json')->success('修改成功');
  84. }
  85. /**
  86. * @param int $groupId
  87. * @param int $id
  88. * @return mixed
  89. * @throws DataNotFoundException
  90. * @throws DbException
  91. * @throws ModelNotFoundException
  92. * @throws FormBuilderException
  93. * @author xaboy
  94. * @day 2020-04-02
  95. */
  96. public function updateTable($groupId, $id)
  97. {
  98. if (!app()->make(GroupRepository::class)->exists($groupId))
  99. return app('json')->fail('组合数据不存在!');
  100. if (!$this->repository->merExists($this->request->merId(), $id))
  101. return app('json')->fail('数据不存在!');
  102. return app('json')->success(formToData($this->repository->updateForm($groupId, $this->request->merId(), $id)));
  103. }
  104. /**
  105. * @param int $groupId
  106. * @param GroupDataValidate $validate
  107. * @param GroupRepository $groupRepository
  108. * @return mixed
  109. * @author xaboy
  110. * @day 2020-04-02
  111. */
  112. public function create($groupId, GroupDataValidate $validate, GroupRepository $groupRepository)
  113. {
  114. $data = $this->request->params([['sort', 0], ['status', 0]]);
  115. $validate->check($data);
  116. if (!$groupRepository->exists($groupId))
  117. return app('json')->fail('数据组不存在');
  118. $data['value'] = $this->request->params($groupRepository->keys($groupId));
  119. $data['group_id'] = $groupId;
  120. $this->repository->create($this->request->merId(), $data);
  121. return app('json')->success('添加成功');
  122. }
  123. /**
  124. * @param int $groupId
  125. * @param int $id
  126. * @param GroupDataValidate $validate
  127. * @param GroupRepository $groupRepository
  128. * @return mixed
  129. * @throws DbException
  130. * @author xaboy
  131. * @day 2020-04-02
  132. */
  133. public function update($groupId,$id, GroupDataValidate $validate, GroupRepository $groupRepository)
  134. {
  135. $data = $this->request->params([['sort', 0], ['status', 0]]);
  136. $validate->check($data);
  137. if (!$groupRepository->exists($groupId))
  138. return app('json')->fail('数据组不存在');
  139. if (!$this->repository->merExists($this->request->merId(), $id))
  140. return app('json')->fail('数据不存在!');
  141. $data['value'] = $this->request->params($groupRepository->keys($groupId));
  142. $this->repository->merUpdate($this->request->merId(), $id, $data);
  143. return app('json')->success('修改成功');
  144. }
  145. /**
  146. * @param int $id
  147. * @return mixed
  148. * @throws DbException
  149. * @author xaboy
  150. * @day 2020-03-30
  151. */
  152. public function delete($id)
  153. {
  154. $this->repository->merDelete($this->request->merId(), $id);
  155. return app('json')->success('删除成功');
  156. }
  157. }