123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\Order;
- use App\OrderScript;
- use App\FxOrder;
- use App\GoodsSkus;
- use App\OrderGoodsSkus;
- use App\FxOrderGoodsSkus;
- use App\Goods;
- use App\Oplog;
- class SyncOrderToMj extends Command {
- protected $signature = 'SyncOrderToMj';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '同步订单到卖家云';
- public function handle()
- {
- $this->SyncOrder();
- //$this->SyncFxOrder();
- }
- public function SyncOrder(){
- //查询需要同步到卖家云的订单
- $today_date = date('Y-m-d');
- echo "\n订单同步时间: " . date('Y-m-d H:i:s');
- $result = Order::where('delivery_date', $today_date)->where('warehouse', 3)->where('is_del', 0)->where('status', 2)->whereNull('orderCode')->lists('id');
- if(empty($result)){
- return;
- }
- foreach($result as $id){
- #再次判断订单是否可同步
- $thisOrder = Order::select('id')->where('delivery_date', $today_date)->where('id', $id)->where('warehouse', 3)->where('is_del', 0)->where('status', 2)->whereNull('orderCode')->first();
- if(empty($thisOrder)){
- continue;
- }
- #新加逻辑 查询收件人所在城市天气情况并更新
- OrderScript::updateWeather($id);
- $res = $this->syncOrderToMj($id);
- if(!$res){
- echo "\n订单ID:".$id." 同步失败";
- }else{
- #同步到卖家云,更新order_goods_skus状态
- $mj_status = array();
- $mj_status['mj_status'] = 1;
- OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- // +++++ 减预占库存到redis
- $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
- $sku_redis = json_decode(json_encode($sku_redis),true);
- Order::diffSkuToRedis('', $sku_redis, -1);
- echo "\n订单ID:".$id." 同步成功";
- }
-
- #记录操作日志
- $self_id = 0;
- $self_name = '脚本';
- $context = "同步订单到卖家云。返回结果:".json_encode($res);
- $type = 0;
- $tables = 'order';
- $data_id = $id;
- Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
- }
- }
- public function SyncFxOrder(){
- //查询需要同步到卖家云的订单
- $today_date = date('Y-m-d');
- echo "\n订单同步时间: " . date('Y-m-d H:i:s');
- $result = FxOrder::where('delivery_date', '=', $today_date)->where('warehouse', 3)->where('is_del', 0)->whereIn('status', [2,3])->lists('id');
- if(empty($result)){
- return;
- }
- foreach($result as $id){
- $mjOrder = $this->getFxMjOrder($id);
- if(!empty($mjOrder)){
- if($mjOrder['status'] == '已取消'){
- $res = $this->updateFxOrderToMj($id);
- echo "\n订单ID:".$id." 卖家取消订单重新更新";
- #同步到卖家云,更新order_goods_skus状态
- $mj_status = array();
- $mj_status['mj_status'] = 1;
- FxOrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- }
- }else{
- #新加逻辑 查询收件人所在城市天气情况并更新
- FxOrder::updateWeather($id);
- $res = $this->syncFxOrderToMj($id);
- if(!$res){
- echo "\n订单ID:".$id." 同步失败";
- }else{
- #同步到卖家云,更新order_goods_skus状态
- $mj_status = array();
- $mj_status['mj_status'] = 1;
- FxOrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
- echo "\n订单ID:".$id." 同步成功";
- }
- }
-
- }
- }
- /**
- * 订单同步到卖家云
- */
- public function syncOrderToMj($id, $order = []){
- if( empty($order) ){
- $order = Order::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
- $order = json_decode(json_encode($order), true);
- $skus = DB::table('order_goods_skus')->where('order_id', $id)->where('is_del', 0)->get();
- $skus = json_decode(json_encode($skus), true);
- foreach($skus as $k=>$v){
- $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
- $goods_info = Goods::where('id', $v['goods_id'])->first();
- $params = array();
- $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;//
- $params['skuCode'] = $v['sku_id'];
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
- $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $order['skuList'][] = $params;
- }
- }
- //卖家云添加订单 ->改为审核通过再同步卖家云
- $mj_add = OrderScript::mjOrderAdd($order);
- //同步卖家云订单数据到本系统
- if( isset($mj_add['order']['code']) ){
- $mjorder = array();
- $mjorder['orderCode'] = $mj_add['order']['code'];
- Order::where('id', $id)->update($mjorder);
- }
- return $mj_add;
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function getMjOrder($id, $outerCode=''){
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE');
- if(!$outerCode){
- $outerCode = Order::where('id', $id)->pluck('outerCode');
- }
- $order['outerCode'] = $outerCode;
- $mjOrder = OrderScript::mjOrderGet($order);
- if(!isset($mjOrder['order']['code'])){
- return false;
- }
- return $mjOrder['order'];
- }
- /**
- * 订单更新同步到卖家云
- */
- public function updateOrderToMj($id, $order = []){
- if( empty($order) ){
- $order = Order::select('orderCode as code','eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
- $order = json_decode(json_encode($order), true);
- }
- $mj_add = OrderScript::mjOrderUpdate($order);
- return $mj_add;
- }
- /**
- * 订单同步到卖家云
- */
- public function syncFxOrderToMj($id, $order = []){
- if( empty($order) ){
- $order = FxOrder::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
- $order = json_decode(json_encode($order), true);
- $skus = DB::table('fx_order_goods_skus')->where('order_id', $id)->where('is_del', 0)->get();
- $skus = json_decode(json_encode($skus), true);
- foreach($skus as $k=>$v){
- $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
- $goods_info = Goods::where('id', $v['goods_id'])->first();
- $params = array();
- $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
- $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
- $params['goodsCode'] = $sku_info->goodsCode;//
- $params['skuCode'] = $v['sku_id'];
- #商品名称
- $params['goodsName'] = $goods_info->name;
- $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
- //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
- $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
- $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
- $params['discountFee'] = 0;
- $params['adjustFee'] = 0;
- $params['refundStatus'] = '正常';
- $order['skuList'][] = $params;
- }
- }
- //卖家云添加订单 ->改为审核通过再同步卖家云
- $mj_add = OrderScript::mjOrderAdd($order);
- //同步卖家云订单数据到本系统
- if( isset($mj_add['order']['code']) ){
- $mjorder = array();
- $mjorder['orderCode'] = $mj_add['order']['code'];
- FxOrder::where('id', $id)->update($mjorder);
- }
- return $mj_add;
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function getFxMjOrder($id, $outerCode=''){
- $order = array();
- $order['eshopCode'] = config('constants.ESHOP_CODE');
- if(!$outerCode){
- $outerCode = FxOrder::where('id', $id)->pluck('outerCode');
- }
- $order['outerCode'] = $outerCode;
- $mjOrder = OrderScript::mjOrderGet($order);
- if(!isset($mjOrder['order']['code'])){
- return false;
- }
- return $mjOrder['order'];
- }
- /**
- * 订单更新同步到卖家云
- */
- public function updateFxOrderToMj($id, $order = []){
- if( empty($order) ){
- $order = FxOrder::select('orderCode as code','eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
- $order = json_decode(json_encode($order), true);
- }
- $mj_add = OrderScript::mjOrderUpdate($order);
- return $mj_add;
- }
- }
|