Update.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php namespace Manage\Api\Page;
  2. use Dever;
  3. use Manage\Lib\Page;
  4. ini_set("memory_limit", -1);
  5. set_time_limit(0);
  6. ini_set('max_input_vars', 10000);
  7. # 更新页 项目着急上线 以后优化并封装
  8. class Update extends Page
  9. {
  10. public function __construct($load = '', $input = true)
  11. {
  12. parent::__construct('update', $load, $input);
  13. }
  14. public function get(&$value = [], &$option = [])
  15. {
  16. $func = $this->checkFunc();
  17. $remote = $show = $spec = $source = $default = [];
  18. $data['update'] = $data['field'] = $data['option'] = [];
  19. $this->setting('field', $data['update'], true, 'text');
  20. foreach ($data['update'] as $k => $v) {
  21. if ($v['type'] == 'tree' || $v['type'] == 'upload' || $v['type'] == 'cascader' || $v['type'] == 'checkbox' || isset($v['multiple'])) {
  22. if (isset($v['value']) && $v['value']) {
  23. $v['value'] = explode(',', $v['value']);
  24. foreach ($v['value'] as $k1 => $v1) {
  25. if (is_numeric($v1)) {
  26. $v['value'][$k1] = (float) $v1;
  27. }
  28. }
  29. } else {
  30. $v['value'] = [];
  31. }
  32. }
  33. if (isset($v['source'])) {
  34. $source[$v['key']] = $v['source'];
  35. }
  36. if (isset($v['remote'])) {
  37. $remote[$v['key']] = [$k, $v['remote'], $v['key']];
  38. if (isset($v['remote_default']) && !$v['remote_default']) {
  39. unset($remote[$v['key']][2]);
  40. }
  41. }
  42. if (isset($v['spec_data'])) {
  43. $spec[$v['key']] = [$k, $v['spec_data'], $v['key'] . '_spec', $v['spec'], $v['spec_field'], $v['spec_template'] ?? ''];
  44. }
  45. if (isset($v['show']) && is_string($v['show'])) {
  46. $show[$v['key']] = [$k, $v['show']];
  47. }
  48. if (isset($v['default']) && is_string($v['default']) && (strstr($v['default'], '(') || strstr($v['default'], '{'))) {
  49. $default[$v['key']] = [$k, $v['default']];
  50. }
  51. $data['field'][$v['key']] = $v['value'];
  52. if ($v['type'] == 'sku') {
  53. $data['field'][$v['key'] . '_spec'] = [];
  54. }
  55. if (isset($v['option'])) {
  56. $data['option'][$v['key']] = $v['option'];
  57. unset($data['update'][$k]['option']);
  58. }
  59. }
  60. $active = $this->column($data);
  61. $data['info_id'] = false;
  62. if (!$this->info && $active) {
  63. $this->info = $this->db->find($active);
  64. if ($this->info) {
  65. $data['info_id'] = $this->info['id'];
  66. if (!$func) {
  67. $func = $this->getFunc('edit', '编辑', 1);
  68. if (!$func && $this->menu && $this->menu['show'] == 1) {
  69. Dever::error('无操作权限');
  70. }
  71. }
  72. }
  73. } elseif (!$func) {
  74. $func = $this->getFunc('update', '更新', 1);
  75. if (!$func && $this->menu && $this->menu['show'] == 1) {
  76. Dever::error('无操作权限');
  77. }
  78. }
  79. if ($this->info) {
  80. $info = $this->info;
  81. $this->setInfo($info, $data, $remote, $show, $source, $default, 1, $this->config['field']);
  82. if ($spec) {
  83. foreach ($spec as $k => $v) {
  84. $data['update'][$v[0]]['remote'] = Dever::url($v[1], ['value' => '', 'table' => $this->db->config['load'], 'id' => false]);
  85. $result = Dever::call($v[1], [$v[3], $v[4], $this->info['id']]);
  86. if ($result) {
  87. $data['field'][$v[2]] = $result;
  88. }
  89. }
  90. }
  91. } elseif ($value) {
  92. $field = [];
  93. if (isset($this->config['field']) && $this->config['field']) {
  94. foreach ($this->config['field'] as $k => $v) {
  95. if (isset($v['field'])) {
  96. $field[$v['field']] = $v;
  97. if (!isset($field[$v['field']]['index'])) {
  98. $field[$v['field']]['index'][] = $k;
  99. }
  100. } else {
  101. $field[$k] = $v;
  102. }
  103. }
  104. }
  105. foreach ($value as $k => &$v) {
  106. $this->setInfo($v, $data, $remote, $show, $source, $default, 2, $field);
  107. $option[$k] = $data['option'];
  108. }
  109. if (isset($data['reset'])) {
  110. foreach ($data['reset'] as $k1 => $v1) {
  111. if (isset($data['option'][$k1])) {
  112. unset($data['option'][$k1]);
  113. $data['field'][$k1] = $v1;
  114. }
  115. }
  116. }
  117. } else {
  118. if ($remote) {
  119. $info = [];
  120. foreach ($remote as $k => $v) {
  121. $data['update'][$v[0]]['remote'] = Dever::url($v[1], ['value' => '', 'table' => $this->db->config['load'], 'id' => false]);
  122. if (isset($v[2]) && isset($data['option'][$v[2]]) && $data['option'][$v[2]] && $m = Dever::issets($data['option'][$v[2]][0], 'id')) {
  123. $result = Dever::call($v[1], [$m, $this->db->config['load'], false]);
  124. if ($result) {
  125. $this->setUpdate($info, $data, $result);
  126. }
  127. }
  128. }
  129. }
  130. if ($show) {
  131. foreach ($show as $k => $v) {
  132. $data['update'][$v[0]]['show'] = true;
  133. }
  134. }
  135. if ($default) {
  136. foreach ($default as $k => $v) {
  137. $data['update'][$v[0]]['value'] = $this->getShow($v[1], []);
  138. $data['field'][$k] = $data['update'][$v[0]]['value'];
  139. }
  140. }
  141. }
  142. $data['desc'] = $this->config['desc'] ?? '';
  143. $data['drag'] = $this->config['drag'] ?? false;
  144. $this->layout($data);
  145. $data['control'] = $this->control($data);
  146. $this->tab($data, 'step');
  147. if (!$data['step']) {
  148. $this->tab($data);
  149. }
  150. return $data;
  151. }
  152. private function setInfo(&$info, &$data, $remote, $show, $source, $default, $type = 1, $field = [])
  153. {
  154. if ($source) {
  155. foreach ($source as $k => $v) {
  156. $t = [];
  157. foreach ($v as $v1) {
  158. $t[] = $info[$v1] ?? '';
  159. }
  160. $info[$k] = implode(',', $t);
  161. }
  162. }
  163. foreach ($info as $k => $v) {
  164. if ($v === null) {
  165. $v = '';
  166. }
  167. if (isset($data['field'][$k])) {
  168. if (is_array($data['field'][$k])) {
  169. if ($v) {
  170. $v = explode(',', $v);
  171. foreach ($v as $k1 => $v1) {
  172. if (is_numeric($v1)) {
  173. $v[$k1] = (float) $v1;
  174. }
  175. }
  176. } else {
  177. $v = [];
  178. }
  179. $info[$k] = $v;
  180. }
  181. # 处理一下select,后续优化
  182. if (isset($field[$k]) && isset($field[$k]['type']) && $field[$k]['type'] == 'select' && !$v) {
  183. $v = $info[$k] = '';
  184. }
  185. if (isset($field[$k]) && isset($field[$k]['update'])) {
  186. $v = $field[$k]['update'];
  187. }
  188. if ($k == 'cdate') {
  189. $field[$k]['type'] = 'date';
  190. }
  191. if (isset($field[$k]) && isset($field[$k]['type']) && $field[$k]['type'] == 'date' && $v) {
  192. $v = date('Y-m-d H:i:s', $v);
  193. }
  194. if ($type == 1) {
  195. $data['field'][$k] = $v;
  196. }
  197. if (isset($remote[$k])) {
  198. $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1], ['value' => '', 'table' => $this->db->config['load'], 'id' => false]);
  199. if ($field[$k]['type'] == 'cascader' && !isset($field[$k]['option'])) {
  200. } else {
  201. $result = Dever::call($remote[$k][1], [$v, $this->db->config['load'], $info['id'] ?? false]);
  202. if ($result) {
  203. $this->setUpdate($info, $data, $result);
  204. }
  205. }
  206. }
  207. if (isset($show[$k])) {
  208. $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info);
  209. $info[$k] = $data['update'][$show[$k][0]]['show'];
  210. }
  211. }
  212. if (isset($field[$k]) && isset($field[$k]['index'])) {
  213. foreach ($field[$k]['index'] as $v1) {
  214. $info[$v1] = $v;
  215. }
  216. }
  217. }
  218. if ($default) {
  219. foreach ($default as $k => $v) {
  220. $data['update'][$v[0]]['value'] = $this->getShow($v[1], $info);
  221. $info[$k] = $data['field'][$k] = $data['update'][$v[0]]['value'];
  222. }
  223. }
  224. }
  225. private function setUpdate(&$info, &$data, $result, $remote = [])
  226. {
  227. foreach ($data['update'] as $k => $v) {
  228. if (isset($result[$v['key']])) {
  229. # 批量更新时,默认数据需要重置
  230. if (!isset($data['reset'][$v['key']])) {
  231. $data['reset'][$v['key']] = $data['field'][$v['key']];
  232. }
  233. if (isset($result[$v['key']]['option'])) {
  234. $data['option'][$v['key']] = $result[$v['key']]['option'];
  235. unset($result[$v['key']]['option']);
  236. }
  237. if (empty($data['field'][$v['key']]) && isset($result[$v['key']]['value'])) {
  238. if (is_array($data['field'][$v['key']]) && !is_array($result[$v['key']]['value'])) {
  239. $data['field'][$v['key']] = explode(',', $result[$v['key']]['value']);
  240. } else {
  241. $data['field'][$v['key']] = $result[$v['key']]['value'];
  242. }
  243. }
  244. if (isset($result[$v['key']]['set']) && $info) {
  245. $info = array_merge($info, $result[$v['key']]['set']);
  246. }
  247. $data['update'][$k] = array_merge($data['update'][$k], $result[$v['key']]);
  248. }
  249. }
  250. }
  251. private function control(&$data)
  252. {
  253. $result = [];
  254. if (isset($this->config['control']) && $this->config['control']) {
  255. foreach ($this->config['control'] as $k => $v) {
  256. if (is_string($v)) {
  257. parse_str($v, $v);
  258. }
  259. if (strstr($k, ',')) {
  260. $k = explode(',', $k);
  261. foreach ($k as $k2) {
  262. if (isset($data['update'])) $this->controlShow($data, $k2, $v);
  263. $result[$k2] = $v;
  264. }
  265. } else {
  266. if (isset($data['update'])) $this->controlShow($data, $k, $v);
  267. $result[$k] = $v;
  268. }
  269. }
  270. }
  271. return $result;
  272. }
  273. private function controlShow(&$data, $k, $v)
  274. {
  275. foreach ($data['update'] as $k1 => $v1) {
  276. if ($v1['key'] == $k) {
  277. $show = true;
  278. foreach ($v as $k2 => $v2) {
  279. if (is_array($v2)) {
  280. $temp = false;
  281. foreach ($v2 as $k3 => $v3) {
  282. if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) {
  283. $temp = true;
  284. } elseif ($data['field'][$k2] == $v3) {
  285. $temp = true;
  286. }
  287. }
  288. $show = $temp;
  289. } else {
  290. if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) {
  291. $show = false;
  292. } elseif ($data['field'][$k2] != $v2) {
  293. $show = false;
  294. }
  295. }
  296. }
  297. $data['update'][$k1]['show'] = $show;
  298. if ($show == false && isset($data['update'][$k1]['field'])) {
  299. # 对重新命名的字段删除
  300. if ($data['update'][$k1]['type'] == 'editor') {
  301. $data['field'][$v1['key']] = '';
  302. } else {
  303. unset($data['field'][$v1['key']]);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. private function tab(&$data, $type = 'tab')
  310. {
  311. $field = $this->input('field', '');
  312. $data[$type] = [];
  313. //if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  314. if (empty($data['layout']) && isset($this->config[$type])) {
  315. foreach ($this->config[$type] as $k => $v) {
  316. if (is_string($v)) {
  317. $field = [];
  318. $data[$type][] = array
  319. (
  320. 'name' => $k,
  321. 'update' => $this->getUpdate($v, $data['update'], $field),
  322. 'field' => $field,
  323. );
  324. } else {
  325. $field = [];
  326. $result = [];
  327. $result['name'] = $k;
  328. foreach ($v as $v1) {
  329. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  330. }
  331. $result['field'] = $field;
  332. $data[$type][] = $result;
  333. }
  334. }
  335. $data['update'] = [];
  336. }
  337. }
  338. private function layout(&$data)
  339. {
  340. $field = $this->input('field', '');
  341. $data['layout'] = [];
  342. if (isset($this->config['layout'])) {
  343. foreach ($this->config['layout'] as $k => $v) {
  344. $field = [];
  345. $data['layout'][] = $this->getUpdate($v, $data['update'], $field, '100%');
  346. }
  347. $data['update'] = [];
  348. }
  349. }
  350. private function getUpdate($set, $update, &$field, $width = '')
  351. {
  352. $result = [];
  353. if (is_string($set)) {
  354. $set = explode(',', $set);
  355. foreach ($set as $k => $v) {
  356. foreach ($update as $value) {
  357. if ($value['key'] == $v) {
  358. $width && $value['width'] = $width;
  359. $result[] = $value;
  360. $field[] = $v;
  361. }
  362. }
  363. }
  364. } else {
  365. foreach ($set as $k => $v) {
  366. foreach ($update as $value) {
  367. if ($value['key'] == $k) {
  368. $width && $value['width'] = $width;
  369. $result[] = array('span' => $v, 'update' => [$value]);
  370. $field[] = $k;
  371. }
  372. }
  373. }
  374. }
  375. return $result;
  376. }
  377. public function do_commit(){}
  378. public function do()
  379. {
  380. $this->checkFunc();
  381. $update = [];
  382. $this->setting('field', $update, true, 'text');
  383. if (empty($this->config['upAdmin'])) {
  384. $this->config['upAdmin'] = false;
  385. }
  386. if ($update) {
  387. $data = $other = $sku = [];
  388. $input = base64_decode(Dever::input('data'));
  389. $input = Dever::json_decode($input);
  390. $id = false;
  391. if (isset($input['id']) && $input['id'] > 0) {
  392. $id = $input['id'];
  393. }
  394. $control = $this->control($data);
  395. foreach ($update as $k => $v) {
  396. if (isset($input[$v['key']])) {
  397. if (isset($v['rules'])) {
  398. $this->checkRules($v, $input[$v['key']]);
  399. }
  400. if ($v['type'] == 'sku') {
  401. if (isset($input[$v['key'] . '_spec']) && isset($input[$v['key']])) {
  402. $sku[$v['key']] = [$v['where'], $v['content']['field'], $v['spec'], $v['spec_field'], $input[$v['key'] . '_spec'], $input[$v['key']]];
  403. }
  404. } elseif (strpos($v['key'], '/') && $v['type'] != 'hidden') {
  405. if (isset($v['field'])) {
  406. $value = $input[$v['key']] ?? false;
  407. if (strpos($v['key'], '#')) {
  408. $v['key'] = str_replace('#', '', $v['key']);
  409. }
  410. $other_id = $input[$v['key'] . '_id'] ?? 0;
  411. $value = array
  412. (
  413. 0 => ['id' => $other_id, $v['field'] => $value]
  414. );
  415. if (isset($other[$v['key']])) {
  416. $other[$v['key']][3][0] += $value[0];
  417. } else {
  418. $other[$v['key']] = [$v['where'], false, false, $value];
  419. }
  420. } else {
  421. $other[$v['key']] = [$v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]];
  422. }
  423. } else {
  424. $this->doData($data, $v['key'], $input[$v['key']], $this->config['field'], $control);
  425. }
  426. } elseif ($id) {
  427. $data[$v['key']] = '';
  428. }
  429. if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
  430. unset($data[$v['key']]);
  431. }
  432. }
  433. if (!$data && !$other && !$sku) {
  434. Dever::error('无效数据');
  435. }
  436. if ($data) {
  437. if (isset($this->config['check']) && $this->config['check']) {
  438. $this->exists($this->db, $this->config['check'], $id, $data, $this->config['field']);
  439. }
  440. $result = $this->start($id, $data);
  441. if ($result == 'end') {
  442. return ['msg' => '操作成功', 'upAdmin' => $this->config['upAdmin']];
  443. }
  444. if ($id) {
  445. $info = $this->db->find($id);
  446. if ($info) {
  447. $state = $this->db->update($info['id'], $data);
  448. if ($state) {
  449. $id = $info['id'];
  450. }
  451. } else {
  452. $data['id'] = $id;
  453. $id = $this->db->insert($data);
  454. }
  455. } else {
  456. $id = $this->db->insert($data);
  457. }
  458. }
  459. if (!$id) {
  460. Dever::error('操作失败');
  461. }
  462. $this->other($id, $data, $other);
  463. $this->sku($id, $data, $sku);
  464. $this->end($id, $data);
  465. return ['msg' => '操作成功', 'upAdmin' => $this->config['upAdmin']];
  466. }
  467. }
  468. private function doData(&$data, $key, $value, $field = [], $control = [])
  469. {
  470. if (is_array($value)) {
  471. # 用最傻的办法做,往往是最好的。。
  472. if (isset($value[0]) && !is_array($value[0])) {
  473. $value = implode(',', $value);
  474. } else {
  475. $value = Dever::json_encode($value);
  476. }
  477. }
  478. if (isset($field[$key]) && isset($field[$key]['field'])) {
  479. if ($control && isset($control[$key])) {
  480. # 如果有重命名字段并且是控制项,需要单独设置
  481. $state = true;
  482. foreach ($control[$key] as $k => $v) {
  483. if ($data[$k] != $v) {
  484. //$state = false;
  485. }
  486. }
  487. if ($state) {
  488. $key = $field[$key]['field'];
  489. }
  490. } else {
  491. $key = $field[$key]['field'];
  492. }
  493. }
  494. if ($value && isset($field[$key]) && $handle = Dever::issets($field[$key], 'handle')) {
  495. $value = Dever::call($handle, [$value]);
  496. if (is_array($value) && isset($value[$key])) {
  497. foreach ($value as $k => $v) {
  498. $data[$k] = trim($v);
  499. }
  500. return;
  501. }
  502. } elseif (isset($field[$key]) && isset($field[$key]['type']) && $field[$key]['type'] == 'date' && $value) {
  503. $value = \Dever\Helper\Date::mktime($value);
  504. }
  505. /*
  506. if (empty($data[$key])) {
  507. $data[$key] = trim($value);
  508. }
  509. */
  510. $data[$key] = trim($value);
  511. }
  512. private function exists($db, $check, $id, $data, $field)
  513. {
  514. $check = explode(',', $check);
  515. $where = [];
  516. $name = [];
  517. foreach ($check as $k => $v) {
  518. if (isset($data[$v]) && $data[$v]) {
  519. if (isset($field[$v]) && isset($field[$v]['name'])) {
  520. $n = $field[$v]['name'];
  521. } elseif (isset($db->config['struct'][$v])) {
  522. $n = $db->config['struct'][$v]['name'];
  523. } else {
  524. $n = $v;
  525. }
  526. $where[$v] = $data[$v];
  527. $name[] = $n;
  528. }
  529. }
  530. if ($where) {
  531. if ($id) {
  532. $where['id'] = ['!=', $id];
  533. }
  534. $info = $db->find($where);
  535. if ($info) {
  536. $name = implode('、', $name);
  537. Dever::error($name . '已存在');
  538. }
  539. }
  540. }
  541. private function start($id, &$data)
  542. {
  543. if (isset($this->config['start']) && $this->config['start']) {
  544. $data['id'] = $id;
  545. if (is_array($this->config['start'])) {
  546. $result = $data;
  547. foreach ($this->config['start'] as $k => $v) {
  548. $result = Dever::call($v, [$this->db, $result]);
  549. }
  550. } else {
  551. $result = Dever::call($this->config['start'], [$this->db, $data]);
  552. }
  553. if ($result) {
  554. if ($result == 'end') {
  555. return $result;
  556. }
  557. if (is_object($result)) {
  558. $this->db = $result;
  559. } else {
  560. $data = $result;
  561. }
  562. }
  563. }
  564. }
  565. private function end($id, $data)
  566. {
  567. if (isset($this->config['end']) && $this->config['end']) {
  568. $data['id'] = $id;
  569. if (is_array($this->config['end'])) {
  570. foreach ($this->config['end'] as $k => $v) {
  571. Dever::call($v, [$this->db, $data]);
  572. }
  573. } else {
  574. Dever::call($this->config['end'], [$this->db, $data]);
  575. }
  576. }
  577. }
  578. private function other($rid, $data, $other)
  579. {
  580. if ($other) {
  581. foreach ($other as $k => $v) {
  582. if (strpos($k, '#')) {
  583. $k = str_replace('#', '', $k);
  584. }
  585. $set = new Update($k, false);
  586. $common = $v[0];
  587. $update = $v[1];
  588. $drag = $v[2];
  589. $input = $v[3];
  590. $value = [];
  591. foreach ($input as $k1 => $v1) {
  592. if (isset($v1['id']) && $v1['id']) {
  593. $value['id'] = $v1['id'];
  594. }
  595. foreach ($common as $k2 => $v2) {
  596. if (!is_array($v2)) {
  597. if ($v2 == 'id') {
  598. $value[$k2] = $rid;
  599. } elseif (isset($data[$v2])) {
  600. $value[$k2] = $data[$v2];
  601. } else {
  602. $value[$k2] = $v2;
  603. }
  604. }
  605. }
  606. if ($update) {
  607. foreach ($update as $k2 => $v2) {
  608. if (isset($v1[$k2])) {
  609. $this->doData($value, $k2, $v1[$k2], $set->config['field']);
  610. } else {
  611. $value[$k2] = '';
  612. }
  613. }
  614. } else {
  615. $value += $v1;
  616. }
  617. if ($drag) {
  618. $value[$drag] = $k1+1;
  619. }
  620. $db = Dever::db($k);
  621. if (isset($db->config['manage']['update']['check'])) {
  622. $this->exists($db, $db->config['manage']['update']['check'], $value['id'], $value, $db->config['manage']['update']['field']);
  623. }
  624. if (isset($value['id']) && $value['id'] > 0) {
  625. $id = $value['id'];
  626. unset($value['id']);
  627. $db->update($id, $value);
  628. } else {
  629. $db->insert($value);
  630. }
  631. }
  632. }
  633. }
  634. }
  635. private function sku($rid, $data, $sku)
  636. {
  637. if ($sku) {
  638. if (isset($data['spec_type']) && $data['spec_type'] <= 2) {
  639. return;
  640. }
  641. foreach ($sku as $k => $v) {
  642. if (strpos($k, '#')) {
  643. $k = str_replace('#', '', $k);
  644. }
  645. $common = $v[0];
  646. $update = $v[1];
  647. $spec_table = $v[2];
  648. $spec_value_table = $spec_table . '_value';
  649. $spec_field = $v[3];
  650. $spec = $v[4];
  651. $input = $v[5];
  652. $spec_value = [];
  653. Dever::db($spec_table)->update([$spec_field => $rid], ['state' => 2]);
  654. Dever::db($spec_value_table)->update([$spec_field => $rid], ['state' => 2]);
  655. foreach ($spec as $k1 => &$v1) {
  656. $spec_data = [];
  657. $spec_data['state'] = 1;
  658. $spec_data[$spec_field] = $rid;
  659. $spec_data['name'] = $v1['name'];
  660. $spec_data['sort'] = $k1+1;
  661. if (isset($v1['id']) && $v1['id']) {
  662. Dever::db($spec_table)->update($v1['id'], $spec_data);
  663. } else {
  664. $v1['id'] = Dever::db($spec_table)->insert($spec_data);
  665. }
  666. if ($v1['id']) {
  667. foreach ($v1['value'] as $k2 => &$v2) {
  668. $spec_value_data = [];
  669. $spec_value_data['state'] = 1;
  670. $spec_value_data[$spec_field] = $rid;
  671. $spec_value_data['spec_id'] = $v1['id'];
  672. $spec_value_data['value'] = $v2['value'] ?? $v2['name'];
  673. $spec_value_data['pic'] = $v2['pic'] ?? '';
  674. $spec_value_data['sort'] = $k2+1;
  675. $spec_value_data['is_checked'] = $v2['checked'] == 'true' ? 1 : 2;
  676. if (isset($v2['id']) && $v2['id']) {
  677. Dever::db($spec_value_table)->update($v2['id'], $spec_value_data);
  678. } else {
  679. $v2['id'] = Dever::db($spec_value_table)->insert($spec_value_data);
  680. }
  681. $spec_value[$v1['key']][$spec_value_data['value']] = [$v2['id'], $spec_data['sort']];
  682. }
  683. }
  684. }
  685. Dever::db($spec_table)->delete([$spec_field => $rid, 'state' => 2]);
  686. Dever::db($spec_value_table)->delete([$spec_field => $rid, 'state' => 2]);
  687. Dever::db($k)->update([$spec_field => $rid], ['state' => 2]);
  688. foreach ($input as $k1 => $v1) {
  689. $value = [];
  690. if (isset($v1['id'])) {
  691. $value['id'] = $v1['id'];
  692. }
  693. foreach ($common as $k2 => $v2) {
  694. if (!is_array($v2)) {
  695. if ($v2 == 'id') {
  696. $value[$k2] = $rid;
  697. } elseif (isset($data[$v2])) {
  698. $value[$k2] = $data[$v2];
  699. } else {
  700. $value[$k2] = $v2;
  701. }
  702. }
  703. }
  704. foreach ($update as $k2 => $v2) {
  705. if (isset($v1[$k2])) {
  706. if (is_array($v1[$k2])) {
  707. $v1[$k2] = implode(',', $v1[$k2]);
  708. }
  709. $value[$k2] = $v1[$k2];
  710. }
  711. }
  712. $value['key'] = [];
  713. foreach ($v1 as $k2 => $v2) {
  714. if (isset($spec_value[$k2]) && isset($spec_value[$k2][$v2])) {
  715. $value['key'][$spec_value[$k2][$v2][1]] = $spec_value[$k2][$v2][0];
  716. }
  717. }
  718. if ($value['key']) {
  719. $value['key'] = implode(',' , $value['key']);
  720. }
  721. $value['state'] = 1;
  722. if (isset($value['id']) && $value['id'] > 0) {
  723. $id = $value['id'];
  724. unset($value['id']);
  725. Dever::db($k)->update($id, $value);
  726. } else {
  727. Dever::db($k)->insert($value);
  728. }
  729. }
  730. Dever::db($k)->delete([$spec_field => $rid, 'state' => 2]);
  731. }
  732. }
  733. }
  734. }