SystemGroupDataServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system\config;
  12. use app\dao\system\config\SystemGroupDataDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. /**
  17. * 组合数据数据集
  18. * Class SystemGroupDataServices
  19. * @package app\services\system\config
  20. * @method delete($id, ?string $key = null) 删除数据
  21. * @method get(int $id, ?array $field = []) 获取一条数据
  22. * @method save(array $data) 保存数据
  23. * @method update($id, array $data, ?string $key = null) 修改数据
  24. */
  25. class SystemGroupDataServices extends BaseServices
  26. {
  27. /**
  28. * SystemGroupDataServices constructor.
  29. * @param SystemGroupDataDao $dao
  30. */
  31. public function __construct(SystemGroupDataDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取某个配置下的数据从新组合成新得数据返回
  37. * @param string $configName
  38. * @param int $limit
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getConfigNameValue(string $configName, int $limit = 0)
  45. {
  46. /** @var SystemGroupServices $systemGroupServices */
  47. $systemGroupServices = app()->make(SystemGroupServices::class);
  48. $value = $this->dao->getGroupDate((int)$systemGroupServices->getConfigNameId($configName), $limit);
  49. $data = [];
  50. foreach ($value as $key => $item) {
  51. $data[$key]["id"] = $item["id"];
  52. if (isset($item['status'])) $data[$key]["status"] = $item["status"];
  53. $fields = json_decode($item["value"], true) ?: [];
  54. foreach ($fields as $index => $field) {
  55. if ($field['type'] == 'upload') {
  56. $data[$key][$index] = set_file_url($field['value']);
  57. } else {
  58. $data[$key][$index] = $field["value"];
  59. }
  60. }
  61. }
  62. return $data;
  63. }
  64. /**
  65. * 获取组合数据列表
  66. * @param array $where
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getGroupDataList(array $where)
  73. {
  74. [$page, $limit] = $this->getPageValue();
  75. $list = $this->dao->getGroupDataList($where, $page, $limit);
  76. $count = $this->dao->count($where);
  77. $type = '';
  78. $gid = (int)$where['gid'];
  79. /** @var SystemGroupServices $services */
  80. $services = app()->make(SystemGroupServices::class);
  81. $group = $services->getOne(['id' => $gid], 'id,config_name,fields');
  82. $header = json_decode($group['fields'], true) ?? [];
  83. $title = [];
  84. $param = [];
  85. foreach ($header as $item) {
  86. if ($group['config_name'] == 'order_details_images' && $item['title'] == 'order_status') {
  87. $status = str_replace("\r\n", "\n", $item["param"]);//防止不兼容
  88. $status = explode("\n", $status);
  89. if (is_array($status) && !empty($status)) {
  90. foreach ($status as $index => $v) {
  91. $vl = explode('=>', $v);
  92. if (isset($vl[0]) && isset($vl[1])) {
  93. $param[$vl[0]] = $vl[1];
  94. }
  95. }
  96. }
  97. }
  98. if ($item['type'] == 'upload' || $item['type'] == 'uploads') {
  99. $title[$item['title']] = [];
  100. $type = $item['title'];
  101. } else {
  102. $title[$item['title']] = '';
  103. }
  104. }
  105. foreach ($list as $key => $value) {
  106. $list[$key] = array_merge($value, $title);
  107. $infos = json_decode($value['value'], true) ?: [];
  108. foreach ($infos as $index => $info) {
  109. if ($group['config_name'] == 'order_details_images' && $index == 'order_status') {
  110. $list[$key][$index] = ($param[$info['value']] ?? '') . '/' . $info['value'];
  111. } else {
  112. if ($info['type'] == 'upload') {
  113. $list[$key][$index] = [set_file_url($info['value'])];
  114. } elseif ($info['type'] == 'checkbox') {
  115. $list[$key][$index] = implode(",", $info["value"]);
  116. } else {
  117. $list[$key][$index] = $info['value'];
  118. }
  119. }
  120. }
  121. unset($list[$key]['value']);
  122. }
  123. return compact('list', 'count', 'type');
  124. }
  125. /**
  126. * 根据gid判断出是否能再次添加组合数据
  127. * @param int $gid
  128. * @param int $count
  129. * @param string $key
  130. * @return bool
  131. */
  132. public function isGroupGidSave(int $gid, int $count, string $key): bool
  133. {
  134. /** @var SystemGroupServices $services */
  135. $services = app()->make(SystemGroupServices::class);
  136. $configName = $services->value(['id' => $gid], 'config_name');
  137. if ($configName == $key) {
  138. return $this->dao->count(['gid' => $gid]) >= $count;
  139. } else {
  140. return false;
  141. }
  142. }
  143. /**
  144. * 创建表单
  145. * @param int $gid
  146. * @param array $groupData
  147. * @return mixed
  148. * @throws \FormBuilder\Exception\FormBuilderException
  149. */
  150. public function createGroupForm(int $gid, array $groupData = [])
  151. {
  152. $groupDataValue = isset($groupData["value"]) ? json_decode($groupData["value"], true) : [];
  153. /** @var SystemGroupServices $services */
  154. $services = app()->make(SystemGroupServices::class);
  155. $fields = $services->getValueFields($gid);
  156. $f[] = Form::hidden('gid', $gid);
  157. foreach ($fields as $key => $value) {
  158. $info = [];
  159. if (isset($value["param"])) {
  160. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  161. $params = explode("\n", $value["param"]);
  162. if (is_array($params) && !empty($params)) {
  163. foreach ($params as $index => $v) {
  164. $vl = explode('=>', $v);
  165. if (isset($vl[0]) && isset($vl[1])) {
  166. $info[$index]["value"] = $vl[0];
  167. $info[$index]["label"] = $vl[1];
  168. }
  169. }
  170. }
  171. }
  172. $fvalue = $groupDataValue[$value['title']]['value'] ?? '';
  173. switch ($value["type"]) {
  174. case 'input':
  175. if ($gid == 55 && $value['title'] == 'sign_num') {
  176. $f[] = Form::number($value["title"], $value["name"], (int)$fvalue)->precision(0);
  177. } else {
  178. $f[] = Form::input($value["title"], $value["name"], $fvalue);
  179. }
  180. break;
  181. case 'textarea':
  182. $f[] = Form::input($value["title"], $value["name"], $fvalue)->type('textarea')->placeholder($value['param']);
  183. break;
  184. case 'radio':
  185. $f[] = Form::radio($value["title"], $value["name"], $fvalue ?: (int)$info[0]["value"] ?? '')->options($info);
  186. break;
  187. case 'checkbox':
  188. $f[] = Form::checkbox($value["title"], $value["name"], $fvalue ?: $info[0] ?? '')->options($info);
  189. break;
  190. case 'select':
  191. $f[] = Form::select($value["title"], $value["name"], $fvalue !== '' ? $fvalue : $info[0] ?? '')->options($info)->multiple(false);
  192. break;
  193. case 'upload':
  194. if (!empty($fvalue)) {
  195. $image = is_string($fvalue) ? $fvalue : $fvalue[0];
  196. } else {
  197. $image = '';
  198. }
  199. $f[] = Form::frameImage($value["title"], $value["name"], $this->url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => $value["title"], 'big' => 1], true), $image)->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false]);
  200. break;
  201. case 'uploads':
  202. if ($fvalue) {
  203. if (is_string($fvalue)) $fvalue = [$fvalue];
  204. $images = !empty($fvalue) ? $fvalue : [];
  205. } else {
  206. $images = [];
  207. }
  208. $f[] = Form::frameImages($value["title"], $value["name"], $this->url(config('app.admin_prefix', 'admin') . '/widget.images/index', ['fodder' => $value["title"], 'big' => 1, 'type' => 'many', 'maxLength' => 5], true), $images)->maxLength(5)->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['footer' => false])->spin(0);
  209. break;
  210. default:
  211. $f[] = Form::input($value["title"], $value["name"], $fvalue);
  212. break;
  213. }
  214. }
  215. $f[] = Form::number('sort', '排序', (int)($groupData["sort"] ?? 1))->precision(0);
  216. $f[] = Form::radio('status', '状态', (int)($groupData["status"] ?? 1))->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  217. return $f;
  218. }
  219. /**
  220. * 获取添加组合数据表单
  221. * @param int $gid
  222. * @return array
  223. * @throws \FormBuilder\Exception\FormBuilderException
  224. */
  225. public function createForm(int $gid)
  226. {
  227. return create_form('添加数据', $this->createGroupForm($gid), $this->url('/setting/group_data'));
  228. }
  229. /**
  230. * 获取修改组合数据表单
  231. * @param int $gid
  232. * @param int $id
  233. * @return array
  234. * @throws \FormBuilder\Exception\FormBuilderException
  235. */
  236. public function updateForm(int $gid, int $id)
  237. {
  238. $groupData = $this->dao->get($id);
  239. if (!$groupData) {
  240. throw new AdminException(100026);
  241. }
  242. return create_form('编辑数据', $this->createGroupForm($gid, $groupData->toArray()), $this->url('/setting/group_data/' . $id), 'PUT');
  243. }
  244. /**
  245. * 根据id获取当前记录中的数据
  246. * @param $id
  247. * @return mixed
  248. * @throws \think\db\exception\DataNotFoundException
  249. * @throws \think\db\exception\ModelNotFoundException
  250. * @throws \think\exception\DbException
  251. */
  252. public function getDateValue($id)
  253. {
  254. $value = $this->dao->get($id);
  255. $data["id"] = $value["id"];
  256. $data["status"] = $value["status"];
  257. $fields = json_decode($value["value"], true);
  258. foreach ($fields as $index => $field) {
  259. $data[$index] = $field["value"];
  260. }
  261. return $data;
  262. }
  263. /**
  264. * 根据id获取数据
  265. * @param array $ids
  266. * @param string $field
  267. */
  268. public function getGroupDataColumn(array $ids)
  269. {
  270. $systemGroup = [];
  271. if (!empty($ids)) {
  272. $systemGroupData = $this->dao->idByGroupList($ids);
  273. if (!empty($systemGroupData))
  274. $systemGroup = array_combine(array_column($systemGroupData, 'id'), $systemGroupData);
  275. }
  276. return $systemGroup;
  277. }
  278. /**
  279. * 根据gid删除数据
  280. * @param int $gid
  281. * @return mixed
  282. */
  283. public function delGroupDate(int $gid)
  284. {
  285. return $this->dao->delGroupDate($gid);
  286. }
  287. /**
  288. * 批量保存
  289. * @param array $params
  290. * @param string $config_name
  291. * @return bool
  292. * @throws \Exception
  293. */
  294. public function saveAllData(array $params, string $config_name)
  295. {
  296. /** @var SystemGroupServices $systemGroupServices */
  297. $systemGroupServices = app()->make(SystemGroupServices::class);
  298. $gid = $systemGroupServices->value(['config_name' => $config_name], 'id');
  299. if (!$gid) throw new AdminException(100026);
  300. $group = $systemGroupServices->getOne(['id' => $gid], 'id,config_name,fields');
  301. $fields = json_decode($group['fields'], true) ?? [];
  302. $this->transaction(function () use ($gid, $params, $fields) {
  303. $this->delGroupDate($gid);
  304. $data = [];
  305. $sort = count($params);
  306. foreach ($params as $k => $v) {
  307. $value = [];
  308. foreach ($v as $key => $param) {
  309. foreach ($fields as $index => $field) {
  310. if ($key == $field["title"]) {
  311. if ($param == "") {
  312. throw new AdminException(400607, ['name' => $field["name"]]);
  313. } else {
  314. $value[$key]["type"] = $field["type"];
  315. $value[$key]["value"] = $param;
  316. }
  317. }
  318. }
  319. }
  320. $data[$k] = [
  321. "gid" => $gid,
  322. "add_time" => time(),
  323. "value" => json_encode($value),
  324. "sort" => $sort,
  325. "status" => $v["status"] ?? 1
  326. ];
  327. $sort--;
  328. }
  329. $this->dao->saveAll($data);
  330. });
  331. \crmeb\services\CacheService::clear();
  332. return true;
  333. }
  334. /**
  335. * 检查秒杀时间段
  336. * @param SystemGroupServices $services
  337. * @param $gid
  338. * @param $params
  339. * @param int $id
  340. * @return mixed
  341. */
  342. public function checkSeckillTime(SystemGroupServices $services, $gid, $params, $id = 0)
  343. {
  344. $name = $services->value(['id' => $gid], 'config_name');
  345. if ($name == 'routine_seckill_time') {
  346. if ($params['time'] == '') {
  347. throw new AdminException(400190);
  348. }
  349. if (!$params['continued']) {
  350. throw new AdminException(400191);
  351. }
  352. if (!preg_match('/^(\d|1\d|2[0-3])$/', $params['time'])) {
  353. throw new AdminException(400192);
  354. }
  355. if (!preg_match('/^([1-9]|1\d|2[0-4])$/', $params['continued'])) {
  356. throw new AdminException(400193);
  357. }
  358. if (($params['time'] + $params['continued']) > 24) throw new AdminException(400194);
  359. $list = $this->dao->getColumn(['gid' => $gid], 'value', 'id');
  360. if ($id) unset($list[$id]);
  361. $times = $time = [];
  362. foreach ($list as $item) {
  363. $info = json_decode($item, true);
  364. for ($i = 0; $i < $info['continued']['value']; $i++) {
  365. $times[] = $info['time']['value'] + $i;
  366. }
  367. }
  368. for ($i = 0; $i < $params['continued']; $i++) {
  369. $time[] = $params['time'] + $i;
  370. }
  371. foreach ($time as $v) {
  372. if (in_array($v, $times)) throw new AdminException(400195);
  373. }
  374. }
  375. }
  376. }