package com.gyee.runeconomy.service.auto.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import com.gyee.runeconomy.dto.response.ProEconPointCodeDTO;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.mapper.auto.ProEconPointCodeMapper;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.model.auto.ProEconPointCode;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay1;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay4;
import com.gyee.runeconomy.service.auto.IProEconPointCodeService;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay1Service;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay4Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 指标查询表 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-23
 */
@Service
public class ProEconPointCodeServiceImpl extends ServiceImpl<ProEconPointCodeMapper, ProEconPointCode> implements IProEconPointCodeService {


    @Autowired
    private IProEconPowerstationInfoDay1Service proEconPowerstationInfoDay1Service;

    @Autowired
    private ProBasicPowerstationServiceImpl proBasicPowerstationService;

    @Autowired
    private IProEconPowerstationInfoDay4Service proEconPowerstationInfoDay4Service;


    @Override
    public List<ProEconPointCode> getEconPointCodeList() {
        QueryWrapper<ProEconPointCode> qw = new QueryWrapper<>();
        List<ProEconPointCode> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public ProEconPointCode getProEconPointCode(String nemCode) {
        QueryWrapper<ProEconPointCode> qw = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(nemCode)) {
            qw.lambda().eq(ProEconPointCode::getNemCode, nemCode);
        }
        ProEconPointCode one = baseMapper.selectOne(qw);
        return one;
    }

    @Override
    public List<ProEconPointCodeDTO> getEconPointRanking(String regionId, String companyId, String pointCode, Date date, String dateType, boolean popup, String statype) {
        List<ProBasicPowerstation> powerstationList = proBasicPowerstationService.getProBasicPowerstationlist(regionId, companyId, statype);

        ProEconPointCode proEconPointCode = getProEconPointCode(pointCode);
        if (null == proEconPointCode) {
            proEconPointCode = getProEconPointCode("fnlyl");
            pointCode = "fnlyl";
        }
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();

        switch (pointCode) {
            case "fdl":
                dtos = fdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "qfdl":
                dtos = qfdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "fnlyl":
                dtos = fnlyl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "slssdl":
                dtos = slssdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "jxssdl":
                dtos = jxssdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "xdjclssdl":
                dtos = xdjclssdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "gzssdl":
                dtos = gzssdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "xnssdl":
                dtos = xnssdl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "fwjsl":
                dtos = fwjsl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "ztzhjsl":
                dtos = ztzhjsl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "gzxqjsl":
                dtos = gzxqjsl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "slssl":
                dtos = slssl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "jxssl":
                dtos = jxssl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "xdjclssl":
                dtos = xdjclssl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "gzssl":
                dtos = gzssl(powerstationList, date, dateType, proEconPointCode);
                break;
            case "xnssl":
                dtos = xnssl(powerstationList, date, dateType, proEconPointCode);
                break;
        }


        List<String> newsto = dtos.stream().map(ProEconPointCodeDTO::getStationcode).collect(Collectors.toList());
        List<String> newsta = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());
        List<String> reduce1 = newsta.stream().filter(item -> !newsto.contains(item)).collect(Collectors.toList());
        for (String i : reduce1) {
            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(proEconPointCode.getDescription() + "(" + proEconPointCode.getUnit() + ")");
            if (null == CacheContext.wpmap.get(i)) {
                continue;
            }
            dto.setStationcode(null == CacheContext.wpmap.get(i) ? "" : CacheContext.wpmap.get(i).getNemCode());
            dto.setStationname(null == CacheContext.wpmap.get(i) ? "" : CacheContext.wpmap.get(i).getName());
            dtos.add(dto);
        }
        List<ProEconPointCodeDTO> dtoss = dtos.stream().sorted(Comparator.comparing(ProEconPointCodeDTO::getValue).reversed()).limit(popup ? dtos.size() : 5).collect(Collectors.toList());
        return dtoss;
    }


    /**
     * 发电量
     */
    private List<ProEconPointCodeDTO> fdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();

        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(i.getRfdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue(i.getYfdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue(i.getNfdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }


    /**
     * 欠发电量
     */
    private List<ProEconPointCodeDTO> qfdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(i.getRqfdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue(i.getYqfdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue(i.getNqfdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }


    // 风能利用率
    private List<ProEconPointCodeDTO> fnlyl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            if (compare0(i.getRllfdl())) {
                switch (dateType) {
                    case "r":
                        //发电量/理论发电量*100
                        //divide(fdinfo.getRllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"))
                        dto.setValue(i.getRfdl().divide(i.getRllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())));
                        break;
                    case "y":
                        dto.setValue(i.getRfdl().divide(i.getYllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())));
                        break;
                    case "n":
                        dto.setValue(i.getRfdl().divide(i.getNllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())));
                        break;
                }
                dto.setValue(compare100(dto.getValue()));
            }


            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 受累损失电量
     */
    private List<ProEconPointCodeDTO> slssdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue((i.getRcwsltqssdl().add(i.getRcwsldwssdl())).divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue((i.getYcwsltqssdl().add(i.getYcwsldwssdl())).divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue((i.getNcwsltqssdl().add(i.getNcwsldwssdl())).divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }


    /**
     * 检修损失电量
     */
    private List<ProEconPointCodeDTO> jxssdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(i.getRjxssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue(i.getYjxssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue(i.getNjxssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 限电损失电量
     */
    private List<ProEconPointCodeDTO> xdjclssdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue((i.getRxdtjssdl().add(i.getRxdjclssdl())).divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue((i.getYxdtjssdl().add(i.getYxdjclssdl())).divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue((i.getNxdtjssdl().add(i.getNxdjclssdl())).divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 故障损失电量
     */
    private List<ProEconPointCodeDTO> gzssdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(i.getRgzssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue(i.getYgzssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue(i.getNgzssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 性能损失电量
     */
    private List<ProEconPointCodeDTO> xnssdl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(i.getRxnssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "y":
                    dto.setValue(i.getYxnssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
                case "n":
                    dto.setValue(i.getNxnssdl().divide(new BigDecimal(pointCode.getRatio()), 2, BigDecimal.ROUND_HALF_UP));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 复位及时率
     */
    private List<ProEconPointCodeDTO> fwjsl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay4> list = proEconPowerstationInfoDay4Service.getEconThreeRateCurvelist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(compare100(i.getRfwjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
                case "y":
                    dto.setValue(compare100(i.getYfwjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
                case "n":
                    dto.setValue(compare100(i.getNfwjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 状态转换及时率
     */
    private List<ProEconPointCodeDTO> ztzhjsl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay4> list = proEconPowerstationInfoDay4Service.getEconThreeRateCurvelist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(compare100(i.getRztzhjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
                case "y":
                    dto.setValue(compare100(i.getYztzhjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
                case "n":
                    dto.setValue(compare100(i.getNztzhjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 故障消缺及时率
     */
    private List<ProEconPointCodeDTO> gzxqjsl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay4> list = proEconPowerstationInfoDay4Service.getEconThreeRateCurvelist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(compare100(i.getRgzxqjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
                case "y":
                    dto.setValue(compare100(i.getYgzxqjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
                case "n":
                    dto.setValue(compare100(i.getNgzxqjsl().divide(new BigDecimal(pointCode.getRatio()))));
                    break;
            }
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 受累损失率
     */
    private List<ProEconPointCodeDTO> slssl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());

            switch (dateType) {
                case "r":
                    dto.setValue(compare0(i.getRllfdl()) ? (i.getRcwsltqssdl().add(i.getRcwsldwssdl())).divide(i.getRllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "y":
                    dto.setValue(compare0(i.getYllfdl()) ? (i.getYcwsltqssdl().add(i.getYcwsldwssdl())).divide(i.getYllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "n":
                    dto.setValue(compare0(i.getNllfdl()) ? (i.getNcwsltqssdl().add(i.getNcwsldwssdl())).divide(i.getNllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
            }
            dto.setValue(compare100(dto.getValue()));

            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 检修损失率
     */
    private List<ProEconPointCodeDTO> jxssl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());


            switch (dateType) {
                case "r":
                    dto.setValue(compare0(i.getRllfdl()) ? i.getRjxssdl().divide(i.getRllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "y":
                    dto.setValue(compare0(i.getYllfdl()) ? i.getYjxssdl().divide(i.getYllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "n":
                    dto.setValue(compare0(i.getNllfdl()) ? i.getNjxssdl().divide(i.getNllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
            }
            dto.setValue(compare100(dto.getValue()));
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 限电损失率
     */
    private List<ProEconPointCodeDTO> xdjclssl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(compare0(i.getRllfdl()) ? i.getRxdtjssdl().divide(i.getRxdjclssdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "y":
                    dto.setValue(compare0(i.getYllfdl()) ? i.getYxdtjssdl().divide(i.getYxdjclssdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "n":
                    dto.setValue(compare0(i.getNllfdl()) ? i.getNxdtjssdl().divide(i.getNxdjclssdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
            }
            dto.setValue(compare100(dto.getValue()));
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 故障损失率
     */
    private List<ProEconPointCodeDTO> gzssl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(compare0(i.getRllfdl()) ? i.getRgzssdl().divide(i.getRllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "y":
                    dto.setValue(compare0(i.getYllfdl()) ? i.getYgzssdl().divide(i.getYllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "n":
                    dto.setValue(compare0(i.getNllfdl()) ? i.getNgzssdl().divide(i.getNllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
            }
            dto.setValue(compare100(dto.getValue()));
            dtos.add(dto);
        });

        return dtos;
    }

    /**
     * 性能损失率
     */
    private List<ProEconPointCodeDTO> xnssl(List<ProBasicPowerstation> powerstationList, Date date, String dateType, ProEconPointCode pointCode) {
        List<String> stationlist = powerstationList.stream().map(ProBasicPowerstation::getNemCode).collect(Collectors.toList());

        List<ProEconPowerstationInfoDay1> list = proEconPowerstationInfoDay1Service.getEconFiveLosslist(stationlist, date);
        List<ProEconPointCodeDTO> dtos = new ArrayList<>();
        list.stream().forEach(i -> {

            ProEconPointCodeDTO dto = new ProEconPointCodeDTO();
            dto.setDescription(pointCode.getDescription() + "(" + pointCode.getUnit() + ")");
            dto.setStationcode(i.getForeignKeyId());
            dto.setStationname(CacheContext.wpmap.get(i.getForeignKeyId()).getName());
            switch (dateType) {
                case "r":
                    dto.setValue(compare0(i.getRllfdl()) ? i.getRxnssdl().divide(i.getRllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "y":
                    dto.setValue(compare0(i.getYllfdl()) ? i.getYxnssdl().divide(i.getYllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
                case "n":
                    dto.setValue(compare0(i.getNllfdl()) ? i.getNxnssdl().divide(i.getNllfdl(), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(pointCode.getRatio())) : new BigDecimal(0));
                    break;
            }
            dto.setValue(compare100(dto.getValue()));
            dtos.add(dto);
        });

        return dtos;
    }


    private BigDecimal compare100(BigDecimal a) {
        BigDecimal b = new BigDecimal(100);

        if (null == a) {
            a = new BigDecimal(0);

        }
        if (b.compareTo(a) == -1) {
            a = new BigDecimal(100);

        }

        if (a.compareTo(BigDecimal.ZERO) == -1) {
            a = new BigDecimal(0);
        }
        return a;

    }

    private boolean compare0(BigDecimal a) {
        BigDecimal b = new BigDecimal(0.00);
        if (b.compareTo(a.setScale(2, BigDecimal.ROUND_HALF_UP)) == 0) {
            return false;
        }
        return true;
    }

}
