package com.xhpc.order.controller;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.order.service.IXhpcStatisticsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @date 2021/8/31 16:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/statistics")
@Api(value = "统计接口", tags = "统计接口")
public class XhpcStatisticsController extends BaseController {

    @Autowired
    private IXhpcStatisticsService xhpcStatisticsService;
    /**
     * 小时统计
     * @param chargingStationIds         电站id集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getTimeIntervalPage")
    public TableDataInfo 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<Map<String, Object>> list = xhpcStatisticsService.getTimeIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
        return getDataTable(list);
    }

    /**
     * 日期统计
     * @param chargingStationIds         电站id集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getDateIntervalPage")
    public TableDataInfo 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)
    {
        List<Map<String, Object>> list = xhpcStatisticsService.getDateIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
        return getDataTable(list);
    }
    /**
     * 电站统计
     * @param chargingStationIds  电站集合
     * @param operatorIds       运营商集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getStationIntervalPage")
    public TableDataInfo 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)
    {
        List<Map<String, Object>> list = xhpcStatisticsService.getStationIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,operatorIds,internetUserId,operatorId,startTime,endTime,userId,type);
        return getDataTable(list);
    }

    /**
     * 运营商统计
     * @param operatorIds        运营商集合
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getOperatorIntervalPage")
    public TableDataInfo getOperatorIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String operatorIds, Long operatorId,String startTime, String endTime, Long userId,Integer type)
    {
        List<Map<String, Object>> list = xhpcStatisticsService.getOperatorIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,operatorIds,operatorId,startTime,endTime,userId,type);
        return getDataTable(list);
    }

    /**
     * 流量方统计
     * @param internetUserIds   流量方集合
     * @param internetUserId      流量方id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getInternetUserIntervalPage")
    public TableDataInfo getInternetUserIntervalPage(HttpServletRequest request,String internetUserIds,Long internetUserId,String startTime, String endTime, Long userId,Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getInternetUserIntervalPage(request,internetUserIds,internetUserId,startTime,endTime,userId,type);
        return getDataTable(list);
    }


    /**
     * 终端统计
     * @param chargingStationId  电站集合
     * @param terminalId        终端集合
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getTerminalIntervalPage")
    public TableDataInfo getTerminalIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,String terminalId,Long operatorId,String startTime, String endTime,Long userId,Integer type)
    {
        List<Map<String, Object>> list = xhpcStatisticsService.getTerminalIntervalPage(request,tenantId,chargingStationId,terminalId,operatorId,startTime,endTime,userId,type);
        return getDataTable(list);
    }

    /**
     * 电站列表
     * @param operatorId      运营商id
     * @param userId          用户id
     * @param type            1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getChargingStationList")
    public AjaxResult getChargingStationList(HttpServletRequest request,String tenantId,Long operatorId,Long userId,Integer type)
    {

        return xhpcStatisticsService.getChargingStationList(request,tenantId,operatorId, userId, type);
    }

    @GetMapping("/getChargingPileList")
    public AjaxResult getChargingPileList(HttpServletRequest request,String tenantId,Long chargingStationId,Long operatorId, Long userId,Integer type)
    {

        return xhpcStatisticsService.getChargingPileList(request,tenantId,chargingStationId,operatorId, userId, type);
    }


    /**
     * 运营商列表
     * @param operatorId      运营商id
     * @param userId          用户id
     * @param type            1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getOperatorList")
    public AjaxResult getOperatorList(HttpServletRequest request,Long operatorId, Long userId,Integer type,String tenantId)
    {
        return xhpcStatisticsService.getOperatorList(request,operatorId, userId, type,tenantId);
    }

    /**
     * 租户列表
     * @return
     */
    @GetMapping("/getTenantList")
    public AjaxResult getTenantList()
    {
        return xhpcStatisticsService.getTenantList();
    }


    /**
     * 流量方列表
     * @param internetUserId  流量方id
     * @param userId          用户id
     * @param type            1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getInternetUserIList")
    public AjaxResult getInternetUserIList(HttpServletRequest request,Long internetUserId, Long userId,Integer type)
    {
        return xhpcStatisticsService.getInternetUserIList(request,internetUserId, userId, type);
    }

    /**
     * 终端列表
     * @param chargingStationId  电站id
     * @param userId          用户id
     * @param type            1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getTerminalList")
    public AjaxResult getTerminalList(HttpServletRequest request,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long userId, Integer type) {

        return xhpcStatisticsService.getTerminalList(request,tenantId,operatorId,chargingStationId,chargingPileId, userId, type);
    }

    /**
     * 订单来源地方，权限划分
     * @param userId              用户id
     * @param type                1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getSourceList")
    public AjaxResult getSourceList(HttpServletRequest request, Long userId,  Integer type) {
        return xhpcStatisticsService.getSourceList(request,userId, type);
    }

    /**
     * 订单来源（第三方、支付宝、微信、运营商），权限划分
     * @param internetUserIds     订单来源集合
     * @return
     */
    @GetMapping("/getSumSourceList")
    public TableDataInfo getSumSourceList(HttpServletRequest request,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long terminalId,String startTime, String endTime, String internetUserIds, String chargingStationIds) {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getSumSourceList(request,tenantId,operatorId,chargingStationId,chargingPileId,terminalId,startTime,endTime,internetUserIds,chargingStationIds);
        return getDataTable(list);
    }

    /**
     * 机构统计
     * @param chargingStationIds         电站id集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @param source           订单来源（0 C端用户 1 流量方用户 2社区用户 3B端用户）
     * @return
     */
    @GetMapping("/getMechanismPage")
    public TableDataInfo 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<Map<String, Object>> list = xhpcStatisticsService.getMechanismPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type,source);
        return getDataTable(list);
    }

    // -------- 导出数据 ------------


    /**
     * 小时统计导出
     * @param chargingStationIds         电站id集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @PostMapping("/timeInterval/export")
    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 {
        xhpcStatisticsService.timeIntervalExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId, chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
    }

    /**
     * 日期统计导出
     * @param chargingStationIds         电站id集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @PostMapping("/dateInterval/export")
    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) throws IOException {
        xhpcStatisticsService.dateIntervalExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId, chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);
    }
    /**
     * 电站统计导出
     * @param chargingStationIds  电站集合
     * @param operatorIds       运营商集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @PostMapping("/stationInterval/export")
    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) throws IOException {
        xhpcStatisticsService.stationIntervalExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId, chargingStationIds,operatorIds,internetUserId,operatorId,startTime,endTime,userId,type);
    }

    /**
     * 运营商统计导出
     * @param operatorIds        运营商集合
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @PostMapping("/operatorInterval/export")
    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) throws IOException {
        xhpcStatisticsService.operatorIntervalExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId, operatorIds,operatorId,startTime,endTime,userId,type);
    }

    /**
     * 订单来源统计导出
     * @return
     */
    @PostMapping("/internetUserInterval/export")
    public void internetUserIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long terminalId, String startTime, String endTime,String internetUserIds, String chargingStationIds) throws IOException {
        xhpcStatisticsService.getSumSourceListExport(request,response,tenantId,operatorId,chargingStationId,chargingPileId,terminalId, startTime,endTime,internetUserIds,chargingStationIds);
    }


    /**
     * 终端统计
     * @param chargingStationId  电站集合
     * @param terminalId        终端集合
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @PostMapping("/terminalInterval/export")
    public void terminalIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId, Long chargingStationId,String terminalId,Long operatorId,String startTime, String endTime,Long userId,Integer type) throws IOException {
        xhpcStatisticsService.terminalIntervalExport(request,response,tenantId, chargingStationId,terminalId,operatorId,startTime,endTime,userId,type);
    }

    /**
     * 机构导出
     * @param chargingStationIds         电站id集合
     * @param internetUserId      流量方id
     * @param operatorId          运营商id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @param source           订单来源（0 C端用户 1 流量方用户 2社区用户 3B端用户）
     * @return
     */
    @PostMapping("/getMechanismPage/export")
    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,@RequestParam("source")Integer source)throws IOException
    {
        xhpcStatisticsService.getMechanismExport(request,response,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type,source);
    }



    //-------------------------------------------------首页------------------------------------------------------------

    /**
     * 今日订单量、今日订电量、今日营收、今日充电枪状态
     */
    @GetMapping("/getToday")
    public AjaxResult getToday(Long operatorId,Long chargingStationId,Long chargingPileId,Integer type)
    {
        return AjaxResult.success(xhpcStatisticsService.getToday(operatorId,chargingStationId,chargingPileId,type));
    }

    /**
     * 今日电枪实时状态
     */
    @GetMapping("/getTodayTerminal")
    public AjaxResult getTodayTerminal(Long operatorId,Long chargingStationId,Long chargingPileId)
    {
        return AjaxResult.success(xhpcStatisticsService.getTodayTerminal(operatorId,chargingStationId,chargingPileId));
    }

    /**
     * 充电数据
     * @param type       1 近30天 2上个月 3本月 4.近12月
     */
    @GetMapping("/getDateOrder")
    public AjaxResult getDateOrder(Long operatorId,Long chargingStationId,Long chargingPileId,Integer type,String startTime,String endTime)
    {
        return AjaxResult.success(xhpcStatisticsService.getDateOrder(1,operatorId,chargingStationId,chargingPileId,type,startTime,endTime));
    }

    /**
     * 创建订单量
     * @param type       1 近30天 2上个月 3本月 4.近12月
     */
    @GetMapping("/getDateOrderCount")
    public AjaxResult getDateOrderCount(Long operatorId,Long chargingStationId,Long chargingPileId,Integer type,String startTime,String endTime)
    {
        return AjaxResult.success(xhpcStatisticsService.getDateOrder(2,operatorId,chargingStationId,chargingPileId,type,startTime,endTime));
    }


}
