package com.huimv.guowei.admin.controller;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.huimv.guowei.admin.common.utils.Result;
import com.huimv.guowei.admin.common.utils.ResultCode;
import com.huimv.guowei.admin.entity.EnvWarningThreshold;
import com.huimv.guowei.admin.service.IEnvWarningInfoService;
import com.huimv.guowei.admin.service.IEnvWarningThresholdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
/**
*
* 前端控制器
*
*
* @author author
* @since 2023-06-01
*/
@RestController
@RequestMapping("/env-warning-threshold")
@CrossOrigin
public class EnvWarningThresholdController {
@Autowired
private IEnvWarningThresholdService envWarningThresholdService;
@PostMapping("/saveThreshold")
public Result saveThreshold(@RequestBody Map paramsMap){
String farmId = paramsMap.get("farmId");
String maxTem = paramsMap.get("maxTem");
String minTem = paramsMap.get("minTem");
String maxHum = paramsMap.get("maxHum");
String minHum = paramsMap.get("minHum");
String maxPh = paramsMap.get("maxPh");
String minPh = paramsMap.get("minPh");
maxTem = nullParameter(maxTem);
minTem = nullParameter(minTem);
maxHum = nullParameter(maxHum);
minHum = nullParameter(minHum);
maxPh = nullParameter(maxPh);
minPh = nullParameter(minPh);
EnvWarningThreshold envWarningThreshold = new EnvWarningThreshold();
envWarningThreshold.setFarmId(Integer.parseInt(farmId));
envWarningThreshold.setMaxHum(Double.parseDouble(maxHum));
envWarningThreshold.setMinHum(Double.parseDouble(minHum));
envWarningThreshold.setMaxTem(Double.parseDouble(maxTem));
envWarningThreshold.setMinTem(Double.parseDouble(minTem));
envWarningThreshold.setMaxPh(Double.parseDouble(maxPh));
envWarningThreshold.setMinPh(Double.parseDouble(minPh));
envWarningThresholdService.saveOrUpdate(envWarningThreshold,new UpdateWrapper().eq("farm_id",farmId));
return Result.SUCCESS();
}
@PostMapping("/getThreshold")
public Result getThreshold(@RequestBody Map paramsMap){
String farmId = paramsMap.get("farmId");
EnvWarningThreshold envWarningThreshold = envWarningThresholdService.getOne(new QueryWrapper().eq("farm_id", farmId));
if (ObjectUtil.isEmpty(envWarningThreshold)) {
EnvWarningThreshold envWarningThreshold1 = new EnvWarningThreshold();
envWarningThreshold1.setMaxTem((double) 0);
envWarningThreshold1.setMinTem((double) 0);
envWarningThreshold1.setMaxHum((double) 0);
envWarningThreshold1.setMinHum((double) 0);
envWarningThreshold1.setMaxPh((double) 0);
envWarningThreshold1.setMinPh((double) 0);
return new Result(ResultCode.SUCCESS,envWarningThreshold1);
}else {
return new Result(ResultCode.SUCCESS,envWarningThreshold);
}
}
public String nullParameter(String param){
if (StringUtils.isEmpty(param)){
param = "0";
}
return param;
}
}