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

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.DataUill;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.warning.entity.BaseWarningInfo;
import com.huimv.produce.warning.entity.BaseWarningInfoDto;
import com.huimv.produce.warning.mapper.BaseWarningInfoMapper;
import com.huimv.produce.warning.service.IBaseWarningInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-12-02
 */
@Service
public class BaseWarningInfoServiceImpl extends ServiceImpl<BaseWarningInfoMapper, BaseWarningInfo> implements IBaseWarningInfoService {
    @Autowired
    private BaseWarningInfoMapper baseWarningInfoMapper;

    @Override
    public Result listWarningInfoZoology(Map<String, String> map) {
        String farmId = map.get("farmId");
        String alarmType = map.get("alarmType");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");
        String current = map.get("current");
        String size = map.get("size");

        Date now = new Date();

        QueryWrapper<BaseWarningInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id", farmId);
        wrapper.orderByDesc("warning_time");

        if (StringUtils.isBlank(alarmType)) {
            wrapper.in("alarm_type", 7, 8, 9, 10);
        } else {
            wrapper.eq("alarm_type", alarmType);
        }

        //自定义
        if ("4".equals(type)) {
            endDate = endDate + " 23:59:59";
            startDate = startDate + " 00:00:00";
            wrapper.between("warning_time", startDate, endDate);

        }
        //本月
        else if ("3".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfMonth(now));
        }
        //本周
        else if ("2".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfWeek(now));
        }
        //今日
        else if ("1".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfDay(now));
        }

        if (StringUtils.isBlank(current)) {
            current = "1";
        }
        if (StringUtils.isBlank(size)) {
            size = "20";
        }
        Page<BaseWarningInfo> page = page(new Page<>(Integer.parseInt(current), Integer.parseInt(size)), wrapper);

        return new Result(ResultCode.SUCCESS, page);
    }

    @Override
    public Result listWarningInfoSewage(Map<String, String> map) {
        String farmId = map.get("farmId");
        String alarmType = map.get("alarmType");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");
        String current = map.get("current");
        String size = map.get("size");

        Date now = new Date();

        QueryWrapper<BaseWarningInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id", farmId);

        wrapper.orderByDesc("warning_time");
        if (StringUtils.isBlank(alarmType)) {
            wrapper.in("alarm_type", 15, 16, 17, 18);
        } else {
            wrapper.eq("alarm_type", alarmType);
        }

        //自定义
        if ("4".equals(type)) {
            endDate = endDate + " 23:59:59";
            startDate = startDate + " 00:00:00";
            wrapper.between("warning_time", startDate, endDate);

        }
        //本月
        else if ("3".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfMonth(now));
        }
        //本周
        else if ("2".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfWeek(now));
        }
        //今日
        else if ("1".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfDay(now));
        }

        if (StringUtils.isBlank(current)) {
            current = "1";
        }
        if (StringUtils.isBlank(size)) {
            size = "20";
        }
        Page<BaseWarningInfo> page = page(new Page<>(Integer.parseInt(current), Integer.parseInt(size)), wrapper);

        return new Result(ResultCode.SUCCESS, page);
    }

    @Override
    public Result listWarningInfo(Map<String, String> map) {
        String farmId = map.get("farmId");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");
        String current = map.get("current");
        String size = map.get("size");

        Date now = new Date();

        QueryWrapper<BaseWarningInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id", farmId);

        wrapper.orderByDesc("warning_time");


        //自定义
        if ("4".equals(type)) {
            endDate = endDate + " 23:59:59";
            startDate = startDate + " 00:00:00";
            wrapper.between("warning_time", startDate, endDate);

        }
        //本月
        else if ("3".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfMonth(now));
        }
        //本周
        else if ("2".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfWeek(now));
        }
        //今日
        else if ("1".equals(type)) {
            wrapper.ge("warning_time", DateUtil.beginOfDay(now));
        }

        if (StringUtils.isBlank(current)) {
            current = "1";
        }
        if (StringUtils.isBlank(size)) {
            size = "20";
        }
        Page<BaseWarningInfo> page = page(new Page<>(Integer.parseInt(current), Integer.parseInt(size)), wrapper);

        return new Result(ResultCode.SUCCESS, page);
    }

    @Override
    public Result listWarningInfoScreen(Map<String, String> map) {
        String type = map.get("type");
        String farmId = map.get("farmId");
        String current = map.get("current");
        String size = map.get("size");

        if (StringUtils.isBlank(current)) {
            current = "1";
        }
        if (StringUtils.isBlank(size)) {
            size = "20";
        }

        QueryWrapper<BaseWarningInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("warning_time");
        wrapper.ge("warning_time", DateUtil.beginOfDay(new Date()));
        wrapper.eq("farm_id", farmId);
        //生态监测
        if ("1".equals(type)) {
            wrapper.ne("warning_name", "环境监测");
        }
        //环境监测
        if ("2".equals(type)) {
            wrapper.eq("warning_name", "环境监测");
        }


        //一级报警
        if ("3".equals(type)) {
            wrapper.clear();
            wrapper.ge("warning_time", DateUtil.offsetWeek(new Date(), -1));
            wrapper.eq("farm_id", farmId);
            wrapper.eq("level", 1);
        }
        //二级报警
        if ("4".equals(type)) {
            wrapper.clear();
            wrapper.ge("warning_time", DateUtil.offsetWeek(new Date(), -1));
            wrapper.eq("farm_id", farmId);
            wrapper.eq("level", 2);
        }
        //三级报警
        if ("5".equals(type)) {
            wrapper.clear();
            wrapper.ge("warning_time", DateUtil.offsetWeek(new Date(), -1));
            wrapper.eq("farm_id", farmId);
            wrapper.eq("level", 3);
        }
//        演示版
//        Page<BaseWarningInfo> page = page(new Page<>(Integer.parseInt(current), Integer.parseInt(size)),wrapper);
        wrapper.last("limit 0,9");
        return new Result(ResultCode.SUCCESS, this.list(wrapper));
    }

    @Override
    public Result listAllWarning(Map<String, String> map) {
        String farmId = map.get("farmId");
        String startDate = map.get("startDate");
        String endDate = map.get("endDate");
        String type = map.get("type");

        //判空
        if (farmId == null) {
            farmId = "331023001";
        }
        //pageNo
        String pageNo = map.get("pageNo");
        if (org.apache.commons.lang3.StringUtils.isBlank(pageNo)) {
            pageNo = "1";
        }
        //pageSize
        String pageSize = map.get("pageSize");
        if (org.apache.commons.lang3.StringUtils.isBlank(pageSize)) {
            pageSize = "20";
        }

        List<BaseWarningInfoDto> baseWarningInfoDtos = null;
        QueryWrapper<BaseWarningInfo> wrapper = new QueryWrapper<>();
        Date now = new Date();
        DateTime dateTime = DateUtil.beginOfDay(now);


        Page<BaseWarningInfoDto> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        wrapper.orderByDesc("upload_time");
        wrapper.eq("farm_id",farmId);
        //默认显示
        IPage page1 = null;
        if (type == null) {
            wrapper.ge("upload_time",dateTime);

            page1 =  baseWarningInfoMapper.listData(page,wrapper);
//            baseWarningInfoDtos = baseWarningInfoMapper.listDayBaseWarn(farmId);
        } else {
            //自定义查询
            if ("4".equals(type)) {
                startDate = startDate + " 00:00:00";
               endDate = endDate + " 23:59:59";
               wrapper.between("upload_time",startDate,endDate);
                page1 = baseWarningInfoMapper.listData(page, wrapper);
             //  baseWarningInfoDtos =baseWarningInfoMapper.listBaseWarn(farmId, startDate, endDate);
            }
            //本月
            else if ("3".equals(type)) {
                Date timesMonthmorning = DataUill.getTimesMonthmorning();
                wrapper.ge("upload_time",timesMonthmorning );
                page1 = baseWarningInfoMapper.listData(page, wrapper);
            //  baseWarningInfoDtos = baseWarningInfoMapper.listMonthBaseWarn(farmId);
            }
            //本周
            else if ("2".equals(type)) {
                Date timesWeekmorning = DateUtil.beginOfWeek(new Date());
                wrapper.ge("upload_time",timesWeekmorning );
                page1 =  baseWarningInfoMapper.listData(page,wrapper);
            //    baseWarningInfoDtos = baseWarningInfoMapper.listWeekBaseWarn(farmId);
            }
            //今日
            else if ("1".equals(type)) {
                Date timesmorning = DataUill.getTimesmorning();
                wrapper.ge("upload_time",dateTime);
                page1 =baseWarningInfoMapper.listData(page,wrapper);
              //  baseWarningInfoDtos = baseWarningInfoMapper.listDayBaseWarn(farmId);
           }
        }

        return new Result(ResultCode.SUCCESS, page1);
    }

    @Override
    public Result setWarningStatus(Map<String, String> map) {
        String id = map.get("id");
        String status = map.get("status");
        BaseWarningInfo baseWarningInfo = new BaseWarningInfo();
        baseWarningInfo.setId(Integer.parseInt(id));
        baseWarningInfo.setReadStatus(Integer.parseInt(status));
        int rows = baseWarningInfoMapper.updateById(baseWarningInfo);
        if (rows == 0) {
            return new Result(ResultCode.FAIL);
        } else {
            return new Result(ResultCode.SUCCESS);
        }
    }

    @Override
    public Result newAlarm(Map<String, String> map) throws ParseException {
        String farmId = map.get("farmId");
        String level = map.get("level");
        String msg = map.get("msg");
        String levelName = map.get("levelName");
        String warningName = map.get("warningName");
        String warningTime = map.get("warningTime");
        String uploadTime = map.get("uploadTime");
        String alarmType = map.get("alarmType");
        com.huimv.produce.produce.utils.DateUtil dateUtil = new com.huimv.produce.produce.utils.DateUtil();
        BaseWarningInfo baseWarningInfo = new BaseWarningInfo();
        baseWarningInfo.setFarmId(Integer.parseInt(farmId));
        baseWarningInfo.setLevel(Integer.parseInt(level));
        baseWarningInfo.setMsg(msg);
        baseWarningInfo.setLevelName(levelName);
        baseWarningInfo.setWarningName(warningName);
        baseWarningInfo.setWarningTime(dateUtil.parseDateTime(warningTime));
        baseWarningInfo.setUploadTime(dateUtil.parseDateTime(uploadTime));
        baseWarningInfo.setAlarmType(Integer.parseInt(alarmType));
        baseWarningInfo.setReadStatus(0);
        int rows = baseWarningInfoMapper.insert(baseWarningInfo);
        if (rows == 0) {
            return new Result(ResultCode.FAIL);
        } else {
            return new Result(ResultCode.SUCCESS);
        }
    }

    /**
     * 最新预警状态(大屏)
     * @param map
     * @return
     */
    @Override
    public Result getLastWarning(Map<String, String> map) {
        String farmId = map.get("farmId");
        if (farmId == null) {
            farmId = "331023001";
        }
        Integer readStatus = 1;

        //今日预警记录
        List<BaseWarningInfoDto> list = baseWarningInfoMapper.listDayBaseWarn(farmId);
        //今日预警统计数量
        int todayCount = baseWarningInfoMapper.listDayBaseWarn(farmId).size();
        //本周预警统计数量
        int weekCount = baseWarningInfoMapper.listWeekBaseWarn(farmId).size();
        //本周预警已处理比率(1表示已处理)
        QueryWrapper<BaseWarningInfo> wrapper = new QueryWrapper();
        wrapper.eq("read_status", readStatus);
        int count = baseWarningInfoMapper.selectCount(wrapper).intValue();
        Map<String, Object> map1 = new HashMap<>();
        if (weekCount != 0) {
            float num = (float) count * 100 / weekCount;
            DecimalFormat df = new DecimalFormat("0.00%");
            String handleRate = df.format(num);
            map1.put("todayCount", todayCount);
            map1.put("handleRate", handleRate);
            map1.put("weekCount", weekCount);
            map1.put("list", list);
            return new Result(ResultCode.SUCCESS, map1);
        } else {
            map1.put("todayCount", 0);
            map1.put("handleRate",0);
            map1.put("weekCount", 0);
            map1.put("list", list);
        }

        return new Result(ResultCode.SUCCESS,map1);
    }


}
