package com.zt.plat.module.qms.business.config.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.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigXRFConversionRateDO;
import com.zt.plat.module.qms.business.config.service.ConfigXRFConversionRateService;
import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 荧光数据采集特殊检测项目转换率配置")
@RestController
@RequestMapping("/qms/config-xrf-conversion-rate")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ConfigXRFConversionRateController implements BusinessControllerMarker {


    @Resource
    private ConfigXRFConversionRateService configXRFConversionRateService;

    @PostMapping("/create")
    @Operation(summary = "创建荧光数据采集特殊检测项目转换率配置")
    @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:create')")
    public CommonResult<ConfigXRFConversionRateRespVO> createConfigXRFConversionRate(@Valid @RequestBody ConfigXRFConversionRateSaveReqVO createReqVO) {
        return success(configXRFConversionRateService.createConfigXRFConversionRate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新荧光数据采集特殊检测项目转换率配置")
    @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:update')")
    public CommonResult<Boolean> updateConfigXRFConversionRate(@Valid @RequestBody ConfigXRFConversionRateSaveReqVO updateReqVO) {
        configXRFConversionRateService.updateConfigXRFConversionRate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除荧光数据采集特殊检测项目转换率配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:delete')")
    public CommonResult<Boolean> deleteConfigXRFConversionRate(@RequestParam("id") Long id) {
        configXRFConversionRateService.deleteConfigXRFConversionRate(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除荧光数据采集特殊检测项目转换率配置")
                @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:delete')")
    public CommonResult<Boolean> deleteConfigXRFConversionRateList(@RequestBody BatchDeleteReqVO req) {
        configXRFConversionRateService.deleteConfigXRFConversionRateListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得荧光数据采集特殊检测项目转换率配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:query')")
    public CommonResult<ConfigXRFConversionRateRespVO> getConfigXRFConversionRate(@RequestParam("id") Long id) {
        ConfigXRFConversionRateDO configXRFConversionRate = configXRFConversionRateService.getConfigXRFConversionRate(id);
        return success(BeanUtils.toBean(configXRFConversionRate, ConfigXRFConversionRateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得荧光数据采集特殊检测项目转换率配置分页")
    @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:query')")
    public CommonResult<PageResult<ConfigXRFConversionRateRespVO>> getConfigXRFConversionRatePage(@Valid ConfigXRFConversionRatePageReqVO pageReqVO) {
        PageResult<ConfigXRFConversionRateDO> pageResult = configXRFConversionRateService.getConfigXRFConversionRatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigXRFConversionRateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出荧光数据采集特殊检测项目转换率配置 Excel")
    @PreAuthorize("@ss.hasPermission('qms:config-XRF-conversion-rate:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigXRFConversionRateExcel(@Valid ConfigXRFConversionRatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigXRFConversionRateDO> list = configXRFConversionRateService.getConfigXRFConversionRatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "荧光数据采集特殊检测项目转换率配置.xls", "数据", ConfigXRFConversionRateRespVO.class,
                        BeanUtils.toBean(list, ConfigXRFConversionRateRespVO.class));
    }

}