package com.ruixuan.report.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.common.utils.Result;
import com.ruixuan.common.utils.http.HttpUtils;
import com.ruixuan.data.utils.ExportExcelUtils;
import com.ruixuan.report.service.IKuaishouLiveAccountReportService;
import com.ruixuan.report.utils.AccountReportConstants;
import com.ruixuan.report.utils.JsonResourceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 账户报表Controller
 *
 * @author ruoyi
 * @date 2023-03-22
 */
@Api(tags = "账户报表")
@Slf4j
@RestController
@RequestMapping("/report/account")
public class KuaishouLiveAccountReportController extends BaseController {
    @Autowired
    private IKuaishouLiveAccountReportService KuaishouLiveAccountReportService;

    @ApiOperation(value = "今日数据题头")
    @GetMapping("/operateHomePageHead")
    public Result operateHomePageHead(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId) {
        JSONObject result = KuaishouLiveAccountReportService.operateHomePageHead(userId);
        return Result.success(result);
    }

    @ApiOperation(value = "项目维度数据")
    @GetMapping("/getProjectCostList")
    public Result getProjectCostList(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId,
                                     @ApiParam("startDate") @RequestParam(value = "startDate", required = true) String startDate,
                                     @ApiParam("endDate") @RequestParam(value = "endDate", required = true) String endDate) {
        List<JSONObject> result = KuaishouLiveAccountReportService.getProjectCostList(userId, startDate, endDate);
        return Result.success(result);
    }

    @ApiOperation(value = "运营组维度数据")
    @GetMapping("/getOperationsGroupCostList")
    public Result getOperationsGroupCostList(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId,
                                             @ApiParam("startDate") @RequestParam(value = "startDate", required = true) String startDate,
                                             @ApiParam("endDate") @RequestParam(value = "endDate", required = true) String endDate,
                                             @ApiParam("pageNum") @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                             @ApiParam("pageSize") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return KuaishouLiveAccountReportService.getOperationsGroupCostList(userId, startDate, endDate, pageNum, pageSize);
    }

    @ApiOperation(value = "运营数据")
    @GetMapping("/getOperateCostList")
    public Result getOperateCostList(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId,
                                     @ApiParam("startDate") @RequestParam(value = "startDate", required = true) String startDate,
                                     @ApiParam("endDate") @RequestParam(value = "endDate", required = true) String endDate) {
        List<JSONObject> result = KuaishouLiveAccountReportService.getOperateCostList(userId, startDate, endDate);
        return Result.success(result);
    }

    @ApiOperation(value = "运营数据趋势图")
    @GetMapping("/getOperateCostTrendChartList")
    public Result getOperateCostTrendChartList(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId,
                                               @ApiParam("startDate") @RequestParam(value = "startDate", required = true) String startDate,
                                               @ApiParam("type 1-查自己") @RequestParam(value = "type", required = true) String type,
                                               @ApiParam("endDate") @RequestParam(value = "endDate", required = true) String endDate) {
        JSONObject result = KuaishouLiveAccountReportService.getOperateCostTrendChartList(userId, type, startDate, endDate);
        return Result.success(result);

    }

    @ApiOperation(value = "获取统计报表题头")
    @PostMapping("/getStatisticalReportHead")
    public Result getStatisticalReportHead(@RequestBody JSONObject request) {
        JSONObject result = KuaishouLiveAccountReportService.getStatisticalReportHead(request);
        return Result.success(result);
    }

    @ApiOperation(value = "访问量统计趋势图")
    @PostMapping("/getvisitorStatisticsTrendChartList")
    public Result getvisitorStatisticsTrendChartList(@RequestBody JSONObject request) {
        List<JSONObject> result = KuaishouLiveAccountReportService.getvisitorStatisticsTrendChartList(request);
        return Result.success(result);
    }

    @ApiOperation(value = "趋势图节点账户消耗")
    @PostMapping("/nodeAccountCost")
    public Result nodeAccountCost(@RequestBody JSONObject request) {
        List<JSONObject> result = KuaishouLiveAccountReportService.nodeAccountCost(request);
        return Result.success(result);
    }


    @ApiOperation(value = "账户数据报表")
    @GetMapping("/getAccountDataReport")
    public TableDataInfo getAccountDataReport(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId,
                                              @ApiParam("accountIdList") @RequestParam(value = "accountIdList", required = false) String accountIdList,
                                              @ApiParam("startDate") @RequestParam(value = "startDate", required = false) String startDate,
                                              @ApiParam("endDate") @RequestParam(value = "endDate", required = false) String endDate) {
        List<JSONObject> list = KuaishouLiveAccountReportService.getAccountDataReport(userId, accountIdList, startDate, endDate);
        return getDataTable(list);
    }

    @ApiOperation(value = "获取报表字段")
    @GetMapping("/getReportFields")
    public Result getReportFields() {
        return KuaishouLiveAccountReportService.getReportFields();
    }

    @ApiOperation(value = "记录人员报表字段")
    @PostMapping("/addKuaishouFields")
    public Result addKuaishouFields(@RequestBody JSONObject request) {
        return KuaishouLiveAccountReportService.addKuaishouFields(request);
    }

    @ApiOperation(value = "获取人员报表字段")
    @GetMapping("/getKuaishouFieldsByUserId")
    public Result addKuaishouFields(@ApiParam("userId") @RequestParam(value = "userId", required = true) Long userId,
                                    @ApiParam("columnType") @RequestParam(value = "columnType", required = true) String columnType) {
        return KuaishouLiveAccountReportService.getKuaishouFieldsByUserId(userId, columnType);
    }

    @ApiOperation(value = "导出账户数据报表")
    @PostMapping("/exportAccountDataReport")
    public void exportAccountDataReport(@RequestBody JSONObject request, HttpServletResponse response) {
        JSONArray columns = request.getJSONArray("columns");
        if (columns.isEmpty()) {
            log.error("请选择需要导出的字段");
            return;
        }
        List<String> titles = new ArrayList<>();
        titles.add("时间");
        titles.addAll(JsonResourceUtil.joinTitle(AccountReportConstants.getKuaishouAccountReport(), columns));

        //此处增加一个元素是为了保证数据有序
        columns.add(0, "time");
        List<JSONObject> list = KuaishouLiveAccountReportService.exportAccountDataReport(request);

        List<List<Object>> excelList = new ArrayList<>();
        list.forEach(data -> {
            List<Object> temp = new ArrayList<>();
            columns.forEach(k -> temp.add(data.get(k.toString())));
            excelList.add(temp);
        });

        try {
            String[] headers = titles.toArray(new String[titles.size()]);
            OutputStream os = response.getOutputStream();
            ExportExcelUtils eeu = new ExportExcelUtils();
            XSSFWorkbook workbook = new XSSFWorkbook();
            eeu.exportExcel(workbook, 0, "快手账户报表", headers, excelList);
            HttpUtils.setResponseHeader(response, "快手账户报表.xlsx");
            workbook.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}