kitchenModel->where('id',$this->kitchenId)->first(); $cycle = $this->cycleModel->where('id',$this->cycleId)->first(); if (empty($kitchen) || empty($cycle)) { throw new Exception('kitchenOrCycleError:'.json_encode([ 'kitchen_id' => $this->kitchenId, 'cycle_id' => $this->cycleId, 'cycle' => $cycle, 'kitchen' => $kitchen, ])); } $orderGoods = $this->orderGoodModel ->where('cycle_id',$cycle->id) ->where('kitchen_id',$this->kitchenId) ->whereIn('status',[ OrderCode::FINISH,OrderCode::CANCEL,OrderCode::FINISH_REFUND ]) ->get(); if ($orderGoods->isEmpty()) throw new Exception('orderGoodError:dataNull'); $orderGoods = $orderGoods->toArray(); $skuIds = array_unique(array_column($orderGoods, 'sku_id')); $skuInfo = $this->skuModel->whereIn('id',$skuIds)->pluck('spu_id','id')->toArray(); if (empty($skuInfo)) throw new Exception('skuInfoError:dataNull'); $spuInfo = $this->spuModel->whereIn('id',array_unique(array_values($skuInfo)))->pluck('chef_id','id')->toArray(); $currentDate = date('Y-m-d H:i:s'); $insertData = []; foreach ($orderGoods as $orderGood) { if (empty($insertData[$orderGood['sku_id']])) { $insertData[$orderGood['sku_id']] = [ 'date' => $cycle->dates, 'chef_id' => $spuInfo[$skuInfo[$orderGood['sku_id']]]['chef_id'] ?? 0, 'sku_id' => $orderGood['sku_id'], 'cycle_id' => $this->cycleId, 'kitchen_id' => $this->kitchenId, 'sale' => 0, 'refund' => 0, 'cancel' => 0, 'create_time' => $currentDate, 'update_time' => $currentDate ]; } match ($orderGoods['status']) { OrderCode::FINISH => $insertData[$orderGood['sku_id']]['sale'] += $orderGood['quantity'], OrderCode::FINISH_REFUND => $insertData[$orderGood['sku_id']]['refund'] += $orderGood['quantity'], OrderCode::CANCEL => $insertData[$orderGood['sku_id']]['cancel'] += $orderGood['quantity'], }; } if (empty($insertData)) throw new Exception('insertDataError:dataNull'); Db::transaction(function () use ($insertData) { $delFlag = $this->chefStatementModel->where('cycle_id',$this->cycleId)->where('kitchen_id',$this->kitchenId)->delete(); $insertFlag = (new ChefStatement)->insert($insertData); if (!$delFlag || !$insertFlag) { throw new Exception('insertDataError:'.json_encode([ 'data' => $insertData, 'cycle_id' => $this->cycleId, 'kitchen_id' => $this->kitchenId, ])); } }); } }