package nc.impl.fa.deployout.rule;

import java.util.ArrayList;
import java.util.List;

import nc.bs.am.framework.action.ActionContext;
import nc.bs.am.framework.action.IRule;
import nc.bs.framework.common.NCLocator;
import nc.impl.am.db.BillPersistUtil;
import nc.impl.fa.deployout.DeployInBillVOGenerator;
import nc.itf.fa.prv.IDeployIn;
import nc.pubitf.para.ISysInitDAO;
import nc.vo.am.common.AbstractAggBill;
import nc.vo.am.common.util.AMMessageUtils;
import nc.vo.am.messagenote.MessageNoteVO;
import nc.vo.am.proxy.AMProxy;
import nc.vo.fa.deployin.DeployInHeadVO;
import nc.vo.fa.deployin.DeployInVO;
import nc.vo.fa.deployout.DeployOutHeadVO;
import nc.vo.fa.deployout.DeployOutVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.am.common.util.StringUtils;

/**
 * <p>
 * <b>生成调入单</b>
 * </p>
 * 
 * @author kongfy
 * @version 6.0 2011-11-01
 */
public class InsertDeployInVOAfterRule <E extends AbstractAggBill>  implements IRule<E>{
	
	private String msgResId = "DeployoutToDeployin";
	private String billTypeCode = "HD-01";
	private final static String APPCODE = "201203508A";
	private final static String PARAMS = "appcode=201203508A&status=browse&pagecode=201203508A_card";

	@Override
	public void process(ActionContext<E> context, E... aggVos) throws BusinessException {
		List<DeployOutVO> deployOutList = new ArrayList<DeployOutVO>();
		for (E aggVO : aggVos) {
			DeployOutVO billVO = (DeployOutVO) aggVO;
			//碧优资同步生成的调出单，不走标准的生成调入单。  -- ccy 2022年1月10日
			ISysInitDAO InitDAO = NCLocator.getInstance().lookup(ISysInitDAO.class);
			String src_byz = InitDAO.getSysInitTempVO("src_byz").getDefaultvalue();
			if(!src_byz.equals(((DeployOutHeadVO) billVO.getParentVO()).getDef10())) {
				// 2：生成对应调入公司的调入单：因为要查询未被减少的卡片账簿，所以必须在调用减少接口前执行
				DeployInVO inBillVO = DeployInBillVOGenerator.getInstance().generateDeployInVO(billVO);
				if(null == inBillVO){
					return;
				}
				inBillVO = getDeployInService().insertDeployInVO( inBillVO);
				// 创建并发送调入单消息
				buildAndSendDeployInMsg(inBillVO);
				((DeployOutHeadVO) billVO.getParentVO()).setPk_deployin(inBillVO.getParentVO().getPrimaryKey());
				
				//更新
				billVO.getParentVO().setStatus(VOStatus.UPDATED);
				billVO.getParentVO().setAttributeValue(DeployOutHeadVO.BILL_CODE_IN, ((DeployInHeadVO)inBillVO.getParentVO()).getBill_code());
				deployOutList.add(billVO); 
			}
			
			// new BillPersistUtil().update(new DeployOutVO[]{billVO});
		}
		// 最终批量更新
		if(null != deployOutList && deployOutList.size() > 0){
			new BillPersistUtil().update(deployOutList.toArray(new DeployOutVO[0]));
		}
		
	}
	
	/**
	 * 创建并发送调入单消息
	 */
	private void buildAndSendDeployInMsg(AbstractAggBill billVO) throws BusinessException {
		DeployInHeadVO headVO = (DeployInHeadVO) billVO.getParentVO();
		// 只有生成调入单才发消息
        if(headVO != null && StringUtils.isNotEmpty(headVO.getPk_deployin())){
        	// 消息发送
        	MessageNoteVO noteVo = new MessageNoteVO();
        	noteVo.setSrc_pk_bill(headVO.getPk_deployin());
        	noteVo.setSrc_billno(headVO.getBill_code());
        	noteVo.setSrc_billtype(headVO.getBill_type());
        	noteVo.setSender(headVO.getBillmaker());
        	noteVo.setSenddate(headVO.getBillmaketime());
        	noteVo.setPk_group(headVO.getPk_group());
        	noteVo.setPk_org(headVO.getPk_org());
        	AMMessageUtils.sendMessage(new MessageNoteVO[]{noteVo}, msgResId, billTypeCode, billVO);
        	// NCCloud发消息  alter by liuzy10  注：需要节点负责人判断是否给nccloud发消息
    		AMMessageUtils.sendMessage(APPCODE,PARAMS,new MessageNoteVO[]{noteVo},msgResId, billTypeCode, billVO);
        }
	}
	
	public IDeployIn getDeployInService() {
		return AMProxy.lookup(IDeployIn.class);
	}
}
