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

import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodSaveReqVO;
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.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSubSampleMethodDO;
import com.zt.plat.module.qms.business.config.service.ConfigSubSampleMethodService;

@Tag(name = "管理后台 - 子样与分样检测方法配置表")
@RestController
@RequestMapping("/qms/config-sub-sample-method")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ConfigSubSampleMethodController implements BusinessControllerMarker {


    @Resource
    private ConfigSubSampleMethodService configSubSampleMethodService;

    @PostMapping("/create")
    @Operation(summary = "创建子样与分样检测方法配置表")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:create')")
    public CommonResult<ConfigSubSampleMethodRespVO> createConfigSubSampleMethod(@Valid @RequestBody ConfigSubSampleMethodSaveReqVO createReqVO) {
        return success(configSubSampleMethodService.createConfigSubSampleMethod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新子样与分样检测方法配置表")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:update')")
    public CommonResult<Boolean> updateConfigSubSampleMethod(@Valid @RequestBody ConfigSubSampleMethodSaveReqVO updateReqVO) {
        configSubSampleMethodService.updateConfigSubSampleMethod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除子样与分样检测方法配置表")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:delete')")
    public CommonResult<Boolean> deleteConfigSubSampleMethod(@RequestParam("id") Long id) {
        configSubSampleMethodService.deleteConfigSubSampleMethod(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除子样与分样检测方法配置表")
                //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:delete')")
    public CommonResult<Boolean> deleteConfigSubSampleMethodList(@RequestBody BatchDeleteReqVO req) {
        configSubSampleMethodService.deleteConfigSubSampleMethodListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得子样与分样检测方法配置表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:query')")
    public CommonResult<ConfigSubSampleMethodRespVO> getConfigSubSampleMethod(@RequestParam("id") Long id) {
        ConfigSubSampleMethodDO configSubSampleMethod = configSubSampleMethodService.getConfigSubSampleMethod(id);
        return success(BeanUtils.toBean(configSubSampleMethod, ConfigSubSampleMethodRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得子样与分样检测方法配置表分页")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:query')")
    public CommonResult<PageResult<ConfigSubSampleMethodRespVO>> getConfigSubSampleMethodPage(@Valid ConfigSubSampleMethodPageReqVO pageReqVO) {
        PageResult<ConfigSubSampleMethodDO> pageResult = configSubSampleMethodService.getConfigSubSampleMethodPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigSubSampleMethodRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出子样与分样检测方法配置表 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-method:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigSubSampleMethodExcel(@Valid ConfigSubSampleMethodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigSubSampleMethodDO> list = configSubSampleMethodService.getConfigSubSampleMethodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "子样与分样检测方法配置表.xls", "数据", ConfigSubSampleMethodRespVO.class,
                        BeanUtils.toBean(list, ConfigSubSampleMethodRespVO.class));
    }

}