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.ProEconEquipmentInfoDay1;
import com.gyee.generation.model.auto.ProEconWindturbineGoodness;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay1Service;
import com.gyee.generation.service.auto.IProEconWindturbineGoodnessService;
import com.gyee.generation.service.auto.ITurbineInfoMinService;
import com.gyee.generation.task.thread.GoodnessOfFitThread;
import com.gyee.generation.util.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

@Service
public class WindturbineGoodnessService {

    @Resource
    private CoefficientService coefficientService;
    @Resource
    private GoodnessOfFitService goodnessOfFitService;
    @Resource
    private IProEconWindturbineGoodnessService proEconWindturbineGoodnessService;

    @Resource
    private IProEconEquipmentInfoDay1Service proEconEquipmentInfoDay1Service;
    @Resource
    private ITurbineInfoMinService turbineInfoMinService;

    @Resource
    @Qualifier("applicationTaskExecutor")
    private Executor executor;

    public void calWindturbineGoodness(Date recordDate) throws Exception {


        //        //判断是否有重复记录，先删除重复记录
        //        QueryWrapper<ProEconWindturbineGoodness> queryWrapper = new QueryWrapper<>();
        //        queryWrapper.eq("record_date",DateUtils.truncate(recordDate));
        //        List<Long> idls = proEconWindturbineGoodnessService.list(queryWrapper).stream()
        ////                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate))==0
        ////                        && CacheContext.wtmap.containsKey(i.getWindturbineId()))
        //                .map(ProEconWindturbineGoodness::getId)
        //                .collect(Collectors.toList());
        //
        ////        if (idls.size() > 0) {
        ////
        ////            proEconWindturbineGoodnessService.removeByIds(idls);
        ////
        ////        }
        //        List<Long> tempids=new ArrayList<>();
        //
        //        for(int i=0;i<idls.size();i++)
        //        {
        //            tempids.add(idls.get(i));
        //            if(tempids.size()==1000)
        //            {
        //                proEconWindturbineGoodnessService.removeByIds(tempids);
        //                tempids=new ArrayList<>();
        //            }
        //        }
        //
        //        if(!tempids.isEmpty())
        //        {
        //            proEconWindturbineGoodnessService.removeByIds(tempids);
        //        }

        QueryWrapper<ProEconEquipmentInfoDay1> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("record_date", DateUtils.truncate(recordDate));
        List<ProEconEquipmentInfoDay1> ed1ls = proEconEquipmentInfoDay1Service.list(queryWrapper2);

        Map<String, ProEconEquipmentInfoDay1> equipmentInfoDay1Map = new HashMap<>();

        if (!ed1ls.isEmpty()) {
            for (ProEconEquipmentInfoDay1 ed1 : ed1ls) {
                equipmentInfoDay1Map.put(ed1.getWindturbineId(), ed1);
            }
        }
        List<ProEconWindturbineGoodness> vos = new ArrayList<>();

        System.out.println("拟合优度风机数量" + CacheContext.wtfdls.size());

        final CountDownLatch latch = new CountDownLatch(+CacheContext.wtfdls.size());
        for (int i = 0; i < CacheContext.wtfdls.size(); i++) {
            new Thread(new GoodnessOfFitThread(executor, i, recordDate, coefficientService,
                    goodnessOfFitService,turbineInfoMinService,
                    equipmentInfoDay1Map, vos, latch)).start();

            // proEconWindturbineGoodnessService.save(po);
        }
        latch.await();
        vos.sort(new Comparator<ProEconWindturbineGoodness>() {
            @Override
            public int compare(ProEconWindturbineGoodness o1, ProEconWindturbineGoodness o2) {
                double d1 = o1.getDayGoodness() + o1.getDayCoefficient();
                double d2 = o2.getDayGoodness() + o2.getDayCoefficient();
                if (d1 < d2) {
                    return 1;
                } else if (d1 > d2) {
                    return 1;
                } else {
                    return 0;
                }

            }
        });

        for (int i = 0; i < vos.size(); i++) {
            ProEconWindturbineGoodness vo = vos.get(i);
            vo.setDayTop(i + 1);
        }

        vos.sort(new Comparator<ProEconWindturbineGoodness>() {
            @Override
            public int compare(ProEconWindturbineGoodness o1, ProEconWindturbineGoodness o2) {
                double d1 = o1.getMonthGoodness() + o1.getMonthCoefficient();
                double d2 = o2.getMonthGoodness() + o2.getMonthCoefficient();
                if (d1 < d2) {
                    return 1;
                } else if (d1 > d2) {
                    return 1;
                } else {
                    return 0;
                }

            }
        });

        for (int i = 0; i < vos.size(); i++) {
            ProEconWindturbineGoodness vo = vos.get(i);
            vo.setMonthTop(i + 1);
        }

        vos.sort(new Comparator<ProEconWindturbineGoodness>() {
            @Override
            public int compare(ProEconWindturbineGoodness o1, ProEconWindturbineGoodness o2) {
                double d1 = o1.getYearGoodness() + o1.getYearCoefficient();
                double d2 = o2.getYearGoodness() + o2.getYearCoefficient();
                if (d1 < d2) {
                    return 1;
                } else if (d1 > d2) {
                    return 1;
                } else {
                    return 0;
                }

            }
        });

        for (int i = 0; i < vos.size(); i++) {
            ProEconWindturbineGoodness vo = vos.get(i);
            vo.setYearTop(i + 1);
        }
        System.out.println("拟合优度风机存储数量" + vos.size());


        //判断是否有重复记录，先删除重复记录
        QueryWrapper<ProEconWindturbineGoodness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate));
        List<Long> idls = proEconWindturbineGoodnessService.list(queryWrapper).stream()
                //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate))==0
                //                        && CacheContext.wtmap.containsKey(i.getWindturbineId()))
                .map(ProEconWindturbineGoodness::getId)
                .collect(Collectors.toList());

        //        if (idls.size() > 0) {
        //
        //            proEconWindturbineGoodnessService.removeByIds(idls);
        //
        //        }
        List<Long> tempids = new ArrayList<>();

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

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

        List<ProEconWindturbineGoodness> templs = new ArrayList<>();
        for (ProEconWindturbineGoodness vo : vos) {
            templs.add(vo);
            if (templs.size() == 1000) {
                proEconWindturbineGoodnessService.saveBatch(templs);
                templs = new ArrayList<>();
            }
        }

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

        System.out.println("拟合优度风机存储结束");
    }

}
