package com.gyee.runeconomy.service.threerate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.common.util.DoubleUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.dto.response.FwjslDTO;
import com.gyee.runeconomy.dto.response.QxjslDTO;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.model.auto.ProEconEquipmentInfoDay5;
import com.gyee.runeconomy.model.auto.ProEconFaultLiminatedefects;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.IProBasicPowerstationService;
import com.gyee.runeconomy.service.auto.IProEconEquipmentInfoDay5Service;
import com.gyee.runeconomy.service.auto.IProEconFaultLiminatedefectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
public class ThreeRateService {

    @Autowired
    private IProEconEquipmentInfoDay5Service proEconEquipmentInfoDay5Service;

    @Autowired
    private IProBasicPowerstationService proBasicPowerstationService;

    @Autowired
    private IProEconFaultLiminatedefectsService proEconFaultLiminatedefectsService;

    @Resource
    private TokenService tokenService;

    /**
     * 复位及时率
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public List<FwjslDTO> getResetTimelyRateInfo(String companyId, String windpowerstationId, String beginDate, String endDate) {
        List<FwjslDTO> resultList = new ArrayList<>();
        List<ProEconEquipmentInfoDay5> list = proEconEquipmentInfoDay5Service.getResetTimelyRateList(companyId, windpowerstationId, beginDate, endDate);


        getData(resultList, list);
        SortUtils.sort(resultList, "ordernum", SortUtils.ASC);
        return resultList;

    }


    /**
     * 状态转换率
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public Map<String, List<FwjslDTO>> getStateConversionRateInfo(String companyId, String windpowerstationId, String beginDate, String endDate) {
        Map<String, List<FwjslDTO>> resultMap = new HashMap<>();
        List<FwjslDTO> resultList1 = new ArrayList<>();
        List<FwjslDTO> resultList2 = new ArrayList<>();
        List<FwjslDTO> resultList3 = new ArrayList<>();
        resultMap.put("小于3米风速", resultList1);
        resultMap.put("大于3米风速", resultList2);
        resultMap.put("大于4米风速", resultList3);
        QueryWrapper<ProEconEquipmentInfoDay5> qw = new QueryWrapper<>();
        List<ProEconEquipmentInfoDay5> list = proEconEquipmentInfoDay5Service.getStateConversionRateList(companyId, windpowerstationId, beginDate, endDate);


        getData1(resultList1, list);
        getData2(resultList2, list);
        getData3(resultList3, list);


        SortUtils.sort(resultList1, "ordernum", SortUtils.ASC);
        SortUtils.sort(resultList2, "ordernum", SortUtils.ASC);
        SortUtils.sort(resultList3, "ordernum", SortUtils.ASC);


        return resultMap;
    }


    /**
     * 消缺及时率
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public List<QxjslDTO> getDefectsEliminatRateList(String companyId, String windpowerstationId, String beginDate, String endDate) {

        //        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        //        List<ProBasicPowerstation> wpls = tokenService.getWpls(request);
        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        Map<String, Integer> station = wpls
                .stream().collect(Collectors.toMap(ProBasicPowerstation::getId, ProBasicPowerstation::getOrderNum));

        List<String> wpids = new ArrayList<>();
        //场站模糊查询
        if (StringUtils.isNotEmpty(companyId) && CacheContext.cpwpmap.containsKey(companyId)) {
            // List<ProBasicPowerstation> wplist = wplsToken.stream().filter(wp -> wp.getRegionId().equals(companyId) || wp.getCompanyId().equals(companyId)).collect(Collectors.toList());

            List<ProBasicPowerstation> wplist = CacheContext.cpwpmap.get(companyId);
            wpids = wplist.stream().map(ProBasicPowerstation::getId).collect(Collectors.toList());

        }
        if (StringUtils.isNotEmpty(windpowerstationId)) {
            wpids.add(windpowerstationId);
        }

        List<QxjslDTO> resultList = new ArrayList<>();
        QueryWrapper<ProEconFaultLiminatedefects> qw = new QueryWrapper<>();
        qw.select(" windpowerstation_id,sum(fault_count) faultcount,sum(right_count) rightcount ");
        qw.in("windpowerstation_id", wpids);
        qw.ge("date_time", DateUtils.parseDate(beginDate)).le("date_time", DateUtils.parseDate(endDate));

        //        if (wpids.size() > 0) {
        //            qw.lambda().in(ProEconFaultLiminatedefects::getWindpowerstationId, wpids);
        //
        //        }

        qw.groupBy("windpowerstation_id");
        List<ProEconFaultLiminatedefects> faulteliminatedefectsList = proEconFaultLiminatedefectsService.list(qw);
        AtomicReference<Long> gzCount = new AtomicReference<>((long) 0);
        AtomicReference<Long> xqCount = new AtomicReference<>((long) 0);
        if (null == faulteliminatedefectsList || faulteliminatedefectsList.isEmpty()) {
            QxjslDTO vo = new QxjslDTO();
            vo.setOrdernum(0);
            vo.setWpid("惠安风电");
            vo.setGzCount(0L);
            vo.setXqCount(0L);
            vo.setXqjsl(0.0);
            resultList.add(vo);
        }else {
            faulteliminatedefectsList.stream().forEach(i -> {
                gzCount.updateAndGet(v -> new Long((long) (v + i.getFaultCount())));
                xqCount.updateAndGet(v -> new Long((long) (v + i.getRightCount())));
                QxjslDTO vo = new QxjslDTO();

                vo.setOrdernum(station.get(i.getWindpowerstationId()));


                vo.setWpid(CacheContext.wpmap.get(i.getWindpowerstationId()).getName());
                vo.setGzCount(i.getFaultCount().longValue());
                vo.setXqCount(i.getRightCount().longValue());
                vo.setXqjsl(i.getFaultCount() != 0 ? DoubleUtils.getRoundingNum(((double) (i.getRightCount()) / (double) (i.getFaultCount()) * 100.0), 2) : 0.0);
                resultList.add(vo);
            });
        }
        QxjslDTO vo = new QxjslDTO();
        vo.setWpid("合计");


        vo.setOrdernum(station.size() + 1);


        vo.setGzCount(gzCount.get());
        vo.setXqCount(xqCount.get());
        vo.setXqjsl(gzCount.get() != 0 ? DoubleUtils.getRoundingNum(((double) (xqCount.get()) / (double) (gzCount.get()) * 100.0), 2) : 0.0);
        resultList.add(vo);
        SortUtils.sort(resultList, "ordernum", SortUtils.ASC);


        return resultList;
    }


    private void getData(List<FwjslDTO> resultList, List<ProEconEquipmentInfoDay5> list) {

        Map<String, Integer> station = proBasicPowerstationService.list()
                .stream().collect(Collectors.toMap(ProBasicPowerstation::getId, ProBasicPowerstation::getOrderNum));

        AtomicReference<Double> wfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> sfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> swfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> esfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> yfwcs = new AtomicReference<>((double) 0);
        list.stream().forEach(i -> {
            wfz.updateAndGet(v -> new Double((double) (v + i.getTimerate16())));
            sfz.updateAndGet(v -> new Double((double) (v + i.getTimerate17())));
            swfz.updateAndGet(v -> new Double((double) (v + i.getTimerate18())));
            esfz.updateAndGet(v -> new Double((double) (v + i.getTimerate19())));
            yfwcs.updateAndGet(v -> new Double((double) (v + i.getTimerate20())));
            FwjslDTO vo = new FwjslDTO();
            vo.setOrdernum(station.get(i.getWindpowerstationId()));
            vo.setWpid(CacheContext.wpmap.get(i.getWindpowerstationId()).getName());
            vo.setYfwcs(i.getTimerate20());

            vo.setWfz(i.getTimerate16());
            vo.setSfz(i.getTimerate17());
            vo.setSwfz(i.getTimerate18());
            vo.setEsfz(i.getTimerate19());

            vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
            resultList.add(vo);
        });
        FwjslDTO vo = new FwjslDTO();
        vo.setWpid("合计");

        vo.setOrdernum(station.size() + 1);

        vo.setYfwcs(DoubleUtils.getRoundingNum(yfwcs.get(), 2));
        vo.setWfz(DoubleUtils.getRoundingNum(wfz.get(), 2));
        vo.setSfz(DoubleUtils.getRoundingNum(sfz.get(), 2));
        vo.setSwfz(DoubleUtils.getRoundingNum(swfz.get(), 2));
        vo.setEsfz(DoubleUtils.getRoundingNum(esfz.get(), 2));
        vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
        resultList.add(vo);
    }

    private void getData1(List<FwjslDTO> resultList, List<ProEconEquipmentInfoDay5> list) {

        Map<String, Integer> station = proBasicPowerstationService.list()
                .stream().collect(Collectors.toMap(ProBasicPowerstation::getId, ProBasicPowerstation::getOrderNum));

        AtomicReference<Double> wfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> sfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> swfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> esfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> yfwcs = new AtomicReference<>((double) 0);
        list.stream().forEach(i -> {
            wfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate1() ? 0.00 : i.getTimerate1()))));
            sfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate2() ? 0.00 : i.getTimerate2()))));
            swfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate3() ? 0.00 : i.getTimerate3()))));
            esfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate4() ? 0.00 : i.getTimerate4()))));
            yfwcs.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate13() ? 0.00 : i.getTimerate13()))));
            FwjslDTO vo = new FwjslDTO();
            vo.setOrdernum(station.get(i.getWindpowerstationId()));
            vo.setWpid(CacheContext.wpmap.get(i.getWindpowerstationId()).getName());
            vo.setYfwcs(i.getTimerate13());

            vo.setWfz(i.getTimerate1());
            vo.setSfz(i.getTimerate2());
            vo.setSwfz(i.getTimerate3());
            vo.setEsfz(i.getTimerate4());

            vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
            resultList.add(vo);
        });
        FwjslDTO vo = new FwjslDTO();
        vo.setWpid("合计");

        vo.setOrdernum(station.size() + 1);
        vo.setYfwcs(DoubleUtils.getRoundingNum(yfwcs.get(), 2));
        vo.setWfz(DoubleUtils.getRoundingNum(wfz.get(), 2));
        vo.setSfz(DoubleUtils.getRoundingNum(sfz.get(), 2));
        vo.setSwfz(DoubleUtils.getRoundingNum(swfz.get(), 2));
        vo.setEsfz(DoubleUtils.getRoundingNum(esfz.get(), 2));
        vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
        resultList.add(vo);
    }

    private void getData2(List<FwjslDTO> resultList, List<ProEconEquipmentInfoDay5> list) {


        Map<String, Integer> station = proBasicPowerstationService.list()
                .stream().collect(Collectors.toMap(ProBasicPowerstation::getId, ProBasicPowerstation::getOrderNum));
        AtomicReference<Double> wfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> sfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> swfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> esfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> yfwcs = new AtomicReference<>((double) 0);
        list.stream().forEach(i -> {
            wfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate5() ? 0.00 : i.getTimerate5()))));
            sfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate6() ? 0.00 : i.getTimerate6()))));
            swfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate7() ? 0.00 : i.getTimerate7()))));
            esfz.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate8() ? 0.00 : i.getTimerate8()))));
            yfwcs.updateAndGet(v -> new Double((double) (v + (0 == i.getTimerate14() ? 0.00 : i.getTimerate14()))));
            FwjslDTO vo = new FwjslDTO();


            vo.setOrdernum(station.get(i.getWindpowerstationId()));
            vo.setWpid(CacheContext.wpmap.get(i.getWindpowerstationId()).getName());
            vo.setYfwcs(i.getTimerate14());

            vo.setWfz(i.getTimerate5());
            vo.setSfz(i.getTimerate6());
            vo.setSwfz(i.getTimerate7());
            vo.setEsfz(i.getTimerate8());

            vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
            resultList.add(vo);
        });
        FwjslDTO vo = new FwjslDTO();
        vo.setWpid("合计");

        vo.setOrdernum(station.size() + 1);


        vo.setYfwcs(DoubleUtils.getRoundingNum(yfwcs.get(), 2));
        vo.setWfz(DoubleUtils.getRoundingNum(wfz.get(), 2));
        vo.setSfz(DoubleUtils.getRoundingNum(sfz.get(), 2));
        vo.setSwfz(DoubleUtils.getRoundingNum(swfz.get(), 2));
        vo.setEsfz(DoubleUtils.getRoundingNum(esfz.get(), 2));
        vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
        resultList.add(vo);
    }

    private void getData3(List<FwjslDTO> resultList, List<ProEconEquipmentInfoDay5> list) {


        Map<String, Integer> station = proBasicPowerstationService.list()
                .stream().collect(Collectors.toMap(ProBasicPowerstation::getId, ProBasicPowerstation::getOrderNum));


        AtomicReference<Double> wfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> sfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> swfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> esfz = new AtomicReference<>((double) 0);
        AtomicReference<Double> yfwcs = new AtomicReference<>((double) 0);
        list.stream().forEach(i -> {
            wfz.updateAndGet(v -> new Double((double) (v + i.getTimerate9())));
            sfz.updateAndGet(v -> new Double((double) (v + i.getTimerate10())));
            swfz.updateAndGet(v -> new Double((double) (v + i.getTimerate11())));
            esfz.updateAndGet(v -> new Double((double) (v + i.getTimerate12())));
            yfwcs.updateAndGet(v -> new Double((double) (v + i.getTimerate15())));
            FwjslDTO vo = new FwjslDTO();

            vo.setOrdernum(station.get(i.getWindpowerstationId()));


            vo.setWpid(CacheContext.wpmap.get(i.getWindpowerstationId()).getName());
            vo.setYfwcs(i.getTimerate15());

            vo.setWfz(i.getTimerate9());
            vo.setSfz(i.getTimerate10());
            vo.setSwfz(i.getTimerate11());
            vo.setEsfz(i.getTimerate12());

            vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
            vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
            resultList.add(vo);
        });
        FwjslDTO vo = new FwjslDTO();
        vo.setWpid("合计");
        vo.setOrdernum(station.size() + 1);
        vo.setYfwcs(DoubleUtils.getRoundingNum(yfwcs.get(), 2));
        vo.setWfz(DoubleUtils.getRoundingNum(wfz.get(), 2));
        vo.setSfz(DoubleUtils.getRoundingNum(sfz.get(), 2));
        vo.setSwfz(DoubleUtils.getRoundingNum(swfz.get(), 2));
        vo.setEsfz(DoubleUtils.getRoundingNum(esfz.get(), 2));
        vo.setWfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getWfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setSwfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getSwfz() / vo.getYfwcs() * 100, 2) : 0.0);
        vo.setEsfzfwl(vo.getYfwcs() != 0 ? DoubleUtils.getRoundingNum(vo.getEsfz() / vo.getYfwcs() * 100, 2) : 0.0);
        resultList.add(vo);
    }

}
