GroupDataRepository.php 8.5 KB

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