diff --git a/app/Admin/Controllers/AgriculturalBaseController.php b/app/Admin/Controllers/AgriculturalBaseController.php index a9dff3b..ec7ec38 100644 --- a/app/Admin/Controllers/AgriculturalBaseController.php +++ b/app/Admin/Controllers/AgriculturalBaseController.php @@ -18,10 +18,12 @@ class AgriculturalBaseController extends AdminController */ protected function grid() { - return Grid::make(new AgriculturalBase(), function (Grid $grid) { + $builder = AgriculturalBase::with('parent'); + return Grid::make($builder, function (Grid $grid) { $grid->model()->where('type', BaseType::Base); // $grid->column('id')->sortable(); $grid->column('name'); + $grid->column('parent.name', '所属镇街')->label(); $grid->column('person'); $grid->column('address'); $grid->column('address_lat'); @@ -55,6 +57,7 @@ class AgriculturalBaseController extends AdminController $model = $form->model(); $form->display('id'); + $form->select('parent_id', '所属镇街')->options(AgriculturalBase::town()->get()->pluck('name', 'id'))->required(); $form->text('name'); $form->text('person'); $form->number('areas'); diff --git a/app/Admin/Controllers/CropController.php b/app/Admin/Controllers/CropController.php index b18ab86..f170955 100644 --- a/app/Admin/Controllers/CropController.php +++ b/app/Admin/Controllers/CropController.php @@ -21,7 +21,7 @@ class CropController extends AdminController protected function grid() { return Grid::make(Crop::with(['category', 'parent']), function (Grid $grid) { - $grid->column('id')->sortable(); + $grid->id(); $grid->column('category.name', '行业'); $grid->column('parent.name', '上级'); // $grid->column('crop_type'); @@ -33,12 +33,20 @@ class CropController extends AdminController $grid->column('created_at')->sortable(); // $grid->column('updated_at') + $grid->model()->orderBy('created_at', 'desc'); + $grid->showCreateButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crops.create')); $grid->showQuickEditButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crops.edit')); $grid->showDeleteButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crops.destroy')); $grid->filter(function (Grid\Filter $filter) { $filter->like('name')->width(3); + $filter->equal('category_id')->select(Keywords::where('type_key', 'crops-category')->get()->pluck('name', 'id'))->width(3); + $filter->equal('crop_type')->select([ + 1=> '基地', + 2=> '镇街' + ])->width(3); + }); }); } diff --git a/app/Admin/Controllers/CropYieldController.php b/app/Admin/Controllers/CropYieldController.php index c3c951b..bd9830b 100644 --- a/app/Admin/Controllers/CropYieldController.php +++ b/app/Admin/Controllers/CropYieldController.php @@ -46,7 +46,7 @@ class CropYieldController extends AdminController $grid->showDeleteButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.crop_yields.destroy')); $grid->filter(function (Grid\Filter $filter) { - $filter->equal('base_id')->select(AgriculturalBase::all()->pluck('name', 'name'))->width(3); + $filter->equal('base_id')->select(AgriculturalBase::all()->pluck('name', 'id'))->width(3); $filter->equal('crop_id')->select(Crop::where('is_end', 1)->pluck('name', 'id'))->width(3); }); }); @@ -124,15 +124,19 @@ class CropYieldController extends AdminController return $v; }); - $form->currency('output')->symbol('元')->default(0)->required()->saving(function ($v) { + $form->currency('output')->symbol('元')->default(0)->saving(function ($v) { if ($v === null) { $v = 0; } return $v; }); - // $form->text('created_by'); - // $form->text('updated_by'); + + $form->hidden('category_id'); + $form->saving(function($form){ + $crop = Crop::findOrFail($form->crop_id); + $form->category_id = $crop->category_id; + }); $form->display('created_at'); $form->display('updated_at'); diff --git a/app/Admin/Controllers/DeviceController.php b/app/Admin/Controllers/DeviceController.php new file mode 100644 index 0000000..0057e77 --- /dev/null +++ b/app/Admin/Controllers/DeviceController.php @@ -0,0 +1,101 @@ +column('id')->sortable(); + $grid->column('base.name'); + $grid->column('sn'); + $grid->column('name'); + $grid->column('model'); + $grid->column('monitoring_point'); + $grid->column('type'); + $grid->column('status'); + // $grid->column('extends'); + // $grid->column('created_by'); + // $grid->column('updated_by'); + $grid->column('created_at')->sortable(); + + $grid->model()->orderBy('created_at', 'desc'); + + $grid->showCreateButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.devices.create')); + $grid->showQuickEditButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.devices.edit')); + $grid->showDeleteButton(! config('admin.permission.enable') || Admin::user()->can('dcat.admin.devices.destroy')); + + $grid->filter(function (Grid\Filter $filter) { + $filter->equal('id'); + + }); + }); + } + + /** + * Make a show builder. + * + * @param mixed $id + * + * @return Show + */ + protected function detail($id) + { + return Show::make($id, new Device(), function (Show $show) { + $show->field('id'); + $show->field('agricultural_base_id'); + $show->field('sn'); + $show->field('name'); + $show->field('model'); + $show->field('monitoring_point'); + $show->field('type'); + $show->field('status'); + $show->field('extends'); + $show->field('created_by'); + $show->field('updated_by'); + $show->field('created_at'); + $show->field('updated_at'); + }); + } + + /** + * Make a form builder. + * + * @return Form + */ + protected function form() + { + return Form::make(new Device(), function (Form $form) { + $form->display('id'); + $form->text('agricultural_base_id'); + $form->text('sn'); + $form->text('name'); + $form->text('model'); + $form->text('monitoring_point'); + $form->text('type'); + $form->radio('status')->options([ + + ]); + // $form->text('extends'); + // $form->text('created_by'); + // $form->text('updated_by'); + + $form->display('created_at'); + $form->display('updated_at'); + }); + } +} diff --git a/app/Admin/routes.php b/app/Admin/routes.php index 90eb06a..9c2a9a4 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -21,4 +21,6 @@ Route::group([ $router->get('api/crops/{id}', 'CropController@info')->name('api.crops.info'); $router->resource('crop-yields', 'CropYieldController')->names('crop_yields'); + + $router->resource('devices', 'DeviceController')->names('devices'); }); diff --git a/app/Http/Controllers/AgriculturalBaseController.php b/app/Http/Controllers/AgriculturalBaseController.php index ca936dd..ce90a5f 100644 --- a/app/Http/Controllers/AgriculturalBaseController.php +++ b/app/Http/Controllers/AgriculturalBaseController.php @@ -56,8 +56,7 @@ class AgriculturalBaseController extends Controller public function show(AgriculturalBase $agriculturalBasic) { - $agriculturalBasic->load('crops'); - + $agriculturalBasic->load(['crops', 'devices']); return $this->json(AgriculturalBaseResource::make($agriculturalBasic)); } diff --git a/app/Http/Resources/AgriculturalBaseResource.php b/app/Http/Resources/AgriculturalBaseResource.php index 4bd7691..94568e3 100644 --- a/app/Http/Resources/AgriculturalBaseResource.php +++ b/app/Http/Resources/AgriculturalBaseResource.php @@ -27,6 +27,7 @@ class AgriculturalBaseResource extends JsonResource 'workforce' => $this->workforce ?? 0, //人数 'cultivated' => $this->cultivated ?? 0, //种养殖面积 'crops' => CropResource::collection($this->whenLoaded('crops')), + 'devices' => DeviceResource::collection($this->whenLoaded('devices')), ]; } } diff --git a/app/Models/AgriculturalBase.php b/app/Models/AgriculturalBase.php index 28c73ed..8ef63b9 100644 --- a/app/Models/AgriculturalBase.php +++ b/app/Models/AgriculturalBase.php @@ -22,6 +22,10 @@ class AgriculturalBase extends Model 'parent_id', 'cultivated', ]; + public function parent(){ + return $this->belongsTo(self::class, 'parent_id'); + } + public function scopeBase($q) { return $q->where('type', BaseType::Base); diff --git a/app/Models/CropYield.php b/app/Models/CropYield.php index 8bbf5a1..6e45e2a 100644 --- a/app/Models/CropYield.php +++ b/app/Models/CropYield.php @@ -15,7 +15,7 @@ class CropYield extends Model ]; protected $fillable = [ - 'base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'user_id', + 'base_id', 'crop_id', 'time_year', 'yield', 'cultivated', 'output', 'created_by', 'updated_by', 'quarter', 'extends', 'category_id', diff --git a/app/Models/Device.php b/app/Models/Device.php index fa9748d..6d12435 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -2,16 +2,17 @@ namespace App\Models; -use App\Enums\DeviceStatus; use App\Enums\DeviceType; +use App\Enums\DeviceStatus; use EloquentFilter\Filterable; -use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Dcat\Admin\Traits\HasDateTimeFormatter; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Device extends Model { - use HasFactory, Filterable; + use HasFactory, Filterable, HasDateTimeFormatter; protected $attributes = [ 'status' => DeviceStatus::Offline, diff --git a/database/seeders/CropSeeder.php b/database/seeders/CropSeeder.php new file mode 100644 index 0000000..b2df1db --- /dev/null +++ b/database/seeders/CropSeeder.php @@ -0,0 +1,179 @@ + '粮食作物', '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); + } + } + } +} diff --git a/database/seeders/CropYieldSeeder.php b/database/seeders/CropYieldSeeder.php new file mode 100644 index 0000000..c5c1816 --- /dev/null +++ b/database/seeders/CropYieldSeeder.php @@ -0,0 +1,45 @@ +where('crop_type', 1)->get(); + + $insertData = []; + foreach ($crops as $crop) { + $base = AgriculturalBase::where('type', 1)->inRandomOrder()->first(); + 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(10000, 99999), + 'cultivated'=> rand(100, 999), + 'output'=> rand(100000, 999999), + ]; + } + } + } + if(count($insertData) > 1){ + DB::table('crop_yields')->truncate(); + CropYield::insert($insertData); + } + } +} diff --git a/dcat_admin_ide_helper.php b/dcat_admin_ide_helper.php index 379c8d0..3be5485 100644 --- a/dcat_admin_ide_helper.php +++ b/dcat_admin_ide_helper.php @@ -511,6 +511,7 @@ namespace Dcat\Admin { /** * @method \App\Admin\Form\Amap amap(...$params) + * @method \App\Admin\Form\CropExtends cropExtends(...$params) */ class Form { diff --git a/lang/zh_CN/device.php b/lang/zh_CN/device.php new file mode 100644 index 0000000..d95bca5 --- /dev/null +++ b/lang/zh_CN/device.php @@ -0,0 +1,21 @@ + [ + 'Device' => 'Device', + 'device' => 'Device', + ], + 'fields' => [ + 'agricultural_base_id' => '基地', + 'sn' => '序列号', + 'name' => '名称', + 'model' => '型号', + 'monitoring_point' => '监控点', + 'type' => '设备类型', + 'status' => '状态', + 'extends' => '扩展信息', + 'created_by' => '创建人', + 'updated_by' => '修改人', + ], + 'options' => [ + ], +];