package com.njcn.serviceimpl.user;

import com.njcn.enums.DicDataTypeEnum;
import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.mapper.configuration.LineMapper;
import com.njcn.mapper.user.DeptsMapper;
import com.njcn.mapper.user.UserSetMapper;
import com.njcn.pojo.commons.SimpleValue;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.data.Detailed;
import com.njcn.pojo.data.InfoData;
import com.njcn.pojo.data.Legder;
import com.njcn.pojo.data.LimitrateDays;
import com.njcn.pojo.user.Depts;
import com.njcn.pojo.user.UserSet;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.user.DeptsService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.njcn.utils.redis.RedisCacheUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.annotation.Target;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DeptsServiceImpl implements DeptsService {
    @Resource
    DeptsMapper ds;
    @Resource
    DicDataMapper ddm;

    @Autowired
    private LineMapper lineMapper;

    @Resource
    private UserUtil userUtil;

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    UserSetMapper userSetMapper;

    @Override
    public List<List> getD(@Param(value = "deptsIndex") String deptsIndex) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<List> deptsList = new ArrayList<List>();
        List<Depts> deptsdetail = ds.getId(deptsIndex);
        //查询是否有子节点
        if (deptsdetail.isEmpty()) {
            return deptsList;
        } else {
            for (Depts depts : deptsdetail) {
                List<String> temp = new ArrayList<String>();
                temp.add(depts.getDeptsName());
                String parentNode = depts.getParentNodeId();
                Depts d = ds.selectName(parentNode);
                if (d == null) {
                    temp.add("根节点");
                } else {
                    temp.add(d.getDeptsName());
                }
                temp.add(sdf.format(depts.getUpdateTime()));
                int state = depts.getState();
                String area = depts.getArea();
                DicData data = ddm.selectId(area);
                String areaName = "";
                if (data == null) {
                    areaName = "行政区域被删除";
                } else {
                    areaName = data.getDicName();
                }
                temp.add(areaName);
                String state1 = "";
                int customDept = depts.getCustomDept();
                if (customDept == 0) {
                    temp.add("非自定义");
                } else if (customDept == 1) {
                    temp.add("web自定义");
                } else if (customDept == 2) {
                    temp.add("app自定义");
                }
                temp.add(depts.getDeptsDescription());
                if (state == 1) {
                    state1 = "正常";
                } else {
                    state1 = "删除";
                }
                temp.add(state1);

                if (depts.getParentNodeId().equals("0") || ds.getNodeLength(depts.getDeptsIndex()).size() > 0) {
                    temp.add(
                            "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateDeptsByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                    );
                } else {
                    temp.add("<button type='button' class='btn  themeBGColor ' style='margin-right:10px;' href='javascript:void(0);' onclick='DeptsGdByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-handshake-o'> </i>绑定监测点</button>" +
                            "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateDeptsByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                            + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteDeptsByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-trash'></i>删除</button>"
                    );
                }

                deptsList.add(temp);
            }
        }
        return deptsList;
    }

    @Override
    public List<Depts> selectDeptAll() {
        return ds.selectDeptAll();
    }

    /**
     * 根据传来的区域获取父子节点地图信息
     *
     * @param area 部门索引
     */
    @Override
    public SimpleValue getMapInfo(String area) {
        SimpleValue simpleValue = new SimpleValue();
        Depts depts;
        String areaName;
        if ("overview".equalsIgnoreCase(area)) {
            depts = userUtil.getCurrentDept();
            areaName = depts.getArea();
        } else {
            //根据索引获取部门
            depts = userUtil.getDeptsByIndex(area);
            areaName = depts.getArea();
        }

        //先判断是否为自定义,自定义则取当前身份的
        if (depts.getCustomDept().equals(1)) {
            depts = userUtil.getCurrentDept();
            areaName = depts.getArea();
        }

        //获取父部门
        String parentId = depts.getParentNodeId();
        Depts parentDepts = userUtil.getDeptsByIndex(parentId);
        if (null == parentDepts || "全国".equals(parentDepts.getArea()) || "中国".equals(parentDepts.getArea())) {
            simpleValue.setName(areaName);
        } else {
            simpleValue.setName(parentDepts.getArea());
        }
        simpleValue.setSubName(areaName);
        return simpleValue;
    }

    @Override
    public Depts selectAreaDept(String deptsIndex) {
        Depts depts = new Depts();
        try {
            depts = ds.selectByPrimaryKey(deptsIndex);
        } catch (Exception e) {
            return depts;
        }
        return depts;
    }


    @Override
    public List<List> getDepts() {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<List> deptsList = new ArrayList<List>();
        List<Depts> deptsdetail = ds.selectDeptAll();
        if (deptsdetail.isEmpty()) {
            return deptsList;
        } else {
            for (Depts depts : deptsdetail) {
                List<String> temp = new ArrayList<String>();
                temp.add(depts.getDeptsName());
                String parentNode = depts.getParentNodeId();
                Depts d = ds.selectName(parentNode);
                if (d == null) {
                    temp.add("根节点");
                } else {
                    temp.add(d.getDeptsName());
                }
                temp.add(sdf.format(depts.getUpdateTime()));
                int state = depts.getState();
                String area = depts.getArea();
                DicData data = ddm.selectId(area);
                String areaName = "";
                if (data == null) {
                    areaName = "行政区域被删除";
                } else {
                    areaName = data.getDicName();
                }
                temp.add(areaName);
                String state1 = "";
                int customDept = depts.getCustomDept();
                if (customDept == 0) {
                    temp.add("非自定义");
                } else if (customDept == 1) {
                    temp.add("web自定义");
                } else if (customDept == 2) {
                    temp.add("app自定义");
                }
                temp.add(depts.getDeptsDescription());
                if (state == 1) {
                    state1 = "正常";
                } else {
                    state1 = "删除";
                }
                temp.add(state1);

                if (depts.getParentNodeId().equals("0") || ds.getNodeLength(depts.getDeptsIndex()).size() > 0) {
                    temp.add(
                            "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateDeptsByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                    );
                } else {
                    temp.add("<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='DeptsGdByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-handshake-o'> </i>绑定监测点</button>" +
                            "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateDeptsByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                            + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteDeptsByIndex(\"" + depts.getDeptsIndex() + "\")' ><i class='fa fa-trash'></i>删除</button>"
                    );
                }

                deptsList.add(temp);
            }
        }
        return deptsList;
    }

    @Override
    public Depts getDeptsByLikeName(String name) {
        name = "%" + name + "%";
        return ds.queryDeptsByLikeAreaName(name);
    }


    /**
     * 获取该系统下的所有部门用以新增用户
     */
    @Override
    public List<Depts> selectAll() {
        return ds.selectDeptAlls();
    }


    @Override
    public String saveDepts(String deptsName, String deptsDescription, String parentNodeId, String area, int customDept) {
        // TODO Auto-generated method stub
        //进行正则判断
        if (!StringUtils.isBlank(deptsName)) {
            if (!StringUtils.isBlank(area)) {
                if (PubUtils.patternDepts(deptsName)) {
                    //部门名不能重复
                    Depts depts = new Depts();
                    depts.setDeptsName(deptsName);
                    depts.setState(1);
                    depts = ds.selectOne(depts);
                    List<Depts> lists = new ArrayList<Depts>();
                    String node = "";
                    if (null == depts) {
                        Depts depts2 = new Depts();
                        depts2.setDeptsIndex(UUID.randomUUID().toString());
                        depts2.setDeptsName(deptsName);
                        depts2.setArea(area);
                        depts2.setParentNodeId(parentNodeId);
                        depts2.setDeptsDesc(ds.getDeficDesc(parentNodeId));
                        User user = TokenManager.getToken();
                        depts2.setUserIndex(user.getUserIndex());
                        depts2.setUpdateTime(new Date());
                        depts2.setCustomDept(customDept);
                        depts2.setDeptsDescription(deptsDescription);
                        depts2.setState(1);//1为正常
                        try {
                            int flag = ds.insert(depts2);
                            redisCacheUtil.clearConData();
                            return "新增部门成功！";
                        } catch (Exception e) {
                            return "新增部门失败，请稍后再试。";
                        }
                    } else {
                        return "部门名称已存在";
                    }
                } else {
                    return "部门名称由汉字组成，长度为0-20";
                }
            } else {
                return "区域不可为空";
            }
        } else {
            return "部门名称不可为空";
        }
    }


    @Override
    public String deleteDeptsByIndex(String index) {
        // TODO Auto-generated method stub
        //先判断部门是否存在
        Depts temp = new Depts();
        temp.setDeptsIndex(index);
        temp = ds.selectOne(temp);
        if (temp.getParentNodeId().equals("0")) {
            return "该部门为省级部门，不可删除";
        }
        if (null == temp) {
            return "该部门不存在，已删除";
        } else {
            try {
                UserSet userSet = new UserSet();
                userSet.setDeptsIndex(index);
                userSet = userSetMapper.selectOne(userSet);
                if (userSet == null) {
                    temp.setState(0);
                    ds.updateByPrimaryKey(temp);
                    redisCacheUtil.clearConData();
                    return "删除部门成功！";
                } else {
                    return "删除部门失败！该部门已被用户所绑定";
                }
            } catch (Exception e) {
                return "删除部门失败，请稍后再试";
            }
        }
    }

    @Override
    public Depts getAll(String deptsIndex) {
        // TODO Auto-generated method stub
        Depts depts = ds.selectByPrimaryKey(deptsIndex);
        String yh = TokenManager.getSysType();
        System.out.printf(yh);
        //获取对应的区域
        List<DicData> areas = ddm.setAreaNo(deptsIndex, TokenManager.getSysType(), DicDataTypeEnum.areaEnum.getName());
        depts.setAreas(areas);
        return depts;
    }

    @Override
    public Depts getAllArea(String deptsIndex) {
        // TODO Auto-generated method stub
        Depts depts = ds.selectByPrimaryKey(deptsIndex);
        //获取对应的区域
        List<DicData> areas = ddm.getMangType(DicDataTypeEnum.areaEnum.getName());
        depts.setAreas(areas);
        return depts;
    }


    @Override
    public int updateDepts(Depts depts) {
        // TODO Auto-generated method stub
        redisCacheUtil.clearConData();
        return ds.updateDepts(depts);


    }


    @Override
    public List<Depts> selName(String deptsIndex) {
        // TODO Auto-generated method stub
        return ds.selName(deptsIndex);
    }


    @Override
    public Depts selectName(String parentNodeId) {
        // TODO Auto-generated method stub
        return ds.selectName(parentNodeId);
    }


    @Override
    public List<Depts> selparentnode(String deptsIndex) {
        // TODO Auto-generated method stub
        return ds.selparentnode(deptsIndex);
    }


    @Override
    public List<Depts> getId(String deptsIndex) {
        return ds.getId(deptsIndex);
    }


    @Override
    public List<LimitrateDays> getLimitRateDays(String name,Date start, Date end, int type, int... to) {
        Depts depts = userUtil.getDeptsByUser(TokenManager.getToken());
        List<Depts> listDepts = lineMapper.getSubDeptsIndex(depts.getDeptsIndex(), null);
        List<Integer> listAll = new ArrayList<>(); //所有监测点信息
        Map<String, String> map = new HashMap<>();

        if (listDepts == null || listDepts.size() == 0) { //没有子部门
            listDepts.add(depts);
        }

        String str = "(c.flicker_overtime > 0 or c.ubalance_overtime > 0 or c.freq_dev_overtime > 0 or c.uaberrance_overtime > 0 or c.voltage_dev_overtime > 0 ";

        for (int k = 2; k < 26; k++) {
            str += "or c.uharm_" + k + "_overtime > 0 or c.iharm_" + k + "_overtime > 0 ";
        }

        str += ")";

        List<LimitrateDays> limitrateDays = new ArrayList<>();

        for (Depts depts1 : listDepts) {
            List<Integer> list = lineMapper.getDeptsLineIndex(depts1.getDeptsIndex(), userUtil.getSysType(), false);

            if (list == null || list.size() == 0) { //子部门为最后一个层级
                list = lineMapper.getDeptsLineIndexs(depts1.getDeptsIndex(), userUtil.getSysType(), false);
            }

            if (list != null && list.size() > 0) {
                listAll.addAll(list);
                listAll = listAll.stream().distinct().collect(Collectors.toList());
                //如果是行政区域按这种方式统计处理
                if (type == 0) {
                    map = new HashMap<>();

                    if (depts1.getCustomDept().intValue() == 1) {
                        map.put(depts1.getDeptsIndex(), depts1.getDeptsName());
                    } else {
                        map.put(depts1.getDeptsIndex(), depts1.getAreaName());
                    }

                    //分片查询
                    if (list.size() > 1000) {
                        int times = ((list.size() % 1000 == 0) ? list.size() / 1000 : (list.size() / 1000 + 1));
                        for (int i = 1; i <= times; i++) {
                            if (list.size() > 1000) {
                                List<Integer> temp = list.subList(0, 1000);
                                List<InfoData> listAreaTotal = lineMapper.getAreaBytimeZero(temp, str, start, end, 0, depts1.getDeptsIndex());
                                List<InfoData> listAreaData = lineMapper.getAreaBytimeOne(temp, str, start, end, 1, depts1.getDeptsIndex());

                                translateData(map, listAreaTotal, listAreaData, start, end, limitrateDays);
                                temp.clear();
                            } else {
                                List<InfoData> listAreaTotal = lineMapper.getAreaBytimeZero(list, str, start, end, 0, depts1.getDeptsIndex());
                                List<InfoData> listAreaData = lineMapper.getAreaBytimeOne(list, str, start, end, 1, depts1.getDeptsIndex());

                                translateData(map, listAreaTotal, listAreaData, start, end, limitrateDays);
                            }
                        }
                    } else {
                        List<InfoData> listAreaTotal = lineMapper.getAreaBytimeZero(list, str, start, end, 0, depts1.getDeptsIndex());
                        List<InfoData> listAreaData = lineMapper.getAreaBytimeOne(list, str, start, end, 1, depts1.getDeptsIndex());

                        translateData(map, listAreaTotal, listAreaData, start, end, limitrateDays);
                    }
                }
            }
        }

        getMapData(name,listAll, map, type);

        if (listAll.size() > 1000) {
            int times = ((listAll.size() % 1000 == 0) ? listAll.size() / 1000 : (listAll.size() / 1000 + 1));

            for (int i = 1; i <= times; i++) {
                if (listAll.size() > 1000) {
                    List<Integer> temp = listAll.subList(0, 1000);

                    switch (type) {
                        case 1:
                            List<InfoData> listLoadTypeTotal = lineMapper.getLoadTypeBytimeZero(temp, str, start, end, 0);
                            List<InfoData> listLoadTypeData = lineMapper.getLoadTypeBytimeOne(temp, str, start, end, 1);

                            translateData(map, listLoadTypeTotal, listLoadTypeData, start, end, limitrateDays);
                            temp.clear();
                            break;
                        case 2:
                            List<InfoData> listVoltageTotal = lineMapper.getVoltageBytimeZero(temp, str, start, end, 0);
                            List<InfoData> listVoltageData = lineMapper.getVoltageBytimeOne(temp, str, start, end, 1);

                            translateData(map, listVoltageTotal, listVoltageData, start, end, limitrateDays);
                            temp.clear();
                            break;
                        case 3:
                            List<InfoData> listMonitorTotal = lineMapper.getMonitorBytimeZero(temp, str, start, end, 0);
                            List<InfoData> listMonitorData = lineMapper.getMonitorBytimeOne(temp, str, start, end, 1);

                            for (InfoData infoData : listMonitorTotal) {
                                if (infoData.getDicName() == null) {
                                    infoData.setDicName("非"+name+"上报");
                                    infoData.setDicIndex("0");
                                } else {
                                    infoData.setDicName(name+"上报");
                                    infoData.setDicIndex("1");
                                }
                            }

                            for (InfoData infoData : listMonitorData) {
                                if (infoData.getDicName() == null) {
                                    infoData.setDicName("非"+name+"上报");
                                    infoData.setDicIndex("0");
                                } else {
                                    infoData.setDicName(name+"上报");
                                    infoData.setDicIndex("1");
                                }
                            }

                            translateData(map, listMonitorTotal, listMonitorData, start, end, limitrateDays);
                            break;
                    }
                } else {
                    switch (type) {
                        case 1:
                            List<InfoData> listLoadTypeTotal = lineMapper.getLoadTypeBytimeZero(listAll, str, start, end, 0);
                            List<InfoData> listLoadTypeData = lineMapper.getLoadTypeBytimeOne(listAll, str, start, end, 1);

                            translateData(map, listLoadTypeTotal, listLoadTypeData, start, end, limitrateDays);
                            break;
                        case 2:
                            List<InfoData> listVoltageTotal = lineMapper.getVoltageBytimeZero(listAll, str, start, end, 0);
                            List<InfoData> listVoltageData = lineMapper.getVoltageBytimeOne(listAll, str, start, end, 1);

                            translateData(map, listVoltageTotal, listVoltageData, start, end, limitrateDays);
                            break;
                        case 3:
                            List<InfoData> listMonitorTotal = lineMapper.getMonitorBytimeZero(listAll, str, start, end, 0);
                            List<InfoData> listMonitorData = lineMapper.getMonitorBytimeOne(listAll, str, start, end, 1);

                            for (InfoData infoData : listMonitorTotal) {
                                if (infoData.getDicName() == null) {
                                    infoData.setDicName("非"+name+"上报");
                                    infoData.setDicIndex("0");
                                } else {
                                    infoData.setDicName(name+"上报");
                                    infoData.setDicIndex("1");
                                }
                            }

                            for (InfoData infoData : listMonitorData) {
                                if (infoData.getDicName() == null) {
                                    infoData.setDicName("非"+name+"上报");
                                    infoData.setDicIndex("0");
                                } else {
                                    infoData.setDicName(name+"上报");
                                    infoData.setDicIndex("1");
                                }
                            }

                            translateData(map, listMonitorTotal, listMonitorData, start, end, limitrateDays);
                            break;
                    }
                }
            }
        } else {
            switch (type) {
                case 1:
                    List<InfoData> listLoadTypeTotal = lineMapper.getLoadTypeBytimeZero(listAll, str, start, end, 0);
                    List<InfoData> listLoadTypeData = lineMapper.getLoadTypeBytimeOne(listAll, str, start, end, 1);

                    translateData(map, listLoadTypeTotal, listLoadTypeData, start, end, limitrateDays);
                    break;
                case 2:
                    List<InfoData> listVoltageTotal = lineMapper.getVoltageBytimeZero(listAll, str, start, end, 0);
                    List<InfoData> listVoltageData = lineMapper.getVoltageBytimeOne(listAll, str, start, end, 1);

                    translateData(map, listVoltageTotal, listVoltageData, start, end, limitrateDays);
                    break;
                case 3:
                    List<InfoData> listMonitorTotal = lineMapper.getMonitorBytimeZero(listAll, str, start, end, 0);
                    List<InfoData> listMonitorData = lineMapper.getMonitorBytimeOne(listAll, str, start, end, 1);

                    for (InfoData infoData : listMonitorTotal) {
                        if (infoData.getDicName() == null) {
                            infoData.setDicName("非"+name+"上报");
                            infoData.setDicIndex("0");
                        } else {
                            infoData.setDicName(name+"上报");
                            infoData.setDicIndex("1");
                        }
                    }

                    for (InfoData infoData : listMonitorData) {
                        if (infoData.getDicName() == null) {
                            infoData.setDicName("非"+name+"上报");
                            infoData.setDicIndex("0");
                        } else {
                            infoData.setDicName(name+"上报");
                            infoData.setDicIndex("1");
                        }
                    }

                    translateData(map, listMonitorTotal, listMonitorData, start, end, limitrateDays);
                    break;
            }
        }

        if (to.length > 0 && to[0] == 1) { //自定义时间汇总
            for (LimitrateDays limitrateDays1 : limitrateDays) {
                List<LimitrateDays.Result> list = new ArrayList<>();
                LimitrateDays.Result rr = new LimitrateDays().new Result();
                Float f = null;
                int count = 0;

                for (LimitrateDays.Result result : limitrateDays1.getList()) {
                    if (result.getTotal() == null) {
                        continue;
                    } else {
                        f = (f == null ? 0f : f);
                        f += (float) result.getData() / result.getTotal();
                        count++;
                    }
                }

                if (f == null) {
                    rr.setTarget(f);
                } else {
                    rr.setTarget((float) (Math.round(f / count * 10000)) / 100);
                }

                list.add(rr);
                limitrateDays1.setList(list);
            }
        } else {
            Calendar calendar = Calendar.getInstance();
            Calendar calendar1 = Calendar.getInstance();
            calendar.setTime(start);
            calendar1.setTime(end);

            //获取年份
            int year = calendar.get(Calendar.YEAR);
            int year1 = calendar1.get(Calendar.YEAR);
            //获取月份
            int month = calendar.get(Calendar.MONTH) + 1;
            int month1 = calendar1.get(Calendar.MONTH) + 1;
            //获取起止时间的天数
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int day1 = calendar1.get(Calendar.DAY_OF_MONTH);
            //相隔几年
            int offset = year1 - year;
            //循环次数
            int index = offset * 12 + month1 - month;

            if (month1 - month > 0 || year1 - year > 0) { //按月汇总统计
                for (LimitrateDays limitrateDays1 : limitrateDays) {
                    List<LimitrateDays.Result> list = new ArrayList<>();
                    int yearIndex = 0;

                    for (int i = 0; i <= index; i++) { //循环的是月
                        int  mon  = (i + month) % 12 == 0 ? 12 : (i + month) % 12;
                        LimitrateDays.Result rr = new LimitrateDays().new Result();
                        Float f = null;
                        int count = 0;

                        if (mon == 1 && i > 0) {
                            yearIndex++;
                        }

                        rr.setYear(year + yearIndex);
                        rr.setMonth(mon);

                        Calendar calendar2 = Calendar.getInstance();
                        calendar2.set(Calendar.MONTH, mon);
                        int monthEnd = (i == index ? day1 : calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
                        int monthStart = (i == 0 ? day : 1);

                        for (int k = monthStart; k <= monthEnd; k++) { //天数循环
                            for (LimitrateDays.Result result : limitrateDays1.getList()) {
                                if (result.getMonth().intValue() == mon && result.getYear().intValue() == year + yearIndex && result.getDay().intValue() == k) {
                                    if (result.getTotal() == null) {
                                        continue;
                                    } else {
                                        f = (f == null ? 0f : f);
                                        f += (float) result.getData() / result.getTotal();
                                        count++;
                                    }
                                }
                            }
                        }

                        if (f == null) {
                            rr.setTarget(f);
                        } else {
                            rr.setTarget((float) (Math.round(f / count * 10000)) / 100);
                        }

                        list.add(rr);
                    }

                    limitrateDays1.setList(list);
                }
            } else { //按天统计
                for (LimitrateDays limitrateDays1 : limitrateDays) {
                    for (LimitrateDays.Result result : limitrateDays1.getList()) {
                        Float f = 0f;

                        if (result.getTotal() == null) {
                            f = null;
                        } else {
                            f = (float) (Math.round((float) result.getData().intValue() / result.getTotal().intValue() * 10000)) / 100;
                        }

                        result.setTarget(f);
                    }
                }
            }
        }
        if (type == 2) {
            //如果是电压等级，则排序处理
            limitrateDays.sort(null);
        }
        return limitrateDays;
    }

    private void translateData(Map<String, String> map, List<InfoData> totalData, List<InfoData> data,
                               Date start, Date end, List<LimitrateDays> limitrateDays) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(end);

        //获取年份
        int year = calendar.get(Calendar.YEAR);
        int year1 = calendar1.get(Calendar.YEAR);
        //获取月份
        int month = calendar.get(Calendar.MONTH) + 1;
        int month1 = calendar1.get(Calendar.MONTH) + 1;
        //获取起止时间的天数
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int day1 = calendar1.get(Calendar.DAY_OF_MONTH);
        //相隔几年
        int offset = year1 - year;
        //循环次数
        int index = offset * 12 + month1 - month;

        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {
            Iterator<LimitrateDays> iter = limitrateDays.iterator();
            Map.Entry<String, String> m = iterator.next();
            List<LimitrateDays.Result> list = new ArrayList<>();
            LimitrateDays limitrateDays1 = new LimitrateDays();
            boolean bb = false;

            while (iter.hasNext()) {
                LimitrateDays limitrateDays2 = iter.next();

                if (limitrateDays2.getIndex().equals(m.getKey())) {
                    limitrateDays1 = limitrateDays2;
                    list = limitrateDays1.getList();
                    bb = true;
                    break;
                }
            }

            if (!bb) {
                limitrateDays1.setIndex(m.getKey());
                limitrateDays1.setName(m.getValue());
            }

            int yearIndex = 0;
            for (int i = 0; i <= index; i++) { //循环的是月
                //月份
                int  mon  = (i + month) % 12 == 0 ? 12 : (i + month) % 12;

                if (mon == 1 && i > 0) {
                    yearIndex++;
                }

                Calendar calendar2 = Calendar.getInstance();
                calendar2.set(Calendar.MONTH, mon);
                int monthEnd = (i == index ? day1 : calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
                int monthStart = (i == 0 ? day : 1);

                for (int k = monthStart; k <= monthEnd; k++) { //天数循环
                    Iterator<InfoData> iterator1 = totalData.iterator();
                    LimitrateDays.Result result = new LimitrateDays().new Result();

                    if (!bb) {
                        result.setYear(year + yearIndex);
                        result.setMonth(mon);
                        result.setDay(k);
                    } else {
                        for (LimitrateDays.Result result1 : list) {
                            if ((result1.getDay().intValue() == k) && (result1.getYear().intValue() == year + yearIndex)
                                    && (result1.getMonth().intValue() == mon)) {
                                result = result1;
                            }
                        }
                    }

                    while (iterator1.hasNext()) {
                        InfoData infoData = iterator1.next();

                        if (infoData.getDicIndex().equals(m.getKey())) {
                            Calendar calendar3 = Calendar.getInstance();
                            calendar3.setTime(infoData.getTotalDate());

                            int mDay = calendar3.get(Calendar.DAY_OF_MONTH);
                            int mMonth = calendar3.get(Calendar.MONTH) + 1;
                            int mYear = calendar3.get(Calendar.YEAR);

                            if ((mDay == k) && (mYear == year + yearIndex) && (mMonth == mon)) {
                                Iterator<InfoData> iterator2 = data.iterator();
                                result.setTotal((result.getTotal() == null ? 0 : result.getTotal().intValue()) + infoData.getCountTotal().intValue());

                                while (iterator2.hasNext()) {
                                    InfoData infoData1 = iterator2.next();

                                    if (infoData1.getDicName().equals(infoData.getDicName()) && infoData1.getTotalDate().equals(infoData.getTotalDate())) {
                                        result.setData(result.getData().intValue() + infoData1.getCountTotal().intValue());
                                        break;
                                    } else if (!iterator2.hasNext()) {
                                        result.setData(result.getData().intValue() + 0);//没查到数据不越限的数据，就是都越限，符合性为0
                                        break;
                                    }
                                }

                                break;
                            } else if (!iterator1.hasNext()) {
                                result.setTotal(null);
                                result.setData(result.getData().intValue() + 0); ////没有数据默认为不展示符合性,分子置为0
                            }
                        }
                    }

                    if (!bb) {
                        list.add(result);
                    }
                }
            }

            if (!bb) {
                limitrateDays1.setList(list);
                limitrateDays.add(limitrateDays1);
            }
        }
    }

    private void getMapData(String name,List<Integer> list, Map<String, String> map, int type) {
        if (list.size() > 1000) {
            int times = ((list.size() % 1000 == 0) ? list.size() / 1000 : (list.size() / 1000 + 1));

            for (int i = 1; i <= times; i++) {
                if (list.size() > 1000) {
                    List<Integer> temp = list.subList(0, 1000);

                    switch (type) {
                        case 2:
                            List<InfoData> list1 = lineMapper.getVoltage(temp);

                            for (InfoData infoData : list1) {
                                map.put(infoData.getDicIndex(), infoData.getDicName());
                            }
                            temp.clear();
                            break;
                        case 1:
                            List<InfoData> list2 = lineMapper.getLoadType(temp);

                            for (InfoData infoData : list2) {
                                map.put(infoData.getDicIndex(), infoData.getDicName());
                            }
                            temp.clear();
                            break;
                        case 3:
                            map.put("0", name+"上报");
                            map.put("1", "非"+name+"上报");
                            break;
                    }
                } else {
                    switch (type) {
                        case 2:
                            List<InfoData> list1 = lineMapper.getVoltage(list);

                            for (InfoData infoData : list1) {
                                map.put(infoData.getDicIndex(), infoData.getDicName());
                            }
                            break;
                        case 1:
                            List<InfoData> list2 = lineMapper.getLoadType(list);

                            for (InfoData infoData : list2) {
                                map.put(infoData.getDicIndex(), infoData.getDicName());
                            }
                            break;
                        case 3:
                            map.put("0", name+"上报");
                            map.put("1", "非"+name+"上报");
                            break;
                    }
                }
            }
        } else {
            switch (type) {
                case 2:
                    List<InfoData> list1 = lineMapper.getVoltage(list);

                    for (InfoData infoData : list1) {
                        map.put(infoData.getDicIndex(), infoData.getDicName());
                    }
                    break;
                case 1:
                    List<InfoData> list2 = lineMapper.getLoadType(list);

                    for (InfoData infoData : list2) {
                        map.put(infoData.getDicIndex(), infoData.getDicName());
                    }
                    break;
                case 3:
                    map.put("0", name+"上报");
                    map.put("1", "非"+name+"上报");
                    break;
            }
        }
    }


    @Override
    public List<Detailed> getDetailed(Date start, Date end, String guid, String type) {
        List<Integer> list = new ArrayList<>();

        for (int i = 2; i < 26; i++) {
            list.add(i);
        }

        Depts depts = userUtil.getDeptsByUser(TokenManager.getToken());
        List<Integer> listLine;

        if (type.equals("2")) { //电压等级
            listLine = lineMapper.getPointIndexbyVoltage(depts.getDeptsIndex(), userUtil.getSysType(), guid);//获取所有lineid
        } else if (type.equals("1")) { //干扰源类型终
            listLine = lineMapper.getPointIndexbyType(depts.getDeptsIndex(), userUtil.getSysType(), guid);//获取所有lineid
        } else if (type.equals("3")) { //国网上报
            listLine = lineMapper.getPointbyMonitor(depts.getDeptsIndex(), userUtil.getSysType(), guid);
        } else { //行政区域
            listLine = lineMapper.getDeptsLineIndex(guid, userUtil.getSysType(), false);

            if (listLine == null || listLine.size() == 0) { //子部门为最后一个层级
                listLine = lineMapper.getDeptsLineIndexs(guid, userUtil.getSysType(), false);
            }
        }

        //分片1000个点
        List<Detailed> list1 = new ArrayList<>();

        if (listLine.size() > 1000) {
            int times = ((listLine.size() % 1000 == 0) ? listLine.size() / 1000 : (listLine.size() / 1000 + 1));

            for (int i = 1; i <= times; i++) {
                if (listLine.size() > 1000) {
                    List<Integer> temp = listLine.subList(0, 1000);
                    List<Detailed> list2 = lineMapper.getDetailed(start, end, temp, list);
                    list1.addAll(list2);
                    temp.clear();
                } else {
                    List<Detailed> list2 = lineMapper.getDetailed(start, end, listLine, list);
                    list1.addAll(list2);
                }
            }
        } else {
            list1 = lineMapper.getDetailed(start, end, listLine, list);
        }


        for (Detailed detailed : list1) {
            detailed.setValue();
        }

        return list1;
    }
}
