
package com.tmerclub.cloud.flow.controller.platform;


import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.flow.dto.FlowAnalysisDTO;
import com.tmerclub.cloud.flow.service.FlowService;
import com.tmerclub.cloud.flow.service.FlowUserAnalysisService;
import com.tmerclub.cloud.flow.vo.FlowAnalysisDataExcelVO;
import com.tmerclub.cloud.flow.vo.FlowAnalysisVO;
import com.tmerclub.cloud.flow.vo.FlowSourDataExcelVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 流量概括
 *
 * @author YXF
 * @date 2021-06-02
 */
@RestController
@RequestMapping("/p/flowAnalysis")
@Tag(name = "platform-流量概括")
public class FlowAnalysisController {

    @Autowired
    private FlowUserAnalysisService flowUserAnalysisService;

    @Autowired
    private FlowService flowService;



    @GetMapping("/handle")
    @Operation(summary = "处理统计数据", description = "处理统计数据")
    public ServerResponseEntity<Long> handle() {
        flowService.statisticalFlowUserLog();
        return ServerResponseEntity.success();
    }


    /**
     * 流量总览
     */
    @GetMapping("/flowAll")
    @Operation(summary = "流量总览", description = "流量总览")
    public ServerResponseEntity<FlowAnalysisVO> page(FlowAnalysisDTO flowAnalysisDTO) {
        //获取时间范围内的统计数据
        FlowAnalysisVO flowAnalysisDto = flowUserAnalysisService.getFlowAnalysisData(flowAnalysisDTO);
        return ServerResponseEntity.success(flowAnalysisDto);
    }

    /**
     * 流量趋势
     */
    @GetMapping("/flowTrend")
    @Operation(summary = "流量趋势", description = "流量趋势")
    public ServerResponseEntity<List<FlowAnalysisVO>> flowTrend(FlowAnalysisDTO flowAnalysisDTO) {
        // 获取开始和结束时间
        List<FlowAnalysisVO> flowAnalysisDtoList = flowUserAnalysisService.flowTrend(flowAnalysisDTO);
        return ServerResponseEntity.success(flowAnalysisDtoList);
    }

    /**
     * 流量来源构成
     */
    @GetMapping("/flowSour")
    @Operation(summary = "流量来源构成", description = "流量来源构成")
    public ServerResponseEntity<List<FlowAnalysisVO>> flowSour(FlowAnalysisDTO flowAnalysisDTO) {
        List<FlowAnalysisVO> flowAnalysisDtoList = flowUserAnalysisService.flowSour(flowAnalysisDTO);
        return ServerResponseEntity.success(flowAnalysisDtoList);
    }

    @GetMapping("/analysisDataExport")
    @Operation(summary = "导出excel", description = "导出流量总览")
    public ServerResponseEntity<Void> analysisDataExport(HttpServletResponse response, FlowAnalysisDTO flowAnalysisDTO) {
        List<FlowAnalysisDataExcelVO> list = flowUserAnalysisService.listFlowAnalysisDataExcel(flowAnalysisDTO);
        ExcelUtil.soleExcel(response, list, FlowAnalysisDataExcelVO.EXCEL_NAME, FlowAnalysisDataExcelVO.MERGE_ROW_INDEX, FlowAnalysisDataExcelVO.MERGE_COLUMN_INDEX, FlowAnalysisDataExcelVO.class);
        return ServerResponseEntity.success();
    }

    @GetMapping("/flowTrendExport")
    @Operation(summary = "导出excel", description = "导出流量趋势")
    public ServerResponseEntity<Void> flowTrendExport(HttpServletResponse response, FlowAnalysisDTO flowAnalysisDTO) {
        flowUserAnalysisService.exportFlowTrend(response, flowAnalysisDTO);
        return ServerResponseEntity.success();
    }

    @GetMapping("/flowSourExport")
    @Operation(summary = "导出excel", description = "导出成交转换")
    public ServerResponseEntity<Void> flowSourExport(HttpServletResponse response, FlowAnalysisDTO flowAnalysisDTO) {
        List<FlowSourDataExcelVO> list = flowUserAnalysisService.listFlowSourDataExcel(flowAnalysisDTO);
        ExcelUtil.soleExcel(response, list, FlowSourDataExcelVO.EXCEL_NAME, FlowSourDataExcelVO.MERGE_ROW_INDEX, FlowSourDataExcelVO.MERGE_COLUMN_INDEX, FlowSourDataExcelVO.class);
        return ServerResponseEntity.success();
    }
}
