package org.agt.module.license.controller.admin.report;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.agt.framework.apilog.core.annotation.ApiAccessLog;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.framework.common.pojo.PageParam;
import org.agt.framework.common.pojo.PageResult;
import org.agt.framework.common.util.object.BeanUtils;
import org.agt.framework.excel.core.util.ExcelUtils;
import org.agt.module.license.controller.admin.common.vo.CommonExportReqVO;
import org.agt.module.license.controller.admin.report.vo.ReportLicensePageReqVO;
import org.agt.module.license.controller.admin.report.vo.ReportLicenseRespVO;
import org.agt.module.license.controller.admin.report.vo.ReportProgressProjectReqVO;
import org.agt.module.license.controller.admin.report.vo.ReportProgressProjectRespVO;
import org.agt.module.license.service.report.ReportService;
import org.jsoup.Jsoup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

import static org.agt.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static org.agt.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 报表")
@RestController
@RequestMapping("/license/report")
@Validated
public class ReportController {

    @Resource
    private ReportService reportService;

    @GetMapping("/progress/project-page")
    @Operation(summary = "获得项目维度的评论列表")
    public CommonResult<PageResult<ReportProgressProjectRespVO>> getCommentByProject(@Valid ReportProgressProjectReqVO query) {
        PageResult<ReportProgressProjectRespVO> pageResult = reportService.getCommentByProject(query);
        return success(pageResult);
    }

    @GetMapping("/progress/staff-page")
    @Operation(summary = "获得人员维度的评论列表")
    public CommonResult<PageResult<ReportProgressProjectRespVO>> getCommentByStaff(@Valid ReportProgressProjectReqVO query) {
        PageResult<ReportProgressProjectRespVO> pageResult = reportService.getCommentByStaff(query);
        return success(pageResult);
    }

    @GetMapping("/license/expire")
    @Operation(summary = "获得license到期列表")
    public CommonResult<PageResult<ReportLicenseRespVO>> getExpiryLicense(@Valid ReportLicensePageReqVO query) {
        PageResult<ReportLicenseRespVO> pageResult = reportService.getExpiryLicense(query);
        return success(pageResult);
    }

    @GetMapping("/project/export-excel")
    @Operation(summary = "导出项目维度的评论列表 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectExcel(@Valid ReportProgressProjectReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        if ("all".equals(pageReqVO.getMode())) {
            CommonExportReqVO exportReqVO = BeanUtils.toBean(pageReqVO, CommonExportReqVO.class);
            pageReqVO = BeanUtils.toBean(exportReqVO, ReportProgressProjectReqVO.class);
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<ReportProgressProjectRespVO> pageResult = reportService.getCommentByProject(pageReqVO);

        for (ReportProgressProjectRespVO record : pageResult.getList()) {
            record.setContent(Jsoup.parse(record.getContent()).wholeText());
        }

        // 导出 Excel
        ExcelUtils.writeSelected(response, "", pageReqVO.getSheetName(), ReportProgressProjectRespVO.class,
                pageResult.getList(), pageReqVO.getIncludeFields());
    }

    @GetMapping("/staff/export-excel")
    @Operation(summary = "导出人员维度的评论列表 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStaffExcel(@Valid ReportProgressProjectReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        if ("all".equals(pageReqVO.getMode())) {
            CommonExportReqVO exportReqVO = BeanUtils.toBean(pageReqVO, CommonExportReqVO.class);
            pageReqVO = BeanUtils.toBean(exportReqVO, ReportProgressProjectReqVO.class);
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<ReportProgressProjectRespVO> pageResult = reportService.getCommentByStaff(pageReqVO);

        for (ReportProgressProjectRespVO record : pageResult.getList()) {
            record.setContent(Jsoup.parse(record.getContent()).wholeText());
        }

        // 导出 Excel
        ExcelUtils.writeSelected(response, "", pageReqVO.getSheetName(), ReportProgressProjectRespVO.class,
                pageResult.getList(), pageReqVO.getIncludeFields());
    }

    @GetMapping("/expire/export-excel")
    @Operation(summary = "导出到期的License列表 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpireExcel(@Valid ReportLicensePageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        if ("all".equals(pageReqVO.getMode())) {
            CommonExportReqVO exportReqVO = BeanUtils.toBean(pageReqVO, CommonExportReqVO.class);
            pageReqVO = BeanUtils.toBean(exportReqVO, ReportLicensePageReqVO.class);
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<ReportLicenseRespVO> pageResult = reportService.getExpiryLicense(pageReqVO);
        // 导出 Excel
        ExcelUtils.writeSelected(response, "", pageReqVO.getSheetName(), ReportLicenseRespVO.class,
                pageResult.getList(), pageReqVO.getIncludeFields());
    }

}