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

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.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.warning.entity.BaseWarningInfo;
import com.huimv.produce.warning.mapper.BaseWarningInfoMapper;
import com.huimv.produce.warning.service.IBaseWarningInfoService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

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

    @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));
    }


}
