Update.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php namespace Manage\Api\Page;
  2. use Dever;
  3. use Manage\Lib\Page;
  4. # 更新页
  5. class Update extends Page
  6. {
  7. public function __construct($load = '', $input = true)
  8. {
  9. parent::__construct('update', $load, $input);
  10. }
  11. public function get(&$value = array(), &$option = array())
  12. {
  13. $func = $this->checkFunc();
  14. $remote = $show = $spec = $source = $default = array();
  15. $data['update'] = $data['field'] = $data['option'] = array();
  16. $this->setting('field', $data['update'], true, 'text');
  17. foreach ($data['update'] as $k => $v) {
  18. if ($v['type'] == 'tree' || $v['type'] == 'upload' || $v['type'] == 'cascader' || $v['type'] == 'checkbox' || isset($v['multiple'])) {
  19. $v['value'] = array();
  20. }
  21. if (isset($v['source'])) {
  22. $source[$v['key']] = $v['source'];
  23. }
  24. if (isset($v['remote'])) {
  25. $remote[$v['key']] = array($k, $v['remote'], $v['key']);
  26. if (isset($v['remote_default']) && !$v['remote_default']) {
  27. unset($remote[$v['key']][2]);
  28. }
  29. }
  30. if (isset($v['spec_data'])) {
  31. $spec[$v['key']] = array($k, $v['spec_data'], $v['key'] . '_spec');
  32. }
  33. if (isset($v['show']) && is_string($v['show'])) {
  34. $show[$v['key']] = array($k, $v['show']);
  35. }
  36. if (isset($v['default']) && is_string($v['default']) && strstr($v['default'], '{')) {
  37. $default[$v['key']] = array($k, $v['default']);
  38. }
  39. $data['field'][$v['key']] = $v['value'];
  40. if ($v['type'] == 'sku') {
  41. $data['field'][$v['key'] . '_spec'] = [];
  42. }
  43. if (isset($v['option'])) {
  44. $data['option'][$v['key']] = $v['option'];
  45. unset($data['update'][$k]['option']);
  46. }
  47. }
  48. $this->column($data);
  49. $data['info_id'] = false;
  50. if (!$this->info && $data['column'] && $data['column']['where'] == 'id') {
  51. $this->info = $this->db->find($data['column']['active']);
  52. if ($this->info) {
  53. $data['info_id'] = $this->info['id'];
  54. if (!$func) {
  55. $func = $this->getFunc('edit', '编辑', 1);
  56. if (!$func && $this->menu && $this->menu['show'] == 1) {
  57. Dever::error('无操作权限');
  58. }
  59. }
  60. }
  61. } elseif (!$func) {
  62. $func = $this->getFunc('update', '更新', 1);
  63. if (!$func && $this->menu && $this->menu['show'] == 1) {
  64. Dever::error('无操作权限');
  65. }
  66. }
  67. if ($this->info) {
  68. $info = $this->info;
  69. $this->setInfo($info, $data, $remote, $show, $source, $default);
  70. if ($spec) {
  71. foreach ($spec as $k => $v) {
  72. $data['update'][$spec[$k][0]]['remote'] = Dever::url($spec[$k][1]);
  73. $result = Dever::call($spec[$k][1], array($this->info['id']));
  74. if ($result) {
  75. $data['field'][$spec[$k][2]] = $result;
  76. }
  77. }
  78. }
  79. } elseif ($value) {
  80. foreach ($value as $k => &$v) {
  81. $this->setInfo($v, $data, $remote, $show, $source, $default, 2);
  82. $option[$k] = $data['option'];
  83. }
  84. if (isset($data['reset'])) {
  85. foreach ($data['reset'] as $k1 => $v1) {
  86. if (isset($data['option'][$k1])) {
  87. unset($data['option'][$k1]);
  88. $data['field'][$k1] = $v1;
  89. }
  90. }
  91. }
  92. } else {
  93. if ($remote) {
  94. $info = array();
  95. foreach ($remote as $k => $v) {
  96. $data['update'][$v[0]]['remote'] = Dever::url($v[1]);
  97. if (isset($v[2]) && isset($data['option'][$v[2]]) && $data['option'][$v[2]] && $m = Dever::isset($data['option'][$v[2]][0], 'id')) {
  98. $result = Dever::call($v[1], array($m), 'api');
  99. if ($result) {
  100. $this->setUpdate($info, $data, $result);
  101. }
  102. }
  103. }
  104. }
  105. if ($show) {
  106. foreach ($show as $k => $v) {
  107. $data['update'][$v[0]]['show'] = true;
  108. }
  109. }
  110. }
  111. $data['desc'] = $this->config['desc'] ?? '';
  112. $data['drag'] = $this->config['drag'] ?? false;
  113. $this->layout($data);
  114. $this->control($data);
  115. $this->tab($data, 'step');
  116. if (!$data['step']) {
  117. $this->tab($data);
  118. }
  119. return $data;
  120. }
  121. private function setInfo(&$info, &$data, $remote, $show, $source, $default, $type = 1)
  122. {
  123. if ($source) {
  124. foreach ($source as $k => $v) {
  125. $t = array();
  126. foreach ($v as $v1) {
  127. $t[] = $info[$v1] ?? '';
  128. }
  129. $info[$k] = implode(',', $t);
  130. }
  131. }
  132. foreach ($info as $k => $v) {
  133. if (isset($data['field'][$k])) {
  134. if (is_array($data['field'][$k])) {
  135. if ($v) {
  136. $v = explode(',', $v);
  137. foreach ($v as $k1 => $v1) {
  138. if (is_numeric($v1)) {
  139. $v[$k1] = (float) $v1;
  140. }
  141. }
  142. } else {
  143. $v = array();
  144. }
  145. $info[$k] = $v;
  146. }
  147. if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['update'])) {
  148. $v = $this->config['field'][$k]['update'];
  149. }
  150. if (strstr($k, 'date')) {
  151. $v = date('Y-m-d H:i:s', $v);
  152. }
  153. if ($type == 1) {
  154. $data['field'][$k] = $v;
  155. }
  156. if (isset($remote[$k])) {
  157. $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1]);
  158. $result = Dever::call($remote[$k][1], array($v), 'api');
  159. if ($result) {
  160. $this->setUpdate($info, $data, $result);
  161. }
  162. }
  163. if (isset($show[$k])) {
  164. $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info);
  165. }
  166. }
  167. }
  168. if ($default) {
  169. foreach ($default as $k => $v) {
  170. $data['update'][$v[0]]['value'] = $this->getShow($v[1], $info);
  171. $data['field'][$k] = $this->getShow($v[1], $info);
  172. }
  173. }
  174. }
  175. private function setUpdate(&$info, &$data, $result, $remote = array())
  176. {
  177. foreach ($data['update'] as $k => $v) {
  178. if (isset($result[$v['key']])) {
  179. # 批量更新时,默认数据需要重置
  180. if (!isset($data['reset'][$v['key']])) {
  181. $data['reset'][$v['key']] = $data['field'][$v['key']];
  182. }
  183. if (isset($result[$v['key']]['option'])) {
  184. $data['option'][$v['key']] = $result[$v['key']]['option'];
  185. unset($result[$v['key']]['option']);
  186. }
  187. if (empty($data['field'][$v['key']]) && isset($result[$v['key']]['value'])) {
  188. if (is_array($data['field'][$v['key']]) && !is_array($result[$v['key']]['value'])) {
  189. $data['field'][$v['key']] = explode(',', $result[$v['key']]['value']);
  190. } else {
  191. $data['field'][$v['key']] = $result[$v['key']]['value'];
  192. }
  193. }
  194. if (isset($result[$v['key']]['set']) && $info) {
  195. $info = array_merge($info, $result[$v['key']]['set']);
  196. }
  197. $data['update'][$k] = array_merge($data['update'][$k], $result[$v['key']]);
  198. }
  199. }
  200. }
  201. private function control(&$data)
  202. {
  203. $data['control'] = array();
  204. if (isset($this->config['control']) && $data['control'] = $this->config['control']) {
  205. foreach ($data['control'] as $k => $v) {
  206. foreach ($data['update'] as $k1 => $v1) {
  207. if ($v1['key'] == $k) {
  208. $show = true;
  209. foreach ($v as $k2 => $v2) {
  210. if (is_array($v2)) {
  211. $temp = false;
  212. foreach ($v2 as $k3 => $v3) {
  213. if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) {
  214. $temp = true;
  215. } elseif ($data['field'][$k2] == $v3) {
  216. $temp = true;
  217. }
  218. }
  219. $show = $temp;
  220. } else {
  221. if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) {
  222. $show = false;
  223. } elseif ($data['field'][$k2] != $v2) {
  224. $show = false;
  225. }
  226. }
  227. }
  228. $data['update'][$k1]['show'] = $show;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. private function tab(&$data, $type = 'tab')
  235. {
  236. $field = $this->input('field', '');
  237. $data[$type] = array();
  238. if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  239. foreach ($this->config[$type] as $k => $v) {
  240. if (is_string($v)) {
  241. $field = array();
  242. $data[$type][] = array
  243. (
  244. 'name' => $k,
  245. 'update' => $this->getUpdate($v, $data['update'], $field),
  246. 'field' => $field,
  247. );
  248. } else {
  249. $field = array();
  250. $result = array();
  251. $result['name'] = $k;
  252. foreach ($v as $v1) {
  253. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  254. }
  255. $result['field'] = $field;
  256. $data[$type][] = $result;
  257. }
  258. }
  259. $data['update'] = array();
  260. }
  261. }
  262. private function layout(&$data)
  263. {
  264. $field = $this->input('field', '');
  265. $data['layout'] = array();
  266. if (!$field && isset($this->config['layout'])) {
  267. foreach ($this->config['layout'] as $k => $v) {
  268. $field = array();
  269. $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
  270. }
  271. $data['update'] = array();
  272. }
  273. }
  274. private function getUpdate($set, $update, &$field)
  275. {
  276. $result = array();
  277. if (is_string($set)) {
  278. $set = explode(',', $set);
  279. foreach ($set as $k => $v) {
  280. foreach ($update as $value) {
  281. if ($value['key'] == $v) {
  282. $result[] = $value;
  283. $field[] = $v;
  284. }
  285. }
  286. }
  287. } else {
  288. foreach ($set as $k => $v) {
  289. foreach ($update as $value) {
  290. if ($value['key'] == $k) {
  291. $result[] = array('span' => $v, 'update' => array($value));
  292. $field[] = $k;
  293. }
  294. }
  295. }
  296. }
  297. return $result;
  298. }
  299. public function do_commit(){}
  300. public function do()
  301. {
  302. $this->checkFunc();
  303. $update = array();
  304. $this->setting('field', $update, true, 'text');
  305. if ($update) {
  306. $data = $other = $sku = array();
  307. $input = Dever::input();
  308. $id = false;
  309. if (isset($input['id']) && $input['id'] > 0) {
  310. $id = $input['id'];
  311. }
  312. foreach ($update as $k => $v) {
  313. if (isset($input[$v['key']])) {
  314. if (isset($v['rules'])) {
  315. $this->checkRules($v, $input[$v['key']]);
  316. }
  317. if ($v['type'] == 'sku') {
  318. $sku[$v['key']] = array($v['where'], $v['content']['field'], $v['spec'], $v['spec_field'], $input[$v['key'] . '_spec'], $input[$v['key']]);
  319. } elseif (strpos($v['key'], '/')) {
  320. $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]);
  321. } else {
  322. $this->doData($data, $v['key'], $input[$v['key']]);
  323. }
  324. } elseif ($id) {
  325. $data[$v['key']] = '';
  326. }
  327. if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
  328. unset($data[$v['key']]);
  329. }
  330. }
  331. if (!$data && !$other && !$sku) {
  332. Dever::error('无效数据');
  333. }
  334. if ($data) {
  335. $this->exists($id, $data);
  336. $this->start($id, $data);
  337. if ($id) {
  338. $info = $this->db->find($id);
  339. if ($info) {
  340. $state = $this->db->update($info['id'], $data);
  341. if ($state) {
  342. $id = $info['id'];
  343. }
  344. } else {
  345. $data['id'] = $id;
  346. $id = $this->db->insert($data);
  347. }
  348. } else {
  349. $id = $this->db->insert($data);
  350. }
  351. }
  352. if (!$id) {
  353. Dever::error('操作失败');
  354. }
  355. $this->other($id, $data, $other);
  356. $this->sku($id, $data, $sku);
  357. $this->end($id, $data);
  358. return '操作成功';
  359. }
  360. }
  361. private function doData(&$data, $key, $value)
  362. {
  363. if (is_array($value)) {
  364. if (isset($value[0])) {
  365. $value = ltrim(implode(',', $value), ',');
  366. } else {
  367. $value = Dever::json_encode($value);
  368. }
  369. }
  370. if ($value && isset($this->config['field'][$key]) && $handle = Dever::isset($this->config['field'][$key], 'handle')) {
  371. $value = Dever::call($handle, array($value));
  372. if (is_array($value) && isset($value[$key])) {
  373. foreach ($value as $k => $v) {
  374. $data[$k] = trim($v);
  375. }
  376. return;
  377. }
  378. }
  379. $data[$key] = trim($value);
  380. }
  381. private function exists($id, $data)
  382. {
  383. if (isset($this->config['check']) && $this->config['check']) {
  384. $check = explode(',', $this->config['check']);
  385. $where = array();
  386. $name = array();
  387. foreach ($check as $k => $v) {
  388. if (isset($data[$v])) {
  389. if (isset($this->config['field'][$v]) && isset($this->config['field'][$v]['name'])) {
  390. $n = $this->config['field'][$v]['name'];
  391. } elseif (isset($this->db->config['struct'][$v])) {
  392. $n = $this->db->config['struct'][$v]['name'];
  393. } else {
  394. $n = $v;
  395. }
  396. $where[$v] = $data[$v];
  397. $name[] = $n;
  398. }
  399. }
  400. if ($where) {
  401. if ($id) {
  402. $where['id'] = array('!=', $id);
  403. }
  404. $info = $this->db->find($where);
  405. if ($info) {
  406. $name = implode('、', $name);
  407. Dever::error($name . '已存在');
  408. }
  409. }
  410. }
  411. }
  412. private function start($id, &$data)
  413. {
  414. if (isset($this->config['start']) && $this->config['start']) {
  415. $data['id'] = $id;
  416. $data = Dever::call($this->config['start'], array($data));
  417. }
  418. }
  419. private function end($id, $data)
  420. {
  421. if (isset($this->config['end']) && $this->config['end']) {
  422. $data['id'] = $id;
  423. Dever::call($this->config['end'], array($data));
  424. }
  425. }
  426. private function other($rid, $data, $other)
  427. {
  428. if ($other) {
  429. foreach ($other as $k => $v) {
  430. if (strpos($k, '#')) {
  431. $k = str_replace('#', '', $k);
  432. }
  433. $common = $v[0];
  434. $update = $v[1];
  435. $drag = $v[2];
  436. $input = $v[3];
  437. $value = array();
  438. foreach ($input as $k1 => $v1) {
  439. if (isset($v1['id'])) {
  440. $value['id'] = $v1['id'];
  441. }
  442. foreach ($common as $k2 => $v2) {
  443. if (!is_array($v2)) {
  444. if ($v2 == 'id') {
  445. $value[$k2] = $rid;
  446. } elseif (isset($data[$v2])) {
  447. $value[$k2] = $data[$v2];
  448. } else {
  449. $value[$k2] = $v2;
  450. }
  451. }
  452. }
  453. foreach ($update as $k2 => $v2) {
  454. if (isset($v1[$k2])) {
  455. $this->doData($value, $k2, $v1[$k2]);
  456. } else {
  457. $value[$k2] = '';
  458. }
  459. }
  460. if ($drag) {
  461. $value[$drag] = $k1+1;
  462. }
  463. if (isset($value['id']) && $value['id'] > 0) {
  464. $id = $value['id'];
  465. unset($value['id']);
  466. Dever::db($k)->update($id, $value);
  467. } else {
  468. Dever::db($k)->insert($value);
  469. }
  470. }
  471. }
  472. }
  473. }
  474. private function sku($rid, $data, $sku)
  475. {
  476. if ($sku) {
  477. foreach ($sku as $k => $v) {
  478. if (strpos($k, '#')) {
  479. $k = str_replace('#', '', $k);
  480. }
  481. $common = $v[0];
  482. $update = $v[1];
  483. $spec_table = $v[2];
  484. $spec_value_table = $spec_table . '_value';
  485. $spec_field = $v[3];
  486. $spec = $v[4];
  487. $input = $v[5];
  488. $value = $spec_value = array();
  489. Dever::db($spec_table)->update(array($spec_field => $rid), array('state' => 2));
  490. Dever::db($spec_value_table)->update(array($spec_field => $rid), array('state' => 2));
  491. foreach ($spec as $k1 => &$v1) {
  492. $spec_data = array();
  493. $spec_data['state'] = 1;
  494. $spec_data[$spec_field] = $rid;
  495. $spec_data['name'] = $v1['name'];
  496. $spec_data['sort'] = $k1+1;
  497. if (isset($v1['id']) && $v1['id']) {
  498. Dever::db($spec_table)->update($v1['id'], $spec_data);
  499. } else {
  500. $v1['id'] = Dever::db($spec_table)->insert($spec_data);
  501. }
  502. if ($v1['id']) {
  503. foreach ($v1['value'] as $k2 => &$v2) {
  504. $spec_value_data = array();
  505. $spec_value_data['state'] = 1;
  506. $spec_value_data[$spec_field] = $rid;
  507. $spec_value_data['spec_id'] = $v1['id'];
  508. $spec_value_data['value'] = $v2['value'] ?? $v2['name'];
  509. $spec_value_data['sort'] = $k2+1;
  510. $spec_value_data['is_checked'] = $v2['checked'] == 'true' ? 1 : 2;
  511. if (isset($v2['id']) && $v2['id']) {
  512. Dever::db($spec_value_table)->update($v2['id'], $spec_value_data);
  513. } else {
  514. $v2['id'] = Dever::db($spec_value_table)->insert($spec_value_data);
  515. }
  516. $spec_value[$v1['key']][$spec_value_data['value']] = array($v2['id'], $spec_data['sort']);
  517. }
  518. }
  519. }
  520. Dever::db($spec_table)->delete(array($spec_field => $rid, 'state' => 2));
  521. Dever::db($spec_value_table)->delete(array($spec_field => $rid, 'state' => 2));
  522. Dever::db($k)->update(array($spec_field => $rid), array('state' => 2));
  523. foreach ($input as $k1 => $v1) {
  524. if (isset($v1['id'])) {
  525. $value['id'] = $v1['id'];
  526. }
  527. foreach ($common as $k2 => $v2) {
  528. if (!is_array($v2)) {
  529. if ($v2 == 'id') {
  530. $value[$k2] = $rid;
  531. } elseif (isset($data[$v2])) {
  532. $value[$k2] = $data[$v2];
  533. } else {
  534. $value[$k2] = $v2;
  535. }
  536. }
  537. }
  538. foreach ($update as $k2 => $v2) {
  539. if (isset($v1[$k2])) {
  540. $value[$k2] = $v1[$k2];
  541. }
  542. }
  543. $value['key'] = array();
  544. foreach ($v1 as $k2 => $v2) {
  545. if (isset($spec_value[$k2]) && isset($spec_value[$k2][$v2])) {
  546. $value['key'][$spec_value[$k2][$v2][1]] = $spec_value[$k2][$v2][0];
  547. }
  548. }
  549. if ($value['key']) {
  550. $value['key'] = implode(',' , $value['key']);
  551. }
  552. $value['state'] = 1;
  553. if (isset($value['id']) && $value['id'] > 0) {
  554. $id = $value['id'];
  555. unset($value['id']);
  556. Dever::db($k)->update($id, $value);
  557. } else {
  558. Dever::db($k)->insert($value);
  559. }
  560. }
  561. Dever::db($k)->delete(array($spec_field => $rid, 'state' => 2));
  562. }
  563. }
  564. }
  565. }