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.ProEconPowerstationInfoDay2Mapper;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay2;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay2Service;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 场站五损时间 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-22
 */
@Service
public class ProEconPowerstationInfoDay2ServiceImpl extends ServiceImpl<ProEconPowerstationInfoDay2Mapper, ProEconPowerstationInfoDay2> implements IProEconPowerstationInfoDay2Service {

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

        QueryWrapper<ProEconPowerstationInfoDay2> qw = null;


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


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

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

        return resultmap;
    }
}
