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

import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainCorrelationPageReqVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainCorrelationRespVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentMainCorrelationSaveReqVO;
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 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.ReportDocumentMainCorrelationDO;
import com.zt.plat.module.qms.business.reportdoc.service.ReportDocumentMainCorrelationService;

@Tag(name = "管理后台 - 检测报告关系表")
@RestController
@RequestMapping("/qms/report-document-main-correlation")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ReportDocumentMainCorrelationController implements BusinessControllerMarker {


    @Resource
    private ReportDocumentMainCorrelationService reportDocumentMainCorrelationService;

    @PostMapping("/create")
    @Operation(summary = "创建检测报告关系表")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:create')")
    public CommonResult<ReportDocumentMainCorrelationRespVO> createReportDocumentMainCorrelation(@Valid @RequestBody ReportDocumentMainCorrelationSaveReqVO createReqVO) {
        return success(reportDocumentMainCorrelationService.createReportDocumentMainCorrelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测报告关系表")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:update')")
    public CommonResult<Boolean> updateReportDocumentMainCorrelation(@Valid @RequestBody ReportDocumentMainCorrelationSaveReqVO updateReqVO) {
        reportDocumentMainCorrelationService.updateReportDocumentMainCorrelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检测报告关系表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:delete')")
    public CommonResult<Boolean> deleteReportDocumentMainCorrelation(@RequestParam("id") Long id) {
        reportDocumentMainCorrelationService.deleteReportDocumentMainCorrelation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除检测报告关系表")
                @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:delete')")
    public CommonResult<Boolean> deleteReportDocumentMainCorrelationList(@RequestBody BatchDeleteReqVO req) {
        reportDocumentMainCorrelationService.deleteReportDocumentMainCorrelationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检测报告关系表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:query')")
    public CommonResult<ReportDocumentMainCorrelationRespVO> getReportDocumentMainCorrelation(@RequestParam("id") Long id) {
        ReportDocumentMainCorrelationDO reportDocumentMainCorrelation = reportDocumentMainCorrelationService.getReportDocumentMainCorrelation(id);
        return success(BeanUtils.toBean(reportDocumentMainCorrelation, ReportDocumentMainCorrelationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测报告关系表分页")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:query')")
    public CommonResult<PageResult<ReportDocumentMainCorrelationRespVO>> getReportDocumentMainCorrelationPage(@Valid ReportDocumentMainCorrelationPageReqVO pageReqVO) {
        PageResult<ReportDocumentMainCorrelationDO> pageResult = reportDocumentMainCorrelationService.getReportDocumentMainCorrelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportDocumentMainCorrelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测报告关系表 Excel")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main-correlation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportDocumentMainCorrelationExcel(@Valid ReportDocumentMainCorrelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportDocumentMainCorrelationDO> list = reportDocumentMainCorrelationService.getReportDocumentMainCorrelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测报告关系表.xls", "数据", ReportDocumentMainCorrelationRespVO.class,
                        BeanUtils.toBean(list, ReportDocumentMainCorrelationRespVO.class));
    }

}