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

import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSubSampleService;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import org.springframework.util.ObjectUtils;
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 static com.zt.plat.module.qms.enums.ErrorCodeConstants.BUSINESS_SAMPLE_DISPATCH_DETAIL_NOT_EXISTS;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.BUSINESS_SUB_SAMPLE_NOT_EXISTS;

import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleDispatchDetailDO;
import com.zt.plat.module.qms.business.bus.service.BusinessSampleDispatchDetailService;

@Tag(name = "管理后台 - 样品调拨明细")
@RestController
@RequestMapping("/qms/business-sample-dispatch-detail")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class BusinessSampleDispatchDetailController implements BusinessControllerMarker {


    @Resource private BusinessSampleDispatchDetailService businessSampleDispatchDetailService;
    @Resource private BusinessSubSampleService businessSubSampleService;

    @GetMapping("/searchBySampleCode")
    @Operation(summary = "扫码查询待归还样品")
    public CommonResult<BusinessSampleDispatchDetailExtendRespVO> searchBySampleCode(HttpServletRequest request) {
        String sampleReturnCode = request.getParameter("sampleReturnCode");
        if(ObjectUtils.isEmpty(sampleReturnCode))
            return CommonResult.error(BUSINESS_SAMPLE_DISPATCH_DETAIL_NOT_EXISTS.getCode(), "请输入样品编号！");
        BusinessSampleDispatchDetailPageReqVO reqVO = new BusinessSampleDispatchDetailPageReqVO();
        reqVO.setSampleReturnCode(sampleReturnCode);
        reqVO.setBorrowStatus("1");
        reqVO.setGivebackStatus("0");

        PageResult<BusinessSampleDispatchDetailExtendRespVO> pageResult = businessSampleDispatchDetailService.getBusinessSampleDispatchDetailPage(reqVO);
        if(pageResult.getTotal() == 0)
            return CommonResult.error(BUSINESS_SAMPLE_DISPATCH_DETAIL_NOT_EXISTS.getCode(), "未查询到此样品！");
        if(pageResult.getList().size() > 1)
            return CommonResult.error(BUSINESS_SAMPLE_DISPATCH_DETAIL_NOT_EXISTS.getCode(), "样品编号重复，请联系管理员检查数据！");
        BusinessSampleDispatchDetailExtendRespVO businessSampleDispatchDetailExtendRespVO = pageResult.getList().get(0);
        return success(businessSampleDispatchDetailExtendRespVO);
    }

    @PostMapping("/create")
    @Operation(summary = "创建样品调拨明细")
//    @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:create')")
    public CommonResult<BusinessSampleDispatchDetailRespVO> createBusinessSampleDispatchDetail(@Valid @RequestBody BusinessSampleDispatchDetailSaveReqVO createReqVO) {
        return success(businessSampleDispatchDetailService.createBusinessSampleDispatchDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品调拨明细")
//    @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:update')")
    public CommonResult<Boolean> updateBusinessSampleDispatchDetail(@Valid @RequestBody BusinessSampleDispatchDetailSaveReqVO updateReqVO) {
        businessSampleDispatchDetailService.updateBusinessSampleDispatchDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品调拨明细")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:delete')")
    public CommonResult<Boolean> deleteBusinessSampleDispatchDetail(@RequestParam("id") Long id) {
        businessSampleDispatchDetailService.deleteBusinessSampleDispatchDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品调拨明细")
                @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:delete')")
    public CommonResult<Boolean> deleteBusinessSampleDispatchDetailList(@RequestBody BatchDeleteReqVO req) {
        businessSampleDispatchDetailService.deleteBusinessSampleDispatchDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品调拨明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:query')")
    public CommonResult<BusinessSampleDispatchDetailRespVO> getBusinessSampleDispatchDetail(@RequestParam("id") Long id) {
        BusinessSampleDispatchDetailDO businessSampleDispatchDetail = businessSampleDispatchDetailService.getBusinessSampleDispatchDetail(id);
        return success(BeanUtils.toBean(businessSampleDispatchDetail, BusinessSampleDispatchDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品调拨明细分页")
//    @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:query')")
    public CommonResult<PageResult<BusinessSampleDispatchDetailExtendRespVO>> getBusinessSampleDispatchDetailPage(@Valid BusinessSampleDispatchDetailPageReqVO pageReqVO) {
        PageResult<BusinessSampleDispatchDetailExtendRespVO> pageResult = businessSampleDispatchDetailService.getBusinessSampleDispatchDetailPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品调拨明细 Excel")
    @PreAuthorize("@ss.hasPermission('qms:business-sample-dispatch-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBusinessSampleDispatchDetailExcel(@Valid BusinessSampleDispatchDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BusinessSampleDispatchDetailExtendRespVO> list = businessSampleDispatchDetailService.getBusinessSampleDispatchDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品调拨明细.xls", "数据", BusinessSampleDispatchDetailRespVO.class,
                        BeanUtils.toBean(list, BusinessSampleDispatchDetailRespVO.class));
    }

}