package com.gyee.runeconomy.service;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.runeconomy.model.auto.ProEconWtAlysisDay;
import com.gyee.runeconomy.model.auto.TurbineInfoDay;
import com.gyee.runeconomy.service.auto.IProEconWtAlysisDayService;
import com.gyee.runeconomy.service.auto.ITurbineInfoDayService;
import com.gyee.runeconomy.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class WtAlysisDayService {

    @Resource
    private IProEconWtAlysisDayService proEconWtAlysisDayService;
    @Resource
    private ITurbineInfoDayService turbineInfoDayService;


    public Page<ProEconWtAlysisDay> wtAlysisDayList(Integer pageNum, Integer pageSize, String wtId, String beginDate, String endDate) {

        if (StringUtils.empty(pageNum)) {
            pageNum = 1;
        }
        if (StringUtils.empty(pageSize)) {
            pageSize = 10;
        }
        //构造分页构造器
        Page<ProEconWtAlysisDay> pageInfo = new Page<>(pageNum, pageSize);
        if (StringUtils.notEmp(wtId) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {
            String format = "yyyy-MM-dd";
            SimpleDateFormat df = new SimpleDateFormat(format);
            Date begin = null;
            Date end = null;
            try {
                begin = df.parse(beginDate);
                end = df.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }


            //构造条件构造器
            LambdaQueryWrapper<ProEconWtAlysisDay> queryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件
            queryWrapper.eq(ProEconWtAlysisDay::getWindturbineId, wtId).
                    ge(ProEconWtAlysisDay::getRecordDate, begin).
                    le(ProEconWtAlysisDay::getRecordDate, end);
            queryWrapper.orderByAsc(ProEconWtAlysisDay::getRecordDate);
            //执行查询
            proEconWtAlysisDayService.page(pageInfo, queryWrapper);


            QueryWrapper<TurbineInfoDay> qw = new QueryWrapper<>();
            qw.lambda().eq(TurbineInfoDay::getTurbineId, wtId).between(TurbineInfoDay::getRecordDate, begin, end)
                    .orderByAsc(TurbineInfoDay::getRecordDate);
            IPage<TurbineInfoDay> page01 = new Page<>(pageNum, pageSize);
            IPage<TurbineInfoDay> page1 = turbineInfoDayService.page(page01, qw);
            Map<Date, TurbineInfoDay> dayMap = page1.getRecords().stream().collect(Collectors.toMap(TurbineInfoDay::getRecordDate, Function.identity()));

            pageInfo.getRecords().forEach(rsl -> {
                TurbineInfoDay day = dayMap.get(rsl.getRecordDate());
                rsl.setRfdl(NumberUtil.round(day.getRfdl(),2).doubleValue());
                double yfdl = calFdl(rsl.getWindturbineId(), "month", rsl.getRecordDate());
                rsl.setYfdl(NumberUtil.round(yfdl,2).doubleValue());
                double nfdl = calFdl(rsl.getWindturbineId(), "year", rsl.getRecordDate());
                rsl.setNfdl(NumberUtil.round(nfdl,2).doubleValue());
                rsl.setRpjfs(NumberUtil.round(day.getPjfs(),2).doubleValue());
                double v = day.getGzss() + day.getJhjxss() + day.getXdss() + day.getXnss() + day.getSlss();
                rsl.setRssdl(NumberUtil.round(v,2).doubleValue());
                rsl.setRgzxs(NumberUtil.round(day.getGzMin() / 60,2).doubleValue());
                rsl.setRjxxs(NumberUtil.round(day.getJxMin() / 60,2).doubleValue());
                rsl.setRyxxs(NumberUtil.round(day.getYxMin() / 60,2).doubleValue());
            });

        }

        return pageInfo;
    }


    public double calFdl(String windturbineId, String type, Date endDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(endDate);
        Date firstDay = null;
        if ("month".equals(type)) {
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            firstDay = calendar.getTime();
        } else if ("year".equals(type)) {
            calendar.set(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            firstDay = calendar.getTime();
        }
        QueryWrapper<TurbineInfoDay> currentqw = new QueryWrapper<>();
        currentqw.select("station_id,MAX(record_date) AS record_date,SUM(rfdl) AS rfdl")
                .ge("record_date", firstDay)
                .le("record_date", endDate)
                .eq("turbine_id", windturbineId)
                .like("station_id", "_FDC_")
                .groupBy("station_id");
        List<TurbineInfoDay> monthData = turbineInfoDayService.list(currentqw);
        return monthData.get(0).getRfdl();
    }


}
