package com.gyee.runeconomy.service.outputspeed;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.ProEconInOrOutSpeedTotal;
import com.gyee.runeconomy.service.auto.IProEconInOrOutSpeedTotalService;
import com.gyee.runeconomy.util.DateUtils;
import com.gyee.runeconomy.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class OutputSpeedService {

    @Resource
    private IProEconInOrOutSpeedTotalService proEconInOrOutSpeedTotalService;

    public Page<ProEconInOrOutSpeedTotal> outputSpeedlist(Integer pageNum, Integer pageSize, String cmId, String type, String wpId, String recorddate) {

        if (StringUtils.empty(pageNum)) {
            pageNum = 1;
        }
        if (StringUtils.empty(pageSize)) {
            pageSize = 10;
        }
        //构造分页构造器
        Page<ProEconInOrOutSpeedTotal> pageInfo = new Page<>(pageNum, pageSize);
        if (StringUtils.notEmp(cmId) && StringUtils.notEmp(recorddate)) {
            Date date = com.gyee.common.util.DateUtils.parseDate(recorddate);
            //构造条件构造器
            LambdaQueryWrapper<ProEconInOrOutSpeedTotal> queryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件


            if (StringUtils.notEmp(wpId)) {
                queryWrapper.eq(ProEconInOrOutSpeedTotal::getWindpowerstationId, wpId).eq(ProEconInOrOutSpeedTotal::getTypes, type).
                        eq(ProEconInOrOutSpeedTotal::getRecordDate, date);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }

            } else if (StringUtils.notEmp(cmId) && CacheContext.cpmap.containsKey(cmId)) {
                queryWrapper.
                        eq(ProEconInOrOutSpeedTotal::getCompanyId, cmId).

                        eq(ProEconInOrOutSpeedTotal::getRecordDate, date);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }
            } else if (StringUtils.notEmp(cmId) && CacheContext.rgmap.containsKey(cmId)) {
                queryWrapper.eq(ProEconInOrOutSpeedTotal::getRegionId, cmId).

                        eq(ProEconInOrOutSpeedTotal::getRecordDate, date);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }
            }

            //执行查询
            proEconInOrOutSpeedTotalService.page(pageInfo, queryWrapper);
        }

        return pageInfo;

    }


    public Page<ProEconInOrOutSpeedTotal> outputspeedhistorylist(Integer pageNum, Integer pageSize, String cmId, String type, String wpId, String wtId, String beginDate, String endDate) {


        if (StringUtils.empty(pageNum)) {
            pageNum = 1;
        }
        if (StringUtils.empty(pageSize)) {
            pageSize = 10;
        }
        //构造分页构造器
        Page<ProEconInOrOutSpeedTotal> pageInfo = new Page<>(pageNum, pageSize);


        if (StringUtils.notEmp(type) && StringUtils.notEmp(beginDate) && StringUtils.notEmp(endDate)) {

            Date begin = DateUtils.parseDate(beginDate);
            Date end = DateUtils.parseDate(endDate);

            //构造条件构造器
            LambdaQueryWrapper<ProEconInOrOutSpeedTotal> queryWrapper = new LambdaQueryWrapper<>();
            //添加过滤条件


            if (StringUtils.notEmp(wtId)) {
                queryWrapper.eq(ProEconInOrOutSpeedTotal::getWindturbineId, wtId).
                        ge(ProEconInOrOutSpeedTotal::getRecordDate, begin).
                        le(ProEconInOrOutSpeedTotal::getRecordDate, end);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }
            } else if (StringUtils.notEmp(wpId)) {
                queryWrapper.eq(ProEconInOrOutSpeedTotal::getWindpowerstationId, wpId).
                        ge(ProEconInOrOutSpeedTotal::getRecordDate, begin).
                        le(ProEconInOrOutSpeedTotal::getRecordDate, end);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }
            } else if (StringUtils.notEmp(cmId) && CacheContext.cpmap.containsKey(cmId)) {
                queryWrapper.
                        eq(ProEconInOrOutSpeedTotal::getCompanyId, cmId).
                        ge(ProEconInOrOutSpeedTotal::getRecordDate, begin).
                        le(ProEconInOrOutSpeedTotal::getRecordDate, end);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }
            } else if (StringUtils.notEmp(cmId) && CacheContext.rgmap.containsKey(cmId)) {
                queryWrapper.eq(ProEconInOrOutSpeedTotal::getRegionId, cmId).
                        eq(ProEconInOrOutSpeedTotal::getTypes, type).
                        ge(ProEconInOrOutSpeedTotal::getRecordDate, begin).
                        le(ProEconInOrOutSpeedTotal::getRecordDate, end);
                if (StringUtils.notEmp(type) && !type.equals("0")) {
                    queryWrapper.eq(ProEconInOrOutSpeedTotal::getTypes, type);
                }
            }

            //执行查询
            proEconInOrOutSpeedTotalService.page(pageInfo, queryWrapper);


        }

        return pageInfo;

    }
}
