Update.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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) {
  57. Dever::error('无操作权限');
  58. }
  59. }
  60. }
  61. } elseif (!$func) {
  62. $func = $this->getFunc('update', '更新', 1);
  63. if (!$func) {
  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. } else {
  138. $v = array();
  139. }
  140. $info[$k] = $v;
  141. }
  142. if (isset($this->config['field'][$k]) && isset($this->config['field'][$k]['update'])) {
  143. $v = $this->config['field'][$k]['update'];
  144. }
  145. if (strstr($k, 'date')) {
  146. $v = date('Y-m-d H:i:s', $v);
  147. }
  148. if ($type == 1) {
  149. $data['field'][$k] = $v;
  150. }
  151. if (isset($remote[$k])) {
  152. $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1]);
  153. $result = Dever::call($remote[$k][1], array($v), 'api');
  154. if ($result) {
  155. $this->setUpdate($info, $data, $result);
  156. }
  157. }
  158. if (isset($show[$k])) {
  159. $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info);
  160. }
  161. }
  162. }
  163. if ($default) {
  164. foreach ($default as $k => $v) {
  165. $data['update'][$v[0]]['value'] = $this->getShow($v[1], $info);
  166. $data['field'][$k] = $this->getShow($v[1], $info);
  167. }
  168. }
  169. }
  170. private function setUpdate(&$info, &$data, $result, $remote = array())
  171. {
  172. foreach ($data['update'] as $k => $v) {
  173. if (isset($result[$v['key']])) {
  174. # 批量更新时,默认数据需要重置
  175. if (!isset($data['reset'][$v['key']])) {
  176. $data['reset'][$v['key']] = $data['field'][$v['key']];
  177. }
  178. if (isset($result[$v['key']]['option'])) {
  179. $data['option'][$v['key']] = $result[$v['key']]['option'];
  180. unset($result[$v['key']]['option']);
  181. }
  182. if (empty($data['field'][$v['key']]) && isset($result[$v['key']]['value'])) {
  183. if (is_array($data['field'][$v['key']]) && !is_array($result[$v['key']]['value'])) {
  184. $data['field'][$v['key']] = explode(',', $result[$v['key']]['value']);
  185. } else {
  186. $data['field'][$v['key']] = $result[$v['key']]['value'];
  187. }
  188. }
  189. if (isset($result[$v['key']]['set']) && $info) {
  190. $info = array_merge($info, $result[$v['key']]['set']);
  191. }
  192. $data['update'][$k] = array_merge($data['update'][$k], $result[$v['key']]);
  193. }
  194. }
  195. }
  196. private function control(&$data)
  197. {
  198. $data['control'] = array();
  199. if (isset($this->config['control']) && $data['control'] = $this->config['control']) {
  200. foreach ($data['control'] as $k => $v) {
  201. foreach ($data['update'] as $k1 => $v1) {
  202. if ($v1['key'] == $k) {
  203. $show = true;
  204. foreach ($v as $k2 => $v2) {
  205. if (is_array($v2)) {
  206. $temp = false;
  207. foreach ($v2 as $k3 => $v3) {
  208. if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) {
  209. $temp = true;
  210. } elseif ($data['field'][$k2] == $v3) {
  211. $temp = true;
  212. }
  213. }
  214. $show = $temp;
  215. } else {
  216. if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) {
  217. $show = false;
  218. } elseif ($data['field'][$k2] != $v2) {
  219. $show = false;
  220. }
  221. }
  222. }
  223. $data['update'][$k1]['show'] = $show;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. private function tab(&$data, $type = 'tab')
  230. {
  231. $field = $this->input('field', '');
  232. $data[$type] = array();
  233. if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  234. foreach ($this->config[$type] as $k => $v) {
  235. if (is_string($v)) {
  236. $field = array();
  237. $data[$type][] = array
  238. (
  239. 'name' => $k,
  240. 'update' => $this->getUpdate($v, $data['update'], $field),
  241. 'field' => $field,
  242. );
  243. } else {
  244. $field = array();
  245. $result = array();
  246. $result['name'] = $k;
  247. foreach ($v as $v1) {
  248. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  249. }
  250. $result['field'] = $field;
  251. $data[$type][] = $result;
  252. }
  253. }
  254. $data['update'] = array();
  255. }
  256. }
  257. private function layout(&$data)
  258. {
  259. $field = $this->input('field', '');
  260. $data['layout'] = array();
  261. if (!$field && isset($this->config['layout'])) {
  262. foreach ($this->config['layout'] as $k => $v) {
  263. $field = array();
  264. $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
  265. }
  266. $data['update'] = array();
  267. }
  268. }
  269. private function getUpdate($set, $update, &$field)
  270. {
  271. $result = array();
  272. if (is_string($set)) {
  273. $set = explode(',', $set);
  274. foreach ($set as $k => $v) {
  275. foreach ($update as $value) {
  276. if ($value['key'] == $v) {
  277. $result[] = $value;
  278. $field[] = $v;
  279. }
  280. }
  281. }
  282. } else {
  283. foreach ($set as $k => $v) {
  284. foreach ($update as $value) {
  285. if ($value['key'] == $k) {
  286. $result[] = array('span' => $v, 'update' => array($value));
  287. $field[] = $k;
  288. }
  289. }
  290. }
  291. }
  292. return $result;
  293. }
  294. public function do_commit(){}
  295. public function do()
  296. {
  297. $this->checkFunc();
  298. $update = array();
  299. $this->setting('field', $update, true, 'text');
  300. if ($update) {
  301. $data = $other = $sku = array();
  302. $input = Dever::input();
  303. $id = false;
  304. if (isset($input['id']) && $input['id'] > 0) {
  305. $id = $input['id'];
  306. }
  307. foreach ($update as $k => $v) {
  308. if (isset($input[$v['key']])) {
  309. if (isset($v['rules'])) {
  310. $this->checkRules($v, $input[$v['key']]);
  311. }
  312. if ($v['type'] == 'sku') {
  313. $sku[$v['key']] = array($v['where'], $v['content']['field'], $v['spec'], $v['spec_field'], $input[$v['key'] . '_spec'], $input[$v['key']]);
  314. } elseif (strpos($v['key'], '/')) {
  315. $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]);
  316. } else {
  317. $this->doData($data, $v['key'], $input[$v['key']]);
  318. }
  319. } elseif ($id) {
  320. $data[$v['key']] = '';
  321. }
  322. if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
  323. unset($data[$v['key']]);
  324. }
  325. }
  326. if (!$data && !$other && !$sku) {
  327. Dever::error('无效数据');
  328. }
  329. if ($data) {
  330. $this->exists($id, $data);
  331. $this->start($id, $data);
  332. if ($id) {
  333. $info = $this->db->find($id);
  334. if ($info) {
  335. $state = $this->db->update($info['id'], $data);
  336. if ($state) {
  337. $id = $info['id'];
  338. }
  339. } else {
  340. $data['id'] = $id;
  341. $id = $this->db->insert($data);
  342. }
  343. } else {
  344. $id = $this->db->insert($data);
  345. }
  346. }
  347. if (!$id) {
  348. Dever::error('操作失败');
  349. }
  350. $this->other($id, $data, $other);
  351. $this->sku($id, $data, $sku);
  352. $this->end($id, $data);
  353. return '操作成功';
  354. }
  355. }
  356. private function doData(&$data, $key, $value)
  357. {
  358. if (is_array($value)) {
  359. if (isset($value[0])) {
  360. $value = ltrim(implode(',', $value), ',');
  361. } else {
  362. $value = Dever::json_encode($value);
  363. }
  364. }
  365. if ($value && isset($this->config['field'][$key]) && $handle = Dever::isset($this->config['field'][$key], 'handle')) {
  366. $value = Dever::call($handle, array($value));
  367. if (is_array($value) && isset($value[$key])) {
  368. foreach ($value as $k => $v) {
  369. $data[$k] = trim($v);
  370. }
  371. return;
  372. }
  373. }
  374. $data[$key] = trim($value);
  375. }
  376. private function exists($id, $data)
  377. {
  378. if (isset($this->config['check']) && $this->config['check']) {
  379. $check = explode(',', $this->config['check']);
  380. $where = array();
  381. $name = array();
  382. foreach ($check as $k => $v) {
  383. if (isset($data[$v])) {
  384. if (isset($this->config['field'][$v]) && isset($this->config['field'][$v]['name'])) {
  385. $n = $this->config['field'][$v]['name'];
  386. } elseif (isset($this->db->config['struct'][$v])) {
  387. $n = $this->db->config['struct'][$v]['name'];
  388. } else {
  389. $n = $v;
  390. }
  391. $where[$v] = $data[$v];
  392. $name[] = $n;
  393. }
  394. }
  395. if ($where) {
  396. if ($id) {
  397. $where['id'] = array('!=', $id);
  398. }
  399. $info = $this->db->find($where);
  400. if ($info) {
  401. $name = implode('、', $name);
  402. Dever::error($name . '已存在');
  403. }
  404. }
  405. }
  406. }
  407. private function start($id, &$data)
  408. {
  409. if (isset($this->config['start']) && $this->config['start']) {
  410. $data['id'] = $id;
  411. $data = Dever::call($this->config['start'], array($data));
  412. }
  413. }
  414. private function end($id, $data)
  415. {
  416. if (isset($this->config['end']) && $this->config['end']) {
  417. $data['id'] = $id;
  418. Dever::call($this->config['end'], array($data));
  419. }
  420. }
  421. private function other($rid, $data, $other)
  422. {
  423. if ($other) {
  424. foreach ($other as $k => $v) {
  425. if (strpos($k, '#')) {
  426. $k = str_replace('#', '', $k);
  427. }
  428. $common = $v[0];
  429. $update = $v[1];
  430. $drag = $v[2];
  431. $input = $v[3];
  432. $value = array();
  433. foreach ($input as $k1 => $v1) {
  434. if (isset($v1['id'])) {
  435. $value['id'] = $v1['id'];
  436. }
  437. foreach ($common as $k2 => $v2) {
  438. if (!is_array($v2)) {
  439. if ($v2 == 'id') {
  440. $value[$k2] = $rid;
  441. } elseif (isset($data[$v2])) {
  442. $value[$k2] = $data[$v2];
  443. } else {
  444. $value[$k2] = $v2;
  445. }
  446. }
  447. }
  448. foreach ($update as $k2 => $v2) {
  449. if (isset($v1[$k2])) {
  450. $this->doData($value, $k2, $v1[$k2]);
  451. } else {
  452. $value[$k2] = '';
  453. }
  454. }
  455. if ($drag) {
  456. $value[$drag] = $k1+1;
  457. }
  458. if (isset($value['id']) && $value['id'] > 0) {
  459. $id = $value['id'];
  460. unset($value['id']);
  461. Dever::db($k)->update($id, $value);
  462. } else {
  463. Dever::db($k)->insert($value);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. private function sku($rid, $data, $sku)
  470. {
  471. if ($sku) {
  472. foreach ($sku as $k => $v) {
  473. if (strpos($k, '#')) {
  474. $k = str_replace('#', '', $k);
  475. }
  476. $common = $v[0];
  477. $update = $v[1];
  478. $spec_table = $v[2];
  479. $spec_value_table = $spec_table . '_value';
  480. $spec_field = $v[3];
  481. $spec = $v[4];
  482. $input = $v[5];
  483. $value = $spec_value = array();
  484. Dever::db($spec_table)->update(array($spec_field => $rid), array('state' => 2));
  485. Dever::db($spec_value_table)->update(array($spec_field => $rid), array('state' => 2));
  486. foreach ($spec as $k1 => &$v1) {
  487. $spec_data = array();
  488. $spec_data['state'] = 1;
  489. $spec_data[$spec_field] = $rid;
  490. $spec_data['name'] = $v1['name'];
  491. $spec_data['sort'] = $k1+1;
  492. if (isset($v1['id']) && $v1['id']) {
  493. Dever::db($spec_table)->update($v1['id'], $spec_data);
  494. } else {
  495. $v1['id'] = Dever::db($spec_table)->insert($spec_data);
  496. }
  497. if ($v1['id']) {
  498. foreach ($v1['value'] as $k2 => &$v2) {
  499. $spec_value_data = array();
  500. $spec_value_data['state'] = 1;
  501. $spec_value_data[$spec_field] = $rid;
  502. $spec_value_data['spec_id'] = $v1['id'];
  503. $spec_value_data['value'] = $v2['value'] ?? $v2['name'];
  504. $spec_value_data['sort'] = $k2+1;
  505. $spec_value_data['is_checked'] = $v2['checked'] == 'true' ? 1 : 2;
  506. if (isset($v2['id']) && $v2['id']) {
  507. Dever::db($spec_value_table)->update($v2['id'], $spec_value_data);
  508. } else {
  509. $v2['id'] = Dever::db($spec_value_table)->insert($spec_value_data);
  510. }
  511. $spec_value[$v1['key']][$spec_value_data['value']] = array($v2['id'], $spec_data['sort']);
  512. }
  513. }
  514. }
  515. Dever::db($spec_table)->delete(array($spec_field => $rid, 'state' => 2));
  516. Dever::db($spec_value_table)->delete(array($spec_field => $rid, 'state' => 2));
  517. Dever::db($k)->update(array($spec_field => $rid), array('state' => 2));
  518. foreach ($input as $k1 => $v1) {
  519. if (isset($v1['id'])) {
  520. $value['id'] = $v1['id'];
  521. }
  522. foreach ($common as $k2 => $v2) {
  523. if (!is_array($v2)) {
  524. if ($v2 == 'id') {
  525. $value[$k2] = $rid;
  526. } elseif (isset($data[$v2])) {
  527. $value[$k2] = $data[$v2];
  528. } else {
  529. $value[$k2] = $v2;
  530. }
  531. }
  532. }
  533. foreach ($update as $k2 => $v2) {
  534. if (isset($v1[$k2])) {
  535. $value[$k2] = $v1[$k2];
  536. }
  537. }
  538. $value['key'] = array();
  539. foreach ($v1 as $k2 => $v2) {
  540. if (isset($spec_value[$k2]) && isset($spec_value[$k2][$v2])) {
  541. $value['key'][$spec_value[$k2][$v2][1]] = $spec_value[$k2][$v2][0];
  542. }
  543. }
  544. if ($value['key']) {
  545. $value['key'] = implode(',' , $value['key']);
  546. }
  547. $value['state'] = 1;
  548. if (isset($value['id']) && $value['id'] > 0) {
  549. $id = $value['id'];
  550. unset($value['id']);
  551. Dever::db($k)->update($id, $value);
  552. } else {
  553. Dever::db($k)->insert($value);
  554. }
  555. }
  556. Dever::db($k)->delete(array($spec_field => $rid, 'state' => 2));
  557. }
  558. }
  559. }
  560. }