package com.gyee.runeconomy.service.auto.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.mapper.auto.ProEconReportIndicatorPoolMapper;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay1;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay5;
import com.gyee.runeconomy.model.auto.ProEconReportIndicatorPool;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay1Service;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay5Service;
import com.gyee.runeconomy.service.auto.IProEconReportIndicatorPoolService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 报表指标池 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-11-17
 */
@Service
public class ProEconReportIndicatorPoolServiceImpl extends ServiceImpl<ProEconReportIndicatorPoolMapper, ProEconReportIndicatorPool> implements IProEconReportIndicatorPoolService {
    @Resource
    private IProEconPowerstationInfoDay5Service day5Service;

    @Resource
    private IProEconPowerstationInfoDay1Service day1Service;

    @Override
    public void getpool() {

        Date rq = DateUtils.getCurrentDate();

        QueryWrapper<ProEconPowerstationInfoDay5> qw5 = new QueryWrapper();
        qw5.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, rq);
        List<ProEconPowerstationInfoDay5> day5 = day5Service.list(qw5);

        QueryWrapper<ProEconPowerstationInfoDay1> qw1 = new QueryWrapper();
        qw1.lambda().eq(ProEconPowerstationInfoDay1::getRecordDate, rq);
        List<ProEconPowerstationInfoDay1> day1 = day1Service.list(qw1);

        List<ProEconReportIndicatorPool> pool = new ArrayList<>();
        for (ProEconPowerstationInfoDay5 day : day5) {

            ProEconReportIndicatorPool po = new ProEconReportIndicatorPool();

            po.setId(StringUtils.getUUID());
            po.setForeignKeyId(day.getForeignKeyId());
            po.setRecordDate(day.getRecordDate());
            po.setType(day.getType());
            po.setWindpowerstationId(day.getWindpowerstationId());
            po.setProjectId(day.getProjectId());
            po.setLineId(day.getLineId());
            po.setCompanyId(day.getCompanyId());
            po.setRegionId(day.getRegionId());
            po.setLocation(day.getLocation());
            po.setRfdldb(day.getRfdldb());
            po.setRswdldb(day.getRswdldb());
            po.setRgwgwdldb(day.getRgwgwdldb());
            po.setRnwgwdldb(day.getRnwgwdldb());
            po.setRzhcydldb(day.getRzhcydldb());
            po.setRfdcydldb(day.getRfdcydldb());
            po.setYfdldb(day.getYfdldb());
            po.setYswdldb(day.getYswdldb());
            po.setYgwgwdldb(day.getYgwgwdldb());
            po.setYnwgwdldb(day.getYnwgwdldb());
            po.setYzhcydldb(day.getYzhcydldb());
            po.setYfdcydldb(day.getYfdcydldb());
            po.setNfdldb(day.getNfdldb());
            po.setNswdldb(day.getNswdldb());
            po.setNgwgwdldb(day.getNgwgwdldb());
            po.setNnwgwdldb(day.getNnwgwdldb());
            po.setNzhcydldb(day.getNzhcydldb());
            po.setNfdcydldb(day.getNfdcydldb());
            po.setNfdcydldb(day.getNfdcydldb());

            pool.add(po);
        }

        if (pool != null && pool.size() > 1) {

            pool.stream().forEach(i -> {
                Optional<ProEconPowerstationInfoDay1> optional = day1.stream()
                        .filter(j -> j.getForeignKeyId().equals(i.getForeignKeyId()) && j.getRecordDate().equals(i.getRecordDate()))
                        .findFirst();

                if (optional.isPresent()) {
                    ProEconPowerstationInfoDay1 day = optional.get();
                    //日
                    i.setRzdfs(day.getRzdfs());
                    i.setRzxfs(day.getRzxfs());
                    i.setRzdgl(day.getRzdgl());
                    i.setRzxgl(day.getRzxgl());
                    i.setRpjgl(day.getRpjgl());
                    i.setRpjfs(day.getRpjfs());
                    i.setRllfdl(day.getRllfdl());
                    i.setRgzssdl(day.getRgzssdl());
                    i.setRcnslgzssdl(day.getRcnslgzssdl());
                    i.setRjxssdl(day.getRjxssdl());
                    i.setRcnsljxssdl(day.getRcnsljxssdl());
                    i.setRdjssdl(day.getRdjssdl());
                    i.setRqxjclssdl(day.getRqxjclssdl());
                    i.setRsdtjssdl(day.getRsdtjssdl());
                    i.setRxnssdl(day.getRxnssdl());
                    i.setRxdjclssdl(day.getRxdjclssdl());
                    i.setRxdtjssdl(day.getRxdtjssdl());
                    i.setRcwsldwssdl(day.getRcwsldwssdl());
                    i.setRcwsltqssdl(day.getRcwsltqssdl());

                    i.setYzdfs(day.getYzdfs());
                    i.setYzxfs(day.getYzxfs());
                    i.setYzdgl(day.getYzdgl());
                    i.setYzxgl(day.getYzxgl());
                    i.setYpjgl(day.getYpjgl());
                    i.setYpjfs(day.getYpjfs());
                    i.setYllfdl(day.getYllfdl());
                    i.setYgzssdl(day.getYgzssdl());
                }
            });
        }

        pool.stream().forEach(i -> {
            QueryWrapper<ProEconReportIndicatorPool> qwpool = new QueryWrapper<>();
            qwpool.lambda().eq(ProEconReportIndicatorPool::getRecordDate, i.getRecordDate());
            qwpool.lambda().eq(ProEconReportIndicatorPool::getForeignKeyId, i.getForeignKeyId());
            Long count = baseMapper.selectCount(qwpool);
            if (count > 0) {
                baseMapper.update(i, qwpool);
            } else if (count <= 0) {
                baseMapper.insert(i);
            }

        });


    }
}
