package com.pqs9000.serviceimpl.report;

import com.pqs9000.mapper.report.GetFpyMapper;
import com.pqs9000.pojo.report.Condition;
import com.pqs9000.pojo.report.FpyDataByType;
import com.pqs9000.pojo.report.FpyRate;
import com.pqs9000.pojo.report.enumReport.EnumPass;
import com.pqs9000.service.report.GetFpyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GetFpyServiceImpl implements GetFpyService {
    @Autowired
    private GetFpyMapper getFpyMapper;

    @Override
    public List<FpyDataByType> getFpy(Condition condition) {
        FpyDataByType[] fpyDataByTypes = new FpyDataByType[]{
                new FpyDataByType(EnumPass.FPYV),
                new FpyDataByType(EnumPass.FPYI),
                new FpyDataByType(EnumPass.FPYVOLTAGE),
                new FpyDataByType(EnumPass.FPYTHREE),
                new FpyDataByType(EnumPass.FPYTHDV),
                new FpyDataByType(EnumPass.FPYRATE),
                new FpyDataByType(EnumPass.FPYFLICKER),
        };

        List<FpyDataByType> list = Arrays.asList(fpyDataByTypes);

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

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

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

        return list;
    }

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

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

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

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

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

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

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

        int length = 3;

        if (enumPass.getCode() == EnumPass.FPYV.getCode() || enumPass.getCode() == EnumPass.FPYI.getCode()) {
            length = 26;
        }

        for (int j = 2; j < length; j++) {
            FpyRate fpyData = new FpyRate();
            Long l = Long.valueOf(map.get(length > 3 ? enumPass.getDescribe().replace("0", String.valueOf(j)) : enumPass.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);
    }

}
