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

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAnalysisGroupPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAnalysisGroupRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleAnalysisGroupSaveReqVO;
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.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleAnalysisGroupDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSubSampleAnalysisGroupService;
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-sub-sample-analysis-group")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSubSampleAnalysisGroupController implements BusinessControllerMarker {


    @Resource
    private BusinessSubSampleAnalysisGroupService businessSubSampleAnalysisGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建子样分析班组业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:create')")
    public CommonResult<BusinessSubSampleAnalysisGroupRespVO> createBusinessSubSampleAnalysisGroup(@Valid @RequestBody BusinessSubSampleAnalysisGroupSaveReqVO createReqVO) {
        return success(businessSubSampleAnalysisGroupService.createBusinessSubSampleAnalysisGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新子样分析班组业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:update')")
    public CommonResult<Boolean> updateBusinessSubSampleAnalysisGroup(@Valid @RequestBody BusinessSubSampleAnalysisGroupSaveReqVO updateReqVO) {
        businessSubSampleAnalysisGroupService.updateBusinessSubSampleAnalysisGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除子样分析班组业务")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:delete')")
    public CommonResult<Boolean> deleteBusinessSubSampleAnalysisGroup(@RequestParam("id") Long id) {
        businessSubSampleAnalysisGroupService.deleteBusinessSubSampleAnalysisGroup(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除子样分析班组业务")
                //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:delete')")
    public CommonResult<Boolean> deleteBusinessSubSampleAnalysisGroupList(@RequestBody BatchDeleteReqVO req) {
        businessSubSampleAnalysisGroupService.deleteBusinessSubSampleAnalysisGroupListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得子样分析班组业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:query')")
    public CommonResult<BusinessSubSampleAnalysisGroupRespVO> getBusinessSubSampleAnalysisGroup(@RequestParam("id") Long id) {
        BusinessSubSampleAnalysisGroupDO businessSubSampleAnalysisGroup = businessSubSampleAnalysisGroupService.getBusinessSubSampleAnalysisGroup(id);
        return success(BeanUtils.toBean(businessSubSampleAnalysisGroup, BusinessSubSampleAnalysisGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得子样分析班组业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:query')")
    public CommonResult<PageResult<BusinessSubSampleAnalysisGroupRespVO>> getBusinessSubSampleAnalysisGroupPage(@Valid BusinessSubSampleAnalysisGroupPageReqVO pageReqVO) {
        PageResult<BusinessSubSampleAnalysisGroupDO> pageResult = businessSubSampleAnalysisGroupService.getBusinessSubSampleAnalysisGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessSubSampleAnalysisGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出子样分析班组业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-sub-sample-analysis-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSubSampleAnalysisGroupExcel(@Valid BusinessSubSampleAnalysisGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSubSampleAnalysisGroupDO> list = businessSubSampleAnalysisGroupService.getBusinessSubSampleAnalysisGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "子样分析班组业务.xls", "数据", BusinessSubSampleAnalysisGroupRespVO.class,
                        BeanUtils.toBean(list, BusinessSubSampleAnalysisGroupRespVO.class));
    }

}