StoreCopy.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\modules\admin\models\GoodsForm;
  9. use app\modules\admin\models\MerchantForm;
  10. use Yii;
  11. use yii\behaviors\TimestampBehavior;
  12. use yii\db\ActiveRecord;
  13. /**
  14. * This is the model class for table "{{%store_cloud}}".
  15. *
  16. * @property integer $id
  17. */
  18. class StoreCopy extends \yii\db\ActiveRecord
  19. {
  20. /**
  21. * @inheritdoc
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%store_copy}}';
  26. }
  27. public function behaviors()
  28. {
  29. return [
  30. [
  31. 'class' => TimestampBehavior::class,
  32. 'attributes' => [
  33. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  34. ]
  35. ]
  36. ];
  37. }
  38. public static function doCopyStatus($id = 0, $doCopy = 0) {
  39. $model = self::findOne($id);
  40. $type = $model->type;
  41. $type = json_decode($type, true);
  42. if($model->finished != 1 && $doCopy){
  43. self::doCopy($model->from_store_id, $model->to_store_id, $type, $id);
  44. }
  45. $model = self::findOne($id);
  46. return [
  47. 'code' => 0,
  48. 'msg' => 'ok',
  49. 'data' => [
  50. 'model' => $model,
  51. ],
  52. ];
  53. }
  54. // type //home/category/product/center/all
  55. public static function doCopy($from_store_id = 0, $to_store_id = 0, $type = ['home'], $id = 0) {
  56. if($from_store_id == $to_store_id){
  57. return [
  58. 'code' => 1,
  59. 'msg' => '不能选择自己店铺的模板',
  60. ];
  61. }
  62. $res = [
  63. 'code' => 0,
  64. 'msg' => 'success',
  65. ];
  66. if($id){
  67. $model = self::findOne($id);
  68. }else{
  69. $type = json_encode($type);
  70. $model = new self();
  71. $model->from_store_id = $from_store_id;
  72. $model->to_store_id = $to_store_id;
  73. $model->type = $type;
  74. $model->save();
  75. }
  76. $res['data'] = $model;
  77. if(!$id){
  78. // return $res;
  79. }
  80. if($model->finished == 1){
  81. return $res;
  82. }
  83. $finished = 1;
  84. $type = $model->type;
  85. $type = json_decode($type, true);
  86. if(!$id){
  87. if(array_intersect(['delete_goods'], $type)){
  88. $copy = $model->doDelGoods($to_store_id);
  89. //debug_log(['doDelGoods' => $finished, 'copy' => $copy], 'store_copy.log');
  90. if(!$copy && $finished){
  91. $finished = 0;
  92. }
  93. }
  94. if(array_intersect(['delete_cat'], $type)){
  95. $copy = $model->doDelCat($to_store_id);
  96. //debug_log(['doDelCat' => $finished, 'copy' => $copy], 'store_copy.log');
  97. if(!$copy && $finished){
  98. $finished = 0;
  99. }
  100. }
  101. }
  102. if(array_intersect(['home', 'all'], $type)){
  103. $copy = $model->doCopyHome();
  104. //debug_log(['doCopyHome' => $finished, 'copy' => $copy], 'store_copy.log');
  105. if(!$copy && $finished){
  106. $finished = 0;
  107. }
  108. }
  109. if(array_intersect(['center', 'all'], $type)){
  110. $copy = $model->doCopyCenter();
  111. //debug_log(['doCopyCenter' => $finished, 'copy' => $copy], 'store_copy.log');
  112. if(!$copy && $finished){
  113. $finished = 0;
  114. }
  115. }
  116. if(array_intersect(['footnav', 'all'], $type)){
  117. $copy = $model->doCopyFootnav();
  118. //debug_log(['doCopyFootnav' => $finished, 'copy' => $copy], 'store_copy.log');
  119. if(!$copy && $finished){
  120. $finished = 0;
  121. }
  122. }
  123. if(array_intersect(['category', 'all'], $type)){
  124. $copy = $model->doCopyCategory();
  125. //debug_log(['doCopyCategory' => $finished, 'copy' => $copy], 'store_copy.log');
  126. if(!$copy && $finished){
  127. $finished = 0;
  128. }
  129. }
  130. if(array_intersect(['product', 'all'], $type)){
  131. $copy = $model->doCopyProduct();
  132. //debug_log(['doCopyProduct' => $finished, 'copy' => $copy], 'store_copy.log');
  133. if(!$copy && $finished){
  134. $finished = 0;
  135. }
  136. }
  137. $model->finished = $finished;
  138. $model->save();
  139. return $res;
  140. }
  141. public function doDelGoods($store_id) {
  142. Goods::updateAll(['is_delete' => 1], ['store_id' => $store_id]);
  143. return true;
  144. }
  145. public function doDelCat($store_id) {
  146. Cat::updateAll(['is_delete' => 1], ['store_id' => $store_id]);
  147. return true;
  148. }
  149. public function doCopyHome() {
  150. $home_list = json_decode($this->home_list, true);
  151. if(!empty($home_list['from']) && !empty($home_list['to'])){
  152. if(count($home_list['from']) == count($home_list['to'])){
  153. return true;
  154. }
  155. }
  156. $params = [
  157. 'is_delete' => 0,
  158. 'is_index' => 1,
  159. 'store_id' => $this->from_store_id,
  160. ];
  161. $template = NewDiyTemplate::findOne($params);
  162. $ids = [$template->id];
  163. // $controls = NewDiyTemplate::find()
  164. // ->where([
  165. // 'store_id' => $this->from_store_id,
  166. // 'is_delete' => 0,
  167. // 'type' => 100
  168. // ])->one();
  169. // $ids = [$controls->id];
  170. $home_list = [
  171. 'from' => $ids,
  172. ];
  173. $attr = $template->attributes;
  174. $params = [
  175. 'is_delete' => 0,
  176. 'is_index' => 1,
  177. 'store_id' => $this->to_store_id,
  178. ];
  179. $template = NewDiyTemplate::findOne($params);
  180. if ($template) {
  181. $template->is_index = 0;
  182. $template->save();
  183. }
  184. unset($attr['id']);
  185. $attr['store_id'] = $this->to_store_id;
  186. $model = new NewDiyTemplate();
  187. $model->attributes = $attr;
  188. $model->save();
  189. $ids = [$model->id];
  190. // $attr = $controls->attributes;
  191. // unset($attr['id']);
  192. // $attr['store_id'] = $this->to_store_id;
  193. // $model = new NewDiyTemplate();
  194. // $model->attributes = $attr;
  195. // $model->save();
  196. // $ids[] = $model->id;
  197. $home_list['to'] = $ids;
  198. $this->home_list = json_encode($home_list);
  199. $this->save();
  200. return true;
  201. }
  202. public function doCopyCenter() {
  203. $center_list = json_decode($this->center_list, true);
  204. if(!empty($center_list['from']) && !empty($center_list['to'])){
  205. if(count($center_list['from']) == count($center_list['to'])){
  206. return true;
  207. }
  208. }
  209. $list = NewDiyTemplate::find()
  210. ->where([
  211. 'store_id' => $this->from_store_id,
  212. 'is_delete' => 0,
  213. 'name' => 'system'
  214. ])->orderBy(['id' => SORT_ASC])->asArray()->all();
  215. $template = null;
  216. foreach ($list as $value) {
  217. $itemp = json_decode($value['template'], true);
  218. if($itemp['system_type'] == 'my'){
  219. $template = $value;
  220. break;
  221. }
  222. }
  223. $ids = [$template['id']];
  224. $center_list = [
  225. 'from' => $ids,
  226. ];
  227. $attr = $template;
  228. $list = NewDiyTemplate::find()
  229. ->where([
  230. 'store_id' => $this->to_store_id,
  231. 'is_delete' => 0,
  232. 'name' => 'system'
  233. ])->orderBy(['id' => SORT_ASC])->all();
  234. foreach ($list as $value) {
  235. $itemp = json_decode($value['template'], true);
  236. if($itemp['system_type'] == 'my'){
  237. $value->is_delete = 1;
  238. $value->save();
  239. }
  240. }
  241. unset($attr['id']);
  242. $attr['store_id'] = $this->to_store_id;
  243. $model = new NewDiyTemplate();
  244. $model->attributes = $attr;
  245. $model->save();
  246. $ids = [$model->id];
  247. $center_list['to'] = $ids;
  248. $this->center_list = json_encode($center_list);
  249. $this->save();
  250. return true;
  251. }
  252. public function doCopyFootnav() {
  253. $footnav_list = json_decode($this->footnav_list, true);
  254. if(!empty($footnav_list['from']) && !empty($footnav_list['to'])){
  255. if(count($footnav_list['from']) == count($footnav_list['to'])){
  256. return true;
  257. }
  258. }
  259. $template = NewDiyTemplate::find()
  260. ->where([
  261. 'store_id' => $this->from_store_id,
  262. 'is_delete' => 0,
  263. 'name' => 'controls'
  264. ])->orderBy(['id' => SORT_ASC])->asArray()->one();
  265. // $template = null;
  266. $copyItem = [];
  267. $itemp = json_decode($template['template'], true);
  268. if($itemp){
  269. foreach ($itemp as $widget) {
  270. if($widget['controlComName'] == 'widgetNavControl'){
  271. $copyItem = $widget;
  272. break;
  273. }
  274. }
  275. }
  276. $ids = [$template['id']];
  277. $footnav_list = [
  278. 'from' => $ids,
  279. ];
  280. $attr = $template;
  281. $template = NewDiyTemplate::find()
  282. ->where([
  283. 'store_id' => $this->to_store_id,
  284. 'is_delete' => 0,
  285. 'name' => 'controls'
  286. ])->orderBy(['id' => SORT_ASC])->one();
  287. $itemp = json_decode($template['template'], true);
  288. if($itemp){
  289. foreach ($itemp as &$widget) {
  290. if($widget['controlComName'] == 'widgetNavControl'){
  291. $widget = $copyItem;
  292. break;
  293. }
  294. }
  295. }
  296. $template->template = json_encode($itemp);
  297. $template->save();
  298. $ids = [$template->id];
  299. $footnav_list['to'] = $ids;
  300. $this->footnav_list = json_encode($footnav_list);
  301. $this->save();
  302. return true;
  303. }
  304. public function doCopyCategory() {
  305. $diy_category_list = json_decode($this->diy_category_list, true);
  306. if(!empty($diy_category_list['from']) && !empty($diy_category_list['to'])){
  307. if(count($diy_category_list['from']) == count($diy_category_list['to'])){
  308. return true;
  309. }
  310. }
  311. $list = NewDiyTemplate::find()
  312. ->where([
  313. 'store_id' => $this->from_store_id,
  314. 'is_delete' => 0,
  315. 'name' => 'system'
  316. ])->orderBy(['id' => SORT_ASC])->asArray()->all();
  317. $template = null;
  318. $goods_d_template = null;
  319. foreach ($list as $value) {
  320. $itemp = json_decode($value['template'], true);
  321. if($itemp['system_type'] == 'classify'){
  322. $template = $value;
  323. } elseif ($itemp['system_type'] == 'goodsDetail') {
  324. $goods_d_template = $value;
  325. }
  326. }
  327. $ids = [$template['id']];
  328. $diy_category_list = [
  329. 'from' => $ids,
  330. ];
  331. $attr = $template;
  332. $goods_d_attr = $goods_d_template;
  333. $list = NewDiyTemplate::find()
  334. ->where([
  335. 'store_id' => $this->to_store_id,
  336. 'is_delete' => 0,
  337. 'name' => 'system'
  338. ])->orderBy(['id' => SORT_ASC])->all();
  339. foreach ($list as $value) {
  340. $itemp = json_decode($value['template'], true);
  341. if($itemp['system_type'] == 'classify'){
  342. $value->is_delete = 1;
  343. $value->save();
  344. } elseif ($itemp['system_type'] == 'goodsDetail') {
  345. $value->is_delete = 1;
  346. $value->save();
  347. }
  348. }
  349. unset($attr['id']);
  350. unset($goods_d_attr['id']);
  351. $attr['store_id'] = $this->to_store_id;
  352. $goods_d_attr['store_id'] = $this->to_store_id;
  353. $model = new NewDiyTemplate();
  354. $model->attributes = $attr;
  355. $model->save();
  356. $ids = [$model->id];
  357. $model = new NewDiyTemplate();
  358. $model->attributes = $goods_d_attr;
  359. $model->save();
  360. $diy_category_list['to'] = $ids;
  361. $this->diy_category_list = json_encode($diy_category_list);
  362. $this->save();
  363. return true;
  364. }
  365. public function doCopyProductCate() {
  366. $key_value_id = [];
  367. try {
  368. $category_list = json_decode($this->category_list, true);
  369. if(!empty($category_list['from']) && !empty($category_list['to'])){
  370. if(count($category_list['from']) == count($category_list['to'])){
  371. return true;
  372. }
  373. }
  374. $list = Cat::find()
  375. ->where([
  376. 'store_id' => $this->from_store_id,
  377. 'is_delete' => 0,
  378. 'parent_id' => 0
  379. ])->orderBy(['id' => SORT_ASC])->all();
  380. // $category_list = [
  381. // 'from' => array_column($list, 'id'),
  382. // ];
  383. $ids = [];
  384. foreach ($list as $value) {
  385. $attr = $value->attributes;
  386. $from_attr_id = $attr['id'];
  387. $key_value_id[$from_attr_id] = 0;
  388. unset($attr['id']);
  389. $attr['store_id'] = $this->to_store_id;
  390. $attr['parent_id'] = 0;
  391. $model = new Cat();
  392. $model->attributes = $attr;
  393. $model->save();
  394. $ids[] = $model->id;
  395. $key_value_id[$from_attr_id] = $model->id;
  396. }
  397. /*debug_log([
  398. 'key_value_id' => $key_value_id,
  399. 'type' => 'category_list'
  400. ],'store_copy.log');*/
  401. $list = Cat::find()
  402. ->where([
  403. 'store_id' => $this->from_store_id,
  404. 'is_delete' => 0,
  405. ])->andWhere(['>', 'parent_id', 0])->orderBy(['id' => SORT_ASC])->all();
  406. // $category_list = [
  407. // 'from' => array_column($list, 'id'),
  408. // ];
  409. foreach ($list as $value) {
  410. $attr = $value->attributes;
  411. $from_attr_id = $attr['id'];
  412. $key_value_id[$from_attr_id] = 0;
  413. unset($attr['id']);
  414. $attr['store_id'] = $this->to_store_id;
  415. /*debug_log([
  416. 'from_attr_id' => $from_attr_id,
  417. 'parent_id' => $attr['parent_id'],
  418. 'real' => $key_value_id[$attr['parent_id']],
  419. 'type' => 'category_list'
  420. ],'store_copy.log');*/
  421. if ($attr['parent_id'] > 0) {
  422. $attr['parent_id'] = $key_value_id[$attr['parent_id']] ?? 0;
  423. }
  424. $model = new Cat();
  425. $model->attributes = $attr;
  426. $model->save();
  427. $ids[] = $model->id;
  428. $key_value_id[$from_attr_id] = $model->id;
  429. }
  430. $category_list['to'] = $ids;
  431. $this->category_list = json_encode($category_list);
  432. if (!$this->save()) {
  433. throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE));
  434. }
  435. } catch (\Exception $e) {
  436. /*debug_log([
  437. 'message' => $e->getMessage(),
  438. 'line' => $e->getLine(),
  439. 'file' => $e->getFile(),
  440. 'type' => 'category_list'
  441. ],'store_copy.log');*/
  442. $category_list = json_decode($this->category_list, true);
  443. $category_list['message'] = $e->getMessage();
  444. $category_list['line'] = $e->getLine();
  445. $category_list['file'] = $e->getFile();
  446. $this->category_list = json_encode($category_list, JSON_UNESCAPED_UNICODE);
  447. $this->save();
  448. }
  449. return $key_value_id;
  450. }
  451. public function doCopyProductAttr() {
  452. $attr_group_key_value_id = [];
  453. $attr_key_value_id = [];
  454. try {
  455. $attr_group_list = json_decode($this->attr_group_list, true);
  456. if(!empty($attr_group_list['from']) && !empty($attr_group_list['to'])){
  457. if(count($attr_group_list['from']) == count($attr_group_list['to'])){
  458. return true;
  459. }
  460. }
  461. $list = AttrGroup::find()
  462. ->where([
  463. 'store_id' => $this->from_store_id,
  464. 'is_delete' => 0,
  465. ])->orderBy(['id' => SORT_ASC])->all();
  466. $attr_group_list = [
  467. 'from' => array_column($list, 'id'),
  468. ];
  469. $ids = [];
  470. foreach ($list as $value) {
  471. $attr = $value->attributes;
  472. $old_attr_group_id = $attr['id'];
  473. unset($attr['id']);
  474. $attr['store_id'] = $this->to_store_id;
  475. $model = new AttrGroup();
  476. $model->attributes = $attr;
  477. $model->save();
  478. $ids[] = $model->id;
  479. $attr_group_key_value_id[$old_attr_group_id] = $model->id;
  480. }
  481. $attr_group_list['to'] = $ids;
  482. $this->attr_group_list = json_encode($attr_group_list);
  483. if (!$this->save()) {
  484. throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE));
  485. }
  486. } catch (\Exception $e) {
  487. /*debug_log([
  488. 'message' => $e->getMessage(),
  489. 'line' => $e->getLine(),
  490. 'file' => $e->getFile(),
  491. 'type' => 'attr_group_list'
  492. ],'store_copy.log');*/
  493. $attr_group_list = json_decode($this->attr_group_list, true);
  494. $attr_group_list['message'] = $e->getMessage();
  495. $attr_group_list['line'] = $e->getLine();
  496. $attr_group_list['file'] = $e->getFile();
  497. $this->attr_group_list = json_encode($attr_group_list, JSON_UNESCAPED_UNICODE);
  498. $this->save();
  499. }
  500. try {
  501. $attr_list = json_decode($this->attr_list, true);
  502. if(!empty($attr_list['from']) && !empty($attr_list['to'])){
  503. if(count($attr_list['from']) == count($attr_list['to'])){
  504. return;
  505. }
  506. }
  507. $list = Attr::find()
  508. ->where([
  509. 'attr_group_id' => $attr_group_list['from'],
  510. 'is_delete' => 0,
  511. ])->orderBy(['id' => SORT_ASC])->all();
  512. $ids = [];
  513. foreach ($list as $value) {
  514. $ids[] = $value->id;
  515. }
  516. $attr_list = [
  517. 'from' => $ids,
  518. ];
  519. $ids = [];
  520. foreach ($list as $value) {
  521. $attr = $value->attributes;
  522. $old_attr_id = $attr['id'];
  523. unset($attr['id']);
  524. $attr['attr_group_id'] = $attr_group_key_value_id[$attr['attr_group_id']] ?? 0;
  525. $model = new Attr();
  526. $model->attributes = $attr;
  527. $model->save();
  528. $ids[] = $model->id;
  529. $attr_key_value_id[$old_attr_id] = $model->id;
  530. }
  531. $attr_list['to'] = $ids;
  532. $this->attr_list = json_encode($attr_list);
  533. if (!$this->save()) {
  534. throw new \Exception(json_encode($this->errors, JSON_UNESCAPED_UNICODE));
  535. }
  536. } catch (\Exception $e) {
  537. /*debug_log([
  538. 'message' => $e->getMessage(),
  539. 'line' => $e->getLine(),
  540. 'file' => $e->getFile(),
  541. 'type' => 'attr_list'
  542. ],'store_copy.log');*/
  543. $attr_list = json_decode($this->attr_list, true);
  544. $attr_list['message'] = $e->getMessage();
  545. $attr_list['line'] = $e->getLine();
  546. $attr_list['file'] = $e->getFile();
  547. $this->attr_list = json_encode($attr_list, JSON_UNESCAPED_UNICODE);
  548. $this->save();
  549. }
  550. return [
  551. 'attr_group_key_value_id' => $attr_group_key_value_id,
  552. 'attr_key_value_id' => $attr_key_value_id,
  553. ];
  554. }
  555. public function doCopyProduct() {
  556. try {
  557. $product_cate = $this->doCopyProductCate();
  558. $product_attr = $this->doCopyProductAttr();
  559. $goods_ids = json_decode($this->goods_ids, true);
  560. if(!empty($goods_ids['from'])){
  561. if(empty($goods_ids['to'])){
  562. $goods_ids['to'] = [];
  563. }
  564. if(count($goods_ids['from']) <= count($goods_ids['to'])){
  565. return true;
  566. }
  567. }else{
  568. $list = Goods::find()
  569. ->where([
  570. 'store_id' => $this->from_store_id,
  571. 'is_delete' => 0,
  572. ])->andWhere(['<=', 'md_food_id', 0])->select('id')->orderBy(['id' => SORT_ASC])->all();
  573. $ids = [];
  574. foreach ($list as $value) {
  575. $ids[] = $value->id;
  576. }
  577. $goods_ids = [
  578. 'from' => $ids,
  579. ];
  580. if(empty($ids)){
  581. $goods_ids['to'] = [];
  582. }
  583. $this->goods_ids = json_encode($goods_ids);
  584. $this->save();
  585. if(empty($ids)){
  586. return true;
  587. }
  588. // $queue = queue_push(new \app\jobs\StoreCopyJob(['id' => $this->id]));
  589. // \Yii::error(['queue StoreCopyJob in', $queue]);
  590. // return;
  591. }
  592. $attr_list = json_decode($this->attr_list, true);
  593. $category_list = json_decode($this->category_list, true);
  594. $ids = [];
  595. $icount = 20;
  596. $store_cloud_id = StoreCloud::findOne(['store_id' => $this->to_store_id, 'is_delete' => 0]);
  597. foreach (array_slice($goods_ids['from'], count($goods_ids['to'] ?? [])) as $k => $value) {
  598. // if($k >= $icount){
  599. // $queue = queue_push(new \app\jobs\StoreCopyJob(['id' => $this->id]));
  600. // \Yii::error(['queue StoreCopyJob in', $queue]);
  601. // return;
  602. // }
  603. $goods = Goods::findOne($value)->toArray();
  604. $attr = $goods;
  605. $fromId = $attr['id'];
  606. unset($attr['id']);
  607. $attr['store_id'] = $this->to_store_id;
  608. $gattr = json_decode($attr['attr'], true);
  609. // var_dump($attr_list);
  610. foreach($gattr as &$gatt){
  611. foreach($gatt['attr_list'] as &$att){
  612. $att['attr_id'] = $product_attr['attr_key_value_id'][$att['attr_id']];
  613. $att['share_commission_first'] = 0;
  614. $att['share_commission_second'] = 0;
  615. $att['share_commission_third'] = 0;
  616. }
  617. }
  618. $attr['attr'] = json_encode($gattr);
  619. $attr['full_cut'] = '{"pieces":0,"forehead":0}';
  620. $attr['integral'] = '{"give":0,"forehead":0}';
  621. $attr['verify_card_id'] = '';
  622. $model = new Goods();
  623. // var_dump($value, $attr);
  624. $model->setAttributes($attr, false);
  625. // var_dump($model->attributes);die;
  626. $model->save();
  627. if ($model->cloud_goods_id > 0) {
  628. $form = new GoodsForm();
  629. $form->id = $model->cloud_goods_id;
  630. $form->store_id = $model->store_id;
  631. $goodsInfo = $form->saveCloudGoods();
  632. $merchantForm = new MerchantForm();
  633. $merchantForm->token_stroe_cloud_id = $store_cloud_id ?? 0;
  634. $result = $merchantForm->mchGoodsImport($goodsInfo['cloudBindInfo'],$goodsInfo['goods_id'], $model->store_id);
  635. //debug_log($result, 'store_copy.log');
  636. }
  637. // var_dump($model);die;
  638. $goods_ids['to'][] = $model->id;
  639. $this->goods_ids = json_encode($goods_ids);
  640. $this->save();
  641. $cats = GoodsCat::findAll(['goods_id' => $fromId, 'is_delete' => 0]);
  642. foreach ($cats as $cat) {
  643. $attr = $cat->attributes;
  644. unset($attr['id']);
  645. $attr['store_id'] = $this->to_store_id;
  646. $attr['goods_id'] = $model->id;
  647. $attr['cat_id'] = $product_cate[$attr['cat_id']];
  648. $modelCat = new GoodsCat();
  649. $modelCat->attributes = $attr;
  650. $modelCat->save();
  651. }
  652. $gpics = GoodsPic::findAll(['goods_id' => $fromId, 'is_delete' => 0]);
  653. foreach ($gpics as $gpic) {
  654. $attr = $gpic->attributes;
  655. unset($attr['id']);
  656. $attr['goods_id'] = $model->id;
  657. $modelPic = new GoodsPic();
  658. $modelPic->attributes = $attr;
  659. $modelPic->save();
  660. }
  661. }
  662. // if (empty($goods_cache) && empty($cate_cache)) {
  663. // return false;
  664. // } else {
  665. $cate_ = json_decode($this->category_list, true);
  666. $cate_cache = array_combine($cate_['from'], $cate_['to']);
  667. $goods_cache = array_combine($goods_ids['from'], $goods_ids['to']);
  668. $params = [
  669. 'is_delete' => 0,
  670. 'is_index' => 1,
  671. 'store_id' => $this->to_store_id,
  672. ];
  673. $template = NewDiyTemplate::findOne($params);
  674. $template_ = \json_decode($template->template, true);
  675. if (!$template_) {
  676. return;
  677. }
  678. foreach ($template_['templates'] as &$item) {
  679. // 头部导航
  680. if ($item['type'] == 'fastNav') {
  681. $item = $this->replaceFastNav($item, $goods_cache, $cate_cache);
  682. }
  683. // 图片轮播
  684. if ($item['type'] == 'banner') {
  685. $item = $this->replaceBanner($item, $goods_cache, $cate_cache);
  686. }
  687. // 魔方导航
  688. if ($item['type'] == 'cubeNav') {
  689. $item = $this->replaceCubeNav($item, $goods_cache, $cate_cache);
  690. }
  691. // 魔方图片
  692. if ($item['type'] == 'container') {
  693. $item = $this->replaceContainer($item, $goods_cache, $cate_cache);
  694. }
  695. // 产品展示
  696. if ($item['type'] == 'productShow') {
  697. //debug_log($item, 'template.log');
  698. $item = $this->replaceProductShow($item, $goods_cache, $cate_cache);
  699. //debug_log($item, 'template.log');
  700. }
  701. //产品分类
  702. if ($item['type'] == 'productClassify') {
  703. $item = $this->replaceProductClassify($item, $goods_cache, $cate_cache);
  704. }
  705. // 倒计时
  706. if ($item['type'] == 'marketCountDown') {
  707. $item = $this->replaceMarketCountDown($item, $goods_cache, $cate_cache);
  708. }
  709. // 首页推荐
  710. if ($item['type'] == 'indexRecommend') {
  711. // Todo 该模块数据有误,后期解决
  712. // $item = $this->replaceIndexRecommend($item, $goodsCache, $cacheCat);
  713. }
  714. }
  715. $template->template = json_encode($template_, JSON_UNESCAPED_UNICODE);
  716. $template->save();
  717. // }
  718. return true;
  719. } catch (\Exception $e) {
  720. /*debug_log([
  721. 'message' => $e->getMessage(),
  722. 'line' => $e->getLine(),
  723. 'file' => $e->getFile(),
  724. 'type' => 'product_list'
  725. ],'store_copy.log');*/
  726. $category_list = json_decode($this->category_list, true);
  727. $category_list['message'] = $e->getMessage();
  728. $category_list['line'] = $e->getLine();
  729. $category_list['file'] = $e->getFile();
  730. return true;
  731. }
  732. }
  733. // 替换头部导航
  734. private function replaceFastNav($data, $goodsCache, $cacheCat)
  735. {
  736. foreach ($data['params']['list'] as &$item) {
  737. $item = $this->replaceLink($item, $goodsCache, $cacheCat);
  738. }
  739. return $data;
  740. }
  741. // 替换图片轮播
  742. private function replaceBanner($data, $goodsCache, $cacheCat)
  743. {
  744. foreach ($data['params']['bannerList'] as &$item) {
  745. $item = $this->replaceLink($item, $goodsCache, $cacheCat);
  746. }
  747. return $data;
  748. }
  749. // 替换魔方导航
  750. private function replaceCubeNav($data, $goodsCache, $cacheCat)
  751. {
  752. foreach ($data['params']['list'] as &$item) {
  753. $item = $this->replaceLink($item, $goodsCache, $cacheCat);
  754. }
  755. return $data;
  756. }
  757. // 替换魔方图片
  758. private function replaceContainer($data, $goodsCache, $cacheCat)
  759. {
  760. foreach ($data['params']['cubeCoverList'] as &$item) {
  761. $item = $this->replaceLink($item, $goodsCache, $cacheCat);
  762. }
  763. return $data;
  764. }
  765. // 替换产品展示
  766. private function replaceProductShow($data, $goodsCache, $cacheCat)
  767. {
  768. // 替换分类id
  769. if (isset($data['params']['classifyData']) && isset($cacheCat[$data['params']['classifyData']])) {
  770. $data['params']['classifyData'] = $cacheCat[$data['params']['classifyData']];
  771. }
  772. // 替换分类商品id
  773. if (isset($data['params']['classifyArr'])) {
  774. foreach ($data['params']['classifyArr'] as &$item) {
  775. if (isset($goodsCache[$item['id']])) {
  776. $item['id'] = $goodsCache[$item['id']];
  777. }
  778. }
  779. }
  780. // 替换商品id
  781. if (isset($data['params']['selectData'])) {
  782. foreach ($data['params']['selectData'] as &$item) {
  783. if (isset($goodsCache[$item['id']])) {
  784. $item['id'] = $goodsCache[$item['id']];
  785. }
  786. }
  787. }
  788. return $data;
  789. }
  790. // 替换产品分类
  791. private function replaceProductClassify($data, $goodsCache, $cacheCat)
  792. {
  793. foreach ($data['params']['classifyList'] as &$item) {
  794. // 替换分类
  795. if (empty($item['classifyId']) && isset($cacheCat[$item['classifyId']])) {
  796. $item['id'] = $cacheCat[$item['classifyId']];
  797. $item['classifyId'] = $cacheCat[$item['classifyId']];
  798. }
  799. // 替换商品id
  800. foreach ($item['data'] as &$value) {
  801. if (isset($goodsCache[$value['id']])) {
  802. $value['id'] = $goodsCache[$value['id']];
  803. }
  804. }
  805. }
  806. return $data;
  807. }
  808. // 替换倒计时
  809. private function replaceMarketCountDown($data, $goodsCache, $cacheCat)
  810. {
  811. $data['params'] = $this->replaceLink($data['params'], $goodsCache, $cacheCat);
  812. return $data;
  813. }
  814. private function replaceLink($item, $goodsCache, $cacheCat)
  815. {
  816. if (!isset($item['link']['link'])) {
  817. return $item;
  818. }
  819. // 处理分类id
  820. if (
  821. strpos($item['link']['link'], '/pages/cat/cat?cat_id=') !== false &&
  822. $item['link']['params'][0]['value'] != '' &&
  823. isset($cacheCat[$item['link']['params'][0]['value']])
  824. ) {
  825. $item['link']['link'] = '/pages/cat/cat?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
  826. $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
  827. }
  828. // 处理商品列表
  829. if (
  830. strpos($item['link']['link'], '/other/list/list?cat_id=') !== false &&
  831. $item['link']['params'][0]['value'] != '' &&
  832. isset($cacheCat[$item['link']['params'][0]['value']])
  833. ) {
  834. $item['link']['link'] = '/other/list/list?cat_id=' . $cacheCat[$item['link']['params'][0]['value']];
  835. $item['link']['params'][0]['value'] = $cacheCat[$item['link']['params'][0]['value']];
  836. }
  837. // 处理商品详情
  838. if (
  839. strpos($item['link']['link'], '/goods/goods/goods?id=') !== false &&
  840. $item['link']['params'][0]['value'] != '' &&
  841. isset($goodsCache[$item['link']['params'][0]['value']])
  842. ) {
  843. $item['link']['link'] = '/goods/goods/goods?id=' . $goodsCache[$item['link']['params'][0]['value']];
  844. $item['link']['params'][0]['value'] = $goodsCache[$item['link']['params'][0]['value']];
  845. }
  846. return $item;
  847. }
  848. }