| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\controllers\qqdcopy;
- use app\models\Cat;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\GoodsPic;
- use yii\web\Controller;
- /**
- * 商品数据转移
- * 全渠道商品复制到数据库中,表名称qqd_goods,qqd_goods_pic,qqd_goods_cat, qqd_cat
- */
- class CopyGoodsController extends Controller
- {
- /**
- * 同步商品
- * get传参 start_id 起始id end_id 结束ID
- */
- public function actionCopyGoods()
- {
- Goods::deleteAll();
- GoodsCat::deleteAll();
- GoodsPic::deleteAll();
- $start_id = \Yii::$app->request->get('start_id', 1);
- $end_id = \Yii::$app->request->get('end_id', 0);
- $goods_list = QqdGoods::find()
- // ->where([
- // 'and',
- // [
- // '>=',
- // 'id',
- // $start_id
- // ],
- // [
- // '<=',
- // 'id',
- // $end_id
- // ],
- // 'is_delete' => 0
- // ])
- ->all();
- // print_r($goods_list);exit;
- $model = new Goods();
- foreach($goods_list as $goods) {
- $goods_model = clone $model;
- $goods_model->store_id = $goods->store_id;
- $goods_model->name = $goods->name;
- $goods_model->price = $goods->price;
- $goods_model->original_price = $goods->original_price;
- $goods_model->detail = $goods->detail;
- $goods_model->cat_id = $goods->cat_id;
- $goods_model->status = $goods->status;
- $goods_model->attr = $goods->attr;
- $goods_model->service = $goods->service;
- $goods_model->sort = $goods->sort;
- $goods_model->virtual_sales = $goods->virtual_sales;
- $goods_model->cover_pic = $goods->cover_pic;
- $goods_model->video_url = $goods->video_url;
- $goods_model->unit = $goods->unit;
- $goods_model->weight = $goods->weight;
- $goods_model->goods_num = $goods->goods_num;
- $goods_model->use_attr = $goods->use_attr;
- if ($goods_model->save()) {
- // 获取原商品ID
- $goods_cat = QqdGoodsCat::find()->where([
- 'goods_id' => $goods->id,
- 'is_delete' => 0
- ])->all();
- foreach($goods_cat as $item) {
- $cat_model = new GoodsCat();
- $cat_model->cat_id = $item->cat_id;
- $cat_model->store_id = $item->store_id;
- $cat_model->goods_id = $goods_model->id;
- $cat_model->save();
- }
- $goods_pic = QqdGoodsPic::find()->where([
- 'goods_id' => $goods->id,
- 'is_delete' => 0
- ])->all();
- foreach($goods_pic as $pic) {
- $pic_model = new GoodsPic();
- $pic_model->pic_url = $pic->pic_url;
- $pic_model->goods_id = $goods_model->id;
- $pic_model->save();
- }
- } else {
- exit('保存出错');
- }
- }
- exit('同步成功');
- }
- /**
- * 同步分类
- * 会将赤店中所有商品分类删除
- */
- public function actionCopyCat()
- {
- Cat::deleteAll();
- $copy_cat = QqdCat::find()->where([
- 'is_delete' => 0
- ])->all();
- $model_1 = new Cat();
- foreach($copy_cat as $cat) {
- $model = clone $model_1;
- $model->id = $cat->id;
- $model->store_id = $cat->store_id;
- $model->name = $cat->name;
- $model->pic_url = $cat->pic_url;
- $model->sort = $cat->sort;
- $model->parent_id = $cat->parent_id;
- $model->big_pic_url = $cat->big_pic_url;
- $model->advert_pic = $cat->advert_pic;
- $model->advert_url = $cat->advert_url;
- if (!$model->save()) {
- exit('操作异常');
- }
- }
- exit('操作成功');
- }
- }
|