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.ProEconPowerstationInfoDay4Mapper;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay4;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay4Service;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 场站三率mtbf 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-22
 */
@Service
public class ProEconPowerstationInfoDay4ServiceImpl extends ServiceImpl<ProEconPowerstationInfoDay4Mapper, ProEconPowerstationInfoDay4> implements IProEconPowerstationInfoDay4Service {

    @Override
    public Map<String, ProEconPowerstationInfoDay4> getEconThreeRate(String foreignKeyId, String staType, String dateType, Date date) {

        Map<String, ProEconPowerstationInfoDay4> resultmap = new HashMap<>();

        QueryWrapper<ProEconPowerstationInfoDay4> qw = null;

        //当前数据
        List<ProEconPowerstationInfoDay4> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(staType)) {
                qw.lambda().eq(ProEconPowerstationInfoDay4::getForeignKeyId, foreignKeyId + staType);
            }
            qw.lambda().eq(ProEconPowerstationInfoDay4::getRecordDate, date);
            currlist = getBaseMapper().selectList(qw);
            resultmap.put("currThreeInfo", currlist.size() > 0 ? currlist.get(0) : null);
        }


        //环比数据
        List<ProEconPowerstationInfoDay4> ringlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(staType)) {
                qw.lambda().eq(ProEconPowerstationInfoDay4::getForeignKeyId, foreignKeyId + staType);
            }
            Date ring = DateUtils.addMonths(date, -1);
            qw.lambda().eq(ProEconPowerstationInfoDay4::getRecordDate, ring);
            ringlist = getBaseMapper().selectList(qw);
            resultmap.put("ringThreeInfo", ringlist.size() > 0 ? ringlist.get(0) : null);
        }

        //同比
        List<ProEconPowerstationInfoDay4> withlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(staType)) {
                qw.lambda().eq(ProEconPowerstationInfoDay4::getForeignKeyId, foreignKeyId + staType);
            }
            Date with = DateUtils.addYears(date, -1);
            qw.lambda().eq(ProEconPowerstationInfoDay4::getRecordDate, with);
            withlist = getBaseMapper().selectList(qw);
            resultmap.put("withThreeInfo", withlist.size() > 0 ? withlist.get(0) : null);
        }

        return resultmap;
    }

    @Override
    public List<ProEconPowerstationInfoDay4> getEconThreeRateCurve(String foreignKeyId, String staType, String dateType, Date date) {
        QueryWrapper<ProEconPowerstationInfoDay4> qw = new QueryWrapper<>();


        if (StringUtils.isNotEmpty(staType)) {
            qw.lambda().eq(ProEconPowerstationInfoDay4::getForeignKeyId, foreignKeyId + staType);
        }

        //当前数据
        List<ProEconPowerstationInfoDay4> currlist = new ArrayList<>();
        if (null != date) {

            Date bingdate = DateUtils.addDays(date, -6);

            qw.lambda().ge(ProEconPowerstationInfoDay4::getRecordDate, bingdate);
            qw.lambda().le(ProEconPowerstationInfoDay4::getRecordDate, date);
        }

        qw.lambda().orderByAsc(ProEconPowerstationInfoDay4::getRecordDate);

        List<ProEconPowerstationInfoDay4> list = baseMapper.selectList(qw);

        return list;
    }

    @Override
    public List<ProEconPowerstationInfoDay4> getEconThreeRateCurvelist(List<String> foreignKeyIds, Date date) {
        QueryWrapper<ProEconPowerstationInfoDay4> qw = new QueryWrapper<>();

        if (null != foreignKeyIds && foreignKeyIds.size() > 0) {
            qw.lambda().in(ProEconPowerstationInfoDay4::getForeignKeyId, foreignKeyIds);
        }

        if (null != date) {
            qw.lambda().eq(ProEconPowerstationInfoDay4::getRecordDate, date);

        }

        List<ProEconPowerstationInfoDay4> list = baseMapper.selectList(qw);

        return list;
    }
}
