package com.zt.plat.module.qms.business.reportdoc.controller.admin;

import com.zt.plat.module.infra.api.file.FileApi;
import com.zt.plat.module.infra.api.file.dto.FileRespDTO;
import com.zt.plat.module.qms.business.reportdoc.constant.ReportDocConstant;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentFilePageReqVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentFileRespVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentFileSaveReqVO;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentMainDO;
import com.zt.plat.module.qms.business.reportdoc.service.ReportDocumentMainService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentFileDO;
import com.zt.plat.module.qms.business.reportdoc.service.ReportDocumentFileService;

@Tag(name = "管理后台 - 检测报告附件")
@RestController
@RequestMapping("/qms/report-document-file")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.reportdocumentfile")
public class ReportDocumentFileController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = ReportDocumentFileController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource private ReportDocumentFileService reportDocumentFileService;
    @Resource private ReportDocumentMainService reportDocumentMainService;
    @Resource private FileApi fileApi;

    @PostMapping("/create")
    @Operation(summary = "创建检测报告附件")
    @PreAuthorize("@ss.hasPermission('qms:report-document-file:create')")
    public CommonResult<ReportDocumentFileRespVO> createReportDocumentFile(@Valid @RequestBody ReportDocumentFileSaveReqVO createReqVO) {
        return success(reportDocumentFileService.createReportDocumentFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测报告附件")
    @PreAuthorize("@ss.hasPermission('qms:report-document-file:update')")
    public CommonResult<Boolean> updateReportDocumentFile(@Valid @RequestBody ReportDocumentFileSaveReqVO updateReqVO) {
        reportDocumentFileService.updateReportDocumentFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检测报告附件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:report-document-file:delete')")
    public CommonResult<Boolean> deleteReportDocumentFile(@RequestParam("id") Long id) {
        reportDocumentFileService.deleteReportDocumentFile(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除检测报告附件")
                @PreAuthorize("@ss.hasPermission('qms:report-document-file:delete')")
    public CommonResult<Boolean> deleteReportDocumentFileList(@RequestBody BatchDeleteReqVO req) {
        reportDocumentFileService.deleteReportDocumentFileListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检测报告附件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:report-document-file:query')")
    public CommonResult<ReportDocumentFileRespVO> getReportDocumentFile(@RequestParam("id") Long id) {
        ReportDocumentFileDO reportDocumentFile = reportDocumentFileService.getReportDocumentFile(id);
        return success(BeanUtils.toBean(reportDocumentFile, ReportDocumentFileRespVO.class));
    }

    @GetMapping("/getPdfFileObjByMainId")
    @Operation(summary = "获得检测报告附件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ReportDocumentFileRespVO> getPdfFileObjByMainId(ReportDocumentFileSaveReqVO vo) {
        Long mainId = vo.getMainId();
        Integer version = vo.getVersion();
        if(version == null){
            ReportDocumentMainDO mainDO = reportDocumentMainService.getReportDocumentMain(mainId);
//            version = mainDO.getDocumentVersion();
        }
//        if(type != null)
//            entity
        if(ObjectUtils.isEmpty(vo.getType()))
            vo.setType(ReportDocConstant.REPORT_DOCUMENT_BUSINESS_FILE_SOURCE_IWORK);
        ReportDocumentFileDO entity = reportDocumentFileService.getByQueryVO(vo);
        if(entity == null){
            vo.setType("");
            entity = reportDocumentFileService.getByQueryVO(vo);
        }
        if(entity == null){
            return CommonResult.success(null);
        }
        Long fileId = entity.getFileId();
        CommonResult<FileRespDTO> fileRet = fileApi.getFile(fileId);
        FileRespDTO file = fileRet.getData();
        if(fileRet.isSuccess() && file != null)
            entity.setUrl(file.getUrl());
        if(entity == null){
            return CommonResult.success(null);
        }
        return success(BeanUtils.toBean(entity, ReportDocumentFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测报告附件分页")
    @PreAuthorize("@ss.hasPermission('qms:report-document-file:query')")
    public CommonResult<PageResult<ReportDocumentFileRespVO>> getReportDocumentFilePage(@Valid ReportDocumentFilePageReqVO pageReqVO) {
        PageResult<ReportDocumentFileDO> pageResult = reportDocumentFileService.getReportDocumentFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportDocumentFileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测报告附件 Excel")
    @PreAuthorize("@ss.hasPermission('qms:report-document-file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportDocumentFileExcel(@Valid ReportDocumentFilePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportDocumentFileDO> list = reportDocumentFileService.getReportDocumentFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测报告附件.xls", "数据", ReportDocumentFileRespVO.class,
                        BeanUtils.toBean(list, ReportDocumentFileRespVO.class));
    }

}