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

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayReportDataPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayReportDataRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayReportDataSaveReqVO;
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.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayReportDataDO;
import com.zt.plat.module.qms.business.bus.service.BusinessAssayReportDataService;
import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 检测报表数据业务")
@RestController
@RequestMapping("/qms/business-assay-report-data")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessAssayReportDataController implements BusinessControllerMarker {


    @Resource
    private BusinessAssayReportDataService businessAssayReportDataService;

    @PostMapping("/create")
    @Operation(summary = "创建检测报表数据业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:create')")
    public CommonResult<BusinessAssayReportDataRespVO> createBusinessAssayReportData(@Valid @RequestBody BusinessAssayReportDataSaveReqVO createReqVO) {
        return success(businessAssayReportDataService.createBusinessAssayReportData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测报表数据业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:update')")
    public CommonResult<Boolean> updateBusinessAssayReportData(@Valid @RequestBody BusinessAssayReportDataSaveReqVO updateReqVO) {
        businessAssayReportDataService.updateBusinessAssayReportData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检测报表数据业务")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:delete')")
    public CommonResult<Boolean> deleteBusinessAssayReportData(@RequestParam("id") Long id) {
        businessAssayReportDataService.deleteBusinessAssayReportData(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除检测报表数据业务")
                //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:delete')")
    public CommonResult<Boolean> deleteBusinessAssayReportDataList(@RequestBody BatchDeleteReqVO req) {
        businessAssayReportDataService.deleteBusinessAssayReportDataListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检测报表数据业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:query')")
    public CommonResult<BusinessAssayReportDataRespVO> getBusinessAssayReportData(@RequestParam("id") Long id) {
        BusinessAssayReportDataDO businessAssayReportData = businessAssayReportDataService.getBusinessAssayReportData(id);
        return success(BeanUtils.toBean(businessAssayReportData, BusinessAssayReportDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测报表数据业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:query')")
    public CommonResult<PageResult<BusinessAssayReportDataRespVO>> getBusinessAssayReportDataPage(@Valid BusinessAssayReportDataPageReqVO pageReqVO) {
        PageResult<BusinessAssayReportDataDO> pageResult = businessAssayReportDataService.getBusinessAssayReportDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessAssayReportDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测报表数据业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-assay-report-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessAssayReportDataExcel(@Valid BusinessAssayReportDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessAssayReportDataDO> list = businessAssayReportDataService.getBusinessAssayReportDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测报表数据业务.xls", "数据", BusinessAssayReportDataRespVO.class,
                        BeanUtils.toBean(list, BusinessAssayReportDataRespVO.class));
    }

    @GetMapping("/queryWaitingToAddData")
    @Operation(summary = "待编制报表数据查询")
    @Parameter(name = "mainId", description = "报告id", required = true, example = "1024")
    public CommonResult<PageResult<BusinessAssayReportDataRespVO>> queryWaitingToAddData(BusinessAssayReportDataPageReqVO pageReqVO) {
        PageResult<BusinessAssayReportDataDO> page = businessAssayReportDataService.queryWaitingDataForReport(pageReqVO);

        return success(BeanUtils.toBean(page, BusinessAssayReportDataRespVO.class));
    }

}