suyuan/app/api/controller/PlantController.php

89 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2023-11-22 18:03:28 +08:00
<?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);
}
2023-11-27 17:52:52 +08:00
public function detail(): Json
{
$params = $this->request->get(['plant_id']);
if(empty($params['plant_id'])){
return $this->fail('缺少必要参数');
}
$data = Plant::where('id',$params['plant_id'])->findOrEmpty();
if($data->isEmpty()){
return $this->fail('种植信息错误');
}
$data['pic'] = json_decode($data['pic'],true);
return $this->success('请求成功',$data->toArray());
}
2023-11-22 18:03:28 +08:00
//添加种植
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('图片参数格式错误');
}
//获取土地信息
2023-11-27 15:06:47 +08:00
$land = Land::field('user_id,residual_area')->where('id',$params['land_id'])->findOrEmpty();
2023-11-22 18:03:28 +08:00
if($land->isEmpty()){
return $this->fail('土地信息错误');
}
2023-11-27 15:06:47 +08:00
if($land['user_id'] != $this->userId){
return $this->fail('土地信息与用户信息不匹配');
}
2023-11-22 18:03:28 +08:00
if($params['area'] > $land['residual_area']){
return $this->fail('种植面积超过当前土地可种植面积');
}
//创建数据
Db::transaction(function()use($params,$land) {
Plant::create([
'land_id' => $params['land_id'],
2023-11-25 11:04:14 +08:00
'user_id' => $land['user_id'],
2023-11-22 18:03:28 +08:00
'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()
]);
2023-11-22 18:03:28 +08:00
});
return $this->success('种植添加成功');
}
}