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

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.business.bus.controller.vo.*;
import com.zt.plat.module.qms.business.bus.controller.vo.BatchAssessmentDataReportingReqVO.BusinessSubSampleIdAndRetestCount;
import com.zt.plat.module.qms.business.bus.service.SampleAnalysisAuditService;
import com.zt.plat.module.qms.enums.QmsCommonConstant;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import static com.zt.plat.framework.common.pojo.CommonResult.success;

/**
 * 分析审核
 */
@Tag(name = "管理后台 - 样品分析")
@RestController
@RequestMapping("/qms/bus/sample/analysis-audit")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class SampleAnalysisAuditController implements BusinessControllerMarker {
	
	@Resource
	private SampleAnalysisAuditService sampleAnalysisAuditService;
	
	/**
	 * 交叉审核
	 * @return
	 */
	@PostMapping("/crossAudit")
	public CommonResult<?> crossAudit(Long businessAssayTaskId, String auditStatus) {
		sampleAnalysisAuditService.crossAuditByTaskId(businessAssayTaskId, auditStatus);
		return success("成功");
	}
	
	@GetMapping("/getSampleResultAssessmentList")
	public CommonResult<JSONObject> getSampleResultAssessmentList(BusinessAssayTaskDataReqVO reqVO) {
		JSONObject result = sampleAnalysisAuditService.getSampleResultAssessmentList(reqVO);
		return success(result);
	}
	
	@GetMapping("/getResultAssessment")
	public CommonResult<?> getResultAssessment(String assayType, Long sampleId, Long configAssayMethodId) {
		JSONObject result = null;
		if (QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL.equals(assayType)) {//平行
			result = sampleAnalysisAuditService.getParallelResultAssessment(sampleId, configAssayMethodId);
		} else if (QmsCommonConstant.ASSAY_TYPE_DOUBLE_CUP.equals(assayType)) {//双杯
			result = sampleAnalysisAuditService.getDoubleCupResultAssessment(sampleId, configAssayMethodId);
		}
		return success(result);
	}
	
	//平行分析结果判定
	@GetMapping("/getParallelResultAssessment")
	public CommonResult<?> getParallelResultAssessment(Long businessSubSampleId, Long configAssayMethodId) {
		JSONObject result = sampleAnalysisAuditService.getParallelResultAssessment(businessSubSampleId, configAssayMethodId);
		return success(result);
	}
	
	//双杯分析结果判定
	@GetMapping("/getDoubleCupResultAssessment")
	public CommonResult<?> getDoubleCupResultAssessment(Long businessSubParentSampleId, Long configAssayMethodId) {
		JSONObject result = sampleAnalysisAuditService.getDoubleCupResultAssessment(businessSubParentSampleId, configAssayMethodId);
		return success(result);
	}
	
	//修改结果判定
	@PostMapping("/modifyResultAssessment")
	public CommonResult<?> modifyResultAssessment(@RequestBody ModifyResultAssessmentReqVO reqVO) {
		JSONObject result = null;
		if (QmsCommonConstant.ASSAY_TYPE_SINGLE_PARALLEL.equals(reqVO.getAssayType())) {//平行
			result = sampleAnalysisAuditService.modifyParallelResultAssessment(reqVO.getSampleId(), reqVO.getConfigAssayMethodId(), reqVO.getSampleAssessmentId(), reqVO.getBusinessAssayProjectDataIds());
		} else if (QmsCommonConstant.ASSAY_TYPE_DOUBLE_CUP.equals(reqVO.getAssayType())) {//双杯
			
		}
		return success(result);
	}
	
	//判定数据上报
	@PostMapping("/assessmentDataReporting")
	public CommonResult<?> assessmentDataReporting(Long businessSubSampleId, Long configAssayMethodId, Integer retestCount) {
		sampleAnalysisAuditService.assessmentDataReporting(businessSubSampleId, configAssayMethodId, retestCount);
		return success("成功"); 
	}
	
	@PostMapping("/batchAssessmentDataReporting")
	public CommonResult<?> batchAssessmentDataReporting(@RequestBody BatchAssessmentDataReportingReqVO reqVO) {
		for (BusinessSubSampleIdAndRetestCount businessSubSampleIdAndRetestCount : reqVO.getBusinessSubSampleIdAndRetestCounts()) {
			sampleAnalysisAuditService.assessmentDataReporting(businessSubSampleIdAndRetestCount.getBusinessSubSampleId(), reqVO.getConfigAssayMethodId(), businessSubSampleIdAndRetestCount.getRetestCount());
		}
		return success("成功"); 
	}
	
	//重新创建复检委托
	@PostMapping("/createReAnalysis")
	public CommonResult<?> createReAnalysis(Long businessSubSampleId, Long configAssayMethodId, Integer retestCount, Integer taskCount) {
		sampleAnalysisAuditService.createReAnalysis(businessSubSampleId, configAssayMethodId, retestCount, taskCount);
		return success("成功"); 
	}
	
}
