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

import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationExtendRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationPageReqVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationRespVO;
import com.zt.plat.module.qms.business.bus.controller.vo.BusinessSampleEntrustRegistrationSaveReqVO;
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.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSampleEntrustRegistrationService;
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-registration")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSampleEntrustRegistrationController implements BusinessControllerMarker {


    @Resource
    private BusinessSampleEntrustRegistrationService businessSampleEntrustRegistrationService;

    @PostMapping("/create")
    @Operation(summary = "创建委检登记业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-registration:create')")
    public CommonResult<BusinessSampleEntrustRegistrationRespVO> createBusinessSampleEntrustRegistration(@Valid @RequestBody BusinessSampleEntrustRegistrationSaveReqVO createReqVO) {
        return success(businessSampleEntrustRegistrationService.createBusinessSampleEntrustRegistration(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新委检登记业务")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-registration:update')")
    public CommonResult<Boolean> updateBusinessSampleEntrustRegistration(@Valid @RequestBody BusinessSampleEntrustRegistrationSaveReqVO updateReqVO) {
        businessSampleEntrustRegistrationService.updateBusinessSampleEntrustRegistration(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得委检登记业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-registration:query')")
    public CommonResult<BusinessSampleEntrustRegistrationRespVO> getBusinessSampleEntrustRegistration(@RequestParam("id") Long id) {
        BusinessSampleEntrustRegistrationDO businessSampleEntrustRegistration = businessSampleEntrustRegistrationService.getBusinessSampleEntrustRegistration(id);
        return success(BeanUtils.toBean(businessSampleEntrustRegistration, BusinessSampleEntrustRegistrationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得委检登记业务分页")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-registration:query')")
    public CommonResult<PageResult<BusinessSampleEntrustRegistrationExtendRespVO>> getBusinessSampleEntrustRegistrationPage(@Valid BusinessSampleEntrustRegistrationPageReqVO pageReqVO) {
        PageResult<BusinessSampleEntrustRegistrationExtendRespVO> pageResult = businessSampleEntrustRegistrationService.getBusinessSampleEntrustRegistrationPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出委检登记业务 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:business-sample-entrust-registration:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSampleEntrustRegistrationExcel(@Valid BusinessSampleEntrustRegistrationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSampleEntrustRegistrationExtendRespVO> list = businessSampleEntrustRegistrationService.getBusinessSampleEntrustRegistrationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "委检登记业务.xls", "数据", BusinessSampleEntrustRegistrationRespVO.class,
                        BeanUtils.toBean(list, BusinessSampleEntrustRegistrationRespVO.class));
    }

    @GetMapping("/queryWaitingDataForReport")
    @Operation(summary = "查询待编制数据")
    public CommonResult<PageResult<BusinessSampleEntrustRegistrationExtendRespVO>> queryWaitingDataForReport(@Valid BusinessSampleEntrustRegistrationPageReqVO pageReqVO) {
        Long configReportTypeId = pageReqVO.getConfigReportTypeId();
        if(configReportTypeId == null)
            return CommonResult.error(0, "缺少configReportTypeId参数，请刷新后重试！");
        PageResult<BusinessSampleEntrustRegistrationExtendRespVO> result = businessSampleEntrustRegistrationService.queryWaitingDataForReport(pageReqVO);
        return success(result);
    }

}