package com.njcn.serviceimpl.excelreport;


import com.njcn.mapper.excelreport.GetFpyDataMapper;
import com.njcn.pojo.excelreport.ConditionData;
import com.njcn.pojo.excelreport.FpyByType;
import com.njcn.pojo.excelreport.FpyRateData;
import com.njcn.pojo.excelreport.enumReport.EnumPassData;
import com.njcn.service.excelreport.GetFpyDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GetFpyDataServiceImpl implements GetFpyDataService {
    @Autowired
    private GetFpyDataMapper getFpyDataMapper;

    @Override
    public List<FpyByType> getFpy(ConditionData conditionData) {
        FpyByType[] fpyByTypes = new FpyByType[]{
                new FpyByType(EnumPassData.FPYV),
                new FpyByType(EnumPassData.FPYI),
                new FpyByType(EnumPassData.FPYVOLTAGE),
                new FpyByType(EnumPassData.FPYTHREE),
                new FpyByType(EnumPassData.FPYTHDV),
                new FpyByType(EnumPassData.FPYRATE),
                new FpyByType(EnumPassData.FPYFLICKER),
        };

        List<FpyByType> list = Arrays.asList(fpyByTypes);

        //sql谐波电压、电流指标查询拼接
        List<Integer> list1 = new ArrayList<>();

        for (int i = 2; i < 26; i++) {
            list1.add(new Integer(i));
        }

        //有三相
        List<Map<String, Object>> listResult = getFpyDataMapper.getFpy(conditionData, list1, 1);
        translateData(listResult, list, 1);
        //无三相
        List<Map<String, Object>> list2 = getFpyDataMapper.getFpy(conditionData, null, 2);
        translateData(list2, list, 2);

        return list;
    }

    private void translateData(List<Map<String, Object>> list, List<FpyByType> listFpy, int type) {
        Iterator<FpyByType> fpyDataByTypeIterator = listFpy.iterator();

        while (fpyDataByTypeIterator.hasNext()) {//外层指标循环
            FpyByType fpyByType = fpyDataByTypeIterator.next();
            Iterator<Map<String, Object>> iterator = list.iterator();

            while (iterator.hasNext()) {//内层查询结果循环
                Map<String, Object> map = iterator.next();

                switch (fpyByType.getEnumPassData().getCode()) {
                    case 1:
                    case 2:
                    case 3:
                    case 5:
                    case 7: {
                        if (type == 1) {
                            switch (map.get("PHASETYPE").toString()) {
                                case "A": {
                                    fpyByType.setListA(getData(map, fpyByType.getEnumPassData()));
                                    break;
                                }
                                case "B": {
                                    fpyByType.setListB(getData(map, fpyByType.getEnumPassData()));
                                    break;
                                }
                                case "C": {
                                    fpyByType.setListC(getData(map, fpyByType.getEnumPassData()));
                                    break;
                                }
                                case "T": {
                                    fpyByType.setListS(getData(map, fpyByType.getEnumPassData()));
                                    break;
                                }
                            }
                        }

                        break;
                    }
                    case 4:
                    case 6: {
                        if (type == 2) {
                            fpyByType.setListT(getData(map, fpyByType.getEnumPassData()));
                        }
                    }
                }
            }
        }
    }

    private List<FpyRateData> getData(Map<String, Object> map, EnumPassData enumPassData) {
        Long allTime = enumPassData.getCode() == 7 ? Long.valueOf(map.get("FLICKET_ALLTIME").toString()) :Long.valueOf(map.get("ALLTIME").toString());
        List<FpyRateData> list = new ArrayList<>();

        if (allTime.longValue() == 0) {//如果采样点数为0则直接return
            return null;
        }

        int length = 3;

        if (enumPassData.getCode() == EnumPassData.FPYV.getCode() || enumPassData.getCode() == EnumPassData.FPYI.getCode()) {
            length = 26;
        }

        for (int j = 2; j < length; j++) {
            FpyRateData fpyData = new FpyRateData();
            Long l = Long.valueOf(map.get(length > 3 ? enumPassData.getDescribe().replace("0", String.valueOf(j)) : enumPassData.getDescribe()).toString());
            fpyData.setCount(allTime);
            fpyData.setRate(judgeRate(l, allTime));
            list.add(fpyData);
        }

        return list;
    }

    private Float judgeRate(Long l, Long l1) {
        float d = 100.0f - (l.longValue() * 1.0f / l1.longValue()) * 100.0f;
        return new Float(d < 0.0f ? 0.0f : d);
    }
}
