123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php namespace App\Console\Commands;
- use App\Log;
- use App\Order;
- use App\OrderScript;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- class SyncGoods extends Command {
- protected $signature = 'sync_goods';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '分销商同步商品库存';
- public function handle()
- {
- try{
- $list = $this->getGoodsList();
- $this->handleGoodsList($list);
- } catch (\Exception $e) {
- Log::scriptLog('line:'.$e->getLine().' message:'.$e->getMessage(),[],"order/syncGoods",0,'');
- }
- }
- /**
- * 获取商品以及库存列表
- */
- public function getGoodsList() {
- $goodsList = DB::table('seafood.goods')->get();
- $data = json_decode(json_encode($goodsList),true);
- return $data;
- }
- public function handleGoodsList($list) {
- foreach($list as $key=>$goods){
- //查询现在商品表中是否有该商品
- $info = DB::table(env('DB_DATABASE').'.goods')->where('productCode',$goods['productCode'])->first();
- if($info){
- //有商品信息则更新店铺商品信息
- //卖家云必要参数拼接转化
- $mjArr = array();
- $mjArr['eshopCode'] = config('constants.ESHOP_CODE');
- $mjArr['outerCode'] = config('constants.GOODS_PREFIX').$goods['id'];
- $mjArr['name'] = $goods['name'];
- $mjArr['description'] = $goods['description'];
- $mj = OrderScript::mjGoodsUpdate($mjArr);
- if($mj == false){
- // Log::errorLog(json_encode($mjArr),[],"order/syncGoods",0,'');
- die;
- }
- //查询规格信息,更新规格信息
- $result = DB::table('seafood.goods_skus')->where('goodsCode', $goods['id'])->get();
- $result = json_decode(json_encode($result),true);
- foreach ($result as $sku){
- //查询规格信息
- $skuInfo = DB::table(env('DB_DATABASE').'.goods_skus')->where('code',$sku['code'])->first();
- if(!$skuInfo){
- //增加shop sku
- $shopArr = array();
- $shopArr['outerCode'] = config('constants.GOODS_PREFIX').$sku['id'];
- $shopArr['eshopCode'] = config('constants.ESHOP_CODE');
- $shopArr['goodsCode'] = config('constants.GOODS_PREFIX').$sku['goodsCode'];
- $shopArr['propsName'] = $sku['propsName'];
- $shopArr['price'] = $sku['price'];
- $shopArr['skuProductCode'] = $sku['code'];
- $shopArr['createTime'] = $sku['createTime'];
- $shopArr['modifyTime'] = $sku['modifyTime'];
- $sku_add = OrderScript::mjSkuAdd($shopArr);
- if($sku_add == false){
- // Log::errorLog(json_encode($shopArr),[],"order/syncGoods",0,'');
- die;
- }
- //更新商品信息以及添加库存
- $sku['goodsCode'] = $info->id;
- $skus_ad = DB::table(env('DB_DATABASE').'.goods_skus')->insert($sku);
- } else {
- # eshop sku更新
- $shopArr = array();
- $shopArr['eshopCode'] = config('constants.ESHOP_CODE');
- $shopArr['goodsCode'] = config('constants.GOODS_PREFIX').$sku['goodsCode'];
- $shopArr['outerCode'] = config('constants.GOODS_PREFIX').$sku['id'];
- $shopArr['propsName'] = $sku['propsName'];
- $shopArr['price'] = $sku['price'];
- $sku_up = OrderScript::mjSkuUpdate($shopArr);
- if($sku_up == false){
- // Log::errorLog(json_encode($shopArr),[],"order/syncGoods",0,'');
- die;
- }
- $code = $sku['code'];
- unset($sku['id']);
- unset($sku['code']);
- unset($sku['goodsCode']);
- $sku_update = DB::table(env('DB_DATABASE').'.goods_skus')->where('code',$code)->update($sku);
- }
- $productCode = $info->productCode;
- unset($goods['id']);
- unset($goods['productCode']);
- $goods_up = DB::table(env('DB_DATABASE').'.goods')->where('productCode',$productCode)->update($goods);
- }
- } else {
- //没有商品,直接同步店铺商品信息以及库存信息
- $result = DB::table('seafood.goods_skus')->where('goodsCode', $goods['id'])->get();
- $result = json_decode(json_encode($result),true);
- $mjArr = array();
- $skuArr = array();
- if(count($result) > 0){
- foreach ($result as $k=>$sku){
- $skuArr[$k]['propsName'] = $sku['propsName'];
- $skuArr[$k]['eShopCode'] = config('constants.ESHOP_CODE');
- $skuArr[$k]['price'] = $sku['price'];
- $skuArr[$k]['skuProductCode'] = $sku['code'];
- $skuArr[$k]['outerCode'] = config('constants.GOODS_PREFIX').$sku['id'];
- $skuArr[$k]['createTime'] = $sku['createTime'];
- $skuArr[$k]['modifyTime'] = $sku['modifyTime'];
- }
- }
- $mjArr['name'] = $goods['name'];
- $mjArr['description'] = $goods['description'];
- $mjArr['eshopCode'] = config('constants.ESHOP_CODE');
- $mjArr['productCode'] = $goods['productCode'];
- $mjArr['goodsCategoryName'] = $goods['goodsCategoryName'];
- $mjArr['picUrl'] = $goods['picUrl'];
- $mjArr['outerCode'] = config('constants.GOODS_PREFIX').$goods['id'];
- $mjArr['skuList'] = $skuArr;
- $mj = OrderScript::mjGoodsAdd($mjArr);
- if($mj == false){
- // Log::errorLog(json_encode($mjArr),[],"order/syncGoods",0,'');
- die;
- }
- //数据表中插入商品信息以及规格信息
- unset($goods['id']);
- $goods_id = DB::table(env('DB_DATABASE').'.goods')->insertGetId($goods);
- if($goods_id > 0 && count($result)>0 ){
- foreach ($result as $skus){
- $skus['goodsCode'] = $goods_id;
- $sku_ad = DB::table(env('DB_DATABASE').'.goods_skus')->insert($skus);
- }
- }
- }
- }
- echo 'SUCCESS';die;
- }
- }
|