SystemGroupDataServices.php 14 KB

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