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.BusinessSampleEntrustFileDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSampleEntrustFileService;
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-sample-entrust-file")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSampleEntrustFileController implements BusinessControllerMarker {


    @Resource
    private BusinessSampleEntrustFileService businessSampleEntrustFileService;

    @PostMapping("/create")
    @Operation(summary = "创建委检登记附件业务")
    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:create')")
    public CommonResult<BusinessSampleEntrustFileRespVO> createBusinessSampleEntrustFile(@Valid @RequestBody BusinessSampleEntrustFileSaveReqVO createReqVO) {
        return success(businessSampleEntrustFileService.createBusinessSampleEntrustFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新委检登记附件业务")
    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:update')")
    public CommonResult<Boolean> updateBusinessSampleEntrustFile(@Valid @RequestBody BusinessSampleEntrustFileSaveReqVO updateReqVO) {
        businessSampleEntrustFileService.updateBusinessSampleEntrustFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除委检登记附件业务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:delete')")
    public CommonResult<Boolean> deleteBusinessSampleEntrustFile(@RequestParam("id") Long id) {
        businessSampleEntrustFileService.deleteBusinessSampleEntrustFile(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除委检登记附件业务")
                @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:delete')")
    public CommonResult<Boolean> deleteBusinessSampleEntrustFileList(@RequestBody BatchDeleteReqVO req) {
        businessSampleEntrustFileService.deleteBusinessSampleEntrustFileListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得委检登记附件业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:query')")
    public CommonResult<BusinessSampleEntrustFileRespVO> getBusinessSampleEntrustFile(@RequestParam("id") Long id) {
        BusinessSampleEntrustFileDO businessSampleEntrustFile = businessSampleEntrustFileService.getBusinessSampleEntrustFile(id);
        return success(BeanUtils.toBean(businessSampleEntrustFile, BusinessSampleEntrustFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得委检登记附件业务分页")
//    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:query')")
    public CommonResult<PageResult<BusinessSampleEntrustFileExtendRespVO>> getBusinessSampleEntrustFilePage(@Valid BusinessSampleEntrustFilePageReqVO pageReqVO) {
        PageResult<BusinessSampleEntrustFileExtendRespVO> pageResult = businessSampleEntrustFileService.getBusinessSampleEntrustFilePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "获得委检登记附件业务分页")
//    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:query')")
    public CommonResult<List<BusinessSampleEntrustFileExtendRespVO>> getBusinessSampleEntrustFileList(@Valid BusinessSampleEntrustFilePageReqVO pageReqVO) {
        List<BusinessSampleEntrustFileExtendRespVO> listResult = businessSampleEntrustFileService.getBusinessSampleEntrustFileList(pageReqVO);
        return success(listResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出委检登记附件业务 Excel")
    @PreAuthorize("@ss.hasPermission('t:business-sample-entrust-file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSampleEntrustFileExcel(@Valid BusinessSampleEntrustFilePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSampleEntrustFileExtendRespVO> list = businessSampleEntrustFileService.getBusinessSampleEntrustFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "委检登记附件业务.xls", "数据", BusinessSampleEntrustFileRespVO.class,
                        BeanUtils.toBean(list, BusinessSampleEntrustFileRespVO.class));
    }

}