package com.gyee.alarm.service;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.alarm.model.auto.ProEconEarlyReport;
import com.gyee.alarm.model.auto.ProEconEarlyReportWt;
import com.gyee.alarm.service.auto.IProEconEarlyReportService;
import com.gyee.alarm.service.auto.IProEconEarlyReportWtService;

import com.gyee.alarm.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @description:
 * @auther: Wanghs
 * @date: 2022-07-26
 */
@Service
public class EarlyReportService  {

    @Resource
    private IProEconEarlyReportService proEconEarlyReportService;
    @Resource
    private IProEconEarlyReportWtService proEconEarlyReportWtService;


    public List<ProEconEarlyReport> getEarlyReport(String stationId, Date startTime, Date endTime) {
        List<ProEconEarlyReport> earlyReportByDate = proEconEarlyReportService.findEarlyReportByDate(stationId, startTime, endTime);

        return earlyReportByDate;
    }

    public boolean saveEarlyReport(List<ProEconEarlyReport> lst) {
        boolean b = this.saveEarlyReport(lst);
        return b;
    }

    public IPage<ProEconEarlyReport> getReportByPage(Page<ProEconEarlyReport> page, String stationId) {
        QueryWrapper<ProEconEarlyReport> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(stationId))
            wrapper.like("station_id", stationId);
        wrapper.orderByDesc("report_date");
        IPage<ProEconEarlyReport> reportPage =proEconEarlyReportService.page(page, wrapper);
        return reportPage;
    }

    public ProEconEarlyReport getEarlyReportById(String id) {

        ProEconEarlyReport report = null;
        List<ProEconEarlyReport> earlyReportList = proEconEarlyReportService.findEarlyReportById(id);

        if (earlyReportList != null && earlyReportList.size() > 0) {

            report = earlyReportList.get(0);

            List<ProEconEarlyReportWt> collect = report.getProEconEarlyReportWtList().stream().filter(s -> s.getCompared()).collect(Collectors.toList());
            if (collect.size() > 0) {
                List<String> oldReportWindturbineIdList = collect.stream().map(s -> s.getCompareId()).collect(Collectors.toList());

                List<ProEconEarlyReportWt> oldReportWindturbineList = proEconEarlyReportWtService.getReportWindturbineByIds(oldReportWindturbineIdList);

                report.getProEconEarlyReportWtList().forEach(s -> {
                    if (s.getCompared() && StringUtils.isNotBlank(s.getCompareId())) {
                        Optional<ProEconEarlyReportWt> first = oldReportWindturbineList.stream().filter(k -> k.getId().equals(s.getCompared())).findFirst();
                        if (first.isPresent()) {
                            s.setCompareAlarmInfoList(first.get().getAlarmInfoList());
                        }
                    }
                });
            }

        }

        return report;
    }

    public ProEconEarlyReport findLastEarlyReport() {
        ProEconEarlyReport lastReport = proEconEarlyReportService.findLastReport();

        return lastReport;
    }


}
