publicfunctionautoSyncOrder(){
@ini_set('memory_limit','512M');set_time_limit(0);$ftime=date("Y-m-d",strtotime("-3 month")).' 00:00:00';//$fdate=date('Y', strtotime("-2 month"));$db_prex='erp_';$where=array();$where['o.istatus']=5;$where['o.deleted']=0;$where['o.isAfterSales']=0;$where['o.outStorageTime']=['<',$ftime];$orders=Order::alias('o')->join('order_third t','t.order_id=o.forderSn
and t.shopId=o.shopId','left')->field('o.orderId,
o.orderSn,o.istatus,o.createTime,t.torderId')->where($where)->order('o.orderId asc')->select();$count=0;if($orders){foreach($ordersas$order){$fdate=date('Y',strtotime($order->createTime));$sqlArr=array();$sqlArr[]='insert '.$db_prex.$fdate.'_order_goods select * from '.$db_prex.'order_goods where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'order_goods where orderId='.$order->orderId;$sqlArr[]='insert '.$db_prex.$fdate.'_order_batch select * from '.$db_prex.'order_batch where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'order_batch where orderId='.$order->orderId;$sqlArr[]='insert '.$db_prex.$fdate.'_order_stock select * from '.$db_prex.'order_stock where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'order_stock where orderId='.$order->orderId;$sqlArr[]='insert '.$db_prex.$fdate.'_order_pack select * from '.$db_prex.'order_pack where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'order_pack where orderId='.$order->orderId;$sqlArr[]='insert '.$db_prex.$fdate.'_action_order select * from '.$db_prex.'action_order where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'action_order where orderId='.$order->orderId;if($fdate=='2018'){$sqlArr[]='insert '.$db_prex.$fdate.'_action_chart(companyId,orderId,type,createTime,remark) select companyId,orderId,type,createTime,remark from '.$db_prex.'action_chart where orderId='.$order->orderId;}else{$sqlArr[]='insert '.$db_prex.$fdate.'_action_chart select * from '.$db_prex.'action_chart where orderId='.$order->orderId;}$sqlArr[]='delete from '.$db_prex.'action_chart where orderId='.$order->orderId;$sqlArr[]='insert '.$db_prex.$fdate.'_finance_order select * from '.$db_prex.'finance_order where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'finance_order where orderId='.$order->orderId;if($order->torderId){$sqlArr[]='insert '.$db_prex.$fdate.'_order_third select * from '.$db_prex.'order_third where torderId='.$order->torderId;$sqlArr[]='delete from '.$db_prex.'order_third where torderId='.$order->torderId;$sqlArr[]='insert '.$db_prex.$fdate.'_order_third_goods select * from '.$db_prex.'order_third_goods where torderId='.$order->torderId;$sqlArr[]='delete from '.$db_prex.'order_third_goods where torderId='.$order->torderId;$sqlArr[]='insert '.$db_prex.$fdate.'_action_order_third select * from '.$db_prex.'action_order_third where torderId='.$order->torderId;$sqlArr[]='delete from '.$db_prex.'action_order_third where torderId='.$order->torderId;}$sqlArr[]='insert '.$db_prex.$fdate.'_order select * from '.$db_prex.'order where orderId='.$order->orderId;$sqlArr[]='delete from '.$db_prex.'order where orderId='.$order->orderId;Db::startTrans();try{$order->istatus=6;$order->save();foreach($sqlArras$sql){Db::execute($sql);}Db::commit();$count++;}catch(Exception$e){Db::rollback();setLog('自动同步发货订单【'.$order->orderSn.'】异常:'.$e->getLine().'.'.$e->getMessage(),'syncOrderErr');}}}setLog('自动同步'.$count.'条订单!','syncOrder');}