package com.pqs9200.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.njcn.mapper.configuration.LineMapper;
import com.njcn.mapper.configuration.TreeMapper;
import com.njcn.mapper.data.SynDataMapper;
import com.njcn.pojo.commons.device.Tree;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.data.SynData;
import com.njcn.service.configuration.TreeService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.AppConfig;
import com.njcn.utils.BalanceInfo;
import com.njcn.utils.UserUtil;

@Component
public class SynDataTask {
    private static final Logger logger = LoggerFactory.getLogger(SynDataTask.class);
    @Autowired
    private AppConfig appConfig;
    
    @Resource
    private TreeService getDeviceTree;

    @Resource
    private TreeMapper treeMapper;

    @Resource
    private LineMapper lineMapper;
    
    @Resource
    private SynDataMapper synDataMapper;
    	
    @Autowired
    private UserUtil userUtil;

    @Autowired
    private BalanceInfo balanceInfo;
    
    @Scheduled(cron = "0 0 6 * * ?")
    public void getSynData() {
    	Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        Date endTime = calendar.getTime();
        
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        Date startTime = calendar.getTime();

        try {
            getTreedata(null, startTime, endTime);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        
    }
    
    public void getTreedata(String name, Date startTime, Date endTime) {
        String dky = "设备树";

        int pIdNumber = Integer.parseInt(appConfig.getPidNumber());

        List<Tree> list = new ArrayList<>();

        List<Integer> index = lineMapper.listAllIndex();

        if (index.size() == 0) {
            Tree dto = new Tree();
            dto.setId(100);
            dto.setNodeType(0);
            dto.setpId(0);
            dto.setPpId(0);
            dto.setName(dky);
            list.add(dto);
        } else {
        	Tree dt = new Tree();
            dt.setId(100);
            dt.setNodeType(0);
            dt.setpId(0);
            dt.setPpId(0);
            dt.setName(dky);
            list.add(dt);
            
        	List<Tree> proInfo = treeMapper.getTreeProInfo(index);
            List<Tree> gdInfo = treeMapper.getTreeGdInfo(index);
            List<Tree> subInfo = treeMapper.getTreeSubInfo(index);
            List<Tree> devInfo = new ArrayList<>();
            if (appConfig.getSysType() == appConfig.getSysTypeWt()) {
                devInfo = treeMapper.getWTTreeDevInfo(index);
            } else {
                devInfo = treeMapper.getZTTreeDevInfo(index);
            }
            List<Tree> subvInfo = treeMapper.getTreeSubvInfo(index);
            List<Tree> lineInfo = treeMapper.getLineInfo(index);
            if(proInfo == null || gdInfo == null || subInfo == null || devInfo == null || subvInfo == null || lineInfo == null){

            }

            Map<Integer, Integer> value0 = new HashMap<Integer, Integer>();
            int p0 = pIdNumber;
            for (int i = 0; i < proInfo.size(); i++) {
                Tree dto = new Tree();
                dto.setId(++p0);
                dto.setName(proInfo.get(i).getName());
                dto.setpId(100);
                dto.setPpId(100);
                dto.setNodeIndex(proInfo.get(i).getNodeIndex());
                dto.setNodeType(1);

                int proIndex = proInfo.get(i).getNodeIndex();
                List<Line> lines = lineMapper.getLinesByPro(proIndex);
                List<Integer> indexs = new ArrayList<>();
                for (int n = 0; n < lines.size(); n++) {
                    indexs.add(lines.get(n).getLineIndex().intValue());
                }
                Float synDatas = getSynData(indexs, startTime, endTime);
                int lvl = getLevel(synDatas);
                dto.setQualityAss(lvl);
                list.add(dto);
                SynData data1 = new SynData();
                data1.setMyIndex(proIndex);
                data1.setNode(1l);
                data1.setSyndata(synDatas);
                synDataMapper.deleteByIndex(proIndex, 1l);
                int flag1 = synDataMapper.insert(data1);
                value0.put(proInfo.get(i).getNodeIndex(), p0);
            }
            
            Map<Integer, Integer> value = new HashMap<Integer, Integer>();
            int p = pIdNumber*2;
            for (int i = 0; i < gdInfo.size(); i++) {
                Tree dto = new Tree();
                dto.setId(++p);
                dto.setName(gdInfo.get(i).getName());
                dto.setpId(value0.get(gdInfo.get(i).getpId()));
                dto.setPpId(gdInfo.get(i).getpId());
                dto.setNodeIndex(gdInfo.get(i).getNodeIndex());
                dto.setNodeType(2);

                Line gd_line = new Line();
                gd_line.setGdIndex((long) gdInfo.get(i).getNodeIndex());
                List<Line> lines = lineMapper.select(gd_line);
                List<Integer> indexs = new ArrayList<>();
                for (int n = 0; n < lines.size(); n++) {
                    indexs.add(lines.get(n).getLineIndex().intValue());
                }
                Float synDatas = getSynData(indexs, startTime, endTime);
                int lvl = getLevel(synDatas);
                dto.setQualityAss(lvl);
                list.add(dto);
                SynData data2 = new SynData();
                data2.setMyIndex(gdInfo.get(i).getNodeIndex());
                data2.setNode(2l);
                data2.setSyndata(synDatas);
                synDataMapper.deleteByIndex(gdInfo.get(i).getNodeIndex(), 2l);
                int flag2 = synDataMapper.insert(data2);
                value.put(gdInfo.get(i).getNodeIndex(), p);
            }

            Map<Integer, Integer> value1 = new HashMap<Integer, Integer>();
            int p1 = pIdNumber * 3;
            for (int i = 0; i < subInfo.size(); i++) {
                if (value.get(subInfo.get(i).getpId()) != null) {
                    Tree dto = new Tree();
                    dto.setId(++p1);
                    dto.setName(subInfo.get(i).getName());
                    dto.setpId(value.get(subInfo.get(i).getpId()));
                    dto.setPpId(subInfo.get(i).getpId());
                    dto.setNodeIndex(subInfo.get(i).getNodeIndex());
                    dto.setNodeType(3);

                    Line sub_line = new Line();
                    sub_line.setSubIndex((long) subInfo.get(i).getNodeIndex());
                    List<Line> lines = lineMapper.select(sub_line);
                    List<Integer> indexs = new ArrayList<>();
                    for (int n = 0; n < lines.size(); n++) {
                        indexs.add(lines.get(n).getLineIndex().intValue());
                    }
                    Float synDatas = getSynData(indexs, startTime, endTime);
                    int lvl = getLevel(synDatas);
                    dto.setQualityAss(lvl);
                    list.add(dto);
                    SynData data3 = new SynData();
                    data3.setMyIndex(subInfo.get(i).getNodeIndex());
                    data3.setNode(3l);
                    data3.setSyndata(synDatas);
                    synDataMapper.deleteByIndex(subInfo.get(i).getNodeIndex(), 3l);
                    int flag3 = synDataMapper.insert(data3);
                    value1.put(subInfo.get(i).getNodeIndex(), p1);
                }
            }

            Map<Integer, Integer> value2 = new HashMap<Integer, Integer>();
            int p2 = pIdNumber * 4;
            for (int i = 0; i < devInfo.size(); i++) {
                if (value1.get(devInfo.get(i).getpId()) != null) {
                    Tree dto = new Tree();
                    dto.setId(++p2);
                    dto.setName(devInfo.get(i).getName());
                    dto.setpId(value1.get(devInfo.get(i).getpId()));
                    dto.setPpId(devInfo.get(i).getpId());
                    dto.setNodeIndex(devInfo.get(i).getNodeIndex());
                    dto.setNodeType(4);
                    dto.setIp(devInfo.get(i).getIp());

                    Line dev_line = new Line();
                    dev_line.setDevIndex((long) devInfo.get(i).getNodeIndex());
                    List<Line> lines = lineMapper.select(dev_line);
                    List<Integer> indexs = new ArrayList<>();
                    for (int n = 0; n < lines.size(); n++) {
                        indexs.add(lines.get(n).getLineIndex().intValue());
                    }
                    Float synDatas = getSynData(indexs, startTime, endTime);
                    int lvl = getLevel(synDatas);
                    dto.setQualityAss(lvl);
                    list.add(dto);
                    SynData data4 = new SynData();
                    data4.setMyIndex(devInfo.get(i).getNodeIndex());
                    data4.setNode(4l);
                    data4.setSyndata(synDatas);
                    synDataMapper.deleteByIndex(devInfo.get(i).getNodeIndex(), 4l);
                    int flag4 = synDataMapper.insert(data4);
                    value2.put(devInfo.get(i).getNodeIndex(), p2);
                }
            }

            Map<Integer, Integer> value3 = new HashMap<Integer, Integer>();
            int p3 = pIdNumber * 5;
            for (int i = 0; i < subvInfo.size(); i++) {
                if (value2.get(subvInfo.get(i).getpId()) != null) {
                    Tree dto = new Tree();
                    dto.setId(++p3);
                    dto.setName(subvInfo.get(i).getName());
                    dto.setpId(value2.get(subvInfo.get(i).getpId()));
                    dto.setPpId(subvInfo.get(i).getpId());
                    dto.setNodeIndex(subvInfo.get(i).getNodeIndex());
                    dto.setNodeType(5);

                    Line subv_line = new Line();
                    subv_line.setSubvIndex((long) subvInfo.get(i).getNodeIndex());
                    List<Line> lines = lineMapper.select(subv_line);
                    List<Integer> indexs = new ArrayList<>();
                    for (int n = 0; n < lines.size(); n++) {
                        indexs.add(lines.get(n).getLineIndex().intValue());
                    }
                    Float synDatas = getSynData(indexs, startTime, endTime);
                    int lvl = getLevel(synDatas);
                    dto.setQualityAss(lvl);
                    list.add(dto);
                    SynData data5 = new SynData();
                    data5.setMyIndex(subvInfo.get(i).getNodeIndex());
                    data5.setNode(5l);
                    data5.setSyndata(synDatas);
                    synDataMapper.deleteByIndex(subvInfo.get(i).getNodeIndex(), 5l);
                    int flag5 = synDataMapper.insert(data5);
                    value3.put(subvInfo.get(i).getNodeIndex(), p3);
                }
            }

            int p4 = pIdNumber * 6;
            for (int i = 0; i < lineInfo.size(); i++) {
                if (value3.get(lineInfo.get(i).getpId()) != null) {
                    Tree dto = new Tree();
                    dto.setId(++p4);
                    dto.setName(lineInfo.get(i).getName());
                    dto.setpId(value3.get(lineInfo.get(i).getpId()));
                    dto.setPpId(lineInfo.get(i).getpId());
                    dto.setNodeIndex(lineInfo.get(i).getNodeIndex());
                    dto.setNodeType(6);
                    dto.setState(lineInfo.get(i).getState());
                    dto.setStatus(lineInfo.get(i).getStatus());

                    List<Integer> indexs = new ArrayList<>();
                    indexs.add(lineInfo.get(i).getNodeIndex());
                    Float synDatas = getSynData(indexs, startTime, endTime);
                    int lvl = getLevel(synDatas);
                    dto.setQualityAss(lvl);
                    list.add(dto);
                    SynData data6 = new SynData();
                    data6.setMyIndex(lineInfo.get(i).getNodeIndex());
                    data6.setNode(6l);
                    data6.setSyndata(synDatas);
                    synDataMapper.deleteByIndex(lineInfo.get(i).getNodeIndex(), 6l);
                    int flag6 = synDataMapper.insert(data6);
                }
            }

        }
    }
    
    private Float getSynData(List<Integer> lineIndexs, Date startTime, Date endTime) {
        Float synData = 0f;
        synData = balanceInfo.getBalanceInfo(lineIndexs, startTime, endTime);
        if(synData == null){
        	synData = 3.14159f;
        }
        return synData;
    }
    
    private int getLevel(Float synDatas) {
        if (appConfig.getSysType() == appConfig.getSysTypeWt()) {
            if (synDatas == 3.14159f) {
                return 0;
            }
            if (0 <= synDatas && synDatas < 3) {
                return 1;
            } else if (3 <= synDatas && synDatas < 4) {
                return 2;
            } else if (4 <= synDatas && synDatas < 4.5) {
                return 3;
            } else {
                return 4;
            }
        } else {
            if (synDatas == null || synDatas == 3.14159f) {
                return 0;
            }
            if (0 <= synDatas && synDatas < 0.4) {
                return 1;
            } else if (0.4 <= synDatas && synDatas < 0.8) {
                return 2;
            } else if (0.8 <= synDatas && synDatas < 1.2) {
                return 3;
            } else {
                return 4;
            }
        }
    }
}
