package nc.bs.arap.bill.bgtask;

import java.util.LinkedHashMap;
import java.util.Map;

import nc.bs.arap.bill.bgtask.process.NC63F4F5ToNCCF2F3MakeBill;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.uap.lock.PKLock;
import nc.itf.arap.nc63.bill.IPullNC63Bill;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;

/**
 * @Description: 
 * 		后台任务【拉取NC633内转收[F4-Cxx-2 内部单位转账（收方）]->NCC收款单[F2-Cxx-01 收款单（内转）]】
 * 		后台任务【拉取NC633内转付[F5-Cxx-2 内部单位转账（付方）]->NCC付款单[F3-Cxx-D01 付款单（内转）]】
 * @author 谢文如 【碧桂园物业NCC】
 * @date 2020-11-23
 * @version with NCC V2005
 */
public abstract class AbstractNC63F4F5ToNCCF2F3 {
	
	protected Map<String,Integer> execute(NC63F4F5ToNCCF2F3MakeBill makeBill,BgWorkingContext context) throws BusinessException {
		LinkedHashMap<String, Object> paraMap = context.getKeyMap();
		Object nccTradeType = paraMap.get("nccTradeType");
		Object isPullBill = paraMap.get("isPullBill");
		Object cuserid = paraMap.get("cuserid");
		InvocationInfoProxy.getInstance().setUserId(cuserid.toString());
		if(!PKLock.getInstance().addDynamicLock("AbstractNC63F4F5ToNCCF2F3"+nccTradeType)) {
			throw new BusinessException("后台任务执行中，请勿重复执行！");
		}
		int pullCount = 0;
		if("Y".equals(isPullBill)) {
			//拉取NC63数据
			IPullNC63Bill pullNC63Bill = NCLocator.getInstance().lookup(IPullNC63Bill.class);
			pullCount = pullNC63Bill.pullBill_RequiresNew(paraMap);
		}
		//根据NC63数据生成NCC单据
		Map<String,Integer> map = makeBill.makeBill(paraMap);
		map.put("pull", pullCount);
		return map;
	}
	
	/**
	 * 构建返回消息
	 * @return
	 */
	protected String returnResultMsg(Map<String,Integer> map,String returnObj) {
		int pull = 0;
		int edit_error = 0;
		int add_error = 0;
		int edit_success = 0;
		int add_success = 0;
		if(map!=null) {
			pull = map.get("pull");
			add_success = map.get("add_success");
			add_error = map.get("add_error");
			edit_success = map.get("edit_success");
			edit_error = map.get("edit_error");
		}
		String msg = "【 拉取：%s，生成成功：%s，生成失败：%s，修改成功：%s，修改失败：%s，执行时间：%s】%s";
		msg = String.format(msg,pull,add_success,add_error,edit_success,edit_error,new UFDateTime().toString(),returnObj);
		return msg;
	}
	
	
}