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

import com.zt.plat.module.qms.business.config.controller.vo.BaseSamplePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSampleRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.BaseSampleSaveReqVO;
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.dal.dataobject.BaseSampleDO;
import com.zt.plat.module.qms.business.config.service.BaseSampleService;

@Tag(name = "管理后台 - 样品大类管理")
@RestController
@RequestMapping("/qms/base-sample")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BaseSampleController implements BusinessControllerMarker {


    @Resource
    private BaseSampleService baseSampleService;

    @PostMapping("/create")
    @Operation(summary = "创建样品大类管理")
    //@PreAuthorize("@ss.hasPermission('qms:base-sample:create')")
    public CommonResult<BaseSampleRespVO> createBaseSample(@Valid @RequestBody BaseSampleSaveReqVO createReqVO) {
        return success(baseSampleService.createBaseSample(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品大类管理")
    //@PreAuthorize("@ss.hasPermission('qms:base-sample:update')")
    public CommonResult<Boolean> updateBaseSample(@Valid @RequestBody BaseSampleSaveReqVO updateReqVO) {
        baseSampleService.updateBaseSample(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品大类管理")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:base-sample:delete')")
    public CommonResult<Boolean> deleteBaseSample(@RequestParam("id") Long id) {
        baseSampleService.deleteBaseSample(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品大类管理")
                //@PreAuthorize("@ss.hasPermission('qms:base-sample:delete')")
    public CommonResult<Boolean> deleteBaseSampleList(@RequestBody BatchDeleteReqVO req) {
        baseSampleService.deleteBaseSampleListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品大类管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:base-sample:query')")
    public CommonResult<BaseSampleRespVO> getBaseSample(@RequestParam("id") Long id) {
        BaseSampleDO baseSample = baseSampleService.getBaseSample(id);
        return success(BeanUtils.toBean(baseSample, BaseSampleRespVO.class));
    }

    @GetMapping("/getBaseSampleListWithAssayStandardCount")
    @Operation(summary = "获得样品大类选项-含检测标准数")
    public CommonResult<List<BaseSampleRespVO>> getBaseSampleListWithAssayStandardCount() {
        List<BaseSampleDO> pageResult = baseSampleService.getBaseSampleListWithAssayStandardCount();
        return success(BeanUtils.toBean(pageResult, BaseSampleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品大类管理分页")
    //@PreAuthorize("@ss.hasPermission('qms:base-sample:query')")
    public CommonResult<PageResult<BaseSampleRespVO>> getBaseSamplePage(@Valid BaseSamplePageReqVO pageReqVO) {
        PageResult<BaseSampleDO> pageResult = baseSampleService.getBaseSamplePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseSampleRespVO.class));
    }
    
    @GetMapping("/list")
    @Operation(summary = "获得样品大类管理列表")
    public CommonResult<?> getBaseSampleList(BaseSamplePageReqVO reqVO) {
    	List<BaseSampleRespVO> list = baseSampleService.getBaseSampleList(reqVO);
    	return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品大类管理 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:base-sample:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseSampleExcel(@Valid BaseSamplePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseSampleDO> list = baseSampleService.getBaseSamplePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品大类管理.xls", "数据", BaseSampleRespVO.class,
                        BeanUtils.toBean(list, BaseSampleRespVO.class));
    }

}