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.ProEconPowerstationInfoDay7Mapper;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay7;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay7Service;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 离散率系统效率 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-22
 */
@Service
public class ProEconPowerstationInfoDay7ServiceImpl extends ServiceImpl<ProEconPowerstationInfoDay7Mapper, ProEconPowerstationInfoDay7> implements IProEconPowerstationInfoDay7Service {

    @Override
    public Map<String, ProEconPowerstationInfoDay7> getEconThreeRate(String foreignKeyId, String staType, String dateType, Date date) {
        Map<String, ProEconPowerstationInfoDay7> resultmap = new HashMap<>();

        QueryWrapper<ProEconPowerstationInfoDay7> qw = null;

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


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

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

        return resultmap;
    }

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


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

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

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

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

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

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

        return list;
    }
}
