diff --git a/database/seeders/CropYieldSeeder.php b/database/seeders/CropYieldSeeder.php index e0c9223..567b430 100644 --- a/database/seeders/CropYieldSeeder.php +++ b/database/seeders/CropYieldSeeder.php @@ -18,30 +18,33 @@ class CropYieldSeeder extends Seeder public function run() { $type = 2; - //假数据逻辑:每个地区,每年随机一种农作物,4个季度随机值; $bases = AgriculturalBase::where('type', $type)->get(); foreach ($bases as $base){ - for ($i = 2019; $i < 2023; $i++) { - $crop = Crop::where('is_end', 1)->where('crop_type', $type)->inRandomOrder()->first(); - for ($j = 1; $j< 5; $j++) { - $insertData[] = [ - 'base_id' => $base->id, - 'crop_id' => $crop->id, - 'category_id' => $crop->category_id, - 'time_year' => $i, - 'quarter' => $j, - 'yield' => rand(1000, 9999), - 'cultivated'=> rand(100, 999), - 'output'=> rand(10000, 99999), - ]; + $insertData = []; + //随机剔除一个农作物不加数据 + $uncrop = Crop::where('is_end', 1)->where('crop_type', $type)->inRandomOrder()->first(); + $crops = Crop::where('is_end', 1)->where('crop_type', $type)->where('id', '<>', $uncrop->id)->get(); + foreach ($crops as $crop){ + for ($i = 2019; $i < 2023; $i++) { + for ($j = 1; $j< 5; $j++) { + $insertData[] = [ + 'base_id' => $base->id, + 'crop_id' => $crop->id, + 'category_id' => $crop->category_id, + 'time_year' => $i, + 'quarter' => $j, + 'yield' => rand(1000, 9999), + 'cultivated'=> rand(100, 999), + 'output'=> rand(10000, 99999), + ]; + } } } - } - - if(count($insertData) > 1){ - DB::table('crop_yields')->truncate(); - CropYield::insert($insertData); + if(count($insertData) > 1){ + // DB::table('crop_yields')->truncate(); + CropYield::insert($insertData); + } } } }