package com.gyee.generation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.model.auto.ProEconFaultLiminatedefects;
import com.gyee.generation.model.auto.ProEconShutdownEvent;
import com.gyee.generation.service.auto.IProEconFaultLiminatedefectsService;
import com.gyee.generation.service.auto.IProEconShutdownEventService;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class TimelinessRateService {

    //    private static final Logger logger = LoggerFactory.getLogger(TimelinessRateService.class);
    @Resource
    private IEdosUtil edosUtil;


    @Resource
    private IProEconFaultLiminatedefectsService proEconFaultLiminatedefectsService;
    @Resource
    private IProEconShutdownEventService proEconShutdownEventService;


    public void calEquipmentInfoDay(Date currentDate) throws Exception {

        failRate(currentDate);

    }


    //故障消缺及时率

    public void failRate(Date recordDate) {

        Map<Date, Map<String, ProEconFaultLiminatedefects>> dtDictionary = new HashMap<>();
        Calendar c = Calendar.getInstance();
        c.setTime(DateUtils.truncate(recordDate));//本月

        c.set(Calendar.DAY_OF_MONTH, 1);
        Date begin = c.getTime();

        c.add(Calendar.MONTH, 1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date end = c.getTime();

        //        QueryWrapper<ProEconFaultLiminatedefects> queryWrapper = new QueryWrapper<>();
        //        queryWrapper.ge("date_time",recordDate).le("date_time",recordDate);
        //        List<Long> deleteitems = proEconFaultLiminatedefectsService.list(queryWrapper)
        //                .stream()
        ////                .filter(i ->
        ////                (i.getDateTime().compareTo(begin) == 0 || i.getDateTime().after(begin))
        ////                        && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
        ////                 )
        //                .map(ProEconFaultLiminatedefects::getId).collect(Collectors.toList());
        //
        //        List<Long> tempids=new ArrayList<>();
        //
        //        for(int i=0;i<deleteitems.size();i++)
        //        {
        //            tempids.add(deleteitems.get(i));
        //            if(tempids.size()==1000)
        //            {
        //                proEconFaultLiminatedefectsService.removeByIds(tempids);
        //                tempids=new ArrayList<>();
        //            }
        //        }
        //
        //        if(!tempids.isEmpty())
        //        {
        //            proEconFaultLiminatedefectsService.removeByIds(tempids);
        //        }
        //        if (!deleteitems.isEmpty()) {
        //            proEconFaultLiminatedefectsService.removeByIds(deleteitems);
        //        }
        QueryWrapper<ProEconShutdownEvent> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ge("stop_Time", begin).le("stop_Time", end)
                .isNotNull("stoptype_id").isNotNull("stop_Hours").ne("stoptype_id", "wh");
        List<ProEconShutdownEvent> st2 = proEconShutdownEventService.list(queryWrapper2);
        //                .stream()
        //                .filter(i ->
        //                        (i.getStopTime().compareTo(begin) == 0 || i.getStopTime().after(begin))
        //                                && (i.getStopTime().compareTo(end) == 0 || i.getStopTime().before(end))
        //                ).collect(Collectors.toList());

        for (ProEconShutdownEvent item : st2) {
            Date dt = item.getStopTime();
            Map<String, ProEconFaultLiminatedefects> fdDictionary;
            if (dtDictionary.containsKey(dt)) {
                fdDictionary = dtDictionary.get(dt);
            } else {
                fdDictionary = new HashMap<>();
                dtDictionary.put(dt, fdDictionary);
            }
            ProEconFaultLiminatedefects fs;
            if (fdDictionary.containsKey(item.getWindturbineId())) {
                fs = fdDictionary.get(item.getWindturbineId());
                double temp = fs.getFaultCount();
                temp = temp + 1;
                fs.setFaultCount(temp);

            } else {
                fs = new ProEconFaultLiminatedefects();
                fs.setWindturbineId(item.getWindturbineId());
                fs.setRate(0.0);
                fs.setRightCount(0.0);
                fs.setFaultCount(0.0);

                if (CacheContext.wtmap.containsKey(item.getWindturbineId())) {
                    ProBasicEquipment wt = CacheContext.wtmap.get(item.getWindturbineId());
                    fs.setWindpowerstationId(wt.getWindpowerstationId());
                    fs.setProjectId(wt.getProjectId());
                    fs.setLineId(wt.getLineId());
                }
                fs.setDateTime(dt);
                double temp = fs.getFaultCount();
                temp = temp + 1;
                fs.setFaultCount(temp);
                fs.setClassifyId("-1");

                if (!StringUtils.notEmp(fs.getRightCount())) {
                    fs.setRightCount(0.0);
                }

                fdDictionary.put(item.getWindturbineId(), fs);
            }
            if (item.getStopHours() < 24) {

                double temp = fs.getRightCount();
                temp = temp + 1;
                fs.setRightCount(temp);

            }
            if (fs.getFaultCount() != 0) {
                double temp = BigDecimal.valueOf(fs.getRightCount()).divide(BigDecimal.valueOf(fs.getFaultCount()), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();

                fs.setRate(temp);

            }
        }

        c = Calendar.getInstance();
        c.setTime(DateUtils.truncate(recordDate));//本月

        c.set(Calendar.DAY_OF_MONTH, 1);
        begin = c.getTime();

        c.add(Calendar.MONTH, 1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        end = c.getTime();

        QueryWrapper<ProEconFaultLiminatedefects> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("date_time", begin).le("date_time", end);
        List<Long> deleteitems = proEconFaultLiminatedefectsService.list(queryWrapper)
                .stream()
                //                .filter(i ->
                //                (i.getDateTime().compareTo(begin) == 0 || i.getDateTime().after(begin))
                //                        && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                //                 )
                .map(ProEconFaultLiminatedefects::getId).collect(Collectors.toList());

        List<Long> tempids = new ArrayList<>();

        for (int i = 0; i < deleteitems.size(); i++) {
            tempids.add(deleteitems.get(i));
            if (tempids.size() == 1000) {
                proEconFaultLiminatedefectsService.removeByIds(tempids);
                tempids = new ArrayList<>();
            }
        }

        if (!tempids.isEmpty()) {
            proEconFaultLiminatedefectsService.removeByIds(tempids);
        }


        List<ProEconFaultLiminatedefects> dayls = new ArrayList<>();
        for (Map<String, ProEconFaultLiminatedefects> items : dtDictionary.values()) {
            for (ProEconFaultLiminatedefects item : items.values()) {
                dayls.add(item);

            }
        }


        List<ProEconFaultLiminatedefects> templs = new ArrayList<>();
        for (ProEconFaultLiminatedefects vo : dayls) {

            templs.add(vo);
            if (templs.size() == 1000) {
                proEconFaultLiminatedefectsService.saveBatch(templs);
                templs = new ArrayList<>();
            }
        }

        if (!templs.isEmpty()) {
            proEconFaultLiminatedefectsService.saveBatch(templs);
        }

    }

}


