GroupData.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\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. * 组合数据 数据库
  23. */
  24. class GroupData extends BaseController
  25. {
  26. /**
  27. * @var GroupDataRepository
  28. */
  29. protected $repository;
  30. /**
  31. * GroupData constructor.
  32. * @param App $app
  33. * @param GroupDataRepository $repository
  34. */
  35. public function __construct(App $app, GroupDataRepository $repository)
  36. {
  37. parent::__construct($app);
  38. $this->repository = $repository;
  39. }
  40. /**
  41. * 列表
  42. * @param int $groupId
  43. * @return mixed
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. * @author xaboy
  48. * @day 2020-03-30
  49. */
  50. public function lst($groupId)
  51. {
  52. [$page, $limit] = $this->getPage();
  53. $lst = $this->repository->getGroupDataLst($this->request->merId(), intval($groupId), $page, $limit);
  54. return app('json')->success($lst);
  55. }
  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, null, $this->request->merId())));
  69. }
  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. * 修改数据表单
  88. * @param int $groupId
  89. * @param int $id
  90. * @return mixed
  91. * @throws DataNotFoundException
  92. * @throws DbException
  93. * @throws ModelNotFoundException
  94. * @throws FormBuilderException
  95. * @author xaboy
  96. * @day 2020-04-02
  97. */
  98. public function updateTable($groupId, $id)
  99. {
  100. if (!app()->make(GroupRepository::class)->exists($groupId))
  101. return app('json')->fail('组合数据不存在!');
  102. if (!$this->repository->merExists($this->request->merId(), $id))
  103. return app('json')->fail('数据不存在!');
  104. return app('json')->success(formToData($this->repository->updateForm($groupId, $this->request->merId(), $id)));
  105. }
  106. /**
  107. * 创建数据
  108. * @param int $groupId
  109. * @param GroupDataValidate $validate
  110. * @param GroupRepository $groupRepository
  111. * @return mixed
  112. * @author xaboy
  113. * @day 2020-04-02
  114. */
  115. public function create($groupId, GroupDataValidate $validate, GroupRepository $groupRepository)
  116. {
  117. $data = $this->request->params([['sort', 0], ['status', 0]]);
  118. $validate->check($data);
  119. if (!$groupRepository->exists($groupId))
  120. return app('json')->fail('数据组不存在');
  121. $fieldRule = $groupRepository->fields($groupId);
  122. $data['value'] = $this->repository->handleDataValue($fieldRule, $this->request->params(array_column($fieldRule, 'field')));
  123. $data['group_id'] = $groupId;
  124. $this->repository->create($this->request->merId(), $data, $fieldRule);
  125. return app('json')->success('添加成功');
  126. }
  127. /**
  128. * 修改数据
  129. * @param int $groupId
  130. * @param int $id
  131. * @param GroupDataValidate $validate
  132. * @param GroupRepository $groupRepository
  133. * @return mixed
  134. * @throws DbException
  135. * @author xaboy
  136. * @day 2020-04-02
  137. */
  138. public function update($groupId, $id, GroupDataValidate $validate, GroupRepository $groupRepository)
  139. {
  140. $data = $this->request->params([['sort', 0], ['status', 0]]);
  141. $validate->check($data);
  142. if (!$groupRepository->exists($groupId))
  143. return app('json')->fail('数据组不存在');
  144. if (!$this->repository->merExists($this->request->merId(), $id))
  145. return app('json')->fail('数据不存在!');
  146. $fieldRule = $groupRepository->fields($groupId);
  147. $data['value'] = $this->repository->handleDataValue($fieldRule, $this->request->params(array_column($fieldRule, 'field')));
  148. $this->repository->merUpdate($this->request->merId(), $id, $data, $fieldRule);
  149. return app('json')->success('修改成功');
  150. }
  151. /**
  152. * 删除数据
  153. * @param int $id
  154. * @return mixed
  155. * @throws DbException
  156. * @author xaboy
  157. * @day 2020-03-30
  158. */
  159. public function delete($id)
  160. {
  161. $this->repository->merDelete($this->request->merId(), $id);
  162. return app('json')->success('删除成功');
  163. }
  164. /**
  165. * 基础详情 组合数据结构详情
  166. * @param int $id
  167. * @return mixed
  168. * @author xaboy
  169. * @day 2020-04-02
  170. */
  171. public function baseDetail($id)
  172. {
  173. $data = $this->repository->getWhere(['group_data_id' => $id]);
  174. if (!$data) return app('json')->fail('数据不存在');
  175. return app('json')->success($data);
  176. }
  177. }