package com.xhpc.order.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
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.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.IXhpcHistoryOrderService;
import com.xhpc.order.service.IXhpcStatisticsService;

import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author yuyang
 * @date 2021/8/31 16:22
 * @Version 1.0
 */
@Service
public class XhpcStatisticsServiceImpl extends BaseService implements IXhpcStatisticsService {

    @Autowired
    private XhpcStatisticsServiceMapper xhpcStatisticsServiceMapper;
    @Autowired
    private IXhpcHistoryOrderService xhpcHistoryOrderService;
    @Autowired
    private TokenService tokenService;

    @Override
    public List<Map<String, Object>> getTimeIntervalPage(HttpServletRequest request,String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if (getJudge(internetUserId, operatorId, type)){
            return new ArrayList<>();
        }
        List<String> chargingStationId =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationId= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<Map<String, Object>> list =new ArrayList<>();
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        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.getTimeIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getTimeIntervalPage(chargingStationId, 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 chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if (getJudge(internetUserId, operatorId, type)){
            return new ArrayList<>();
        }
        List<String> chargingStationId =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationId= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        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.getDateIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId);
            }else{
            //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getDateIntervalPage(chargingStationId, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getStationIntervalPage(HttpServletRequest request,String chargingStationIds, String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if (getJudge(internetUserId, operatorId, type)){
            return new ArrayList<>();
        }
        List<String> chargingStationId =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationId= 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();
        String tenantId = loginUser.getTenantId();
        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.getStationIntervalPage(chargingStationId,operatorUserId,internetUserId,operatorId,startTime,endTime,1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,operatorUserId,internetUserId,operatorId,startTime,endTime,2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getStationIntervalPage(chargingStationId,operatorUserId,internetUserId,operatorId,startTime,endTime,0,sysUserId,tenantId);
        }


        return list;
    }

    @Override
    public List<Map<String, Object>> getOperatorIntervalPage(HttpServletRequest request,String operatorIds, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        if(getJudge(null,operatorId,type)){
            return 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();
        String tenantId = loginUser.getTenantId();
        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.getOperatorIntervalPage(operatorUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getOperatorIntervalPage(operatorUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getOperatorIntervalPage(operatorUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getInternetUserIntervalPage(HttpServletRequest request,String internetUserIds, Long internetUserId, String startTime, String endTime, Long userId, Integer type) {
        if(getJudge(internetUserId,null,type)){
            return new ArrayList<>();
        }
        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);
    }

    @Override
    public List<Map<String, Object>> getTerminalIntervalPage(HttpServletRequest request,String chargingStationId, String terminalId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<Map<String, Object>> list =new ArrayList<>();
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        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);
            }else{
                //查询赋值的场站
                list = xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
            }
        }else{
            startPage();
            list =xhpcStatisticsServiceMapper.getTerminalIntervalPage(chargingStationId, terminalId, operatorId, startTime, endTime, 0,sysUserId,tenantId);
        }


        return list;
    }

    @Override
    public AjaxResult getChargingStationList(HttpServletRequest request,Long operatorId, Long userId, Integer type) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        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();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, logOperatorId, 1,tenantId));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, sysUserId, 2,tenantId));
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getChargingStationList(operatorId, sysUserId, 0,tenantId));
        }
    }

    @Override
    public AjaxResult getOperatorList(HttpServletRequest request,Long operatorId, Long userId, Integer type) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        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();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, logOperatorId, 1,tenantId));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, sysUserId, 2,tenantId));
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getOperatorList(operatorId, sysUserId, 0,tenantId));
        }
    }

    @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,Long chargingStationId, Long userId, Integer type) {
//获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        String tenantId = loginUser.getTenantId();
        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();
                //运营商看自己的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(chargingStationId, logOperatorId, 1,tenantId));
            }else{
                //查询赋值的场站
                return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(chargingStationId, sysUserId, 2,tenantId));
            }
        }else{
            return AjaxResult.success(xhpcStatisticsServiceMapper.getTerminalList(chargingStationId, 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 startTime, String endTime, String sourceIds, Long userId, Integer type,String chargingStationIds) {
        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();
        String tenantId = loginUser.getTenantId();
        Long sysUserId = sysUser.getUserId();
        return xhpcStatisticsServiceMapper.getSumSourceList(startTime, endTime, sourceList, sysUserId, type,chargingStationIdList,tenantId);
    }

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

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

    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);
        map.put("powerPrice",0.00);
        map.put("servicePrice",0.00);
        map.put("totalPrice",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 chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list = getTimeIntervalPage(request,chargingStationIds, internetUserId, operatorId, startTime, endTime, userId, type);

        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        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("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");

        // 默认的，未添加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 chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list = getDateIntervalPage(request,chargingStationIds, internetUserId, operatorId, startTime, endTime, userId, type);

        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        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("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");

        // 默认的，未添加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 chargingStationIds,String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list = getStationIntervalPage(request,chargingStationIds, operatorIds, internetUserId, operatorId, startTime, endTime, userId, type);

        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        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("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        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 void operatorIntervalExport(HttpServletRequest request,HttpServletResponse response, String operatorIds,Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list = getOperatorIntervalPage(request,operatorIds, operatorId, startTime, endTime, userId, type);

        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        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("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        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 void getSumSourceListExport(HttpServletRequest request,HttpServletResponse response, String startTime, String endTime, String sourceIds,Long userId,Integer type, String chargingStationIds) throws IOException {

        List<Map<String, Object>> list = getSumSourceList(request,startTime, endTime, sourceIds, userId, type,chargingStationIds);

        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        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("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        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 void terminalIntervalExport(HttpServletRequest request,HttpServletResponse response, String chargingStationId, String terminalId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list = getTerminalIntervalPage(request,chargingStationId, terminalId, operatorId, startTime, endTime, userId, type);

        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter(true);
        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("actPowerPrice", "运营商电费");
        writer.addHeaderAlias("actServicePrice", "运营商服务费");
        writer.addHeaderAlias("internetCommission", "流量方总金额抽成");
        writer.addHeaderAlias("internetSvcCommission", "流量方服务费抽成");
        writer.addHeaderAlias("platformCommission", "平台总金额抽成");
        writer.addHeaderAlias("platformSvcCommisssion", "平台服务费抽成");
        writer.addHeaderAlias("operationCommission", "运维总金额抽成");
        writer.addHeaderAlias("operationSvcCommission", "运维服务费抽成");
        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);
    }


}
