where('id', $params['land_id'])->value('user_id'); Db::startTrans(); try { LandPlant::create([ 'user_id' => $userId, 'land_id' => $params['land_id'], 'kind' => $params['kind'], 'breed' => $params['breed'], 'area' => $params['area'], 'user' => $params['user'], 'remark' => $params['remark'], 'status' => $params['status'], 'pic' => json_encode($params['pic']), 'qr_code' => $params['qr_code'], 'plant_date' => strtotime($params['plant_date']), ]); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 编辑 * @param array $params * @return bool * @author likeadmin * @date 2023/11/23 11:32 */ public static function edit(array $params): bool { $userId = Db::name('land')->where('id', $params['land_id'])->value('user_id'); Db::startTrans(); try { LandPlant::where('id', $params['id'])->update([ 'user_id' => $userId, 'land_id' => $params['land_id'], 'kind' => $params['kind'], 'breed' => $params['breed'], 'area' => $params['area'], 'user' => $params['user'], 'remark' => $params['remark'], 'status' => $params['status'], 'pic' => json_encode($params['pic']), 'qr_code' => $params['qr_code'], 'plant_date' => strtotime($params['plant_date']), ]); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 删除 * @param array $params * @return bool * @author likeadmin * @date 2023/11/23 11:32 */ public static function delete(array $params): bool { $landPlant = LandPlant::findOrEmpty($params['id'])->toArray(); Land::where('id', $landPlant['land_id'])->inc('score', $landPlant['area'])->update(); Db::name('land_plant_action')->where('plant_id', $params['id'])->delete(); return LandPlant::destroy($params['id']); } /** * @notes 获取详情 * @param $params * @return array * @author likeadmin * @date 2023/11/23 11:32 */ public static function detail($params): array { return LandPlant::findOrEmpty($params['id'])->toArray(); } public static function suyuan($params): array|bool { if(empty($params['id'])){ self::setError('参数错误'); return false; } $plantInfo = Plant::where('id',$params['id'])->findOrEmpty(); if($plantInfo->isEmpty()){ self::setError('种植信息错误'); return false; } if($plantInfo['status'] != 2){ self::setError('种植信息状态错误'); return false; } $landInfo = Land::where('id',$plantInfo['land_id'])->findOrEmpty(); if($landInfo->isEmpty()){ self::setError('土地信息错误'); return false; } $plantInfo['pic'] = json_decode($plantInfo['pic'],true); $plantInfo['group_day'] = floor(($plantInfo['harvest_date'] - $plantInfo['plant_date']) / 86400); $plantInfo['plant_date'] = date('Y-m-d',$plantInfo['plant_date']); $plantInfo['harvest_date'] = date('Y-m-d',$plantInfo['harvest_date']); $plantInfo['land_name'] = $landInfo['title']; $plantInfo['land_area'] = $landInfo['total_area']; $plantInfo['land_address'] = $landInfo['province_name'].$landInfo['city_name'].$landInfo['county_name'].$landInfo['town_name'].$landInfo['village_name'].$landInfo['group_name']; //获取操作 $action = Action::field('type,type_text,detail,create_time')->where('plant_id',$params['id'])->select()->each(function($item){ $item['detail'] = json_decode($item['detail'],true); return $item; })->toArray(); $plantInfo['actions'] = $action; return $plantInfo->toArray(); } }