GroupData.php 5.7 KB

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