| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace App\Services;
- use App\Batch;
- use App\Events\BroadcastToStation;
- use App\Exceptions\ErrorException;
- use App\Http\Controllers\api\thirdPart\haochuang\SortingController;
- use App\Jobs\BatchTaskJob;
- use App\OracleActAllocationDetails;
- use App\Order;
- use App\OrderBin;
- use App\OrderCommodity;
- use App\Owner;
- use App\Station;
- use Exception;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Http;
- use App\Traits\ServiceAppAop;
- use Illuminate\Support\Facades\Log;
- class BatchService
- {
- use ServiceAppAop;
- protected $modelClass=Batch::class;
- /** @var StationTaskBatchService $stationTaskBatchService */
- private $stationTaskBatchService;
- /** @var StationRuleBatchService $stationRuleBatchService */
- private $stationRuleBatchService;
- /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
- private $stationTaskMaterialBoxService;
- /** @var StationTaskCommodityService $stationTaskCommodityService */
- private $stationTaskCommodityService;
- /** @var StationTaskService $stationTaskService */
- private $stationTaskService;
- public function __construct(){
- $this->stationTaskBatchService=null;
- $this->stationRuleBatchService=null;
- $this->stationTaskMaterialBoxService=null;
- $this->stationTaskCommodityService=null;
- $this->stationTaskService=null;
- }
- public function get(array $params)
- {
- $query = Batch::query();
- foreach ($params as $column=>$param){
- if (is_array($param))$query->whereIn($column,$param);
- else $query->where($column,$param);
- }
- return $query->get();
- }
- public function updateWhereIn($key,$values,$updateKeyValues){
- Batch::query()->whereIn($key,$values)->update($updateKeyValues);
- }
- /**
- * 为波次附加任务,已附加的重复任务不影响
- * @param $batches
- * @throws Exception
- */
- public function assignTasks($batches)
- {
- app('LogService')->log('海柔','assignTasks1',json_encode($batches));
- try{
- $batches = collect($batches);
- $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
- $this->instant($this->stationRuleBatchService,'StationRuleBatchService');
- $this->instant($this->stationTaskService,'StationTaskService');
- $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
- $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
- $stationTasks=null;
- $batches_shouldProcess = $this->stationRuleBatchService->getBatches_shouldProcess($batches); //按规则过滤需要的波次
- if($batches_shouldProcess->isEmpty()) return;
- app('LogService')->log('海柔','assignTasks3',json_encode($batches_shouldProcess));
- DB::transaction(function ()use($batches,&$batches_shouldProcess,&$stationTasks){
- $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
- $this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
- $this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
- $this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
- });
- foreach ($stationTasks as &$stationTask){
- $stationTask->loadMissing([
- "stationTaskCommodities.commodity.barcodes",
- "stationTaskCommodities.stationTaskMaterialBox",
- "stationTaskBatches.batch.owner",
- "stationTaskMaterialBoxes.materialBox",
- ]);
- }
- $jsonStationTasks=json_encode($stationTasks,true);
- broadcast(new BroadcastToStation(BroadcastToStation::ALL_STATION, $jsonStationTasks));
- }catch(Exception $e){
- $batchesJson='';
- foreach ($batches as $batch){
- $batchesJson.=json_encode($batch->code);
- Cache::tags(['波次防重叠'.$batch['id']])->flush();
- }
- Log::error("注册任务失败",["msg"=>$e->getMessage(),"json"=>$batchesJson]);
- throw new Exception('注册任务失败: '. $batchesJson);
- }
- }
- public function getBatchByCodes($codes)
- {
- if(empty($codes))return collect();
- if(count($codes) == 0)return collect();
- return Batch::query()->whereIn('code',$codes)->get();
- }
- /**
- * 检查批次订单信息,存在库位异常 直接删除所有所属商品 重新拉取
- *
- * @param Collection|array $batches
- */
- public function checkBatchOrderInfo($batches)
- {
- if (!is_array($batches))$batches = $batches->toArray();
- $ids = array_column($batches,"id");
- $batches = Batch::query()->whereIn("id",$ids)->with("orders.orderCommodities")->get();
- foreach ($batches as $batch){
- if (!$batch->orders)continue;
- foreach ($batch->orders as $order){
- if (!$order->orderCommodities)app("OrderService")->notExistToRecover($order);
- else{
- $mark = false;
- foreach ($order->orderCommodities as $orderCommodity){
- if (!$orderCommodity->location){
- $mark = true;break;
- }
- }
- if ($mark){
- OrderCommodity::query()->where("order_id",$order->id)->delete();
- app("OrderService")->notExistToRecover($order);
- }
- }
- }
- }
- }
- /**
- * 修复波次
- */
- public function repairBatch(string $code):bool
- {
- if (!$code)return false;
- $wave = DB::connection("oracle")->selectOne(DB::raw("select * from DOC_WAVE_HEADER where WAVENO = ?"),[$code]);
- if (!$wave)return false;
- $owner = app("OwnerService")->codeGetOwner($wave->customerid);
- $obj = [
- "wms_status" => Batch::WMS_STATUS[$wave->wavestatus] ?? $wave->wavestatus,
- "wms_type"=>$wave->descr,
- "created_at"=>date("Y-m-d H:i:s"),
- "wms_created_at"=>$wave->addtime,
- "updated_at"=>$wave->edittime,
- "owner_id"=>$owner->id,
- ];
- $wave = Batch::query()->where("code",$code)->first();
- if (!$wave) $wave = Batch::query()->create($obj);
- else Batch::query()->where("code",$code)->update($obj);
- $orderNos = array_column(DB::connection("oracle")->select(DB::raw("select orderno from DOC_WAVE_DETAILS where WAVENO = ?"),[$code]),"orderno");
- $count = Order::query()->whereIn("code",$orderNos)->count();
- if (count($orderNos)!=$count)app("OrderService")->syncOrderByCodes($orderNos);
- Order::query()->whereIn("code",$orderNos)->update(["batch_id"=>$wave->id]);
- $orders = Order::query()->with(["batch","bin"])->whereIn("code",$orderNos)->get();
- if (count($orderNos)!=count($orders))return false;
- foreach ($orders as $order){
- if ($order->bin)continue;
- $bin = DB::connection("oracle")->selectOne(DB::raw("select seqno from DOC_WAVE_DETAILS where waveno = ? and orderno = ?"),
- [$order->batch->code,$order->code]);
- if ($bin) OrderBin::query()->create([
- 'order_id' => $order->id,
- 'number' => $bin->seqno,
- ]);
- }
- return true;
- }
- public function assignBatch($code)
- {
- $batches = Batch::query()->where("code",$code)->get();
- if (!$batches->count()){
- $s = new SortingController();
- $s->syncOrderBin($code);
- $batches = Batch::query()->where("code",$code)->get();
- }
- if ($batches->count()>0){
- app("BatchService")->assignTasks($batches);
- }
- }
- }
|