Update.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 = array(), &$option = array())
  14. {
  15. $func = $this->checkFunc();
  16. $remote = $show = $spec = $source = $default = array();
  17. $data['update'] = $data['field'] = $data['option'] = array();
  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'] = array();
  30. }
  31. }
  32. if (isset($v['source'])) {
  33. $source[$v['key']] = $v['source'];
  34. }
  35. if (isset($v['remote'])) {
  36. $remote[$v['key']] = array($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']] = array($k, $v['spec_data'], $v['key'] . '_spec', $v['spec'], $v['spec_field']);
  43. }
  44. if (isset($v['show']) && is_string($v['show'])) {
  45. $show[$v['key']] = array($k, $v['show']);
  46. }
  47. if (isset($v['default']) && is_string($v['default']) && strstr($v['default'], '{')) {
  48. $default[$v['key']] = array($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'][$spec[$k][0]]['remote'] = Dever::url($spec[$k][1]);
  84. $result = Dever::call($spec[$k][1], array($spec[$k][3], $spec[$k][4], $this->info['id']));
  85. if ($result) {
  86. $data['field'][$spec[$k][2]] = $result;
  87. }
  88. }
  89. }
  90. } elseif ($value) {
  91. $field = array();
  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 = array();
  119. foreach ($remote as $k => $v) {
  120. $data['update'][$v[0]]['remote'] = Dever::url($v[1]);
  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], array($m), 'api');
  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. }
  135. $data['desc'] = $this->config['desc'] ?? '';
  136. $data['drag'] = $this->config['drag'] ?? false;
  137. $this->layout($data);
  138. $this->control($data);
  139. $this->tab($data, 'step');
  140. if (!$data['step']) {
  141. $this->tab($data);
  142. }
  143. return $data;
  144. }
  145. private function setInfo(&$info, &$data, $remote, $show, $source, $default, $type = 1, $field = array())
  146. {
  147. if ($source) {
  148. foreach ($source as $k => $v) {
  149. $t = array();
  150. foreach ($v as $v1) {
  151. $t[] = $info[$v1] ?? '';
  152. }
  153. $info[$k] = implode(',', $t);
  154. }
  155. }
  156. foreach ($info as $k => $v) {
  157. if ($v == null) {
  158. $v = '';
  159. }
  160. if (isset($data['field'][$k])) {
  161. if (is_array($data['field'][$k])) {
  162. if ($v) {
  163. $v = explode(',', $v);
  164. foreach ($v as $k1 => $v1) {
  165. if (is_numeric($v1)) {
  166. $v[$k1] = (float) $v1;
  167. }
  168. }
  169. } else {
  170. $v = array();
  171. }
  172. $info[$k] = $v;
  173. }
  174. if (isset($field[$k]) && isset($field[$k]['update'])) {
  175. $v = $field[$k]['update'];
  176. }
  177. if ($k == 'cdate') {
  178. $field[$k]['type'] = 'date';
  179. }
  180. if (isset($field[$k]) && isset($field[$k]['type']) && $field[$k]['type'] == 'date' && $v) {
  181. $v = date('Y-m-d H:i:s', $v);
  182. }
  183. if ($type == 1) {
  184. $data['field'][$k] = $v;
  185. }
  186. if (isset($remote[$k])) {
  187. $data['update'][$remote[$k][0]]['remote'] = Dever::url($remote[$k][1]);
  188. if ($field[$k]['type'] == 'cascader' && !isset($field[$k]['option'])) {
  189. } else {
  190. $result = Dever::call($remote[$k][1], array($v), 'api');
  191. if ($result) {
  192. $this->setUpdate($info, $data, $result);
  193. }
  194. }
  195. }
  196. if (isset($show[$k])) {
  197. $data['update'][$show[$k][0]]['show'] = $this->getShow($show[$k][1], $info);
  198. $info[$k] = $data['update'][$show[$k][0]]['show'];
  199. }
  200. }
  201. if (isset($field[$k]) && isset($field[$k]['index'])) {
  202. foreach ($field[$k]['index'] as $v1) {
  203. $info[$v1] = $v;
  204. }
  205. }
  206. }
  207. if ($default) {
  208. foreach ($default as $k => $v) {
  209. $data['update'][$v[0]]['value'] = $this->getShow($v[1], $info);
  210. $info[$k] = $data['field'][$k] = $data['update'][$v[0]]['value'];
  211. }
  212. }
  213. }
  214. private function setUpdate(&$info, &$data, $result, $remote = array())
  215. {
  216. foreach ($data['update'] as $k => $v) {
  217. if (isset($result[$v['key']])) {
  218. # 批量更新时,默认数据需要重置
  219. if (!isset($data['reset'][$v['key']])) {
  220. $data['reset'][$v['key']] = $data['field'][$v['key']];
  221. }
  222. if (isset($result[$v['key']]['option'])) {
  223. $data['option'][$v['key']] = $result[$v['key']]['option'];
  224. unset($result[$v['key']]['option']);
  225. }
  226. if (empty($data['field'][$v['key']]) && isset($result[$v['key']]['value'])) {
  227. if (is_array($data['field'][$v['key']]) && !is_array($result[$v['key']]['value'])) {
  228. $data['field'][$v['key']] = explode(',', $result[$v['key']]['value']);
  229. } else {
  230. $data['field'][$v['key']] = $result[$v['key']]['value'];
  231. }
  232. }
  233. if (isset($result[$v['key']]['set']) && $info) {
  234. $info = array_merge($info, $result[$v['key']]['set']);
  235. }
  236. $data['update'][$k] = array_merge($data['update'][$k], $result[$v['key']]);
  237. }
  238. }
  239. }
  240. private function control(&$data)
  241. {
  242. $data['control'] = array();
  243. if (isset($this->config['control']) && $data['control'] = $this->config['control']) {
  244. foreach ($data['control'] as $k => $v) {
  245. foreach ($data['update'] as $k1 => $v1) {
  246. if ($v1['key'] == $k) {
  247. $show = true;
  248. foreach ($v as $k2 => $v2) {
  249. if (is_array($v2)) {
  250. $temp = false;
  251. foreach ($v2 as $k3 => $v3) {
  252. if (is_array($data['field'][$k2]) && in_array($v3, $data['field'][$k2])) {
  253. $temp = true;
  254. } elseif ($data['field'][$k2] == $v3) {
  255. $temp = true;
  256. }
  257. }
  258. $show = $temp;
  259. } else {
  260. if (is_array($data['field'][$k2]) && !in_array($v2, $data['field'][$k2])) {
  261. $show = false;
  262. } elseif ($data['field'][$k2] != $v2) {
  263. $show = false;
  264. }
  265. }
  266. }
  267. $data['update'][$k1]['show'] = $show;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. private function tab(&$data, $type = 'tab')
  274. {
  275. $field = $this->input('field', '');
  276. $data[$type] = array();
  277. //if (empty($data['layout']) && !$field && isset($this->config[$type])) {
  278. if (empty($data['layout']) && isset($this->config[$type])) {
  279. foreach ($this->config[$type] as $k => $v) {
  280. if (is_string($v)) {
  281. $field = array();
  282. $data[$type][] = array
  283. (
  284. 'name' => $k,
  285. 'update' => $this->getUpdate($v, $data['update'], $field),
  286. 'field' => $field,
  287. );
  288. } else {
  289. $field = array();
  290. $result = array();
  291. $result['name'] = $k;
  292. foreach ($v as $v1) {
  293. $result['layout'][] = $this->getUpdate($v1, $data['update'], $field);
  294. }
  295. $result['field'] = $field;
  296. $data[$type][] = $result;
  297. }
  298. }
  299. $data['update'] = array();
  300. }
  301. }
  302. private function layout(&$data)
  303. {
  304. $field = $this->input('field', '');
  305. $data['layout'] = array();
  306. if (!$field && isset($this->config['layout'])) {
  307. foreach ($this->config['layout'] as $k => $v) {
  308. $field = array();
  309. $data['layout'][] = $this->getUpdate($v, $data['update'], $field);
  310. }
  311. $data['update'] = array();
  312. }
  313. }
  314. private function getUpdate($set, $update, &$field)
  315. {
  316. $result = array();
  317. if (is_string($set)) {
  318. $set = explode(',', $set);
  319. foreach ($set as $k => $v) {
  320. foreach ($update as $value) {
  321. if ($value['key'] == $v) {
  322. $result[] = $value;
  323. $field[] = $v;
  324. }
  325. }
  326. }
  327. } else {
  328. foreach ($set as $k => $v) {
  329. foreach ($update as $value) {
  330. if ($value['key'] == $k) {
  331. $result[] = array('span' => $v, 'update' => array($value));
  332. $field[] = $k;
  333. }
  334. }
  335. }
  336. }
  337. return $result;
  338. }
  339. public function do_commit(){}
  340. public function do()
  341. {
  342. $this->checkFunc();
  343. $update = array();
  344. $this->setting('field', $update, true, 'text');
  345. if ($update) {
  346. $data = $other = $sku = array();
  347. $input = Dever::input();
  348. $id = false;
  349. if (isset($input['id']) && $input['id'] > 0) {
  350. $id = $input['id'];
  351. }
  352. foreach ($update as $k => $v) {
  353. if (isset($input[$v['key']])) {
  354. if (isset($v['rules'])) {
  355. $this->checkRules($v, $input[$v['key']]);
  356. }
  357. if ($v['type'] == 'sku') {
  358. if (isset($input[$v['key'] . '_spec']) && isset($input[$v['key']])) {
  359. $sku[$v['key']] = array($v['where'], $v['content']['field'], $v['spec'], $v['spec_field'], $input[$v['key'] . '_spec'], $input[$v['key']]);
  360. }
  361. } elseif (strpos($v['key'], '/') && $v['type'] != 'hidden') {
  362. if (isset($v['field'])) {
  363. $value = $input[$v['key']] ?? false;
  364. if (strpos($v['key'], '#')) {
  365. $v['key'] = str_replace('#', '', $v['key']);
  366. }
  367. $other_id = $input[$v['key'] . '_id'] ?? 0;
  368. $value = array
  369. (
  370. 0 => array('id' => $other_id, $v['field'] => $value)
  371. );
  372. if (isset($other[$v['key']])) {
  373. $other[$v['key']][3][0] += $value[0];
  374. } else {
  375. $other[$v['key']] = array($v['where'], false, false, $value);
  376. }
  377. } else {
  378. $other[$v['key']] = array($v['where'], $v['content']['field'], $v['content']['drag'], $input[$v['key']]);
  379. }
  380. } else {
  381. $this->doData($data, $v['key'], $input[$v['key']], $this->config['field']);
  382. }
  383. } elseif ($id) {
  384. $data[$v['key']] = '';
  385. }
  386. if (isset($data[$v['key']]) && !$data[$v['key']] && isset($v['empty']) && !$v['empty']) {
  387. unset($data[$v['key']]);
  388. }
  389. }
  390. if (!$data && !$other && !$sku) {
  391. Dever::error('无效数据');
  392. }
  393. if ($data) {
  394. $this->exists($id, $data, $this->config['field']);
  395. $result = $this->start($id, $data);
  396. if ($result == 'end') {
  397. return '操作成功';
  398. }
  399. if ($id) {
  400. $info = $this->db->find($id);
  401. if ($info) {
  402. $state = $this->db->update($info['id'], $data);
  403. if ($state) {
  404. $id = $info['id'];
  405. }
  406. } else {
  407. $data['id'] = $id;
  408. $id = $this->db->insert($data);
  409. }
  410. } else {
  411. $id = $this->db->insert($data);
  412. }
  413. }
  414. if (!$id) {
  415. Dever::error('操作失败');
  416. }
  417. $this->other($id, $data, $other);
  418. $this->sku($id, $data, $sku);
  419. $this->end($id, $data);
  420. return '操作成功';
  421. }
  422. }
  423. private function doData(&$data, $key, $value, $field = array())
  424. {
  425. if (is_array($value)) {
  426. if (isset($value[0])) {
  427. $value = ltrim(implode(',', $value), ',');
  428. } else {
  429. $value = Dever::json_encode($value);
  430. }
  431. }
  432. if (isset($field[$key]) && isset($field[$key]['field'])) {
  433. $key = $field[$key]['field'];
  434. }
  435. if ($value && isset($field[$key]) && $handle = Dever::issets($field[$key], 'handle')) {
  436. $value = Dever::call($handle, array($value));
  437. if (is_array($value) && isset($value[$key])) {
  438. foreach ($value as $k => $v) {
  439. $data[$k] = trim($v);
  440. }
  441. return;
  442. }
  443. } elseif (isset($field[$key]) && isset($field[$key]['type']) && $field[$key]['type'] == 'date' && $value) {
  444. $value = \Dever\Helper\Date::mktime($value);
  445. }
  446. $data[$key] = trim($value);
  447. }
  448. private function exists($id, $data, $field)
  449. {
  450. if (isset($this->config['check']) && $this->config['check']) {
  451. $check = explode(',', $this->config['check']);
  452. $where = array();
  453. $name = array();
  454. foreach ($check as $k => $v) {
  455. if (isset($data[$v]) && $data[$v]) {
  456. if (isset($field[$v]) && isset($field[$v]['name'])) {
  457. $n = $field[$v]['name'];
  458. } elseif (isset($this->db->config['struct'][$v])) {
  459. $n = $this->db->config['struct'][$v]['name'];
  460. } else {
  461. $n = $v;
  462. }
  463. $where[$v] = $data[$v];
  464. $name[] = $n;
  465. }
  466. }
  467. if ($where) {
  468. if ($id) {
  469. $where['id'] = array('!=', $id);
  470. }
  471. $info = $this->db->find($where);
  472. if ($info) {
  473. $name = implode('、', $name);
  474. Dever::error($name . '已存在');
  475. }
  476. }
  477. }
  478. }
  479. private function start($id, &$data)
  480. {
  481. if (isset($this->config['start']) && $this->config['start']) {
  482. $data['id'] = $id;
  483. if (is_array($this->config['start'])) {
  484. $result = $data;
  485. foreach ($this->config['start'] as $k => $v) {
  486. $result = Dever::call($v, array($this->db, $result));
  487. }
  488. } else {
  489. $result = Dever::call($this->config['start'], array($this->db, $data));
  490. }
  491. if ($result) {
  492. if ($result == 'end') {
  493. return $result;
  494. }
  495. if (is_object($result)) {
  496. $this->db = $result;
  497. } else {
  498. $data = $result;
  499. }
  500. }
  501. }
  502. }
  503. private function end($id, $data)
  504. {
  505. if (isset($this->config['end']) && $this->config['end']) {
  506. $data['id'] = $id;
  507. if (is_array($this->config['end'])) {
  508. foreach ($this->config['end'] as $k => $v) {
  509. Dever::call($v, array($this->db, $data));
  510. }
  511. } else {
  512. Dever::call($this->config['end'], array($this->db, $data));
  513. }
  514. }
  515. }
  516. private function other($rid, $data, $other)
  517. {
  518. if ($other) {
  519. foreach ($other as $k => $v) {
  520. if (strpos($k, '#')) {
  521. $k = str_replace('#', '', $k);
  522. }
  523. $set = new Update($k, false);
  524. $common = $v[0];
  525. $update = $v[1];
  526. $drag = $v[2];
  527. $input = $v[3];
  528. $value = array();
  529. foreach ($input as $k1 => $v1) {
  530. if (isset($v1['id']) && $v1['id']) {
  531. $value['id'] = $v1['id'];
  532. }
  533. foreach ($common as $k2 => $v2) {
  534. if (!is_array($v2)) {
  535. if ($v2 == 'id') {
  536. $value[$k2] = $rid;
  537. } elseif (isset($data[$v2])) {
  538. $value[$k2] = $data[$v2];
  539. } else {
  540. $value[$k2] = $v2;
  541. }
  542. }
  543. }
  544. if ($update) {
  545. foreach ($update as $k2 => $v2) {
  546. if (isset($v1[$k2])) {
  547. $this->doData($value, $k2, $v1[$k2], $set->config['field']);
  548. } else {
  549. $value[$k2] = '';
  550. }
  551. }
  552. } else {
  553. $value += $v1;
  554. }
  555. if ($drag) {
  556. $value[$drag] = $k1+1;
  557. }
  558. if (isset($value['id']) && $value['id'] > 0) {
  559. $id = $value['id'];
  560. unset($value['id']);
  561. Dever::db($k)->update($id, $value);
  562. } else {
  563. Dever::db($k)->insert($value);
  564. }
  565. }
  566. }
  567. }
  568. }
  569. private function sku($rid, $data, $sku)
  570. {
  571. if ($sku) {
  572. if (isset($data['spec_type']) && $data['spec_type'] <= 2) {
  573. return;
  574. }
  575. foreach ($sku as $k => $v) {
  576. if (strpos($k, '#')) {
  577. $k = str_replace('#', '', $k);
  578. }
  579. $common = $v[0];
  580. $update = $v[1];
  581. $spec_table = $v[2];
  582. $spec_value_table = $spec_table . '_value';
  583. $spec_field = $v[3];
  584. $spec = $v[4];
  585. $input = $v[5];
  586. $value = $spec_value = array();
  587. Dever::db($spec_table)->update(array($spec_field => $rid), array('state' => 2));
  588. Dever::db($spec_value_table)->update(array($spec_field => $rid), array('state' => 2));
  589. foreach ($spec as $k1 => &$v1) {
  590. $spec_data = array();
  591. $spec_data['state'] = 1;
  592. $spec_data[$spec_field] = $rid;
  593. $spec_data['name'] = $v1['name'];
  594. $spec_data['sort'] = $k1+1;
  595. if (isset($v1['id']) && $v1['id']) {
  596. Dever::db($spec_table)->update($v1['id'], $spec_data);
  597. } else {
  598. $v1['id'] = Dever::db($spec_table)->insert($spec_data);
  599. }
  600. if ($v1['id']) {
  601. foreach ($v1['value'] as $k2 => &$v2) {
  602. $spec_value_data = array();
  603. $spec_value_data['state'] = 1;
  604. $spec_value_data[$spec_field] = $rid;
  605. $spec_value_data['spec_id'] = $v1['id'];
  606. $spec_value_data['value'] = $v2['value'] ?? $v2['name'];
  607. $spec_value_data['sort'] = $k2+1;
  608. $spec_value_data['is_checked'] = $v2['checked'] == 'true' ? 1 : 2;
  609. if (isset($v2['id']) && $v2['id']) {
  610. Dever::db($spec_value_table)->update($v2['id'], $spec_value_data);
  611. } else {
  612. $v2['id'] = Dever::db($spec_value_table)->insert($spec_value_data);
  613. }
  614. $spec_value[$v1['key']][$spec_value_data['value']] = array($v2['id'], $spec_data['sort']);
  615. }
  616. }
  617. }
  618. Dever::db($spec_table)->delete(array($spec_field => $rid, 'state' => 2));
  619. Dever::db($spec_value_table)->delete(array($spec_field => $rid, 'state' => 2));
  620. Dever::db($k)->update(array($spec_field => $rid), array('state' => 2));
  621. foreach ($input as $k1 => $v1) {
  622. if (isset($v1['id'])) {
  623. $value['id'] = $v1['id'];
  624. }
  625. foreach ($common as $k2 => $v2) {
  626. if (!is_array($v2)) {
  627. if ($v2 == 'id') {
  628. $value[$k2] = $rid;
  629. } elseif (isset($data[$v2])) {
  630. $value[$k2] = $data[$v2];
  631. } else {
  632. $value[$k2] = $v2;
  633. }
  634. }
  635. }
  636. foreach ($update as $k2 => $v2) {
  637. if (isset($v1[$k2])) {
  638. $value[$k2] = $v1[$k2];
  639. }
  640. }
  641. $value['key'] = array();
  642. foreach ($v1 as $k2 => $v2) {
  643. if (isset($spec_value[$k2]) && isset($spec_value[$k2][$v2])) {
  644. $value['key'][$spec_value[$k2][$v2][1]] = $spec_value[$k2][$v2][0];
  645. }
  646. }
  647. if ($value['key']) {
  648. $value['key'] = implode(',' , $value['key']);
  649. }
  650. $value['state'] = 1;
  651. if (isset($value['id']) && $value['id'] > 0) {
  652. $id = $value['id'];
  653. unset($value['id']);
  654. Dever::db($k)->update($id, $value);
  655. } else {
  656. Dever::db($k)->insert($value);
  657. }
  658. }
  659. Dever::db($k)->delete(array($spec_field => $rid, 'state' => 2));
  660. }
  661. }
  662. }
  663. }