setName('makeseal') ->setDescription('the makeseal command'); } protected function execute(Input $input, Output $output) { if(Cache::get('makeseal')==1){ return ; } $list = $this->GetInfo(); while ( $list->valid()){ Cache::set('makeseal',1,600); $date= date('Y-m-d H:i:s'); $item = $list->current(); $output->writeln("[{$date}]: {$item['company_code']}=>{$item['fz_date']} 开始执行"); $this->makeSeal($item); $list->next(); } Cache::set('makeseal',0); } public function GetInfo() { $info = ProductFz::where([['status','=',0]])->order("fz_date asc")->group('company_code')->cursor(); foreach ($info as $item){ yield $item->toArray(); } } public function makeSeal($item){ $lastMonth = date('Y-m',strtotime('-1 month',strtotime($item['fz_date']))); $ist = ProductFz::where([['fz_date','=',$lastMonth],['company_code','=',$item['company_code']]])->findOrEmpty(); if(!$ist->isEmpty()&& $ist['status']!=2){ return false; }; $seal = ProductSeal::where([['fz_id','=',$item['id']]])->select(); if($seal->isEmpty()){ $lastSeal = ProductSeal::where([['fz_id','=',$ist->id],['balance_num','>',0]])->select(); if(!$lastSeal->isEmpty()){ (new ProductSeal)->saveAll(array_map(function ($val)use($item){ return [ 'product_id'=>$val['product_id'], 'skuCode'=>$val['skuCode'], 'goodName'=>$val['goodName'], 'spec'=>$val['spec'], 'unit'=>$val['unit'], 'unit_price'=>$val['unit_price'], 'begin_num'=>$val['balance_num'], 'in_num'=>"0", 'out_num'=>"0", 'balance_num'=>$val['balance_num'], 'fz_id'=>$item['id'], ]; },$lastSeal->toArray())); } } (new ProductSeal)->saveAll($this->getProductLog($item,$ist->id)); } public function getProductLog($row,$lastId){ $pastLog= ProductOnlog::withJoin(['Product'],'left') ->where([['fz_date','=',$row['fz_date']],['buyer_code','=',$row['company_code']]]) ->field('type,order_item_id,product_id,skuCode,goodName,spec,unit,product_onlog.unit_price,num')->cursor(); $list=[]; $balance = ProductSeal::where([['fz_id','=',$lastId],['balance_num','>',0]])->column('balance_num','product_id'); foreach ($pastLog as $item){ if(!isset($list[$item['product_id']])){ $list[$item['product_id']] = [ 'id'=>ProductSeal::where([['fz_id','=',$row['id']],['product_id','=',$item['product_id']]])->value('id',null), 'product_id'=>$item['product_id'], 'skuCode'=>$item['skuCode'], 'goodName'=>$item['goodName'], 'spec'=>$item['spec'], 'unit'=>$item['unit'], 'unit_price'=>$item['unit_price'], 'begin_num'=>$balance[$item['product_id']]??"0", 'in_num'=>"0", 'out_num'=>"0", 'balance_num'=>$balance[$item['product_id']]??"0", 'fz_id'=>$row['id'], ]; } if($item['type']==3|| $item['type']==1|| $item['type']==5|| $item['type']==7){ $list[$item['product_id']]['balance_num']=bcadd($list[$item['product_id']]['balance_num'],$item['num'],8); $list[$item['product_id']]['in_num']=bcadd($list[$item['product_id']]['in_num'],$item['num'],8); } if($item['type']==4|| $item['type']==2|| $item['type']==6|| $item['type']==8){ $list[$item['product_id']]['balance_num']=bcsub($list[$item['product_id']]['balance_num'],$item['num'],8); $list[$item['product_id']]['out_num']=bcadd($list[$item['product_id']]['out_num'],$item['num'],8); } } return array_values($list); } }