package com.zt.plat.module.qms.business.bus.liteflow.sample.flow;

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

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;

import com.zt.plat.framework.security.core.LoginUser;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessHandoverRecordSubDO;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSubSampleDO;
import com.zt.plat.module.qms.business.bus.liteflow.slot.SampleFlowContext;

@LiteflowComponent(id = "sampleHandoverRecordSubCmp", name = "子样交接记录")
public class SampleHandoverRecordSubCmp extends NodeComponent {
	
	@Override
	public void process() throws Exception {
		SampleFlowContext sampleFlowContext = this.getContextBean(SampleFlowContext.class);
		LoginUser loginUser = sampleFlowContext.getLoginUser();
		String loginRealname = sampleFlowContext.getLoginRealname();
		LocalDateTime currentDateTime = sampleFlowContext.getCurrentDateTime();
		String sendSampleOper = sampleFlowContext.getSendSampleOper();
		String receiveSampleOper = sampleFlowContext.getReceiveSampleOper();
		List<BusinessSubSampleDO> busSubCsampleList = sampleFlowContext.getBusinessSubSampleList();
		
		List<BusinessHandoverRecordSubDO> busHandoverRecordSubList = new ArrayList<>();
		
		BusinessHandoverRecordSubDO busHandoverRecordSub = null;
		for (BusinessSubSampleDO businessSubSample : busSubCsampleList) {
			busHandoverRecordSub = new BusinessHandoverRecordSubDO();
			busHandoverRecordSub.setId(IdWorker.getId());
			busHandoverRecordSub.setBusinessSubSampleId(businessSubSample.getId());
			busHandoverRecordSub.setSampleCode(businessSubSample.getSampleReturnCode());//由于样品编号、分析编号、归库编号、都是由前往后一致，所以这里取归库编号
			busHandoverRecordSub.setBalanceCode(businessSubSample.getLastBalanceCode());
			busHandoverRecordSub.setSampleWeight(businessSubSample.getLastSampleWeight());
			busHandoverRecordSub.setConfigSampleFlowId(businessSubSample.getConfigSampleFlowId());
			busHandoverRecordSub.setSampleFlowNodeKey(businessSubSample.getSampleFlowNodeKey());
			//上一个交接记录id
			busHandoverRecordSub.setUpSampleRecordId(businessSubSample.getUpSampleRecordId());
			if (businessSubSample.getUpSampleRecordId() != null) {
				BusinessHandoverRecordSubDO upHandoverRecordSub = sampleFlowContext.getBusinessHandoverRecordSubById(businessSubSample.getUpSampleRecordId());
				if (upHandoverRecordSub != null) {
					upHandoverRecordSub.setNextSampleRecordId(busHandoverRecordSub.getId());
				}
			}
			
			if (StringUtils.isNotBlank(sendSampleOper)) {
				busHandoverRecordSub.setSendSampleOperator(sendSampleOper);
				busHandoverRecordSub.setReceiveSampleOperator(loginRealname);//默认收样人为当前操作人
			}
			
			if (StringUtils.isNotBlank(receiveSampleOper)) {//如果传输过来的收样人不为空，则使用传过来的收样人
				busHandoverRecordSub.setReceiveSampleOperator(receiveSampleOper);
			}
			
			busHandoverRecordSub.setOperatorId(loginUser.getId());
			busHandoverRecordSub.setOperator(loginRealname);
			busHandoverRecordSub.setOperationTime(currentDateTime);
			
			busHandoverRecordSubList.add(busHandoverRecordSub);
			
			businessSubSample.setUpSampleRecordId(busHandoverRecordSub.getId());
		}

		sampleFlowContext.setBusinessHandoverRecordSubList(busHandoverRecordSubList);
	}

}
