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 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); $data['plant_date'] = date('Y-m-d H:i:s',$data['plant_date']); return $this->success('请求成功',$data->toArray()); } //添加种植 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('user_id,residual_area')->where('id',$params['land_id'])->findOrEmpty(); if($land->isEmpty()){ return $this->fail('土地信息错误'); } if($land['user_id'] != $this->userId){ 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('种植添加成功'); } }