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

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.constraints.*;
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.BusinessQCCoefficientDataDO;
import com.zt.plat.module.qms.business.bus.service.BusinessQCCoefficientDataService;
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-qc-coefficient-data")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessQCCoefficientDataController implements BusinessControllerMarker {


    @Resource
    private BusinessQCCoefficientDataService businessQCCoefficientDataService;

    @PostMapping("/create")
    @Operation(summary = "创建质控样检测系数任务数据,空白样、标样")
    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:create')")
    public CommonResult<BusinessQCCoefficientDataRespVO> createBusinessQCCoefficientData(@Valid @RequestBody BusinessQCCoefficientDataSaveReqVO createReqVO) {
        return success(businessQCCoefficientDataService.createBusinessQCCoefficientData(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新质控样检测系数任务数据,空白样、标样")
//    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:update')")
    public CommonResult<Boolean> updateBusinessQCCoefficientData(@Valid @RequestBody BusinessQCCoefficientDataSaveReqVO updateReqVO) {
        businessQCCoefficientDataService.updateBusinessQCCoefficientData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质控样检测系数任务数据,空白样、标样")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:delete')")
    public CommonResult<Boolean> deleteBusinessQCCoefficientData(@RequestParam("id") Long id) {
        businessQCCoefficientDataService.deleteBusinessQCCoefficientData(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除质控样检测系数任务数据,空白样、标样")
                @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:delete')")
    public CommonResult<Boolean> deleteBusinessQCCoefficientDataList(@RequestBody BatchDeleteReqVO req) {
        businessQCCoefficientDataService.deleteBusinessQCCoefficientDataListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质控样检测系数任务数据,空白样、标样")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:query')")
    public CommonResult<BusinessQCCoefficientDataRespVO> getBusinessQCCoefficientData(@RequestParam("id") Long id) {
        BusinessQCCoefficientDataDO businessQCCoefficientData = businessQCCoefficientDataService.getBusinessQCCoefficientData(id);
        return success(BeanUtils.toBean(businessQCCoefficientData, BusinessQCCoefficientDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得质控样检测系数任务数据,空白样、标样分页")
//    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:query')")
    public CommonResult<PageResult<BusinessQCCoefficientDataExtendRespVO>> getBusinessQCCoefficientDataPage(@Valid BusinessQCCoefficientDataPageReqVO pageReqVO) {
        PageResult<BusinessQCCoefficientDataExtendRespVO> pageResult = businessQCCoefficientDataService.getBusinessQCCoefficientDataPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "获得质控样检测系数任务数据,空白样、标样分页")
//    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:query')")
    public CommonResult<List<BusinessQCCoefficientDataExtendRespVO>> getBusinessQCCoefficientDataPage(@Valid BusinessQCCoefficientDataReqVO reqVO) {
        List<BusinessQCCoefficientDataExtendRespVO> listResult = businessQCCoefficientDataService.getBusinessQCCoefficientDataList(reqVO);
        return success(listResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质控样检测系数任务数据,空白样、标样 Excel")
    @PreAuthorize("@ss.hasPermission('qms:business-QC-coefficient-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessQCCoefficientDataExcel(@Valid BusinessQCCoefficientDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessQCCoefficientDataExtendRespVO> list = businessQCCoefficientDataService.getBusinessQCCoefficientDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "质控样检测系数任务数据,空白样、标样.xls", "数据", BusinessQCCoefficientDataRespVO.class,
                        BeanUtils.toBean(list, BusinessQCCoefficientDataRespVO.class));
    }

}