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.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(String chargingStationIds,Long internetUserId, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getTimeIntervalPage(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(String chargingStationIds,Long internetUserId, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getDateIntervalPage(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(String chargingStationIds,String operatorIds,Long internetUserId, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getStationIntervalPage(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(String operatorIds, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getOperatorIntervalPage(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(String internetUserIds,Long internetUserId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getInternetUserIntervalPage(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(String chargingStationId,String terminalId,Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getTerminalIntervalPage(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(Long operatorId, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        if (type == null || type==3) {
            return AjaxResult.success();
        }
        return xhpcStatisticsService.getChargingStationList(operatorId, userId, type);
    }

    /**
     * 运营商列表
     * @param operatorId      运营商id
     * @param userId          用户id
     * @param type            1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getOperatorList")
    public AjaxResult getOperatorList(Long operatorId, @RequestParam("userId") Long userId, @RequestParam("type")Integer type)
    {
        if (type == null || type==3) {
            return AjaxResult.success();
        }
        return xhpcStatisticsService.getOperatorList(operatorId, userId, type);
    }

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

    /**
     * 终端列表
     * @param chargingStationId  电站id
     * @param userId          用户id
     * @param type            1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getTerminalList")
    public AjaxResult getTerminalList(Long chargingStationId, @RequestParam("userId") Long userId, @RequestParam("type") Integer type) {
        if (type == null || type == 3) {
            return AjaxResult.success();
        }
        return xhpcStatisticsService.getTerminalList(chargingStationId, userId, type);
    }

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

    /**
     * 订单来源（第三方、支付宝、微信、运营商），权限划分
     * @param internetUserIds     订单来源集合
     * @param userId              用户id
     * @param type                1 平台 2 运营商 3流量方
     * @return
     */
    @GetMapping("/getSumSourceList")
    public TableDataInfo getSumSourceList(String startTime, String endTime, String internetUserIds,@RequestParam("userId") Long userId, @RequestParam("type") Integer type) {
        startPage();
        List<Map<String, Object>> list = xhpcStatisticsService.getSumSourceList(startTime,endTime,internetUserIds,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
     */
    @PostMapping("/timeInterval/export")
    public void timeIntervalExport(HttpServletResponse response, String chargingStationIds,Long internetUserId, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) throws IOException {
        xhpcStatisticsService.timeIntervalExport(response, 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(HttpServletResponse response, String chargingStationIds,Long internetUserId, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) throws IOException {
        xhpcStatisticsService.dateIntervalExport(response, 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(HttpServletResponse response, String chargingStationIds,String operatorIds,Long internetUserId, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) throws IOException {
        xhpcStatisticsService.stationIntervalExport(response, 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(HttpServletResponse response, String operatorIds, Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) throws IOException {
        xhpcStatisticsService.operatorIntervalExport(response, operatorIds,operatorId,startTime,endTime,userId,type);
    }

    /**
     * 流量方统计导出
     * @param internetUserIds   流量方集合
     * @param internetUserId      流量方id
     * @param startTime           订单开始时间
     * @param endTime             订单结束时间
     * @param userId              用户id
     * @param type             1 平台 2 运营商 3流量方
     * @return
     */
    @PostMapping("/internetUserInterval/export")
    public void internetUserIntervalExport(HttpServletResponse response, String internetUserIds,Long internetUserId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) throws IOException {
        xhpcStatisticsService.internetUserIntervalExport(response, internetUserIds,internetUserId,startTime,endTime,userId,type);
    }


    /**
     * 终端统计
     * @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(HttpServletResponse response, String chargingStationId,String terminalId,Long operatorId,String startTime, String endTime, @RequestParam("userId") Long userId, @RequestParam("type")Integer type) throws IOException {
        xhpcStatisticsService.terminalIntervalExport(response, chargingStationId,terminalId,operatorId,startTime,endTime,userId,type);
    }

}
