No Description

SyncGoods.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php namespace App\Console\Commands;
  2. use App\Log;
  3. use App\Order;
  4. use App\OrderScript;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. class SyncGoods extends Command {
  8. protected $signature = 'sync_goods';
  9. /**
  10. * The console command description.
  11. *
  12. * @var string
  13. */
  14. protected $description = '分销商同步商品库存';
  15. public function handle()
  16. {
  17. try{
  18. $list = $this->getGoodsList();
  19. $this->handleGoodsList($list);
  20. } catch (\Exception $e) {
  21. Log::scriptLog('line:'.$e->getLine().' message:'.$e->getMessage(),[],"order/syncGoods",0,'');
  22. }
  23. }
  24. /**
  25. * 获取商品以及库存列表
  26. */
  27. public function getGoodsList() {
  28. $goodsList = DB::table('seafood.goods')->get();
  29. $data = json_decode(json_encode($goodsList),true);
  30. return $data;
  31. }
  32. public function handleGoodsList($list) {
  33. foreach($list as $key=>$goods){
  34. //查询现在商品表中是否有该商品
  35. $info = DB::table(env('DB_DATABASE').'.goods')->where('productCode',$goods['productCode'])->first();
  36. if($info){
  37. //有商品信息则更新店铺商品信息
  38. //卖家云必要参数拼接转化
  39. $mjArr = array();
  40. $mjArr['eshopCode'] = config('constants.ESHOP_CODE');
  41. $mjArr['outerCode'] = config('constants.GOODS_PREFIX').$goods['id'];
  42. $mjArr['name'] = $goods['name'];
  43. $mjArr['description'] = $goods['description'];
  44. $mj = OrderScript::mjGoodsUpdate($mjArr);
  45. if($mj == false){
  46. // Log::errorLog(json_encode($mjArr),[],"order/syncGoods",0,'');
  47. die;
  48. }
  49. //查询规格信息,更新规格信息
  50. $result = DB::table('seafood.goods_skus')->where('goodsCode', $goods['id'])->get();
  51. $result = json_decode(json_encode($result),true);
  52. foreach ($result as $sku){
  53. //查询规格信息
  54. $skuInfo = DB::table(env('DB_DATABASE').'.goods_skus')->where('code',$sku['code'])->first();
  55. if(!$skuInfo){
  56. //增加shop sku
  57. $shopArr = array();
  58. $shopArr['outerCode'] = config('constants.GOODS_PREFIX').$sku['id'];
  59. $shopArr['eshopCode'] = config('constants.ESHOP_CODE');
  60. $shopArr['goodsCode'] = config('constants.GOODS_PREFIX').$sku['goodsCode'];
  61. $shopArr['propsName'] = $sku['propsName'];
  62. $shopArr['price'] = $sku['price'];
  63. $shopArr['skuProductCode'] = $sku['code'];
  64. $shopArr['createTime'] = $sku['createTime'];
  65. $shopArr['modifyTime'] = $sku['modifyTime'];
  66. $sku_add = OrderScript::mjSkuAdd($shopArr);
  67. if($sku_add == false){
  68. // Log::errorLog(json_encode($shopArr),[],"order/syncGoods",0,'');
  69. die;
  70. }
  71. //更新商品信息以及添加库存
  72. $sku['goodsCode'] = $info->id;
  73. $skus_ad = DB::table(env('DB_DATABASE').'.goods_skus')->insert($sku);
  74. } else {
  75. # eshop sku更新
  76. $shopArr = array();
  77. $shopArr['eshopCode'] = config('constants.ESHOP_CODE');
  78. $shopArr['goodsCode'] = config('constants.GOODS_PREFIX').$sku['goodsCode'];
  79. $shopArr['outerCode'] = config('constants.GOODS_PREFIX').$sku['id'];
  80. $shopArr['propsName'] = $sku['propsName'];
  81. $shopArr['price'] = $sku['price'];
  82. $sku_up = OrderScript::mjSkuUpdate($shopArr);
  83. if($sku_up == false){
  84. // Log::errorLog(json_encode($shopArr),[],"order/syncGoods",0,'');
  85. die;
  86. }
  87. $code = $sku['code'];
  88. unset($sku['id']);
  89. unset($sku['code']);
  90. unset($sku['goodsCode']);
  91. $sku_update = DB::table(env('DB_DATABASE').'.goods_skus')->where('code',$code)->update($sku);
  92. }
  93. $productCode = $info->productCode;
  94. unset($goods['id']);
  95. unset($goods['productCode']);
  96. $goods_up = DB::table(env('DB_DATABASE').'.goods')->where('productCode',$productCode)->update($goods);
  97. }
  98. } else {
  99. //没有商品,直接同步店铺商品信息以及库存信息
  100. $result = DB::table('seafood.goods_skus')->where('goodsCode', $goods['id'])->get();
  101. $result = json_decode(json_encode($result),true);
  102. $mjArr = array();
  103. $skuArr = array();
  104. if(count($result) > 0){
  105. foreach ($result as $k=>$sku){
  106. $skuArr[$k]['propsName'] = $sku['propsName'];
  107. $skuArr[$k]['eShopCode'] = config('constants.ESHOP_CODE');
  108. $skuArr[$k]['price'] = $sku['price'];
  109. $skuArr[$k]['skuProductCode'] = $sku['code'];
  110. $skuArr[$k]['outerCode'] = config('constants.GOODS_PREFIX').$sku['id'];
  111. $skuArr[$k]['createTime'] = $sku['createTime'];
  112. $skuArr[$k]['modifyTime'] = $sku['modifyTime'];
  113. }
  114. }
  115. $mjArr['name'] = $goods['name'];
  116. $mjArr['description'] = $goods['description'];
  117. $mjArr['eshopCode'] = config('constants.ESHOP_CODE');
  118. $mjArr['productCode'] = $goods['productCode'];
  119. $mjArr['goodsCategoryName'] = $goods['goodsCategoryName'];
  120. $mjArr['picUrl'] = $goods['picUrl'];
  121. $mjArr['outerCode'] = config('constants.GOODS_PREFIX').$goods['id'];
  122. $mjArr['skuList'] = $skuArr;
  123. $mj = OrderScript::mjGoodsAdd($mjArr);
  124. if($mj == false){
  125. // Log::errorLog(json_encode($mjArr),[],"order/syncGoods",0,'');
  126. die;
  127. }
  128. //数据表中插入商品信息以及规格信息
  129. unset($goods['id']);
  130. $goods_id = DB::table(env('DB_DATABASE').'.goods')->insertGetId($goods);
  131. if($goods_id > 0 && count($result)>0 ){
  132. foreach ($result as $skus){
  133. $skus['goodsCode'] = $goods_id;
  134. $sku_ad = DB::table(env('DB_DATABASE').'.goods_skus')->insert($skus);
  135. }
  136. }
  137. }
  138. }
  139. echo 'SUCCESS';die;
  140. }
  141. }