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

import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleParentPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleParentRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleParentSaveReqVO;
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.ConfigSubSampleParentDO;
import com.zt.plat.module.qms.business.config.service.ConfigSubSampleParentService;

@Tag(name = "管理后台 - 分样配置")
@RestController
@RequestMapping("/qms/config-sub-sample-parent")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ConfigSubSampleParentController implements BusinessControllerMarker {


    @Resource
    private ConfigSubSampleParentService configSubSampleParentService;

    @PostMapping("/create")
    @Operation(summary = "创建分样配置")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:create')")
    public CommonResult<ConfigSubSampleParentRespVO> createConfigSubSampleParent(@Valid @RequestBody ConfigSubSampleParentSaveReqVO createReqVO) {
        return success(configSubSampleParentService.createConfigSubSampleParent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分样配置")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:update')")
    public CommonResult<Boolean> updateConfigSubSampleParent(@Valid @RequestBody ConfigSubSampleParentSaveReqVO updateReqVO) {
        configSubSampleParentService.updateConfigSubSampleParent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分样配置")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:delete')")
    public CommonResult<Boolean> deleteConfigSubSampleParent(@RequestParam("id") Long id) {
        configSubSampleParentService.deleteConfigSubSampleParent(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除分样配置")
                //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:delete')")
    public CommonResult<Boolean> deleteConfigSubSampleParentList(@RequestBody BatchDeleteReqVO req) {
        configSubSampleParentService.deleteConfigSubSampleParentListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分样配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:query')")
    public CommonResult<ConfigSubSampleParentRespVO> getConfigSubSampleParent(@RequestParam("id") Long id) {
        ConfigSubSampleParentDO configSubSampleParent = configSubSampleParentService.getConfigSubSampleParent(id);
        return success(BeanUtils.toBean(configSubSampleParent, ConfigSubSampleParentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分样配置分页")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:query')")
    public CommonResult<PageResult<ConfigSubSampleParentRespVO>> getConfigSubSampleParentPage(@Valid ConfigSubSampleParentPageReqVO pageReqVO) {
        PageResult<ConfigSubSampleParentDO> pageResult = configSubSampleParentService.getConfigSubSampleParentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigSubSampleParentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分样配置 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:config-sub-sample-parent:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportConfigSubSampleParentExcel(@Valid ConfigSubSampleParentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ConfigSubSampleParentDO> list = configSubSampleParentService.getConfigSubSampleParentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分样配置.xls", "数据", ConfigSubSampleParentRespVO.class,
                        BeanUtils.toBean(list, ConfigSubSampleParentRespVO.class));
    }

}