package com.zt.plat.module.qms.thirdpartyapi.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yomahub.liteflow.flow.LiteflowResponse;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustDetailDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustRegistrationDO;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleEntrustDetail;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleEntrustDetailProject;
import com.zt.plat.module.qms.business.bus.liteflow.param.SampleEntrustParam;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleEntrustContext;
import com.zt.plat.module.qms.business.bus.service.SampleEntrustService;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import com.zt.plat.module.qms.thirdpartyapi.controller.vo.ThirdpartySampleEntrustReqVO;

import jakarta.annotation.Resource;

/**
 * 第三方样品委托服务
 */
@Service
public class ThirdpartySampleEntrustServiceImpl implements ThirdpartySampleEntrustService {
	
	@Resource
	private SampleEntrustService sampleEntrustService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void create(List<ThirdpartySampleEntrustReqVO> sampleEntrustReqVOList) {
		List<SampleEntrustDetail> sampleEntrustDetailList = new ArrayList<>();
		SampleEntrustParam sampleEntrustParam = new SampleEntrustParam();
		sampleEntrustParam.setCreateWay("interface");
		sampleEntrustParam.setEntrustTime(LocalDateTime.now());
		sampleEntrustParam.setEntrustType("entrust_inspection");
		sampleEntrustParam.setConfigEntrustSourceKey("jywt");
		sampleEntrustParam.setSampleQuantity(sampleEntrustReqVOList.size());
		
		for (ThirdpartySampleEntrustReqVO thirdpartySampleEntrustReqVO : sampleEntrustReqVOList) {
			List<SampleEntrustDetailProject> sampleEntrustDetailProjectList = new ArrayList<>();
			SampleEntrustDetail sampleEntrustDetail = new SampleEntrustDetail();
			sampleEntrustDetail.setBaseSampleId(thirdpartySampleEntrustReqVO.getBaseSampleId());
			sampleEntrustDetail.setEntrustSampleCode(thirdpartySampleEntrustReqVO.getEntrustSampleCode());
			sampleEntrustDetail.setEntrustSampleName(thirdpartySampleEntrustReqVO.getEntrustSampleName());
			sampleEntrustDetail.setSampleName(thirdpartySampleEntrustReqVO.getEntrustSampleName());
			
			String materialAssayStandardDetailIds = thirdpartySampleEntrustReqVO.getMaterialAssayStandardDetailIds();
			List<String> materialAssayStandardDetailIdList = Arrays.asList(materialAssayStandardDetailIds.split(","));
			for (String materialAssayStandardDetailId : materialAssayStandardDetailIdList) {
				SampleEntrustDetailProject sampleEntrustDetailProject = new SampleEntrustDetailProject();
				sampleEntrustDetailProject.setMaterialAssayStandardDetailId(Long.parseLong(materialAssayStandardDetailId));
				sampleEntrustDetailProject.setIsEnabled(QmsCommonConstant.YES);
				sampleEntrustDetailProjectList.add(sampleEntrustDetailProject);
			}
			
			sampleEntrustDetail.setSampleEntrustDetailProjectList(sampleEntrustDetailProjectList);
			
			sampleEntrustDetailList.add(sampleEntrustDetail);
		}
		
		
		sampleEntrustParam.setSampleEntrustDetailList(sampleEntrustDetailList);
		
		LiteflowResponse liteflowResponse = sampleEntrustService.create(sampleEntrustParam);
		SampleEntrustContext sampleEntrustContext = liteflowResponse.getContextBean(SampleEntrustContext.class);
		BusinessSampleEntrustRegistrationDO sampleEntrustRegistration = sampleEntrustContext.getSampleEntrustRegistration();
		List<BusinessSampleEntrustDetailDO> sampleEntrustDetailList2 = sampleEntrustContext.getSampleEntrustDetailList();
		
	}



}
