GroupRepository.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace app\common\repositories\system\groupData;
  3. use app\common\dao\system\groupData\GroupDao;
  4. use app\common\model\system\groupData\SystemGroup;
  5. use app\common\repositories\BaseRepository;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use FormBuilder\Factory\Elm;
  8. use FormBuilder\Form;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\exception\ValidateException;
  13. use think\facade\Db;
  14. use think\facade\Route;
  15. use think\Model;
  16. /**
  17. * Class GroupRepository
  18. * @package app\common\repositories\system\groupData
  19. * @mixin GroupDao
  20. * @author zfy
  21. * @day 2020-03-27
  22. */
  23. class GroupRepository extends BaseRepository
  24. {
  25. /**
  26. *
  27. */
  28. const TYPES = ['input' => '文本框', 'number' => '数字框', 'textarea' => '多行文本框', 'radio' => '单选框', 'checkbox' => '多选框', 'select' => '下拉框', 'file' => '文件上传', 'image' => '图片上传', 'color' => '颜色选择框', 'cate' => '平台分类'];
  29. /**
  30. * GroupRepository constructor.
  31. * @param GroupDao $dao
  32. */
  33. public function __construct(GroupDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * @param array $data
  39. * @return SystemGroup|Model
  40. * @author zfy
  41. * @day 2020-03-27
  42. */
  43. public function create(array $data)
  44. {
  45. $data['fields'] = $this->tidyFields($data['fields']);
  46. return $this->dao->create($data);
  47. }
  48. /**
  49. * @param int $id
  50. * @param array $data
  51. * @return int
  52. * @throws DbException
  53. * @author zfy
  54. * @day 2020-03-27
  55. */
  56. public function update(int $id, array $data)
  57. {
  58. $data['fields'] = json_encode($this->tidyFields($data['fields']));
  59. return $this->dao->update($id, $data);
  60. }
  61. /**
  62. * @param array $fields
  63. * @return array
  64. * @author zfy
  65. * @day 2020-03-27
  66. */
  67. public function tidyFields(array $fields): array
  68. {
  69. if (!count($fields))
  70. throw new ValidateException('字段最少设置一个');
  71. $data = [];
  72. $fieldKey = [];
  73. foreach ($fields as $field) {
  74. if (!isset($field['type']))
  75. throw new ValidateException('字段类型不能为空');
  76. if (!isset($field['field']))
  77. throw new ValidateException('字段key不能为空');
  78. if (!isset($field['name']))
  79. throw new ValidateException('字段名称不能为空');
  80. if (in_array($field['field'], $fields))
  81. throw new ValidateException('字段key不能重复');
  82. $fieldKey[] = $field['field'];
  83. $data[] = [
  84. 'name' => $field['name'],
  85. 'field' => $field['field'],
  86. 'type' => $field['type'],
  87. 'param' => $field['param'] ?? ''
  88. ];
  89. }
  90. return $data;
  91. }
  92. /**
  93. * @param int|null $id
  94. * @param array $formData
  95. * @return Form
  96. * @throws FormBuilderException
  97. * @author zfy
  98. * @day 2020-03-31
  99. */
  100. public function form(?int $id = null, array $formData = []): Form
  101. {
  102. $form = Elm::createForm(is_null($id) ? Route::buildUrl('groupCreate')->build() : Route::buildUrl('groupUpdate', ['id' => $id])->build());
  103. $form->setRule([
  104. Elm::select('user_type', '后台类型', 0)->options([
  105. ['label' => '总后台配置', 'value' => 0],
  106. ['label' => '商户后台配置', 'value' => 1],
  107. ])->requiredNum(),
  108. Elm::input('group_name', '组合数据名称')->required(),
  109. Elm::input('group_key', '组合数据key')->required(),
  110. Elm::input('group_info', '组合数据说明'),
  111. Elm::number('sort', '排序', 0),
  112. Elm::group('fields', '字段')->rules([
  113. Elm::select('type', '类型')->required()->options(function () {
  114. $options = [];
  115. foreach (self::TYPES as $value => $label) {
  116. $options[] = compact('value', 'label');
  117. }
  118. return $options;
  119. }),
  120. Elm::input('name', '字段名称'),
  121. Elm::input('field', '字段key'),
  122. Elm::textarea('param', '参数'),
  123. ]),
  124. ]);
  125. return $form->setTitle(is_null($id) ? '添加组合数据' : '编辑组合数据')->formData($formData);
  126. }
  127. /**
  128. * @param int $id
  129. * @return Form
  130. * @throws DataNotFoundException
  131. * @throws DbException
  132. * @throws FormBuilderException
  133. * @throws ModelNotFoundException
  134. * @author zfy
  135. * @day 2020-04-02
  136. */
  137. public function updateForm(int $id)
  138. {
  139. return $this->form($id, $this->dao->get($id)->toArray());
  140. }
  141. /**
  142. * @param int $page
  143. * @param int $limit
  144. * @return array
  145. * @throws DbException
  146. * @throws DataNotFoundException
  147. * @throws ModelNotFoundException
  148. * @author zfy
  149. * @day 2020-04-01
  150. */
  151. public function page(int $page, int $limit)
  152. {
  153. $list = $this->dao->page($page, $limit)->hidden(['fields', 'sort'])->select();
  154. $count = $this->dao->count();
  155. return compact('count', 'list');
  156. }
  157. /**
  158. * @param int $id
  159. * @return array
  160. * @author zfy
  161. * @day 2020-04-02
  162. */
  163. public function keys(int $id): array
  164. {
  165. return array_column($this->fields($id), 'field');
  166. }
  167. /**
  168. * @param $id
  169. * @author zfy
  170. * @day 2020-05-16
  171. */
  172. public function delete($id)
  173. {
  174. Db::transaction(function () use ($id) {
  175. $this->delete($id);
  176. /** @var GroupDataRepository $make */
  177. $make = app()->make(GroupDataRepository::class);
  178. $make->clearGroup($id);
  179. });
  180. }
  181. }