package nc.bs.cmp.settlement.listener;

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

import nc.bs.businessevent.BusinessEvent.BusinessUserObj;
import nc.bs.businessevent.IBusinessEvent;
import nc.bs.businessevent.IBusinessListener;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.pf.PfUtilTools;
import nc.itf.uap.IUAPQueryBS;
import nc.itf.uap.pf.IPFBusiAction;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.pubitf.arap.pay.IArapPayBillPubQueryService;
import nc.pubitf.arap.receivable.IArapReceivableBillPubQueryService;
import nc.pubitf.para.ISysInitDAO;
import nc.vo.arap.gathering.AggGatheringBillVO;
import nc.vo.arap.pay.AggPayBillVO;
import nc.vo.arap.pay.PayBillVO;
import nc.vo.arap.receivable.AggReceivableBillVO;
import nc.vo.cmp.settlement.SettlementAggVO;
import nc.vo.cmp.settlement.SettlementHeadVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;


public class ARCPaybillsettleWriter implements IBusinessListener {

	@Override
	public void doAction(IBusinessEvent businessEvent) throws BusinessException {
		BusinessUserObj obj = (BusinessUserObj) businessEvent.getUserObject();
		SettlementAggVO[] aggvos=(SettlementAggVO[]) obj.getUserObj();
		List<String> pklist=new ArrayList<String>();
		for(SettlementAggVO vo:aggvos) {
			  SettlementHeadVO head = (SettlementHeadVO) vo.getParentVO();
			  String pk_tradetype=head.getPk_tradetype();
			  String pkhead =  head.getPk_busibill();
			  if("F3-Cxx-W99".equals(pk_tradetype))
				  pklist.add(pkhead);		  
		}
		
		String[] pkarr=pklist.toArray(new String[pklist.size()]);
		
		AggPayBillVO[] paybills
			=(AggPayBillVO[]) NCLocator.getInstance().lookup(IArapPayBillPubQueryService.class).findBillByPrimaryKey(pkarr);
		ISysInitDAO InitDAO = NCLocator.getInstance().lookup(ISysInitDAO.class);
		for(AggPayBillVO paybill:paybills) {
			PayBillVO payhead=(PayBillVO) paybill.getParentVO();
			String src_byz = InitDAO.getSysInitTempVO("src_byz").getDefaultvalue();
			if(src_byz.equals(payhead.getDef56())) {
				//1??¡Á??¨²???????????¨²??¡Á??¨²??????????(?¨¨???¨´???¨¦??????????????---?¡¤????-?¡¤-????)
				String sql="select distinct def80"
						+ " from ap_payableitem"
						+ " where pk_payablebill = (select distinct top_billid"
						+ " from ap_payitem"
						+ " where dr=0 and top_tradetype = 'F1-Cxx-D104' and pk_paybill = '"+paybill.getPrimaryKey()+"')"
						+ " and dr=0";
				String recpk=(String) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sql, new ColumnProcessor());
				if(recpk!=null) {
					String sqlrec="select count(1) from ar_gatheritem where dr=0 and top_billid='"+recpk+"'";
					int count=(int) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sqlrec, new ColumnProcessor());
					if(count==0) {
							AggReceivableBillVO[] rec
								=NCLocator.getInstance().lookup(IArapReceivableBillPubQueryService.class)
									.findBillByPrimaryKey(new String[] {recpk});
							AggGatheringBillVO gabillvo= (AggGatheringBillVO) PfUtilTools.runChangeData("F0-Cxx-W63",
										"F2-Cxx-W01", rec[0]);
							AggGatheringBillVO[] gabillvos=(AggGatheringBillVO[]) NCLocator.getInstance().lookup(IPFBusiAction.class).processAction("SAVE", "F2", null,
									gabillvo, null, null);
							int i=0;
							AggGatheringBillVO[] gabillvoscommit=null;
							//?¨¢?????¡À?¨°????????¡¤?????????¡À?????saga???????????¨ª?¨º???¨¨???¨¤???¨¢???????????¡§????10???????¨¢??????
							while(i<10) {
								try {
									gabillvoscommit=(AggGatheringBillVO[]) NCLocator.getInstance().lookup(IPFBusiAction.class).processAction("COMMIT", "F2", null,
										gabillvos[0], null, null);
									break;
								}catch(Exception e) {
									if(e.getMessage()!=null&&e.getMessage().contains("???????????¨ª??")) {
										i++;
										continue;
									}
									else
										ExceptionUtils.wrappBusinessException(e.getMessage());
								}
							}
							//2???¨¨??????ARC??¡¤?
							if(gabillvo!=null) {
								try {
									new SettleForARC().sendApprove(null,payhead.getDef57(), 7,null,0);
								}catch(Exception e) {
									if(gabillvoscommit!=null) {
										AggGatheringBillVO[] gabillvosuncommit=(AggGatheringBillVO[]) NCLocator.getInstance().lookup(IPFBusiAction.class).processAction("UNCOMMIT", "F2", null,
												gabillvoscommit[0], null, null);
										NCLocator.getInstance().lookup(IPFBusiAction.class).processAction("DELETE", "F2", null,
												gabillvosuncommit[0], null, null);
									}
									ExceptionUtils.wrappBusinessException(e.getMessage());
								}
							}
					}
				}
			}
		}
	}
}
