CPS对接代码-平台商品更新
in OtherPHP with 0 comment

CPS对接代码-平台商品更新

in OtherPHP with 0 comment

对接-京东联盟CPS对接记录记录

说明

自有产品对接京东联盟后,对导入到自己产品库的京东商品要进行状态更新,在导入时新建了商品信息扩展库记录导入到自有产品库的京东商品,通过更新比对扩展库对自有商品库的京东产品进行价格及状态的更新。


代码示例

<?php
/*
 * @Description: 京东普通商品扩展信息更新脚本文件
 * @Author: Depp
 * @Date: 2020-04-02 16:38:04
 * @LastEditTime : 2020-01-16 17:24:40
 * @LastEditors  : Depp
 */

namespace console\controllers;

use phpDocumentor\Reflection\Element;
use Yii;
use yii\console\Controller;
use common\models\JdImportGoods;

/**
*   主体更新逻辑 
*   每天最多更新商品数为10万个
*   每次更新100条,循环更新,每次更新完睡半秒
*   商品总数小于10万时,更新实际商品数
*   商品总数大于10万<20万时,第一次更新前10万,后一次更新剩余的。
**/

class JdExtensionUpdateController extends \yii\console\Controller
{
    const CYCLENUM=100; //每次跟新条数,最多100
    const DIFFARRAY=['cids','commissionShare','price'];//数据更新条件

    static $totals=100000;//每天更新商品数
    static $client; //京东联盟实例对象

    /**
     * @Description 初始化构造方法
     * @Author      Depp
     * @DateTime    2020-04-02
     * @return      [type]     [description]
     */
    public function init(){
        $config= \Yii::$app->params['JDCPS'];
        self::$client = new \JdMediaSdk\JdFatory($config);
    }

    /**
     * @Description 数据更新方法
     * @Author      Depp
     * @DateTime    2020-04-02
     * @return      [type]     [description]
     */
    public function actionUpdateData(){

        //获取当前更新频次数
        $upFrequencys=self::getFrequencys();
        echo "当前更新频次数为:".$upFrequencys."\r\n";
        //每次更新100,获取更新页数
        $pages=self::getUpdatePages($upFrequencys);
        echo "当前更新页数为:".$pages."\r\n";
        self::processDate(1,$pages,0,$upFrequencys);
    }
    /**
     * @Description 递归循环处理数据
     * @Author      Depp
     * @DateTime    2020-04-02
     * @return      [type]     [description]
     */
    static function processDate($page,$pages,$startId,$upFrequencys){
        //获取要比对的数据
        $str=implode(self::DIFFARRAY,',');
        //取出100条数据
        $CYCLENUM=self::CYCLENUM;
        //拼接skuid串
        $sql="select id,skuId,{$str} from tc_jd_import_goods where updateFrequency !={$upFrequencys} order by id limit {$startId},{$CYCLENUM}";
        $goodsArray=JdImportGoods::findBySql($sql)->asArray()->all();
        $skuIdStr=join(array_column($goodsArray,'skuId'),",");
        //数据处理
        self::jdDateProcess($goodsArray,$skuIdStr,$upFrequencys);
        //递归调用
        $page+=1;
        if($pages>=$page){
            usleep(200000); //睡眠0.2秒
            $startId+=self::CYCLENUM;
            self::processDate($page,$pages,$startId,$upFrequencys);
        }
  }
    /**
     * @Description 处理京东数据静态方法
     * @Author      Depp
     * @DateTime    2020-04-02
     * @return      [type]     [description]
     */
    static function jdDateProcess($goodsArray,$skuids,$currentUpFrequencyNum){
        $jdDate=self::$client->good->info($skuids);
        if(!$jdDate) {
            //京东查询接口出现错误
            echo("根据SKUID查询京东数据失败");die();
        }
        $jdSkuIdArray = array_column($jdDate, 'skuId');

        foreach ($goodsArray as $key => $value){
            $jdkey=array_search($value['skuId'],$jdSkuIdArray);
            if(gettype($jdkey)=='integer'){
                $result=self::diffDate($jdDate[$jdkey],$value);
                if($result){
                    //比对成功不需要更新,只更新频次
                    Yii::$app->db->createCommand()->update('tc_jd_import_goods',['updateFrequency'=>$currentUpFrequencyNum],['and','skuId=:id'],[':id'=>$value['skuId']])->execute();
                }else{
                    $updateArray=[
                        'updateFrequency'=>$currentUpFrequencyNum,
                        'price'=>$jdDate[$jdkey]['unitPrice'],
                        'commissionShare'=>$jdDate[$jdkey]['commisionRatioWl'],
                    ];
                    //比对失败进行更新,也更新平台商品
                    Yii::$app->db->createCommand()->update('tc_jd_import_goods',$updateArray,['and','skuId=:id'],[':id'=>$value['skuId']])->execute();

                    //更新平台商品
                    $updateArray=[
                        'memberprice'=>$jdDate[$jdkey]['unitPrice'],
                        'royalty_ratio'=>$jdDate[$jdkey]['commisionRatioWl'],
                    ];

                    Yii::$app->db->createCommand()->update('tc_shop_goods',$updateArray,['and','ec_skuid=:id'],[':id'=>$value['skuId']])->execute();
                }
            }else{//skuid在京东接口中没有查到
                //更新状态,
                Yii::$app->db->createCommand()->update('tc_jd_import_goods',['status'=>0],['and','skuId=:id'],[':id'=>$value['skuId']])->execute();
                //进行下架
                Yii::$app->db->createCommand()->update('tc_shop_goods',['status'=>0],['and','ec_skuid=:id'],[':id'=>$value['skuId']])->execute();
                echo "接口中没有查到改数据,skuid:".$value['skuId'];
            }
        }
    }
    /**
     * @Description 比对数据
     * @Author      Depp
     * @DateTime    2020-04-02
     * @param       [type]     $skuid [description]
     * @return      [type]            [description]
     */
    static function diffDate($jdDate,$goodsDate){
        $cids=$jdDate['cid'].','.$jdDate['cid2'].','.$jdDate['cid3'];
        if($jdDate['unitPrice']!=$goodsDate['price']||$cids!=$goodsDate['cids']||$jdDate['commisionRatioWl']!=$goodsDate['commissionShare']){
            return false;
        }else{
            return true;
        }
    }

    /**
     * @Description 获取当前的更新频次
     * @Author      Depp
     * @DateTime    2020-04-02
     * @return      [type]     [description]
     */
    static function getFrequencys(){
        //获取现在第一条信息的更新频次
        $cuFrequencys=JdImportGoods::findOne(1)['updateFrequency'];

        if(!$cuFrequencys){//首次更新
            $cuFrequencys=1;
        }else{
            $goodsUpTotals=JdImportGoods::find()->where(['<>','updateFrequency',$cuFrequencys])->andWhere(['status'=>1])->count();
            if(!$goodsUpTotals){
                $cuFrequencys+=1;//上次更新完了
            }
        }
        return $cuFrequencys;
    }

    /**
     * @Description 获取实际更新页数
     * @Author      Depp
     * @DateTime    2020-04-02
     * @return      [type]     [description]
     */
    static function getUpdatePages($cuFrequencys){
        $realGoodTotals=0;
        $setTotals=self::$totals;
        //当前需要更新的商品数量
        $goodsUpTotals=JdImportGoods::find()->where(['<>','updateFrequency',$cuFrequencys])->andWhere(['status'=>1])->count();

        if($setTotals>$goodsUpTotals){
            $realGoodTotals=$goodsUpTotals;
        }else{
            $realGoodTotals=$setTotals;
        }
        echo "当前更新总数为:".$realGoodTotals."\r\n";
        //设置更新商品数量
        return ceil($realGoodTotals/self::CYCLENUM);
    }
}

问镜子里的自己,接下来的日子我每天想做什么… 然后去做。

Responses