CopyGoodsController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\controllers\qqdcopy;
  8. use app\models\Cat;
  9. use app\models\Goods;
  10. use app\models\GoodsCat;
  11. use app\models\GoodsPic;
  12. use yii\web\Controller;
  13. /**
  14. * 商品数据转移
  15. * 全渠道商品复制到数据库中,表名称qqd_goods,qqd_goods_pic,qqd_goods_cat, qqd_cat
  16. */
  17. class CopyGoodsController extends Controller
  18. {
  19. /**
  20. * 同步商品
  21. * get传参 start_id 起始id end_id 结束ID
  22. */
  23. public function actionCopyGoods()
  24. {
  25. Goods::deleteAll();
  26. GoodsCat::deleteAll();
  27. GoodsPic::deleteAll();
  28. $start_id = \Yii::$app->request->get('start_id', 1);
  29. $end_id = \Yii::$app->request->get('end_id', 0);
  30. $goods_list = QqdGoods::find()
  31. // ->where([
  32. // 'and',
  33. // [
  34. // '>=',
  35. // 'id',
  36. // $start_id
  37. // ],
  38. // [
  39. // '<=',
  40. // 'id',
  41. // $end_id
  42. // ],
  43. // 'is_delete' => 0
  44. // ])
  45. ->all();
  46. // print_r($goods_list);exit;
  47. $model = new Goods();
  48. foreach($goods_list as $goods) {
  49. $goods_model = clone $model;
  50. $goods_model->store_id = $goods->store_id;
  51. $goods_model->name = $goods->name;
  52. $goods_model->price = $goods->price;
  53. $goods_model->original_price = $goods->original_price;
  54. $goods_model->detail = $goods->detail;
  55. $goods_model->cat_id = $goods->cat_id;
  56. $goods_model->status = $goods->status;
  57. $goods_model->attr = $goods->attr;
  58. $goods_model->service = $goods->service;
  59. $goods_model->sort = $goods->sort;
  60. $goods_model->virtual_sales = $goods->virtual_sales;
  61. $goods_model->cover_pic = $goods->cover_pic;
  62. $goods_model->video_url = $goods->video_url;
  63. $goods_model->unit = $goods->unit;
  64. $goods_model->weight = $goods->weight;
  65. $goods_model->goods_num = $goods->goods_num;
  66. $goods_model->use_attr = $goods->use_attr;
  67. if ($goods_model->save()) {
  68. // 获取原商品ID
  69. $goods_cat = QqdGoodsCat::find()->where([
  70. 'goods_id' => $goods->id,
  71. 'is_delete' => 0
  72. ])->all();
  73. foreach($goods_cat as $item) {
  74. $cat_model = new GoodsCat();
  75. $cat_model->cat_id = $item->cat_id;
  76. $cat_model->store_id = $item->store_id;
  77. $cat_model->goods_id = $goods_model->id;
  78. $cat_model->save();
  79. }
  80. $goods_pic = QqdGoodsPic::find()->where([
  81. 'goods_id' => $goods->id,
  82. 'is_delete' => 0
  83. ])->all();
  84. foreach($goods_pic as $pic) {
  85. $pic_model = new GoodsPic();
  86. $pic_model->pic_url = $pic->pic_url;
  87. $pic_model->goods_id = $goods_model->id;
  88. $pic_model->save();
  89. }
  90. } else {
  91. exit('保存出错');
  92. }
  93. }
  94. exit('同步成功');
  95. }
  96. /**
  97. * 同步分类
  98. * 会将赤店中所有商品分类删除
  99. */
  100. public function actionCopyCat()
  101. {
  102. Cat::deleteAll();
  103. $copy_cat = QqdCat::find()->where([
  104. 'is_delete' => 0
  105. ])->all();
  106. $model_1 = new Cat();
  107. foreach($copy_cat as $cat) {
  108. $model = clone $model_1;
  109. $model->id = $cat->id;
  110. $model->store_id = $cat->store_id;
  111. $model->name = $cat->name;
  112. $model->pic_url = $cat->pic_url;
  113. $model->sort = $cat->sort;
  114. $model->parent_id = $cat->parent_id;
  115. $model->big_pic_url = $cat->big_pic_url;
  116. $model->advert_pic = $cat->advert_pic;
  117. $model->advert_url = $cat->advert_url;
  118. if (!$model->save()) {
  119. exit('操作异常');
  120. }
  121. }
  122. exit('操作成功');
  123. }
  124. }