GroupData.php 5.0 KB

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