Tryag File Manager
Home
||
Turbo Force
||
B-F Config_Cpanel
Current Path :
/
home
/
yanggyein
/
yanggyein
/
yang_farm
/
app
/
Console
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/yanggyein/yanggyein/yang_farm/app/Console/Kernel.php
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Support\Facades\DB; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ // ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // $schedule->command('inspire')->hourly(); $schedule->call(function () { $inputIdList = DB::select("SELECT P.id AS parts_id, P.state, P.breeding_id, I.weight, I.id AS input_id, I.`date`, I.input_one_feed, I.house_name, I.input_number, I.actual_number, I.weight FROM parts P, `input` I WHERE P.id = I.parts_id AND P.state = ?", [1]); //todo 입추된 동만큼 반복 foreach ($inputIdList as $inputItem) { //todo 1일령 확인 $cullingDeathModel = DB::selectOne("SELECT max(id) AS id, sum(culling_number) AS culling_accumulated, sum(death_number) AS death_accumulated FROM culling_death WHERE input_id = ? AND date <= ? ", [$inputItem->input_id, date('Y-m-d')]); //todo 입추 표준 데이터 테이블 가져오기 $inputModel = DB::selectOne('SELECT * FROM `input` WHERE id = ? ', [$inputItem->input_id]); $realBreedingTable = json_decode($inputModel->real_breeding_table); $closestIndex = 0; // 클로저 인덱스 if ($cullingDeathModel->id === null) { // 1일령 $date = $inputItem->date; // 누적 사료 섭취량 ( 첫날 ) $dailyFeed = $realBreedingTable->daily_feed[$closestIndex]; $dailyWeight = $realBreedingTable->daily_weight[$closestIndex]; $estimatedFeedIntake = $inputItem->actual_number * $dailyFeed; DB::insert("INSERT INTO culling_death ( input_id, `date`, input_number, actual_number, `day`, `weight`, estimated_weight, daily_weight, daily_feed, cumulative_feed, feed_requirement, input_feed, input_feed_type, actual_feed_remaining, start_feed_remaining, estimated_feed_intake, feed_remaining, created_at, updated_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now())", [ $inputItem->input_id, // input_id $date, // date $inputItem->input_number, // input_number 표준 생존 사육 수수 $inputItem->actual_number, // actual_number 표준 생존 사육 수수 1, // day 다음 일령 0, // estimated_weight 입고 중량 + 일일 증체량 1일령 표준 기준 1일령 15g $inputItem->weight, // weight 입고 중량 + 일일 증체량 1일령 표준 기준 1일령 15g $dailyWeight, // daily_weight 증체량 $dailyFeed, // daily_feed 표준 섭취량 $dailyFeed, // cumulative_feed 누적 섭취량 $dailyFeed / ($inputItem->weight), // feed_requirement 요구률 $inputItem->input_one_feed, // input_one_feed 초이사료 입고 '초이사료', // input_feed_type 초이사료 $inputItem->input_one_feed, // actual_feed_remaining 오늘 실제 남은 시작 사료량 $inputItem->input_one_feed, // start_feed_remaining 오늘 시작 사료량 $estimatedFeedIntake, // estimated_feed_intake 오늘 시작 사료량 $inputItem->input_one_feed - $estimatedFeedIntake, // feed_remaining 오늘 남을 사료 예상량 ]); $cullingDeathOne = DB::selectOne("SELECT culling_death.* FROM culling_death INNER JOIN `input` ON `input`.id = culling_death.input_id WHERE input_id = ? ORDER BY culling_death.`date` DESC", [$inputItem->input_id]); $this->wegithProcess($cullingDeathOne->id); } else { // 최대 오늘 날짜 이상은 생성되지 않기 위한 예외처리 $result = DB::selectOne("SELECT count(id) AS cnt FROM culling_death WHERE input_id = ? AND `date` = ? ", [$inputItem->input_id, date('Y-m-d')]); // 제일 마지막에 생성된 도폐사 객체 $latestCullingDeathModel = DB::selectOne("SELECT id, `date`, `day`, feed_remaining, actual_feed_remaining, loss_cumulative_feed, culling_accumulated, death_accumulated, daily_feed, input_feed, input_number, actual_number, add_weight FROM culling_death WHERE id = ? ORDER BY `date` DESC", [$cullingDeathModel->id]); if ($result->cnt === 0) { //todo 실제 입력된 남은 사료가 0 일 경우 (오늘 남은 사료 예상량 : 입력된 사료량 - 오늘 섭취량 ) $startFeedRemaining = $latestCullingDeathModel->actual_feed_remaining == 0 ? $latestCullingDeathModel->feed_remaining : ($latestCullingDeathModel->actual_feed_remaining + ($latestCullingDeathModel->day == 1 ? 0 : $latestCullingDeathModel->input_feed) - $inputItem->actual_number * $latestCullingDeathModel->daily_feed); DB::insert("INSERT INTO culling_death ( `date`, input_id, `day`, culling_accumulated, death_accumulated, input_number, actual_number, start_feed_remaining, loss_cumulative_feed, add_weight, created_at, updated_at ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, now(), now())", [ date('Y-m-d', strtotime($latestCullingDeathModel->date . '+1 days')), // date $inputItem->input_id, // input_id $latestCullingDeathModel->day + 1, // day 다음 일령 $latestCullingDeathModel->culling_accumulated, // culling_accumulated $latestCullingDeathModel->death_accumulated, // death_accumulated $latestCullingDeathModel->input_number, // input_number 표준 생존 사육 수수 $latestCullingDeathModel->actual_number, // actual_number 표준 생존 사육 수수 $startFeedRemaining, // start_feed_remaining 남은 사료량 $latestCullingDeathModel->loss_cumulative_feed, $latestCullingDeathModel->add_weight, // 가중치 ]); //* 입추 누적 도폐사 업데이트 DB::update("UPDATE `input` SET culling_accumulated = ?, death_accumulated = ? WHERE id = ?", [$cullingDeathModel->culling_accumulated, $cullingDeathModel->death_accumulated, $inputItem->input_id]); $cullingDeathOne = DB::selectOne("SELECT culling_death.* FROM culling_death INNER JOIN `input` ON `input`.id = culling_death.input_id WHERE input_id = ? ORDER BY culling_death.`date` DESC", [$inputItem->input_id]); $this->wegithProcess($cullingDeathOne->id); } } } })->dailyAt('00:07'); // })->everyMinute(); } /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__ . '/Commands'); require base_path('routes/console.php'); } /** * *예상 중량 계산 ( 이전에 모든 데이터 업데이트가 되어있어야 한다 ) * * @param [int] $cullingDeathId * @return cullingDeathModel */ public function wegithProcess($cullingDeathId) { // 도폐사 모델 $cullingDeathModel = DB::selectOne("SELECT * FROM culling_death WHERE id = ? ", [$cullingDeathId]); // 업데이트 완료된 입추 모델 호출 $inputModel = DB::selectOne("SELECT * FROM `input` WHERE id = ?", [$cullingDeathModel->input_id]); // 사육 테이블 $inputModelBreedingTable = json_decode($inputModel->real_breeding_table); // 오늘 포함 => 입고 누적 사료 | 누적 손실 사료 // DB::enableQueryLog(); $sumCumulativeFeed = DB::selectOne("SELECT SUM(input_feed) AS sum_input_feed, sum(loss_feed) AS sum_loss_feed FROM culling_death WHERE input_id = ? AND `day` < ? ", [$inputModel->id, $cullingDeathModel->day]); // dd(DB::getQueryLog()); //todo 시작 예상 무게 //* 도폐사 미추가 $yFeedRemaining = 0; // 총 섭취량 => 1일령 입고사량 - 시작 사료량 = 0 방지 | 어제까지 입고된 사료량 합 - 시작 남은 사료량 if ($cullingDeathModel->actual_feed_remaining == 0) { $yFeedRemaining = $sumCumulativeFeed->sum_input_feed == 0 ? 0 : ($sumCumulativeFeed->sum_input_feed - $cullingDeathModel->start_feed_remaining); } else // 실제 남은 사료량이 입력 되었을 때 { // 오늘 남을 사료 예상량 =>실제 남은 사료량 체크 + 오늘 입고 된 사료량 $yFeedRemaining = $sumCumulativeFeed->sum_input_feed == 0 ? 0 : ($sumCumulativeFeed->sum_input_feed - $cullingDeathModel->actual_feed_remaining); } // 누적 섭취 사료량 / 수 => 아침 까지 총 섭취량 / 현재수수 + 오늘 도폐사(시작 수수로 계산하기 위함) $yCumulativeFeed = $yFeedRemaining / ($cullingDeathModel->actual_number + ($cullingDeathModel->death_number + $cullingDeathModel->culling_number)); //* 도폐사 추가 $yLossFeedRemaining = 0; // 총 섭취량 => 1일령 입고사량 - 시작 사료량 = 0 방지 | 어제까지 입고된 사료량 합 - 시작 남은 사료량 if ($cullingDeathModel->actual_feed_remaining == 0) { $yLossFeedRemaining = $sumCumulativeFeed->sum_input_feed == 0 ? 0 : ($sumCumulativeFeed->sum_input_feed - ($sumCumulativeFeed->sum_loss_feed ?? 0) - $cullingDeathModel->start_feed_remaining); } else // 실제 남은 사료량이 입력 되었을 때 { // 오늘 남을 사료 예상량 =>실제 남은 사료량 체크 + 오늘 입고 된 사료량 $yLossFeedRemaining = $sumCumulativeFeed->sum_input_feed == 0 ? 0 : ($sumCumulativeFeed->sum_input_feed - ($sumCumulativeFeed->sum_loss_feed ?? 0) - $cullingDeathModel->actual_feed_remaining); } $yLossCumulativeFeed = $yLossFeedRemaining / ($cullingDeathModel->actual_number + ($cullingDeathModel->death_number + $cullingDeathModel->culling_number)); // 누적 섭취량 기준 근처 인덱스 $yIndex = $this->getClosestIndex($yCumulativeFeed, $inputModelBreedingTable->cumulative_feed); $yLossIndex = $this->getClosestIndex($yLossCumulativeFeed, $inputModelBreedingTable->cumulative_feed); // 누적 섭취량 기준 => 오늘 시작 표준 예상 무게 계산 $startWeight = $this->getWeight($inputModelBreedingTable, $yIndex, $yCumulativeFeed); $startLossWeight = $this->getWeight($inputModelBreedingTable, $yLossIndex, $yLossCumulativeFeed); // index over 방지 $yIndex = $cullingDeathModel->day == 1 ? 0 : $yIndex + 1; $yIndex = $yIndex >= count($inputModelBreedingTable->daily_feed) ? count($inputModelBreedingTable->daily_feed) - 1 : $yIndex; // 누적 섭취량 기준 => 일일 표준 섭취량 $yDailyFeed = $inputModelBreedingTable->daily_feed[$yIndex]; // 누적 섭취량 기준 => 일일 표준 증체량 $yDailyWeight = $inputModelBreedingTable->daily_weight[$yIndex]; // 기준 업데이트 DB::update("UPDATE culling_death SET daily_feed = ?, daily_weight = ?, cumulative_feed = ?, estimated_weight = ?, start_cumulative_feed = ?, total_cumulative_feed = ?, standard_one_weight = ?, actual_one_weight = ?, feed_requirement = ? WHERE id = ? ", [ $yDailyFeed, // daily_feed 일일 표준 섭취량 $yDailyWeight, // daily_weight 일일 표준 증체량 $inputModelBreedingTable->cumulative_feed[$yIndex], // cumulative_feed 일일 표준 누적 사료량 $inputModelBreedingTable->estimated_weight[$yIndex], // estimated_weight 일일 표준 중량 $yCumulativeFeed, // start_cumulative_feed 누적 섭취 사료량 | 수 $yFeedRemaining, // total_cumulative_feed = 토탈 누적 섭취 사료량 $startWeight, // standard_one_weight 누적 사료기준 => 오늘 시작 표준 예상 중량 | 수 $startLossWeight, // actual_one_weight 누적 사료기준 => 오늘 시작 실제 예상 중량 | 수 $yCumulativeFeed == 0 ? 0 : $startWeight / $yCumulativeFeed, $cullingDeathId, // 사육관리 ID ]); //todo 오늘 마감 예상 무게 (데이터 들어오는 것 처리) // 1. 도폐사 업데이트 // 2. 사료입고 // 3. 실제 남은 사료량 업데이트 // 4. 실제 무게 => 업데이트 예정 // 기준이 업데이트 된 도폐사 모델 재 호출 $cullingDeathModel = DB::selectOne("SELECT * FROM culling_death WHERE id = ? ", [$cullingDeathId]); // 오늘 남을 사료량 => 3항식은 지져분하여 변경 $actualFeedRemaining = 0; if ($cullingDeathModel->actual_feed_remaining == 0) { // 오늘 남을 사료 예상량 => 시작사료량 + 오늘 입고된 사료량 - 오늘 섭취 예상 사료량) $actualFeedRemaining = $cullingDeathModel->start_feed_remaining + ($cullingDeathModel->day == 1 ? 0 : $cullingDeathModel->input_feed) - ($cullingDeathModel->daily_feed * $cullingDeathModel->actual_number); } else // 실제 남은 사료량이 입력 되었을 때 { // 오늘 남을 사료 예상량 =>실제 남은 사료량 체크 + 오늘 입고 된 사료량 $actualFeedRemaining = $cullingDeathModel->actual_feed_remaining + ($cullingDeathModel->day == 1 ? 0 : $cullingDeathModel->input_feed) - ($cullingDeathModel->daily_feed * $cullingDeathModel->actual_number); } $actualLossFeedRemaining = 0; if ($cullingDeathModel->actual_feed_remaining == 0) { // 오늘 남을 사료 예상량 => 시작사료량 + 오늘 입고된 사료량 - 오늘 섭취 예상 사료량) $actualLossFeedRemaining = $cullingDeathModel->start_feed_remaining - ($sumCumulativeFeed->sum_loss_feed ?? 0) + ($cullingDeathModel->day == 1 ? 0 : $cullingDeathModel->input_feed) - ($cullingDeathModel->daily_feed * $cullingDeathModel->actual_number); } else // 실제 남은 사료량이 입력 되었을 때 { // 오늘 남을 사료 예상량 =>실제 남은 사료량 체크 + 오늘 입고 된 사료량 $actualLossFeedRemaining = $cullingDeathModel->actual_feed_remaining - ($sumCumulativeFeed->sum_loss_feed ?? 0) + ($cullingDeathModel->day == 1 ? 0 : $cullingDeathModel->input_feed) - ($cullingDeathModel->daily_feed * $cullingDeathModel->actual_number); } // 누적 사료량 | total => 어제까지 입고된 총 사료량 + 오늘 입고된 사료량 $totalCumulativeFeed = $sumCumulativeFeed->sum_input_feed + $cullingDeathModel->input_feed; // 오늘 마감 누적 사료량 | 수 => 총 입고 사료량 - 마감 남을 사료량 / 누적 섭취량 $deadlineCumulativeFeed = ($totalCumulativeFeed - $actualFeedRemaining) / $cullingDeathModel->actual_number; $deadlineCumulativeLossFeed = ($totalCumulativeFeed - $actualLossFeedRemaining) / $cullingDeathModel->actual_number; // 마감 누적 섭취량 기준 근처 인덱스 $index = $this->getClosestIndex($deadlineCumulativeFeed, $inputModelBreedingTable->cumulative_feed); $lossIndex = $this->getClosestIndex($deadlineCumulativeLossFeed, $inputModelBreedingTable->cumulative_feed); // 마감 누적 섭취량 기준 => 오늘 시작 표준 예상 무게 계산 $deadlineWeight = $this->getWeight($inputModelBreedingTable, $index, $deadlineCumulativeFeed); $deadlineLossWeight = $this->getWeight($inputModelBreedingTable, $lossIndex, $deadlineCumulativeLossFeed); // 기준 업데이트 DB::update("UPDATE culling_death SET estimated_feed_intake = ?, feed_remaining = ?, deadline_cumulative_feed = ?, standard_two_weight = ?, actual_two_weight = ? WHERE id = ? ", [ $cullingDeathModel->daily_feed * $cullingDeathModel->actual_number, // estimated_feed_intake 오늘 예상 섭취량 $actualFeedRemaining, // feed_remaining 오늘 마감 예상 사료량 $deadlineCumulativeFeed, // 누적 섭취 사료량 | 수 $deadlineWeight, // 누적 사료기준 => 오늘 시작 표준 예상 중량 | 수 $deadlineLossWeight, // 누적 사료기준 => 오늘 시작 표준 예상 중량 | 수 $cullingDeathId, // 사육관리 ID ]); return $cullingDeathModel; } /* 중량 구하기 */ protected function getWeight($table, $index, $value) { //todo 시작 기울기 $maxIndex = count($table->cumulative_feed) - 1; $subIndex = ($index + 1) > $maxIndex ? $maxIndex : ($index + 1); // dd($index, $subIndex); $x1 = $table->cumulative_feed[$index]; $x2 = $table->cumulative_feed[$subIndex]; $x = $x2 - $x1; $y1 = $table->estimated_weight[$index]; $y2 = $table->estimated_weight[$subIndex]; $y = $y2 - $y1; $slope = ($y == 0 ? 1 : $y) / ($x == 0 ? 1 : $x); // 기울기 //todo y절편 $ySlice = abs($x1 * $slope - $y1); // y절편 // dd($index, $subIndex, $x1, $x2, $y1, $y2, $slope, $ySlice); //* 표준 예상 중량 $estimatedWeight = $value * $slope + $ySlice; return $estimatedWeight; } protected function getClosestIndex($search, $arr) { $closest = null; $closestIndex = null; foreach ($arr as $key => $item) { if ($closest === null || abs($search - $closest) > abs($item - $search)) { $closest = $item; $closestIndex = $key; } } return $closestIndex; } }