package com.xhpc.order.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.xhpc.common.core.constant.StatusConstants;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.order.domain.XhpcStatisticsStation;
import com.xhpc.order.domain.XhpcStatisticsTimeInterval;
import com.xhpc.order.mapper.XhpcStatisticsServiceMapper;
import com.xhpc.order.service.IXhpcStatisticsService;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author yuyang
 * @date 2021/8/31 16:22
 * @Version 1.0
 */
@Service
public class XhpcStatisticsServiceImpl extends BaseService implements IXhpcStatisticsService {

    @Resource
    XhpcStatisticsServiceMapper xhpcStatisticsServiceMapper;
    @Resource
    TokenService tokenService;
    @Resource
    RedisService redisService;

    @Override
    public List<Map<String, Object>> getTimeIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<Map<String, Object>> list =new ArrayList<>();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId);
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 3,logOperatorId,tenantId);
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 4,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId);
        }
        List<Map<String, Object>> page =new ArrayList<>();
        if(list !=null && list.size()>0){
            for (int i = 1; i <=24 ; i++) {
                boolean fan = true;
                for (Map<String, Object> map:list) {
                    int status = (int)map.get("status");
                    if(status==i){
                        page.add(map);
                        fan=false;
                        break;
                    }
                }
                if(fan){
                    if(i==1){
                        page.add(getStatistics("00:00-01:00"));
                    }else if(i==2){
                        page.add(getStatistics("01:00-02:00"));
                    }else if(i==3){
                        page.add(getStatistics("02:00-03:00"));
                    }else if(i==4){
                        page.add(getStatistics("03:00-04:00"));
                    }else if(i==5){
                        page.add(getStatistics("04:00-05:00"));
                    }else if(i==6){
                        page.add(getStatistics("05:00-06:00"));
                    }else if(i==7){
                        page.add(getStatistics("06:00-07:00"));
                    }else if(i==8){
                        page.add(getStatistics("07:00-08:00"));
                    }else if(i==9){
                        page.add(getStatistics("08:00-09:00"));
                    }else if(i==10){
                        page.add(getStatistics("09:00-10:00"));
                    }else if(i==11){
                        page.add(getStatistics("10:00-11:00"));
                    }else if(i==12){
                        page.add(getStatistics("11:00-12:00"));
                    }else if(i==13){
                        page.add(getStatistics("12:00-13:00"));
                    }else if(i==14){
                        page.add(getStatistics("13:00-14:00"));
                    }else if(i==15){
                        page.add(getStatistics("14:00-15:00"));
                    }else if(i==16){
                        page.add(getStatistics("15:00-16:00"));
                    }else if(i==17){
                        page.add(getStatistics("16:00-17:00"));
                    }else if(i==18){
                        page.add(getStatistics("17:00-18:00"));
                    }else if(i==19){
                        page.add(getStatistics("18:00-19:00"));
                    }else if(i==20){
                        page.add(getStatistics("19:00-20:00"));
                    }else if(i==21){
                        page.add(getStatistics("20:00-21:00"));
                    }else if(i==22){
                        page.add(getStatistics("21:00-22:00"));
                    }else if(i==23){
                        page.add(getStatistics("22:00-23:00"));
                    }else if(i==24){
                        page.add(getStatistics("23:00-24:00"));
                    }
                }
            }
        }else{
            page.add(getStatistics("00:00-01:00"));
            page.add(getStatistics("01:00-02:00"));
            page.add(getStatistics("02:00-03:00"));
            page.add(getStatistics("03:00-04:00"));
            page.add(getStatistics("04:00-05:00"));
            page.add(getStatistics("05:00-06:00"));
            page.add(getStatistics("06:00-07:00"));
            page.add(getStatistics("07:00-08:00"));
            page.add(getStatistics("08:00-09:00"));
            page.add(getStatistics("09:00-10:00"));
            page.add(getStatistics("10:00-11:00"));
            page.add(getStatistics("11:00-12:00"));
            page.add(getStatistics("12:00-13:00"));
            page.add(getStatistics("13:00-14:00"));
            page.add(getStatistics("14:00-15:00"));
            page.add(getStatistics("15:00-16:00"));
            page.add(getStatistics("16:00-17:00"));
            page.add(getStatistics("17:00-18:00"));
            page.add(getStatistics("18:00-19:00"));
            page.add(getStatistics("19:00-20:00"));
            page.add(getStatistics("20:00-21:00"));
            page.add(getStatistics("21:00-22:00"));
            page.add(getStatistics("22:00-23:00"));
            page.add(getStatistics("23:00-24:00"));
        }
        return page;
    }
    @Override
    public List<Map<String, Object>> getDateIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) {

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId, chargingStationIdList,internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId, chargingStationIdList,internetUserId, operatorId, startTime, endTime, 3,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId, chargingStationIdList,internetUserId, operatorId, startTime, endTime, 4,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getStationIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds, String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) {

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,3,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,4,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }


        return list;
    }

    @Override
    public List<Map<String, Object>> getOperatorIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String operatorIds, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) {

        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 3,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 4,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getInternetUserIntervalPage(HttpServletRequest request,String internetUserIds, Long internetUserId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) {
        List<String> internetIds =new ArrayList<>();
        if(!"".equals(internetUserIds) && internetUserIds !=null){
            internetIds= Arrays.asList(internetUserIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        Long sysUserId = sysUser.getUserId();
//        if(("".equals(startTime) && "".equals(endTime)) ||(startTime==null && endTime==null)){
//            startTime =date(1);
//            endTime =date(2);
//        }
        return xhpcStatisticsServiceMapper.getInternetUserIntervalPage(internetIds, internetUserId, startTime, endTime, type,sysUserId,tenantId);
    }

    /**
     * 终端统计
     *
     * @param request
     * @param chargingStationId 电站id
     * @param terminalId        终端di
     * @param operatorId        运营商id
     * @param startTime         订单开始时间
     * @param endTime           订单结束时间
     * @param userId            用户id
     * @param type              1 平台 2 运营商 3流量方
     * @return
     */
    @Override
    public List<Map<String, Object>> getTerminalIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId, String terminalId, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<Map<String, Object>> list =new ArrayList<>();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }


        return list;
    }


    @Override
    public AjaxResult getChargingStationList(HttpServletRequest request,String tenantId,Long operatorId, Long userId, Integer type) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, logOperatorId, 1,tenantId));
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //流量方
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, logOperatorId, 3,tenantId));
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                //车队
                Long logOperatorId = sysUser.getInternetUserId();
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, logOperatorId, 4,tenantId));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, sysUserId, 2,tenantId));
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, sysUserId, 0,tenantId));
        }
    }

    /**
     * 电桩列表
     *
     * @param request
     * @param chargingStationId
     * @param operatorId        运营商id
     * @param userId            用户id
     * @param type              1 平台 2 运营商 3流量方
     * @return
     */
    @Override
    public AjaxResult getChargingPileList(HttpServletRequest request,String tenantId, Long chargingStationId, Long operatorId, Long userId, Integer type,Integer display) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingPileList(chargingStationId,operatorId, logOperatorId, 1,tenantId,display));
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //流量方
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingPileList(chargingStationId,operatorId, logOperatorId, 3,tenantId,display));
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                //车队
                Long logOperatorId = sysUser.getInternetUserId();
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingPileList(chargingStationId,operatorId, logOperatorId,4,tenantId,display));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingPileList(chargingStationId,operatorId, sysUserId, 2,tenantId,display));
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingPileList(chargingStationId,operatorId, sysUserId, 0,tenantId,display));
        }
    }

    @Override
    public AjaxResult getOperatorList(HttpServletRequest request,Long operatorId, Long userId, Integer type,String tenantId) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                if(tenantId !=null && !"".equals(tenantId)){
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, logOperatorId, 1,tenantId));
                }else{
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, logOperatorId, 1,null));
                }
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //流量方
                if(tenantId !=null && !"".equals(tenantId)){
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetOperatorList(logOperatorId,tenantId));
                }else{
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetOperatorList(logOperatorId,null));
                }
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                //车队
                Long logOperatorId = sysUser.getInternetUserId();
                if(tenantId !=null && !"".equals(tenantId)){
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetOperatorList(logOperatorId,tenantId));
                }else{
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetOperatorList(logOperatorId,null));
                }
            }else{
                //查询赋值的场站
                if(tenantId !=null && !"".equals(tenantId)){
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, sysUserId, 2,tenantId));
                }else{
                    return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, sysUserId, 2,null));
                }
            }
        }else{
            if(tenantId !=null && !"".equals(tenantId)){
                return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, sysUserId, 0,tenantId));
            }else{
                return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, sysUserId, 0,null));
            }
        }
    }

    @Override
    public AjaxResult getTenantList() {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTenantList(logOperatorId, 1));
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //流量方
                return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetTenantList(logOperatorId));
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //车队
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTenantList(logOperatorId, 4));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTenantList(sysUserId, 2));
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getTenantList(sysUserId, 0));
        }
    }

    @Override
    public AjaxResult getInternetUserIList(HttpServletRequest request,Long internetUserId, Long userId, Integer type) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        Long sysUserId = sysUser.getUserId();
        return AjaxResult.success(xhpcStatisticsServiceMapper.getInternetUserIList(internetUserId, sysUserId, type,tenantId));
    }

    @Override
    public AjaxResult getTerminalList(HttpServletRequest request,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId, Long userId, Integer type) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(operatorId,chargingStationId,chargingPileId, logOperatorId, 1,tenantId));
            }else if(UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //流量方
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(operatorId,chargingStationId,chargingPileId, logOperatorId, 3,tenantId));
            }else if(UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())){
                Long logOperatorId = sysUser.getInternetUserId();
                //车队
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(operatorId,chargingStationId,chargingPileId, logOperatorId, 4,tenantId));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(operatorId,chargingStationId,chargingPileId, sysUserId, 2,tenantId));
            }

        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(operatorId,chargingStationId,chargingPileId, sysUserId, 0,tenantId));
        }
    }

    @Override
    public AjaxResult getSourceList(HttpServletRequest request,Long userId, Integer type) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        Long sysUserId = sysUser.getUserId();
        //桩的统计、该时段金额
        List<Map<String, String>> list = new ArrayList<>();
        if (UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType())) {
            list = xhpcStatisticsServiceMapper.getInternetList(sysUserId,tenantId);
        }else{
            list = xhpcStatisticsServiceMapper.getInternetList(null,tenantId);
            Map<String, String> map1 =new HashMap<>();
            Map<String, String> map2 =new HashMap<>();
            map1.put("name","微信");
            map1.put("id","微信");
            map2.put("name","支付宝");
            map2.put("id","支付宝");
            list.add(map1);
            list.add(map2);
        }
        return AjaxResult.success(list);
    }

    @Override
    public List<Map<String, Object>> getSumSourceList(HttpServletRequest request,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long terminalId,String startTime, String endTime, String sourceIds,String chargingStationIds, String beginStartTime, String beginEndTime) {
        List<String> sourceList =new ArrayList<>();
        if(!"".equals(sourceIds) && sourceIds !=null){
            sourceList= Arrays.asList(sourceIds.split(","));
        }
        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();

        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList,chargingStationIdList,tenantId,logOperatorId,1,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList, chargingStationIdList,tenantId,sysUserId,2,beginStartTime,beginEndTime);
            }
        }else{
            startPage();
            list = xhpcStatisticsServiceMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList, chargingStationIdList,tenantId,sysUserId,0,beginStartTime,beginEndTime);
        }
        return list;
    }


    @Override
    public List<Map<String, Object>> getMechanismPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type,Integer source) {

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getMechanismPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,source);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getMechanismPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId,source);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getMechanismPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId,source);
        }

        return list;
    }

    @Override
    public void addStatisticsTime(XhpcStatisticsTimeInterval xhpcStatisticsTimeInterval) {
        xhpcStatisticsServiceMapper.addStatisticsTime(xhpcStatisticsTimeInterval);
    }

    @Override
    public int getStatisticsTimeHistoryOrderId(Long historyOrderId) {
        return xhpcStatisticsServiceMapper.getStatisticsTimeHistoryOrderId(historyOrderId);
    }

    @Override
    public void addStatisticsStation(XhpcStatisticsStation xhpcStatisticsStation) {
        xhpcStatisticsServiceMapper.addStatisticsStation(xhpcStatisticsStation);
    }

    @Override
    public int getStatisticsStationHistoryOrderId(Long historyOrderId, Integer type) {
        return xhpcStatisticsServiceMapper.getStatisticsStationHistoryOrderId(historyOrderId, type);
    }

    private boolean getJudge(Long internetUserId, Long operatorId, Integer type) {
        if (type == null) {
            return true;
        }
        if (type == 2) {
            if (operatorId == null) {
                return true;
            }
        } else if (type == 3) {
            if (internetUserId == null) {
                return true;
            }
        } else if (type != 1) {
            return true;
        }
        return false;
    }
    private Map<String, Object> getStatistics(String time){
        Map<String, Object> map =new HashMap<>();
        map.put("chargingDegree","0.00");
        map.put("chargingTime","0.00");
        map.put("chargingNumber","0.00");
        map.put("powerPrice","0.00");
        map.put("servicePrice","0.00");
        map.put("totalPrice","0.00");
        map.put("activityPowerPriceTotal","0.00");
        map.put("activityServicePriceTotal","0.00");
        map.put("activityTotalPrice","0.00");
        map.put("promotionDiscount","0.00");
        map.put("actPrice","0.00");
        map.put("actPowerPrice","0.00");
        map.put("actServicePrice","0.00");
        map.put("internetCommission","0.00");
        map.put("internetSvcCommission","0.00");
        map.put("internetDegreeCommission","0.00");
        map.put("platformCommission","0.00");
        map.put("platformSvcCommisssion","0.00");
        map.put("operationCommission","0.00");
        map.put("operationSvcCommission","0.00");
        map.put("time",time);
        return map;
    }

    /**
     * 获取时间
     * type 1 开始时间  2结束时间
     */

    private String date(Integer type){
        if(type ==1){
            //一天的开始，结果：2017-03-01 00:00:00
            Date beginOfDay = DateUtil.beginOfDay(new Date());
            return beginOfDay.toString();
        }
        //一天的结束，结果：2017-03-01 23:59:59
        Date endOfDay = DateUtil.endOfDay(new Date());

        return endOfDay.toString();
    }



    @Override
    public void timeIntervalExport(HttpServletRequest request,HttpServletResponse response, String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list = getTimeIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds, internetUserId, operatorId, startTime, endTime, userId, type);

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("time", "小时时段");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        writer.close();
        IoUtil.close(out);
    }


    @Override
    public void dateIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) throws IOException {
        List<Map<String, Object>> list =new ArrayList<>();

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户

        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            list =xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public void stationIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds,String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) throws IOException {
        List<Map<String, Object>> list =new ArrayList<>();

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();

        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            list =xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("chargingStationName", "电站名称");
        writer.addHeaderAlias("operatorName", "运营商名称");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public void operatorIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String operatorIds,Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) throws IOException {
        List<Map<String, Object>> list =new ArrayList<>();

        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();

        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            list =xhpcStatisticsServiceMapper.getOperatorIntervalPage(chargingStationId,chargingPileId,terminalId,operatorUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }
        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("operatorName", "运营商名称");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");

        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");

        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }


    @Override
    public void getSumSourceListExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long terminalId, String startTime, String endTime, String sourceIds, String chargingStationIds, String beginStartTime, String beginEndTime) throws IOException {

        List<Map<String, Object>> list = getSumSourceList(request,tenantId,operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceIds,chargingStationIds,beginStartTime,beginEndTime);

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("internetUserName", "流量方名称");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");

        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }


    @Override
    public void terminalIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId, Long chargingStationId, String terminalId, Long operatorId, String startTime, String endTime, Long userId, Integer type, String beginStartTime, String beginEndTime) throws IOException {
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<Map<String, Object>> list =new ArrayList<>();
        SysUser sysUser = loginUser.getSysUser();

        Long sysUserId = sysUser.getUserId();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,beginStartTime,beginEndTime);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 2,sysUserId,tenantId,beginStartTime,beginEndTime);
            }
        }else{
            list =xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 0,sysUserId,tenantId,beginStartTime,beginEndTime);
        }
        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("chargingStationName", "电站名称");
        writer.addHeaderAlias("terminalName", "终端名称");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");

        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public void getMechanismExport(HttpServletRequest request, HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type, Integer source) throws IOException {
        List<Map<String, Object>> list =new ArrayList<>();

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getMechanismPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId,source);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getMechanismPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId,source);
            }
        }else{
            list =xhpcStatisticsServiceMapper.getMechanismPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId,source);
        }
        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("internetUserName", "流量方名称");
        writer.addHeaderAlias("chargingDegree", "电量");
        writer.addHeaderAlias("chargingTime", "充电时长（小时）");
        writer.addHeaderAlias("chargingNumber", "充电次数");
        writer.addHeaderAlias("powerPrice", "电费");
        writer.addHeaderAlias("servicePrice", "服务费");
        writer.addHeaderAlias("totalPrice", "订单总价");
        writer.addHeaderAlias("promotionDiscount", "抵扣的总金额");
        writer.addHeaderAlias("actPrice", "用户支付金额");

        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("actPowerPrice", "运营商实收电费");
        writer.addHeaderAlias("actServicePrice", "运营商实收服务费");
        writer.addHeaderAlias("actTotalPrice", "运营商实收总费用");
        writer.addHeaderAlias("clearingPrice", "运营商不参与清分金额");
        writer.addHeaderAlias("actClearingPrice", "运营商清分金额");
        writer.addHeaderAlias("activityPowerPriceTotal", "活动电费");
        writer.addHeaderAlias("activityServicePriceTotal", "活动服务费");
        writer.addHeaderAlias("activityTotalPrice", "活动总金额");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    /**
     * 今日订单量
     *
     * @param operatorId
     * @param chargingStationId
     * @param chargingPileId
     */
    @Override
    public Map<String, Object> getToday(Long operatorId, Long chargingStationId, Long chargingPileId,Integer type) {
        //* 今日订单量、今日订电量、今日营收、今日充电枪状态
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();
        //今日订单量
        //今天时间，格式 yyyy-mm-dd
        String beginOfDay= DateUtil.formatDateTime(DateUtil.beginOfDay(new Date()));
        String endOfDay= DateUtil.formatDateTime(DateUtil.endOfDay(new Date()));

        String lastBeginOfDay = lastOrThisWeek(new Date(), 1, 1);
        String lastEndOfDay = lastOrThisWeek(new Date(), 1, 2);
        String weekBeginOfDay = lastOrThisWeek(new Date(), 2, 1);
        String weekEndOfDay = lastOrThisWeek(new Date(), 2, 2);
        Map<String ,Object> map =new HashMap<>();
        if(tenantId !=null && !"".equals(tenantId)){
            if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getOperatorId();
                    //今日订单量
                    extracted(1,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, lastBeginOfDay, lastEndOfDay, weekBeginOfDay, weekEndOfDay, map, logOperatorId);
                }else if (UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                    //流量方
                    Long logOperatorId = sysUser.getInternetUserId();
                    //今日订单量
                    extracted(3,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, lastBeginOfDay, lastEndOfDay, weekBeginOfDay, weekEndOfDay, map, logOperatorId);
                }else if (UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())) {
                    //流量方
                    Long logOperatorId = sysUser.getInternetUserId();
                    //今日订单量
                    extracted(4,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, lastBeginOfDay, lastEndOfDay, weekBeginOfDay, weekEndOfDay, map, logOperatorId);
                }else{
                    //今日订单量
                    extracted(2,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, lastBeginOfDay, lastEndOfDay, weekBeginOfDay, weekEndOfDay, map, logUserId);
                }
            }else{
                //今日订单量
                extracted(0,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, lastBeginOfDay, lastEndOfDay, weekBeginOfDay, weekEndOfDay, map, null);
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> getOrderElectricityQuantity(Long operatorId, Long chargingStationId, Long chargingPileId, Integer type) {

        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();
        //今日订单量
        //今天时间，格式 yyyy-mm-dd
        String beginOfDay= DateUtil.formatDateTime(DateUtil.beginOfDay(new Date()));
        String endOfDay= DateUtil.formatDateTime(DateUtil.endOfDay(new Date()));

        Map<String ,Object> map =new HashMap<>();
        if(tenantId !=null && !"".equals(tenantId)){
            if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getOperatorId();
                    //今日电量
                    int todayDegreeNUmber = xhpcStatisticsServiceMapper.getTodayDegreeNUmber(1, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
                    map.put("todayDegree",todayDegreeNUmber);
                }else if (UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getInternetUserId();
                    //今日电量
                    int todayDegreeNUmber = xhpcStatisticsServiceMapper.getTodayDegreeNUmber(3, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
                    map.put("todayDegree",todayDegreeNUmber);
                }else if (UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getInternetUserId();
                    //今日电量
                    int todayDegreeNUmber = xhpcStatisticsServiceMapper.getTodayDegreeNUmber(4, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
                    map.put("todayDegree",todayDegreeNUmber);
                }else{
                    //今日电量
                    int todayDegreeNUmber = xhpcStatisticsServiceMapper.getTodayDegreeNUmber(2, logUserId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
                    map.put("todayDegree",todayDegreeNUmber);
                }
            }else{
                //今日电量
                int todayDegreeNUmber = xhpcStatisticsServiceMapper.getTodayDegreeNUmber(0, null, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
                map.put("todayDegree",todayDegreeNUmber);
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> getTodayRrevenue(Long operatorId, Long chargingStationId, Long chargingPileId, Integer type) {
        //* 今日订单量、今日订电量、今日营收、今日充电枪状态
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();
        //今日订单量
        //今天时间，格式 yyyy-mm-dd
        String beginOfDay= DateUtil.formatDateTime(DateUtil.beginOfDay(new Date()));
        String endOfDay= DateUtil.formatDateTime(DateUtil.endOfDay(new Date()));

        Map<String ,Object> map =new HashMap<>();
        if(tenantId !=null && !"".equals(tenantId)){
            if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getOperatorId();
                    //今日营收
                    extracted(1,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, map, logOperatorId);
                }else if (UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getInternetUserId();
                    //今日营收
                    extracted(3,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, map, logOperatorId);
                }else if (UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())) {
                    Long logOperatorId = sysUser.getInternetUserId();
                    //今日营收
                    extracted(4,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, map, logOperatorId);
                }else{
                    //今日营收
                    extracted(2,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, map, logUserId);
                }
            }else{
                //今日营收
                extracted(0,operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay, map, null);
            }
        }
        return map;
    }

    /**
     * 今日电枪实时状态
     *
     * @param operatorId
     * @param chargingStationId
     * @param chargingPileId
     */
    @Override
    public List<Map<String, Object>> getTodayTerminal(Long operatorId, Long chargingStationId, Long chargingPileId) {

        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();

        List<Map<String, Object>> map =new ArrayList<>();
        if(tenantId !=null && !"".equals(tenantId)){
            if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                Long logOperatorId = sysUser.getOperatorId();
                if ("01".equals(sysUser.getUserType())) {
                    //运营商看自己的场站
                    return extracted(1,logOperatorId,operatorId, chargingStationId, chargingPileId, tenantId);
                }else if ("02".equals(sysUser.getInternetUserId())) {
                    //运营商看自己的场站
                    return extracted(3,sysUser.getInternetUserId(),operatorId, chargingStationId, chargingPileId, tenantId);
                }else if ("04".equals(sysUser.getInternetUserId())) {
                    //运营商看自己的场站
                    return extracted(4,sysUser.getInternetUserId(),operatorId, chargingStationId, chargingPileId, tenantId);
                }else{
                    //查询赋值的场站
                    return extracted(2,logUserId,operatorId, chargingStationId, chargingPileId, tenantId);
                }
            }else{
                //全部桩
                return extracted(0,null,operatorId, chargingStationId, chargingPileId, tenantId);
            }
        }
        return map;
    }

    /**
     * 今日电枪实时状态
     *
     * @param operatorId
     * @param chargingStationId
     * @param chargingPileId
     * @param type
     * @param startTime
     * @param endTime
     */
    @Override
    public Map<String, Object> getDateOrder(Integer state,Long operatorId, Long chargingStationId, Long chargingPileId, Integer type, String startTime, String endTime) {
        //1 近30天 2上个月 3本月 4.近12月
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        SysUser sysUser = loginUser.getSysUser();
        Long logUserId = sysUser.getUserId();
        //今日订单量
        //今天时间，格式 yyyy-mm-dd
        Date date = new Date();
        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);
        String conditionBeginOfDay = null;
        String conditionEndOfDay = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(type==null || type ==1){
            conditionBeginOfDay = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -30));
            conditionEndOfDay = DateUtil.formatDateTime(endOfDay);
        }else if(type ==2){
            //获取前月的第一天
            Calendar   c=Calendar.getInstance();//获取当前日期
            c.add(Calendar.MONTH, -1);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            conditionBeginOfDay = format.format(c.getTime());
            //获取前月的最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
            ca.set(Calendar.HOUR, 23);
            ca.set(Calendar.MINUTE, 59);
            ca.set(Calendar.SECOND, 59);
            ca.set(Calendar.MILLISECOND, 999);
            conditionEndOfDay = format.format(ca.getTime());
        }else if(type ==3){
            //获取当前月第一天：
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            conditionBeginOfDay = format.format(c.getTime());
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            ca.set(Calendar.HOUR, 23);
            ca.set(Calendar.MINUTE, 59);
            ca.set(Calendar.SECOND, 59);
            ca.set(Calendar.MILLISECOND, 999);
            conditionEndOfDay = format.format(ca.getTime());
        }else{
            Calendar c=Calendar.getInstance();//获取当前日期
            c.add(Calendar.MONTH, -12);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            conditionBeginOfDay = format.format(c.getTime());
            conditionEndOfDay = DateUtil.formatDateTime(endOfDay);
        }
        if(state==1){
            if(tenantId !=null && !"".equals(tenantId)){
                if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                    Long logOperatorId = sysUser.getOperatorId();
                    if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType()) && StatusConstants.OPERATION_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                        //运营商看自己的场站
                        return extracted(1,type,logOperatorId,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                    }else if (UserTypeUtil.SYS_USER_TYPE_TWO.equals(sysUser.getUserType()) && StatusConstants.INTERNET_POWER_TYPE.equals(sysUser.getDataPowerType())) {
                        //运营商看自己的场站
                        return extracted(1,type,sysUser.getInternetUserId(),operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                    }else if (UserTypeUtil.SYS_MOTORCADE_TYPE_FOUR.equals(sysUser.getUserType()) && StatusConstants.MOTORCADE_TYPE.equals(sysUser.getDataPowerType())) {
                        //运营商看自己的场站
                        return extracted(1,type,sysUser.getInternetUserId(),operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                    }else{
                        //查询赋值的场站
                        return extracted(2,type,logUserId,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                    }
                }else{
                    //全部桩
                    return extracted(0,type,null,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                }
            }
            return new HashMap<>();
        }

        if(tenantId !=null && !"".equals(tenantId)){
            if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                Long logOperatorId = sysUser.getOperatorId();
                if ("01".equals(sysUser.getUserType())) {
                    //运营商看自己的场站
                    return averageOrder(1,type,logOperatorId,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                }else{
                    //查询赋值的场站
                    return averageOrder(2,type,logUserId,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
                }
            }else{
                //全部桩
                return averageOrder(0,type,null,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay) ;
            }
        }
        return new HashMap<>();
    }

    @Override
    public  List<Map<String, Object>>  getEmptyOrder(Long chargingStationId,String startTime,String endTime) {
        //判断登录用户是系统用户还是运维人员
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();
        List<Map<String, Object>> map =new ArrayList<>();
        if(tenantId !=null && !"".equals(tenantId)){
            if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
                if(chargingStationId==null){
                    //运维管理人员
                    map= xhpcStatisticsServiceMapper.getChargingStationEmptyOrder(3,logUserId,tenantId,startTime,endTime);
                }else {
                    //运营商看自己的场站
                    map= xhpcStatisticsServiceMapper.getTerminalEmptyOrder(chargingStationId,3,logUserId,tenantId,startTime,endTime);
                }
            }else{
                if(chargingStationId==null){
                    if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                        Long logOperatorId = sysUser.getOperatorId();
                        if ("01".equals(sysUser.getUserType())) {
                            //运营商看自己的场站
                            map= xhpcStatisticsServiceMapper.getChargingStationEmptyOrder(1,logOperatorId,tenantId,startTime,endTime);
                        }else{
                            //查询赋值的场站
                            map= xhpcStatisticsServiceMapper.getChargingStationEmptyOrder(2,logUserId,tenantId,startTime,endTime);
                        }
                    }else{
                        //全部桩
                        map= xhpcStatisticsServiceMapper.getChargingStationEmptyOrder(0,null,tenantId,startTime,endTime);
                    }
                }else{
                    //单个场站空订单率
                    if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                        Long logOperatorId = sysUser.getOperatorId();
                        if ("01".equals(sysUser.getUserType())) {
                            //运营商看自己的场站
                            map= xhpcStatisticsServiceMapper.getTerminalEmptyOrder(chargingStationId,1,logOperatorId,tenantId,startTime,endTime);
                        }else{
                            //查询赋值的场站
                            map= xhpcStatisticsServiceMapper.getTerminalEmptyOrder(chargingStationId,2,logUserId,tenantId,startTime,endTime);
                        }
                    }else{
                        //全部桩
                        map= xhpcStatisticsServiceMapper.getTerminalEmptyOrder(chargingStationId,0,null,tenantId,startTime,endTime);
                    }
                }

            }
        }
        return map;
    }

    /**
     * 异常订单量列表统计
     *
     * @param operatorId
     * @param chargingStationId
     * @param chargingPileId
     * @param stopReason
     * @param startTime
     * @param endTime
     */
    @Override
    public List<Map<String, Object>> getWorkHistoryOrderList(Long operatorId, Long chargingStationId, Long chargingPileId,Long terminalId, String stopReason, String startTime, String endTime,Integer type) {
        //判断登录用户是系统用户还是运维人员
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();
        List<Map<String, Object>> map =new ArrayList<>();
        //今日订单量
        //今天时间，格式 yyyy-mm-dd
        Date date = new Date();
        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);
        String conditionBeginOfDay = null;
        String conditionEndOfDay = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(type==null){
            conditionBeginOfDay = startTime;
            conditionEndOfDay = endTime;
        }else if(type ==1){
            conditionBeginOfDay = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -30));
            conditionEndOfDay = DateUtil.formatDateTime(endOfDay);
        }else if(type ==2){
            //获取前月的第一天
            Calendar   c=Calendar.getInstance();//获取当前日期
            c.add(Calendar.MONTH, -1);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            conditionBeginOfDay = format.format(c.getTime());
            //获取前月的最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
            ca.set(Calendar.HOUR, 23);
            ca.set(Calendar.MINUTE, 59);
            ca.set(Calendar.SECOND, 59);
            ca.set(Calendar.MILLISECOND, 999);
            conditionEndOfDay = format.format(ca.getTime());
        }else if(type ==3){
            //获取当前月第一天：
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            conditionBeginOfDay = format.format(c.getTime());
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            ca.set(Calendar.HOUR, 23);
            ca.set(Calendar.MINUTE, 59);
            ca.set(Calendar.SECOND, 59);
            ca.set(Calendar.MILLISECOND, 999);
            conditionEndOfDay = format.format(ca.getTime());
        }else{
            Calendar c=Calendar.getInstance();//获取当前日期
            c.add(Calendar.MONTH, -12);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            conditionBeginOfDay = format.format(c.getTime());
            conditionEndOfDay = DateUtil.formatDateTime(endOfDay);
        }
        if(tenantId !=null && !"".equals(tenantId)){
            if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
                //运维管理人员
                if(type !=null && type==4){
                    map= xhpcStatisticsServiceMapper.getWorkHistoryOrderListYY(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,3,logUserId,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                }else{
                    map= xhpcStatisticsServiceMapper.getWorkHistoryOrderList(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,3,logUserId,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                }
            }else{
                if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                    Long logOperatorId = sysUser.getOperatorId();
                    if ("01".equals(sysUser.getUserType())) {
                        //运营商看自己的场站
                        if(type !=null && type==4){
                            map= xhpcStatisticsServiceMapper.getWorkHistoryOrderListYY(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,1,logOperatorId,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                        }else{
                            map= xhpcStatisticsServiceMapper.getWorkHistoryOrderList(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,1,logOperatorId,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                        }

                    }else{
                        //查询赋值的场站
                        if(type !=null && type==4){
                            map= xhpcStatisticsServiceMapper.getWorkHistoryOrderListYY(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,2,logUserId,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                        }else{
                            map= xhpcStatisticsServiceMapper.getWorkHistoryOrderList(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,2,logUserId,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                        }
                    }
                }else{
                    //全部桩
                    if(type !=null && type==4){
                        map= xhpcStatisticsServiceMapper.getWorkHistoryOrderListYY(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,0,null,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                    }else{
                        map= xhpcStatisticsServiceMapper.getWorkHistoryOrderList(operatorId,chargingStationId,chargingPileId,terminalId,stopReason,0,null,tenantId,conditionBeginOfDay,conditionEndOfDay,type);
                    }

                }
            }
        }
        return map;
    }

    /**
     * 工单类型统计
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getWorkOrderNumber() {
        //判断登录用户是系统用户还是运维人员
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        Long logUserId = SecurityUtils.getUserId();
        SysUser sysUser = loginUser.getSysUser();
        List<Map<String, Object>> map =new ArrayList<>();
        if(tenantId !=null && !"".equals(tenantId)){
            if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
                //12 版本号问题 13 校时校价问题 14 费率问题 15 离线问题 16 故障问题 17 未知问题
                map = xhpcStatisticsServiceMapper.getWorkOrderNumber(3,logUserId,tenantId);
                if(map ==null){
                    map =getWorkOrderStatus();
                }
                //异常订单统计
                map.add(xhpcStatisticsServiceMapper.getChargeOrderStatusNumber(3,logUserId,tenantId));
                //退款订单统计
                map.add(xhpcStatisticsServiceMapper.getRefundOderStatusNumber(3,logUserId,tenantId));
            }else{
                if(sysUser.getUserId() !=UserTypeUtil.USER_ID){
                    Long logOperatorId = sysUser.getOperatorId();
                    if ("01".equals(sysUser.getUserType())) {
                        //运营商看自己的场站
                        map = xhpcStatisticsServiceMapper.getWorkOrderNumber(1,logOperatorId,tenantId);
                        if(map.size()==0){
                            map =getWorkOrderStatus();
                        }
                        //异常订单统计
                        map.add(xhpcStatisticsServiceMapper.getChargeOrderStatusNumber(1,logOperatorId,tenantId));
                        //退款订单统计
                        map.add(xhpcStatisticsServiceMapper.getRefundOderStatusNumber(1,logOperatorId,tenantId));
                    }else{
                        //查询赋值的场站
                        map = xhpcStatisticsServiceMapper.getWorkOrderNumber(2,logUserId,tenantId);
                        if(map.size()==0){
                            map =getWorkOrderStatus();
                        }
                        //异常订单统计
                        map.add(xhpcStatisticsServiceMapper.getChargeOrderStatusNumber(2,logUserId,tenantId));
                        //退款订单统计
                        map.add(xhpcStatisticsServiceMapper.getRefundOderStatusNumber(2,logUserId,tenantId));
                    }
                }else{
                    //全部桩
                    map = xhpcStatisticsServiceMapper.getWorkOrderNumber(0,null,tenantId);
                    if(map.size()==0){
                        map =getWorkOrderStatus();
                    }
                    //异常订单统计
                    map.add(xhpcStatisticsServiceMapper.getChargeOrderStatusNumber(0,null,tenantId));
                    //退款订单统计
                    map.add(xhpcStatisticsServiceMapper.getRefundOderStatusNumber(0,null,tenantId));
                }
            }
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> getChargeOrderFaultPage(HttpServletRequest request, Long chargingStationId, String serialNumber, String startTime, String endTime) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        String tenantId = sysUser.getTenantId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getChargeOrderFaultPage(chargingStationId,serialNumber,startTime, endTime, 1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getChargeOrderFaultPage(chargingStationId,serialNumber,startTime,endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getChargeOrderFaultPage(chargingStationId,serialNumber,startTime,endTime,0,sysUserId,tenantId);
        }

        return list;
    }

    @Override
    public void getChargeOrderFaultPageExport(HttpServletRequest request, HttpServletResponse response, Long chargingStationId, String serialNumber, String startTime, String endTime) throws IOException {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        String tenantId = sysUser.getTenantId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getChargeOrderFaultPage(chargingStationId,serialNumber,startTime, endTime, 1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getChargeOrderFaultPage(chargingStationId,serialNumber,startTime,endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getChargeOrderFaultPage(chargingStationId,serialNumber,startTime,endTime,0,sysUserId,tenantId);
        }

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("stationName", "场站名称");
        writer.addHeaderAlias("pileName", "桩名称");
        writer.addHeaderAlias("serialNumber", "订单号");
        writer.addHeaderAlias("remark", "故障原因");
        writer.addHeaderAlias("createTime", "时间");


        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public Map<String, Object> getHistoryOrderMassage(Long chargeOrderId) {




        return null;
    }

    @Override
    public List<Map<String, Object>> getChargingPileStatusPage(HttpServletRequest request, String chargingStationId, String serialNumber,String statusName,String startTime, String endTime) {

        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        String tenantId = sysUser.getTenantId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getChargingPileStatusPage(chargingStationId,serialNumber, statusName, startTime, endTime, 1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getChargingPileStatusPage(chargingStationId,serialNumber, statusName,startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getChargingPileStatusPage(chargingStationId,serialNumber, statusName,startTime, endTime, 0,sysUserId,tenantId);
        }

        return list;
    }

    @Override
    public void getChargingPileStatusPageExport(HttpServletRequest request, HttpServletResponse response, String chargingStationId, String serialNumber, String statusName, String startTime, String endTime) throws IOException {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        String tenantId = sysUser.getTenantId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
            startPage();
            if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcStatisticsServiceMapper.getChargingPileStatusPage(chargingStationId,serialNumber, statusName, startTime, endTime, 1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getChargingPileStatusPage(chargingStationId,serialNumber, statusName,startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getChargingPileStatusPage(chargingStationId,serialNumber, statusName,startTime, endTime, 0,sysUserId,tenantId);
        }

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("stationName", "场站名称");
        writer.addHeaderAlias("pileName", "桩名称");
        writer.addHeaderAlias("statusName", "枪名称");
        writer.addHeaderAlias("number", "离线次数");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }


    private Map<String, Object> averageOrder(Integer status,Integer type,Long logOperatorId,Long operatorId, Long chargingStationId, Long chargingPileId, String tenantId,String conditionBeginOfDay,String conditionEndOfDay) {
        List<Map<String, Object>> dateOrder = xhpcStatisticsServiceMapper.getDateOrderCount(type,status,logOperatorId,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay);
        //  int dateOrderNUmber = xhpcStatisticsServiceMapper.getDateOrderCountNUmber(type, status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, conditionBeginOfDay, conditionEndOfDay);
        Map<String ,Object> map =new HashMap<>();
        if(dateOrder !=null && dateOrder.size()>0 ){
            //  BigDecimal divide = new BigDecimal(dateOrderNUmber).divide(BigDecimal.valueOf(dateOrder.size()), 2, BigDecimal.ROUND_HALF_UP);
            //  map.put("averageOrder",divide);
            map.put("list",dateOrder);
        }else{
            // map.put("averageOrder",0);
            map.put("list",new ArrayList<>());
        }

        return map;
    }
    private Map<String, Object> extracted(Integer status,Integer type,Long logOperatorId,Long operatorId, Long chargingStationId, Long chargingPileId, String tenantId,String conditionBeginOfDay,String conditionEndOfDay) {
        List<Map<String, Object>> dateOrder = xhpcStatisticsServiceMapper.getDateOrder(type,status,logOperatorId,operatorId,chargingStationId,chargingPileId,tenantId,conditionBeginOfDay,conditionEndOfDay);
        //int dateOrderNUmber = xhpcStatisticsServiceMapper.getDateOrderNUmber(type, status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, conditionBeginOfDay, conditionEndOfDay);
        Map<String ,Object> map =new HashMap<>();
        if(dateOrder !=null && dateOrder.size()>0 ){
            //BigDecimal divide = new BigDecimal(dateOrderNUmber).divide(BigDecimal.valueOf(dateOrder.size()), 2, BigDecimal.ROUND_HALF_UP);
            //map.put("averagePower",divide);
            map.put("list",dateOrder);
        }else{
            //map.put("averagePower",0);
            map.put("list",new ArrayList<>());
        }
        return map;
    }




    private List<Map<String, Object>> extracted(Integer status,Long logOperatorId,Long operatorId, Long chargingStationId, Long chargingPileId, String tenantId) {
        List<String> terminal = xhpcStatisticsServiceMapper.getTerminal(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId);

        int offLine =0;//离线
        int fault =0;//故障
        int freeTime =0;//空闲
        int charge =0;//充电
        int unknown =0;//未知
        int insertGun =0;//已插枪
        if(terminal !=null && terminal.size()>0){
            for (String st:terminal) {
                Map<String, Object> cacheMap = redisService.getCacheMap("gun:" + st);
                if(!cacheMap.isEmpty()){
                    if (cacheMap.containsKey("status") &&
                            "离线".equals(cacheMap.get("status").toString())){
                        offLine++;
                    }else if(cacheMap.containsKey("status") &&
                            "故障".equals(cacheMap.get("status").toString())){
                        fault++;
                    }else if(cacheMap.containsKey("status") &&
                            ("空闲".equals(cacheMap.get("status").toString()) || "插枪".equals(cacheMap.get("status").toString()))){
                        if("是".equals(cacheMap.get("vehicleGunStatus").toString())){
                            insertGun++;
                        }else{
                            freeTime++;
                        }
                    }else{
                        charge++;
                    }
                }else{
                    unknown++;
                }
            }
        }

        List<Map<String,Object>> mapList =new ArrayList<>();
        Map<String,Object> objectMap =new HashMap<>();
        objectMap.put("name","离线");
        objectMap.put("value",offLine);
        mapList.add(objectMap);

        List<Map<String,Object>> mapList1 =new ArrayList<>();
        Map<String,Object> objectMap1 =new HashMap<>();
        objectMap1.put("name","故障");
        objectMap1.put("value",fault);
        mapList1.add(objectMap1);
        mapList.addAll(mapList1);

        List<Map<String,Object>> mapList2 =new ArrayList<>();
        Map<String,Object> objectMap2 =new HashMap<>();
        objectMap2.put("name","空闲");
        objectMap2.put("value",freeTime);
        mapList2.add(objectMap2);
        mapList.addAll(mapList2);

        List<Map<String,Object>> mapList3 =new ArrayList<>();
        Map<String,Object> objectMap3 =new HashMap<>();
        objectMap3.put("name","充电");
        objectMap3.put("value",charge);
        mapList3.add(objectMap3);
        mapList.addAll(mapList3);

        List<Map<String,Object>> mapList4 =new ArrayList<>();
        Map<String,Object> objectMap4 =new HashMap<>();
        objectMap4.put("name","未知");
        objectMap4.put("value",unknown);
        mapList4.add(objectMap4);
        mapList.addAll(mapList4);

        List<Map<String,Object>> mapList5 =new ArrayList<>();
        Map<String,Object> objectMap5 =new HashMap<>();
        objectMap5.put("name","已插枪");
        objectMap5.put("value",insertGun);
        mapList5.add(objectMap5);
        mapList.addAll(mapList5);
        return mapList;

    }

    private void extracted(Integer status,Long operatorId, Long chargingStationId, Long chargingPileId, String tenantId, String beginOfDay, String endOfDay, Map<String, Object> map, Long logOperatorId) {
        double t1 = xhpcStatisticsServiceMapper.getTodayAmount(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
        double t2 = xhpcStatisticsServiceMapper.getTodayAmount(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, DateUtil.formatDateTime(DateUtil.offsetDay(DateUtil.parse(beginOfDay), -1)), DateUtil.formatDateTime(DateUtil.offsetDay(DateUtil.parse(endOfDay), -1)));
        Map<String, Object> map1 =new HashMap<>();
        if(t1==0.0 && t2==0.0){
            map1.put("todayAmount",0);
            map1.put("yesterdayAmount",0);
            map1.put("weekNumber",0);
            map1.put("type",true);
            map.put("todayAmount", map1);
        }else if (t1==0.0){
            BigDecimal divide2 = new BigDecimal(t2).multiply(new BigDecimal(100));
            map1.put("todayAmount",0);
            map1.put("yesterdayAmount",t2);
            map1.put("weekNumber",divide2);
            map1.put("type",false);
            map.put("todayAmount", map1);
        }else if(t2==0.0){
            BigDecimal divide1 = new BigDecimal(t1).divide(new BigDecimal(t1),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
            map1.put("todayAmount",t1);
            map1.put("yesterdayAmount",0);
            map1.put("weekNumber",divide1);
            map1.put("type",true);
            map.put("todayAmount", map1);
        }else{
            BigDecimal divide1 = new BigDecimal(t1 - t2).divide(new BigDecimal(t1),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
            BigDecimal divide2 = new BigDecimal(t2 - t1).divide(new BigDecimal(t1),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
            map1.put("todayAmount",t1);
            map1.put("yesterdayAmount",t2);
            map1.put("weekNumber",t1-t2>0?divide1:divide2);
            map1.put("type",t1-t2>0?true:false);
            map.put("todayAmount", map1);
        }
    }

    private void extracted(Integer status, Long operatorId, Long chargingStationId, Long chargingPileId, String tenantId, String beginOfDay, String endOfDay, String lastBeginOfDay, String lastEndOfDay, String weekBeginOfDay, String weekEndOfDay, Map<String, Object> map, Long logOperatorId) {
        List<Map<String,Object>> list = xhpcStatisticsServiceMapper.getTodayStatusNumber(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
        //int todayOrderCount = xhpcStatisticsServiceMapper.getTodayOrderNumber(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, beginOfDay, endOfDay);
//        int t1 = xhpcStatisticsServiceMapper.getTodayNUmber(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, lastBeginOfDay, lastEndOfDay);
//       int t2 = xhpcStatisticsServiceMapper.getTodayNUmber(status, logOperatorId, operatorId, chargingStationId, chargingPileId, tenantId, weekBeginOfDay, weekEndOfDay);
//        Map<String,Object> todayStatusNUmber = new HashMap<>();
//
//        if(t1==0.0 && t2==0.0){
//            todayStatusNUmber.put("weekNumber",0);
//            todayStatusNUmber.put("type",true);
//        }else if (t1==0.0){
//            BigDecimal divide2 = new BigDecimal(t2).multiply(new BigDecimal(100));
//            todayStatusNUmber.put("weekNumber",divide2);
//            todayStatusNUmber.put("type",false);
//        }else if(t2==0.0){
//            todayStatusNUmber.put("weekNumber",100);
//            todayStatusNUmber.put("type",true);
//        }else{
//            BigDecimal divide1 = new BigDecimal(t1 - t2).divide(new BigDecimal(t1),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
//            BigDecimal divide2 = new BigDecimal(t2 - t1).divide(new BigDecimal(t1),2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
//            todayStatusNUmber.put("weekNumber",t1-t2>0?divide1:divide2);
//            todayStatusNUmber.put("type",t1-t2>0?true:false);
//        }
        if(list ==null || list.size()==0){
            List<Map<String,Object>> mapList =new ArrayList<>();
            Map<String,Object> objectMap =new HashMap<>();
            objectMap.put("name","无效订单");
            objectMap.put("value",0);
            mapList.add(objectMap);

            List<Map<String,Object>> mapList1 =new ArrayList<>();
            Map<String,Object> objectMap1 =new HashMap<>();
            objectMap1.put("name","充电中订单");
            objectMap1.put("value",0);
            mapList1.add(objectMap1);
            mapList.addAll(mapList1);

            List<Map<String,Object>> mapList2 =new ArrayList<>();
            Map<String,Object> objectMap2 =new HashMap<>();
            objectMap2.put("name","自动结算订单");
            objectMap2.put("value",0);
            mapList2.add(objectMap2);
            mapList.addAll(mapList2);

            List<Map<String,Object>> mapList3 =new ArrayList<>();
            Map<String,Object> objectMap3 =new HashMap<>();
            objectMap3.put("name","异常订单");
            objectMap3.put("value",0);
            mapList3.add(objectMap3);
            mapList.addAll(mapList3);

            List<Map<String,Object>> mapList4 =new ArrayList<>();
            Map<String,Object> objectMap4 =new HashMap<>();
            objectMap4.put("name","平台结算订单");
            objectMap4.put("value",0);
            mapList4.add(objectMap4);
            mapList.addAll(mapList4);

            List<Map<String,Object>> mapList5 =new ArrayList<>();
            Map<String,Object> objectMap5 =new HashMap<>();
            objectMap5.put("name","其他订单");
            objectMap5.put("value",0);
            mapList5.add(objectMap5);
            mapList.addAll(mapList5);
            map.put("todayOrder",mapList);
        }else{
            map.put("todayOrder",list);
        }
        //  map.put("todayOrderCount",todayOrderCount);
        //  map.put("todayOrderWeek",todayStatusNUmber);
    }

    /**
     * 获取本周和上周的开始时间
     * @param date
     * @param type 1上周 2本周
     * @param status 1 周一 2 周日
     * @return
     */
    public String lastOrThisWeek(Date date,Integer type,Integer status) {

        Calendar c = getCalendar(date);
        int day = c.get(Calendar.DAY_OF_WEEK);

        String week = "";
        //一天的开始，结果：2017-03-01 00:00:00
        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);
        switch (day) {
            case 1:
                //DateUtil.
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -13));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -7));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -6));
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;
            case 2:
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -7));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -1));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(beginOfDay);
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;
            case 3:
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -8));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -2));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -1));
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;
            case 4:
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -9));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -3));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -2));
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;
            case 5:
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -10));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -4));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -3));
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;
            case 6:
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -11));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -5));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -4));
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;

            case 7:
                if(type==1 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(beginOfDay, -12));
                }else if(type==1 && status==2){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -6));
                }else if(type==2 && status==1){
                    week = DateUtil.formatDateTime(DateUtil.offsetDay(endOfDay, -5));
                }else{
                    week = DateUtil.formatDateTime(endOfDay);
                }
                break;
        }
        return week;
    }

    public  Calendar getCalendar(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c;
    }

    public List<Map<String,Object>> getWorkOrderStatus(){
        List<Map<String,Object>> map =new ArrayList<>();
        Map<String, Object> mapStatus = new HashMap<>();
        mapStatus.put("type",12);
        mapStatus.put("name","版本号问题");
        mapStatus.put("number",0);

        Map<String, Object> mapStatus1 = new HashMap<>();
        mapStatus1.put("type",13);
        mapStatus1.put("name","校时校价问题");
        mapStatus1.put("number",0);

        Map<String, Object> mapStatus2 = new HashMap<>();
        mapStatus2.put("type",14);
        mapStatus2.put("name","费率问题");
        mapStatus2.put("number",0);

        Map<String, Object> mapStatus3 = new HashMap<>();
        mapStatus3.put("type",15);
        mapStatus3.put("name","离线问题");
        mapStatus3.put("number",0);

        Map<String, Object> mapStatus4 = new HashMap<>();
        mapStatus4.put("type",16);
        mapStatus4.put("name","故障问题");
        mapStatus4.put("number",0);

        Map<String, Object> mapStatus5 = new HashMap<>();
        mapStatus5.put("type",17);
        mapStatus5.put("name","未知问题");
        mapStatus5.put("number",0);

        map.add(mapStatus);
        map.add(mapStatus1);
        map.add(mapStatus2);
        map.add(mapStatus3);
        map.add(mapStatus4);
        map.add(mapStatus5);
        return  map;
    }





    public static void main(String[] args) {
        Date date = new Date();
        Date beginOfDay = DateUtil.beginOfDay(date);
        Date endOfDay = DateUtil.endOfDay(date);

        String conditionBeginOfDay = null;
        String conditionEndOfDay = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        conditionBeginOfDay = format.format(c.getTime());
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        ca.set(Calendar.HOUR, 23);
        ca.set(Calendar.MINUTE, 59);
        ca.set(Calendar.SECOND, 59);
        ca.set(Calendar.MILLISECOND, 999);
        conditionEndOfDay = format.format(ca.getTime());


        System.out.println(conditionBeginOfDay);
        System.out.println(conditionEndOfDay);
    }


}
