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


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.common.model.StringUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.runeconomy.mapper.auto.ProEconPowerstationInfoDay1Mapper;
import com.gyee.runeconomy.model.auto.ProEconPowerstationInfoDay1;
import com.gyee.runeconomy.service.auto.IProEconPowerstationInfoDay1Service;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * <p>
 * 场站五损电量 服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-11-21
 */
@Service
public class ProEconPowerstationInfoDay1ServiceImpl extends ServiceImpl<ProEconPowerstationInfoDay1Mapper, ProEconPowerstationInfoDay1> implements IProEconPowerstationInfoDay1Service {


    @Override
    public IPage<ProEconPowerstationInfoDay1> list(String pageNum, String pageSize) {
        QueryWrapper<ProEconPowerstationInfoDay1> qw = new QueryWrapper<>();

        Page<ProEconPowerstationInfoDay1> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        IPage<ProEconPowerstationInfoDay1> powerstationInfoDay1s = getBaseMapper().selectPage(page, qw);
        return powerstationInfoDay1s;
    }

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

        QueryWrapper<ProEconPowerstationInfoDay1> qw = null;


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


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

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

        return resultmap;
    }

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

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

        if (null != date) {
            Date bingdate = DateUtils.addDays(date, -6);

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

        }

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

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

        return list;
    }

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

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

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

        }

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

        return list;
    }

    @Override
    public List<ProEconPowerstationInfoDay1> getmax(String foreignKeyId, Date date) {
        QueryWrapper<ProEconPowerstationInfoDay1> qw = null;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endOfDayOfLastDayOfMonth = calendar.getTime();
        //当前数据
        List<ProEconPowerstationInfoDay1> currlist = new ArrayList<>();
        if (null != date) {
            qw = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(foreignKeyId)) {
                qw.lambda().eq(ProEconPowerstationInfoDay1::getForeignKeyId, foreignKeyId);
            }
            qw.lambda().between(ProEconPowerstationInfoDay1::getRecordDate, DateUtils.getMonthFirst(date), endOfDayOfLastDayOfMonth);
            qw.lambda().orderByDesc(ProEconPowerstationInfoDay1::getRecordDate);
            qw.last("limit 1");
            currlist = baseMapper.selectList(qw);

        }
        return currlist;
    }


}
