GroupRepository.php 6.4 KB

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