lcly-data-admin/database/seeders/CropSeeder.php

180 lines
10 KiB
PHP

<?php
namespace Database\Seeders;
use Throwable;
use App\Models\Crop;
use Illuminate\Support\Str;
use Illuminate\Database\Seeder;
use App\Models\AgriculturalBase;
use Illuminate\Support\Facades\DB;
class CropSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$cropsArr = [
//城镇农作物
['name' => '粮食作物', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '谷物', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '稻谷', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '小麦', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '玉米', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '高粱', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '其他谷物', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
]],
['name' => '豆类', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '折粮薯类', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children' => [
['name' => '洋芋', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨']
]],
]],
['name' => '经济作物', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '油料', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '棉花', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '生麻', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '糖料', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '甘蔗', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '甜菜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨']
]],
['name' => '中草药材', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '蔬菜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '芹菜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '油菜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '菠菜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '西红柿', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '生姜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '辣椒', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '其他蔬菜', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
]],
['name' => '瓜果类', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '花卉苗木', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '食用菌', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
]],
['name' => '茶叶水果', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '茶叶', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '园林水果', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
['name' => '食用坚果', 'category_id' => 2, 'crop_type' => 2, 'unit' => '吨'],
]],
['name' => '竹木', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '木材', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '竹材', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
]],
['name' => '林产品', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' => '天然橡胶', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '天然生漆', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '油茶籽', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '油橄榄', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '油用牡丹籽', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '松香', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '紫胶(原胶)', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '竹笋干', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '核桃', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
['name' => '其它林产品', 'category_id' => 5, 'crop_type' => 2, 'unit' => '吨'],
]],
['name' => '猪', 'category_id' => 4, 'crop_type' => 2, 'unit' => '头', 'extends'=> [
['name' =>'当年出栏', 'unit' =>'头'],
['name' =>'期末存栏', 'unit' =>'头'],
['name' =>'肉产量', 'unit' =>'吨'],
]],
['name' => '牛', 'category_id' => 4, 'crop_type' => 2, 'unit' => '头', 'extends'=> [
['name' =>'当年出栏', 'unit' =>'头'],
['name' =>'期末存栏', 'unit' =>'头'],
['name' =>'肉产量', 'unit' =>'吨'],
]],
['name' => '羊', 'category_id' => 4, 'crop_type' => 2, 'unit' => '头', 'extends'=> [
['name' =>'当年出栏', 'unit' =>'头'],
['name' =>'期末存栏', 'unit' =>'头'],
['name' =>'肉产量', 'unit' =>'吨'],
]],
['name' => '活家禽', 'category_id' => 4, 'crop_type' => 2, 'unit' => '头', 'extends'=> [
['name' =>'当年出栏', 'unit' =>'头'],
['name' =>'期末存栏', 'unit' =>'头'],
['name' =>'肉产量', 'unit' =>'吨'],
]],
['name' => '家兔', 'category_id' => 4, 'crop_type' => 2, 'unit' => '头', 'extends'=> [
['name' =>'当年出栏', 'unit' =>'头'],
['name' =>'期末存栏', 'unit' =>'头'],
['name' =>'肉产量', 'unit' =>'吨'],
]],
['name'=>'附加产品', 'category_id' => 4, 'crop_type' => 2, 'unit' => '吨', 'children' => [
['name' =>'奶类', 'category_id' => 4, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' =>'生牛奶', 'category_id' => 4, 'crop_type' => 2, 'unit' => '吨']
]],
['name' =>'羊毛', 'category_id'=> 4, 'crop_type' => 2, 'unit' => '吨', 'children'=> [
['name' =>'山羊毛', 'category_id' => 4, 'crop_type' => 2, 'unit' => '吨', 'extends'=> [
['name' =>'山羊粗毛', 'unit' =>'吨'],
['name' =>'山羊绒', 'unit' =>'吨']
]],
['name' =>'绵羊毛', 'category_id' => 4, 'crop_type' => 2, 'unit' => '吨', 'extends'=> [
['name' =>'细羊毛', 'unit' =>'吨'],
['name' =>'半细羊毛', 'unit' =>'吨']
]]
]],
['name' => '禽蛋', 'category_id'=> 4, 'crop_type' => 2, 'unit' => '吨','children'=> [
['name'=>'鸡蛋', 'category_id'=>4, 'crop_type' => 2, 'unit' => '吨']
]],
['name' => '天然蜂蜜', 'category_id'=> 4, 'crop_type' => 2, 'unit' => '吨'],
['name' => '蚕茧', 'category_id'=> 4, 'crop_type' => 2, 'unit' => '吨']
]],
['name'=>'鱼类', 'category_id' => 3, 'crop_type' => 2, 'unit' => '吨', 'extends'=> [
['name' =>'淡水捕捞', 'unit' =>'吨'],
['name' =>'淡水养殖', 'unit' =>'吨']
]],
['name'=>'虾蟹类', 'category_id' => 3, 'crop_type' => 2, 'unit' => '吨', 'extends'=> [
['name' =>'淡水捕捞', 'unit' =>'吨'],
['name' =>'淡水养殖', 'unit' =>'吨']
]],
['name'=>'贝类', 'category_id' => 3, 'crop_type' => 2, 'unit' => '吨', 'extends'=> [
['name' =>'淡水捕捞', 'unit' =>'吨'],
['name' =>'淡水养殖', 'unit' =>'吨']
]],
['name'=>'其他类', 'category_id' => 3, 'crop_type' => 2, 'unit' => '吨', 'extends'=> [
['name' =>'淡水捕捞', 'unit' =>'吨'],
['name' =>'淡水养殖', 'unit' =>'吨']
]],
//基地农作物
['name'=>'不知火','category_id' => 2, 'crop_type' => 1, 'unit' => '吨'],
['name'=>'耙耙柑','category_id' => 2, 'crop_type' => 1, 'unit' => '吨'],
['name'=>'爱媛','category_id' => 2, 'crop_type' => 1, 'unit' => '吨'],
];
DB::table('crops')->truncate();
try {
DB::begintransaction();
$this->createCrops($cropsArr);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
}
}
public function createCrops(array $crops, ?Crop $parentCrop = null)
{
foreach ($crops as $item) {
$crop = Crop::create([
'name' => $item['name'],
'category_id' => $item['category_id'],
'crop_type' => $item['crop_type'],
'parent_id' => $parentCrop ? ($parentCrop?->id ?? 0) :0,
'path' => $parentCrop ? (($parentCrop?->path ?? '').$parentCrop->id.'-'):'',
'unit' => $item['unit'],
'extends' => isset($item['extends']) ? json_encode($item['extends']) : null,
'is_end' => isset($item['children']) ? 0 : 1,
]);
$children = $item['children'] ?? [];
if($children){
$this->createCrops($children, $crop);
}
}
}
}