SystemGroupDataServices.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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, $type = 'limit')
  73. {
  74. [$page, $limit] = $this->getPageValue();
  75. if ($type == 'all') $page = $limit = 0;
  76. $list = $this->dao->getGroupDataList($where, $page, $limit);
  77. $count = $this->dao->count($where);
  78. $type = '';
  79. $gid = (int)$where['gid'];
  80. /** @var SystemGroupServices $services */
  81. $services = app()->make(SystemGroupServices::class);
  82. $group = $services->getOne(['id' => $gid], 'id,config_name,fields');
  83. $header = json_decode($group['fields'], true) ?? [];
  84. $title = [];
  85. $param = [];
  86. foreach ($header as $item) {
  87. if ($group['config_name'] == 'order_details_images' && $item['title'] == 'order_status') {
  88. $status = str_replace("\r\n", "\n", $item["param"]);//防止不兼容
  89. $status = explode("\n", $status);
  90. if (is_array($status) && !empty($status)) {
  91. foreach ($status as $index => $v) {
  92. $vl = explode('=>', $v);
  93. if (isset($vl[0]) && isset($vl[1])) {
  94. $param[$vl[0]] = $vl[1];
  95. }
  96. }
  97. }
  98. }
  99. if ($item['type'] == 'upload' || $item['type'] == 'uploads') {
  100. $title[$item['title']] = [];
  101. $type = $item['title'];
  102. } else {
  103. $title[$item['title']] = '';
  104. }
  105. }
  106. foreach ($list as $key => $value) {
  107. $list[$key] = array_merge($value, $title);
  108. $infos = json_decode($value['value'], true) ?: [];
  109. foreach ($infos as $index => $info) {
  110. if ($group['config_name'] == 'order_details_images' && $index == 'order_status') {
  111. $list[$key][$index] = ($param[$info['value']] ?? '') . '/' . $info['value'];
  112. } else {
  113. if ($info['type'] == 'upload') {
  114. $list[$key][$index] = [set_file_url($info['value'])];
  115. } elseif ($info['type'] == 'checkbox') {
  116. $list[$key][$index] = implode(",", $info["value"]);
  117. } else {
  118. $list[$key][$index] = $info['value'];
  119. }
  120. }
  121. }
  122. unset($list[$key]['value']);
  123. }
  124. return compact('list', 'count', 'type');
  125. }
  126. /**
  127. * 根据gid判断出是否能再次添加组合数据
  128. * @param int $gid
  129. * @param int $count
  130. * @param string $key
  131. * @return bool
  132. */
  133. public function isGroupGidSave(int $gid, int $count, string $key): bool
  134. {
  135. /** @var SystemGroupServices $services */
  136. $services = app()->make(SystemGroupServices::class);
  137. $configName = $services->value(['id' => $gid], 'config_name');
  138. if ($configName == $key) {
  139. return $this->dao->count(['gid' => $gid]) >= $count;
  140. } else {
  141. return false;
  142. }
  143. }
  144. /**
  145. * 创建表单
  146. * @param int $gid
  147. * @param array $groupData
  148. * @return mixed
  149. * @throws \FormBuilder\Exception\FormBuilderException
  150. */
  151. public function createGroupForm(int $gid, array $groupData = [])
  152. {
  153. $groupDataValue = isset($groupData["value"]) ? json_decode($groupData["value"], true) : [];
  154. /** @var SystemGroupServices $services */
  155. $services = app()->make(SystemGroupServices::class);
  156. $fields = $services->getValueFields($gid);
  157. $f[] = Form::hidden('gid', $gid);
  158. foreach ($fields as $key => $value) {
  159. $info = [];
  160. if (isset($value["param"])) {
  161. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  162. $params = explode("\n", $value["param"]);
  163. if (is_array($params) && !empty($params)) {
  164. foreach ($params as $index => $v) {
  165. $vl = explode('=>', $v);
  166. if (isset($vl[0]) && isset($vl[1])) {
  167. $info[$index]["value"] = $vl[0];
  168. $info[$index]["label"] = $vl[1];
  169. }
  170. }
  171. }
  172. }
  173. $fvalue = $groupDataValue[$value['title']]['value'] ?? '';
  174. switch ($value["type"]) {
  175. case 'input':
  176. if ($gid == 55 && $value['title'] == 'sign_num') {
  177. $f[] = Form::number($value["title"], $value["name"], (int)$fvalue)->precision(0);
  178. } else {
  179. $f[] = Form::input($value["title"], $value["name"], $fvalue);
  180. }
  181. break;
  182. case 'textarea':
  183. $f[] = Form::input($value["title"], $value["name"], $fvalue)->type('textarea')->placeholder($value['param']);
  184. break;
  185. case 'radio':
  186. $f[] = Form::radio($value["title"], $value["name"], $fvalue ?: (int)$info[0]["value"] ?? '')->options($info);
  187. break;
  188. case 'checkbox':
  189. $f[] = Form::checkbox($value["title"], $value["name"], $fvalue ?: $info[0] ?? '')->options($info);
  190. break;
  191. case 'select':
  192. $f[] = Form::select($value["title"], $value["name"], $fvalue !== '' ? $fvalue : $info[0] ?? '')->options($info)->multiple(false);
  193. break;
  194. case 'upload':
  195. if (!empty($fvalue)) {
  196. $image = is_string($fvalue) ? $fvalue : $fvalue[0];
  197. } else {
  198. $image = '';
  199. }
  200. $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]);
  201. break;
  202. case 'uploads':
  203. if ($fvalue) {
  204. if (is_string($fvalue)) $fvalue = [$fvalue];
  205. $images = !empty($fvalue) ? $fvalue : [];
  206. } else {
  207. $images = [];
  208. }
  209. $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);
  210. break;
  211. default:
  212. $f[] = Form::input($value["title"], $value["name"], $fvalue);
  213. break;
  214. }
  215. }
  216. $f[] = Form::number('sort', '排序', (int)($groupData["sort"] ?? 1))->precision(0);
  217. $f[] = Form::radio('status', '状态', (int)($groupData["status"] ?? 1))->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  218. return $f;
  219. }
  220. /**
  221. * 获取添加组合数据表单
  222. * @param int $gid
  223. * @return array
  224. * @throws \FormBuilder\Exception\FormBuilderException
  225. */
  226. public function createForm(int $gid)
  227. {
  228. return create_form('添加数据', $this->createGroupForm($gid), $this->url('/setting/group_data'));
  229. }
  230. /**
  231. * 获取修改组合数据表单
  232. * @param int $gid
  233. * @param int $id
  234. * @return array
  235. * @throws \FormBuilder\Exception\FormBuilderException
  236. */
  237. public function updateForm(int $gid, int $id)
  238. {
  239. $groupData = $this->dao->get($id);
  240. if (!$groupData) {
  241. throw new AdminException(100026);
  242. }
  243. return create_form('编辑数据', $this->createGroupForm($gid, $groupData->toArray()), $this->url('/setting/group_data/' . $id), 'PUT');
  244. }
  245. /**
  246. * 根据id获取当前记录中的数据
  247. * @param $id
  248. * @return mixed
  249. * @throws \think\db\exception\DataNotFoundException
  250. * @throws \think\db\exception\ModelNotFoundException
  251. * @throws \think\exception\DbException
  252. */
  253. public function getDateValue($id)
  254. {
  255. $value = $this->dao->get($id);
  256. $data["id"] = $value["id"];
  257. $data["status"] = $value["status"];
  258. $fields = json_decode($value["value"], true);
  259. foreach ($fields as $index => $field) {
  260. $data[$index] = $field["value"];
  261. }
  262. return $data;
  263. }
  264. /**
  265. * 根据id获取数据
  266. * @param array $ids
  267. * @param string $field
  268. */
  269. public function getGroupDataColumn(array $ids)
  270. {
  271. $systemGroup = [];
  272. if (!empty($ids)) {
  273. $systemGroupData = $this->dao->idByGroupList($ids, '*');
  274. if (!empty($systemGroupData))
  275. $systemGroup = array_combine(array_column($systemGroupData, 'id'), $systemGroupData);
  276. }
  277. return $systemGroup;
  278. }
  279. /**
  280. * 根据gid删除数据
  281. * @param int $gid
  282. * @return mixed
  283. */
  284. public function delGroupDate(int $gid)
  285. {
  286. return $this->dao->delGroupDate($gid);
  287. }
  288. /**
  289. * 批量保存
  290. * @param array $params
  291. * @param string $config_name
  292. * @return bool
  293. * @throws \Exception
  294. */
  295. public function saveAllData(array $params, string $config_name)
  296. {
  297. /** @var SystemGroupServices $systemGroupServices */
  298. $systemGroupServices = app()->make(SystemGroupServices::class);
  299. $gid = $systemGroupServices->value(['config_name' => $config_name], 'id');
  300. if (!$gid) throw new AdminException(100026);
  301. $group = $systemGroupServices->getOne(['id' => $gid], 'id,config_name,fields');
  302. $fields = json_decode($group['fields'], true) ?? [];
  303. $this->transaction(function () use ($gid, $params, $fields) {
  304. $this->delGroupDate($gid);
  305. $data = [];
  306. $sort = count($params);
  307. foreach ($params as $k => $v) {
  308. $value = [];
  309. foreach ($v as $key => $param) {
  310. foreach ($fields as $index => $field) {
  311. if ($key == $field["title"]) {
  312. if ($param == "") {
  313. throw new AdminException(400607, ['name' => $field["name"]]);
  314. } else {
  315. $value[$key]["type"] = $field["type"];
  316. $value[$key]["value"] = $param;
  317. }
  318. }
  319. }
  320. }
  321. $data[$k] = [
  322. "gid" => $gid,
  323. "add_time" => time(),
  324. "value" => json_encode($value),
  325. "sort" => $sort,
  326. "status" => $v["status"] ?? 1
  327. ];
  328. $sort--;
  329. }
  330. $this->dao->saveAll($data);
  331. });
  332. \crmeb\services\CacheService::clear();
  333. return true;
  334. }
  335. /**
  336. * 检查秒杀时间段
  337. * @param SystemGroupServices $services
  338. * @param $gid
  339. * @param $params
  340. * @param int $id
  341. * @return mixed
  342. */
  343. public function checkSeckillTime(SystemGroupServices $services, $gid, $params, $id = 0)
  344. {
  345. $name = $services->value(['id' => $gid], 'config_name');
  346. if ($name == 'routine_seckill_time') {
  347. if ($params['time'] == '') {
  348. throw new AdminException(400190);
  349. }
  350. if (!$params['continued']) {
  351. throw new AdminException(400191);
  352. }
  353. if (!preg_match('/^(\d|1\d|2[0-3])$/', $params['time'])) {
  354. throw new AdminException(400192);
  355. }
  356. if (!preg_match('/^([1-9]|1\d|2[0-4])$/', $params['continued'])) {
  357. throw new AdminException(400193);
  358. }
  359. if (($params['time'] + $params['continued']) > 24) throw new AdminException(400194);
  360. $list = $this->dao->getColumn(['gid' => $gid], 'value', 'id');
  361. if ($id) unset($list[$id]);
  362. $times = $time = [];
  363. foreach ($list as $item) {
  364. $info = json_decode($item, true);
  365. for ($i = 0; $i < $info['continued']['value']; $i++) {
  366. $times[] = $info['time']['value'] + $i;
  367. }
  368. }
  369. for ($i = 0; $i < $params['continued']; $i++) {
  370. $time[] = $params['time'] + $i;
  371. }
  372. foreach ($time as $v) {
  373. if (in_array($v, $times)) throw new AdminException(400195);
  374. }
  375. }
  376. }
  377. }