package com.huimv.produce.warning.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.warning.entity.SysThreshold;
import com.huimv.produce.warning.mapper.SysThresholdMapper;
import com.huimv.produce.warning.service.ISysThresholdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-30
 */
@Service
public class SysThresholdServiceImpl extends ServiceImpl<SysThresholdMapper, SysThreshold> implements ISysThresholdService {

    @Resource
    private SysThresholdMapper sysThresholdMapper;


    @Override
    public Result listWarningInfoWeater(Map<String, String> map) {
        String farmId = map.get("farmId");
        String maxTem = map.get("maxTem");
        String minTem = map.get("minTem");
        SysThreshold threshold = this.getById(farmId);
//        if(ObjectUtil.isEmpty(threshold)){
//             return new Result(10001,"该牧场未设置天气阈值",false);
//        }
//        String weatherMaxTem = threshold.getWeatherMaxTem();
//        String weatherMixTem = threshold.getWeatherMinTem();
//        Map endMap =new HashMap();
//        if (Double.parseDouble(maxTem) > Double.parseDouble(weatherMaxTem)){
//            endMap.put("maxTem",true);
//            endMap.put("maxTemStr","今日最高温度为"+maxTem+"℃,高于阈值"+weatherMaxTem+"℃");
//        }else {
//            endMap.put("maxTem",false);
//            endMap.put("maxTemStr",null);
//        }
//        if (Double.parseDouble(minTem) < Double.parseDouble(weatherMixTem)){
//            endMap.put("minTem",true);
//            endMap.put("minTemStr","今日最低温度为"+minTem+"℃,低于阈值"+weatherMixTem+"℃");
//        }else {
//            endMap.put("minTem",false);
//            endMap.put("minTemStr",null);
//        }
        return new Result(ResultCode.SUCCESS,map);
    }

    @Override
    //阈值
    public Result getThreshold(HttpServletRequest request,Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        SysThreshold sysThreshold = sysThresholdMapper.selectById(farmId);
        if (ObjectUtil.isEmpty(sysThreshold) ){
            sysThreshold = new SysThreshold();
            sysThreshold.setFarmId(Integer.parseInt(farmId));
            sysThreshold.setDryMin("0");
            sysThreshold.setDryTem("0");
            sysThreshold.setMaxHum("0");
            sysThreshold.setMaxTem("0");
            sysThreshold.setMinHum("0");
            sysThreshold.setMinTem("0");
            sysThreshold.setDryDuration("0");
            sysThreshold.setDryNormalTemp("30");
            sysThresholdMapper.insert(sysThreshold);
        }
        return new Result(ResultCode.SUCCESS,sysThreshold);
    }

    @Override
    public Result addThreshold(HttpServletRequest request, Map<String, String> 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 dryTem = paramsMap.get("dryTem");
        String dryMin = paramsMap.get("dryMin");
        String dryDuration = paramsMap.get("dryDuration");
        String dryNormalTemp = paramsMap.get("dryNormalTemp");

        SysThreshold newSysThreshold = new SysThreshold();
        newSysThreshold.setFarmId(Integer.parseInt(farmId));
        newSysThreshold.setMaxTem(maxTem);
        newSysThreshold.setMinTem(minTem);
        newSysThreshold.setMaxHum(maxHum);
        newSysThreshold.setMinHum(minHum);
        newSysThreshold.setDryTem(dryTem);
        newSysThreshold.setDryMin(dryMin);
        newSysThreshold.setDryDuration(dryDuration);
        newSysThreshold.setDryNormalTemp(dryNormalTemp);

        if (Integer.parseInt(maxTem)<Integer.parseInt(minTem)){
            return new Result(10001,"最高温度不能小于最低温度！",false);
        }else if (Integer.parseInt(maxHum)<Integer.parseInt(minHum)){
            return new Result(10001,"最高湿度不能小于最低湿度！",false);
        }else{
            QueryWrapper<SysThreshold> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_Id",farmId);
            if (sysThresholdMapper.exists(queryWrapper)) {
                sysThresholdMapper.updateById(newSysThreshold);
            }else{
                sysThresholdMapper.insert(newSysThreshold);
            }
            return Result.SUCCESS();
        }
    }
}
