72 lines
2.3 KiB
PHP
72 lines
2.3 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 list(): Json
|
|
{
|
|
$params = $this->request->get(['land_id']);
|
|
if(empty($params['land_id'])){
|
|
return $this->fail('缺少必要参数');
|
|
}
|
|
$data = Plant::where('land_id',$params['land_id'])->order('create_time desc')->select()->each(function($item){
|
|
$item['pic'] = json_decode($item['pic'],true);
|
|
$item['plant_date'] = date('Y-m-d',$item['plant_date']);
|
|
return $item;
|
|
})->toArray();
|
|
return $this->success('请求成功',$data);
|
|
}
|
|
|
|
//添加种植
|
|
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'],
|
|
'user_id' => $land['user_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()
|
|
]);
|
|
Land::where('id',$params['land_id'])->update([
|
|
'residual_area'=>$land['residual_area']-$params['area'],
|
|
'update_time' => time()
|
|
]);
|
|
});
|
|
return $this->success('种植添加成功');
|
|
}
|
|
} |