package com.zt.plat.module.qms.business.bus.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.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleParentRetestDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSubSampleParentRetestService;
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-parent-retest")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSubSampleParentRetestController implements BusinessControllerMarker {


    @Resource
    private BusinessSubSampleParentRetestService businessSubSampleParentRetestService;

    @PostMapping("/create")
    @Operation(summary = "创建分样复测业务数据")
    @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:create')")
    public CommonResult<BusinessSubSampleParentRetestRespVO> createBusinessSubSampleParentRetest(@Valid @RequestBody BusinessSubSampleParentRetestSaveReqVO createReqVO) {
        return success(businessSubSampleParentRetestService.createBusinessSubSampleParentRetest(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分样复测业务数据")
    @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:update')")
    public CommonResult<Boolean> updateBusinessSubSampleParentRetest(@Valid @RequestBody BusinessSubSampleParentRetestSaveReqVO updateReqVO) {
        businessSubSampleParentRetestService.updateBusinessSubSampleParentRetest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分样复测业务数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:delete')")
    public CommonResult<Boolean> deleteBusinessSubSampleParentRetest(@RequestParam("id") Long id) {
        businessSubSampleParentRetestService.deleteBusinessSubSampleParentRetest(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除分样复测业务数据")
                @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:delete')")
    public CommonResult<Boolean> deleteBusinessSubSampleParentRetestList(@RequestBody BatchDeleteReqVO req) {
        businessSubSampleParentRetestService.deleteBusinessSubSampleParentRetestListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分样复测业务数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:query')")
    public CommonResult<BusinessSubSampleParentRetestRespVO> getBusinessSubSampleParentRetest(@RequestParam("id") Long id) {
        BusinessSubSampleParentRetestDO businessSubSampleParentRetest = businessSubSampleParentRetestService.getBusinessSubSampleParentRetest(id);
        return success(BeanUtils.toBean(businessSubSampleParentRetest, BusinessSubSampleParentRetestRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分样复测业务数据分页")
    @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:query')")
    public CommonResult<PageResult<BusinessSubSampleParentRetestRespVO>> getBusinessSubSampleParentRetestPage(@Valid BusinessSubSampleParentRetestPageReqVO pageReqVO) {
        PageResult<BusinessSubSampleParentRetestDO> pageResult = businessSubSampleParentRetestService.getBusinessSubSampleParentRetestPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BusinessSubSampleParentRetestRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分样复测业务数据 Excel")
    @PreAuthorize("@ss.hasPermission('qms:business-sub-sample-parent-retest:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSubSampleParentRetestExcel(@Valid BusinessSubSampleParentRetestPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSubSampleParentRetestDO> list = businessSubSampleParentRetestService.getBusinessSubSampleParentRetestPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分样复测业务数据.xls", "数据", BusinessSubSampleParentRetestRespVO.class,
                        BeanUtils.toBean(list, BusinessSubSampleParentRetestRespVO.class));
    }

}