package com.gyee.generation;


import com.alibaba.fastjson.JSONObject;
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.ProEconWtCurveFitting;
import com.gyee.generation.model.auto.ProEconWtCurveFittingMonth;
import com.gyee.generation.model.auto.ProEconWtCurveFittingYear;
import com.gyee.generation.model.vo.CurveType;
import com.gyee.generation.service.auto.IProEconWtCurveFittingMonthService;
import com.gyee.generation.service.auto.IProEconWtCurveFittingService;
import com.gyee.generation.service.auto.IProEconWtCurveFittingYearService;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.redis.RedisService;
import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * OpenCSV CSVReader Example, Read line by line
 *
 * @author pankaj
 */
@SpringBootTest
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class CurveTest {

    @Resource
    private RedisService redisService;
    @Resource
    private IProEconWtCurveFittingMonthService proEconWtCurveFittingMonthService;

    @Resource
    private IProEconWtCurveFittingYearService proEconWtCurveFittingYearService;
    @Resource
    private IProEconWtCurveFittingService proEconWtCurveFittingService;


    @Test
    public void csvTest() throws Exception {
        Calendar c = Calendar.getInstance();

        Date date = DateUtils.truncate(c.getTime());
        for (ProBasicEquipment wt : CacheContext.wtls) {
            QueryWrapper<ProEconWtCurveFitting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("record_date", date)
                    .eq("windturbine_Id", wt.getId());

            List<ProEconWtCurveFitting> dayls = proEconWtCurveFittingService.list(queryWrapper).stream()
                    .sorted(Comparator.comparing(ProEconWtCurveFitting::getSpeed)).collect(Collectors.toList());

            if (!dayls.isEmpty()) {
                String s = JSONObject.toJSONString(dayls);
                redisService.set(CurveType.dayCurve.id + "_" + wt.getId(), s);
            }

            QueryWrapper<ProEconWtCurveFittingMonth> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("year", String.valueOf(c.get(Calendar.YEAR)))
                    .eq("month", String.valueOf(c.get(Calendar.MONTH) + 1))
                    .eq("windturbine_Id", wt.getId());

            List<ProEconWtCurveFittingMonth> monthls = proEconWtCurveFittingMonthService.list(queryWrapper2).stream()
                    .sorted(Comparator.comparing(ProEconWtCurveFittingMonth::getSpeed)).collect(Collectors.toList());

            if (!monthls.isEmpty()) {
                String s = JSONObject.toJSONString(monthls);
                redisService.set(CurveType.monthCurve.id + "_" + wt.getId(), s);
            }

            QueryWrapper<ProEconWtCurveFittingYear> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("year", String.valueOf(c.get(Calendar.YEAR)))
                    .eq("windturbine_Id", wt.getId());

            List<ProEconWtCurveFittingYear> yearls = proEconWtCurveFittingYearService.list(queryWrapper3).stream()
                    .sorted(Comparator.comparing(ProEconWtCurveFittingYear::getSpeed)).collect(Collectors.toList());

            if (!dayls.isEmpty()) {
                String s = JSONObject.toJSONString(dayls);
                redisService.set(CurveType.yearCurve.id + "_" + wt.getId(), s);
            }
        }
        //        electricityMeteringService.saveGenerationDatas();
    }


}
