GroupRepository.php 5.8 KB

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