suyuan/app/api/controller/PlantController.php

54 lines
1.7 KiB
PHP

<?php
namespace app\api\controller;
use app\common\model\land\Land;
use app\common\model\plant\Plant;
use think\facade\Db;
use think\response\Json;
class PlantController extends BaseApiController
{
//添加种植
public function add(): Json
{
//获取参数
$params = $this->request->post(['land_id','kind','breed','area','user','date','pic','remark']);
if(empty($params['land_id']) || empty($params['kind']) || empty($params['breed']) || empty($params['area']) || empty($params['user']) || empty($params['date']) || empty($params['pic'])){
return $this->fail('缺少必要参数');
}
if(!strtotime($params['date'])){
return $this->fail('日期格式错误');
}
$pics = json_decode($params['pic'],true);
if(empty($pics)){
return $this->fail('图片参数格式错误');
}
//获取土地信息
$land = Land::field('residual_area')->where('id',$params['land_id'])->findOrEmpty();
if($land->isEmpty()){
return $this->fail('土地信息错误');
}
if($params['area'] > $land['residual_area']){
return $this->fail('种植面积超过当前土地可种植面积');
}
//创建数据
Db::transaction(function()use($params,$land) {
Plant::create([
'land_id' => $params['land_id'],
'kind' => $params['kind'],
'breed' => $params['breed'],
'area' => $params['area'],
'user' => $params['user'],
'remark' => $params['remark'],
'pic' => $params['pic'],
'plant_date' => strtotime($params['date']),
'status' => 1,
'create_time' => time()
]);
$residual_area = $land['residual_area']-$params['area'];
Land::where('id',$params['land_id'])->update(['residual_area'=>$residual_area]);
});
return $this->success('种植添加成功');
}
}