package nc.bs.ic.base;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.uap.IVOPersistence;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;
/**
 * 
 * 采购发票删除后删除匹配的发票数据
 * @author Yaoyu
 *
 */
public class InvoiceAfterDeleteEvent implements IRule<InvoiceVO>{

	
	private IVOPersistence IVOPersistence = (IVOPersistence) NCLocator.getInstance().lookup(IVOPersistence.class);
	
	@Override
	public void process(InvoiceVO[] vos) {
		try {
				InvoiceVO InvoiceVO = (InvoiceVO) vos[0];
				String vdef17 = (String) InvoiceVO.getParent().getAttributeValue("vdef17");
				if(vdef17 != null && !"".equals(vdef17) ) {
					DeleteDOC(vdef17);
					deleteORC(vdef17);
				}
		} catch (Exception e) {
			ExceptionUtils.wrappBusinessException("删除失败!");
		}
		
	}
	
    private void DeleteDOC(String vdef17) throws Exception {
    	SqlBuilder sql = new  SqlBuilder();
    	sql.append(" name6 = '"+vdef17+"'");
    	sql.append(" AND pk_defdoclist = ( SELECT pk_defdoclist FROM bd_defdoclist WHERE code = 'OCRTHNC' AND nvl( dr, 0 ) = 0 )");
    	IVOPersistence.deleteByClause(DefdocVO.class, sql.toString());
    }
	private void deleteORC(String vdef17)throws Exception {
		SqlBuilder sql = new  SqlBuilder();
		sql.append( " DELETE FROM ocr_defdoc");
		sql.append( " WHERE vdef17 = '"+vdef17+"'");
		new BaseDAO().executeUpdate(sql.toString());
	}




}
