GroupDataRepository.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\common\repositories\system\groupData;
  3. use app\common\dao\BaseDao;
  4. use app\common\dao\system\groupData\GroupDataDao;
  5. use app\common\repositories\BaseRepository;
  6. use app\common\repositories\store\StoreCategoryRepository;
  7. use FormBuilder\Exception\FormBuilderException;
  8. use FormBuilder\Factory\Elm;
  9. use FormBuilder\Form;
  10. use think\db\exception\DataNotFoundException;
  11. use think\db\exception\DbException;
  12. use think\db\exception\ModelNotFoundException;
  13. use think\exception\ValidateException;
  14. use think\facade\Route;
  15. use think\Model;
  16. /**
  17. * Class GroupDataRepository
  18. * @package app\common\repositories\system\groupData
  19. * @mixin GroupDataDao
  20. * @author zfy
  21. * @day 2020-03-30
  22. */
  23. class GroupDataRepository extends BaseRepository
  24. {
  25. /**
  26. * GroupDataRepository constructor.
  27. * @param GroupDataDao $dao
  28. */
  29. public function __construct(GroupDataDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * @param int $merId
  35. * @param array $data
  36. * @param array $fieldRule
  37. * @return BaseDao|Model
  38. * @author zfy
  39. * @day 2020-03-30
  40. */
  41. public function create(int $merId, array $data, array $fieldRule)
  42. {
  43. $this->checkData($data['value'], $fieldRule);
  44. $data['mer_id'] = $merId;
  45. return $this->dao->create($data);
  46. }
  47. /**
  48. * @param $merId
  49. * @param $id
  50. * @param $data
  51. * @param $fieldRule
  52. * @return int
  53. * @throws DbException
  54. * @author zfy
  55. * @day 2020/9/23
  56. */
  57. public function merUpdate($merId, $id, $data, $fieldRule)
  58. {
  59. $this->checkData($data['value'], $fieldRule);
  60. return $this->dao->merUpdate($merId, $id, $data);
  61. }
  62. /**
  63. * @param array $data
  64. * @param array $fieldRule
  65. * @author zfy
  66. * @day 2020/9/23
  67. */
  68. public function checkData(array $data, array $fieldRule)
  69. {
  70. foreach ($fieldRule as $rule) {
  71. if (!isset($data[$rule['field']]) || $data[$rule['field']] === '') {
  72. throw new ValidateException($rule['name'] . '不能为空');
  73. }
  74. if ($rule['type'] === 'number' && $data[$rule['field']] < 0)
  75. throw new ValidateException($rule['name'] . '不能小于0');
  76. }
  77. }
  78. /**
  79. * @param int $merId
  80. * @param int $groupId
  81. * @param int $page
  82. * @param int $limit
  83. * @return array
  84. * @throws DataNotFoundException
  85. * @throws DbException
  86. * @throws ModelNotFoundException
  87. * @author zfy
  88. * @day 2020-03-30
  89. */
  90. public function getGroupDataLst(int $merId, int $groupId, int $page, int $limit): array
  91. {
  92. $query = $this->dao->getGroupDataWhere($merId, $groupId);
  93. $count = $query->count();
  94. $list = $query->field('group_data_id,value,sort,status,create_time')->page($page, $limit)->select()->toArray();
  95. foreach ($list as $k => $data) {
  96. $value = $data['value'];
  97. unset($data['value']);
  98. $data += $value;
  99. $list[$k] = $data;
  100. }
  101. return compact('count', 'list');
  102. }
  103. /**
  104. * @param int $groupId
  105. * @param int|null $id
  106. * @param array $formData
  107. * @return Form
  108. * @throws FormBuilderException
  109. * @author zfy
  110. * @day 2020-04-02
  111. */
  112. public function form(int $groupId, ?int $id = null, ?int $merId = null, array $formData = []): Form
  113. {
  114. $fields = app()->make(GroupRepository::class)->fields($groupId);
  115. if (is_null($merId)) {
  116. $url = is_null($id)
  117. ? Route::buildUrl('groupDataCreate', compact('groupId'))->build()
  118. : Route::buildUrl('groupDataUpdate', compact('groupId', 'id'))->build();
  119. } else {
  120. $url = is_null($id)
  121. ? Route::buildUrl('merchantGroupDataCreate', compact('groupId'))->build()
  122. : Route::buildUrl('merchantGroupDataUpdate', compact('groupId', 'id'))->build();
  123. }
  124. $form = Elm::createForm($url);
  125. $rules = [];
  126. foreach ($fields as $field) {
  127. if ($field['type'] == 'image') {
  128. $rules[] = Elm::frameImage($field['field'], $field['name'], '/' . config('admin.' . ($merId ? 'merchant' : 'admin') . '_prefix') . '/setting/uploadPicture?field=' . $field['field'] . '&type=1')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]);
  129. continue;
  130. } else if ($field['type'] == 'cate') {
  131. $rules[] = Elm::cascader($field['field'], $field['name'])->options(function () use ($id) {
  132. $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
  133. $menus = $storeCategoryRepository->getAllOptions(0, 1, null);
  134. if ($id && isset($menus[$id])) unset($menus[$id]);
  135. $menus = formatCascaderData($menus, 'cate_name');
  136. return $menus;
  137. })->props(['props' => ['checkStrictly' => true, 'emitPath' => false]])->filterable(true)->appendValidate(Elm::validateInt()->required()->message('请选择分类'));
  138. continue;
  139. } else if (in_array($field['type'], ['select', 'checkbox', 'radio'])) {
  140. $options = array_map(function ($val) {
  141. [$value, $label] = explode(':', $val, 2);
  142. return compact('value', 'label');
  143. }, explode("\n", $field['param']));
  144. $rule = Elm::{$field['type']}($field['field'], $field['name'])->options($options);
  145. if ($field['type'] == 'select') {
  146. $rule->filterable(true)->prop('allow-create', true);
  147. }
  148. $rules[] = $rule;
  149. continue;
  150. }
  151. if ($field['type'] == 'file') {
  152. $rules[] = Elm::uploadFile($field['field'], $field['name'], Route::buildUrl('configUpload', ['field' => 'file'])->build())->headers(['X-Token' => request()->token()]);
  153. continue;
  154. }
  155. $rules[] = Elm::{$field['type']}($field['field'], $field['name'], '');
  156. }
  157. $rules[] = Elm::number('sort', '排序', 0);
  158. $rules[] = Elm::switches('status', '是否显示', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启');
  159. $form->setRule($rules);
  160. return $form->setTitle(is_null($id) ? '添加数据' : '编辑数据')->formData($formData);
  161. }
  162. /**
  163. * @param int $groupId
  164. * @param int $merId
  165. * @param int $id
  166. * @return Form
  167. * @throws DataNotFoundException
  168. * @throws DbException
  169. * @throws FormBuilderException
  170. * @throws ModelNotFoundException
  171. * @author zfy
  172. * @day 2020-04-02
  173. */
  174. public function updateForm(int $groupId, int $merId, int $id)
  175. {
  176. $data = $this->dao->getGroupDataWhere($merId, $groupId)->where('group_data_id', $id)->find()->toArray();
  177. $value = $data['value'];
  178. unset($data['value']);
  179. $data += $value;
  180. return $this->form($groupId, $id, $merId, $data);
  181. }
  182. /**
  183. * @param string $key
  184. * @param int $merId
  185. * @param int|null $page
  186. * @param int|null $limit
  187. * @return array
  188. * @author zfy
  189. * @day 2020/5/27
  190. */
  191. public function groupData(string $key, int $merId, ?int $page = null, ?int $limit = 10)
  192. {
  193. /** @var GroupRepository $make */
  194. $make = app()->make(GroupRepository::class);
  195. $groupId = $make->keyById($key);
  196. if (!$groupId) return [];
  197. return $this->dao->getGroupData($merId, $groupId, $page, $limit);
  198. }
  199. /**
  200. * @param string $key
  201. * @param int $merId
  202. * @param int|null $page
  203. * @param int|null $limit
  204. * @return int
  205. * @author zfy
  206. * @day 2020/5/27
  207. */
  208. public function getGroupDataCount(string $key, int $merId)
  209. {
  210. /** @var GroupRepository $make */
  211. $make = app()->make(GroupRepository::class);
  212. $groupId = $make->keyById($key);
  213. if (!$groupId) 0;
  214. return $this->dao->groupDataCount($merId, $groupId);
  215. }
  216. /**
  217. * @param int $id
  218. * @param int $merId
  219. * @return mixed|void
  220. * @throws DataNotFoundException
  221. * @throws DbException
  222. * @throws ModelNotFoundException
  223. * @author zfy
  224. * @day 2020/6/2
  225. */
  226. public function idByData(int $id, int $merId)
  227. {
  228. $data = $this->dao->merGet($id, $merId);
  229. if (!$data) return;
  230. return json_decode($data['value']);
  231. }
  232. /**
  233. * @param string $key
  234. * @param int $merId
  235. * @param int|null $page
  236. * @param int|null $limit
  237. * @return array
  238. * @author zfy
  239. * @day 2020/6/3
  240. */
  241. public function groupDataId(string $key, int $merId, ?int $page = null, ?int $limit = 10)
  242. {
  243. $make = app()->make(GroupRepository::class);
  244. $groupId = $make->keyById($key);
  245. if (!$groupId) return [];
  246. return $this->dao->getGroupDataId($merId, $groupId, $page, $limit);
  247. }
  248. }