GroupDataRepository.php 9.4 KB

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