Update.php 31 KB

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