package nc.bs.arap.pay.bgtask.process;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.arap.nc63.pay.CMPPayBillDetailVO_NC63;
import nc.vo.arap.nc63.pay.CMPPaybillVO_NC63;
import nc.vo.pf.pub.util.SQLUtil;
import nc.vo.pub.BusinessException;
/**
 * @Description: 
 * 		【拉取NC633内转付[F5-Cxx-2 内部单位转账（付方）]->NCC付款单[F3-Cxx-D01 付款单（内转）]】
 * @author 谢文如 【碧桂园物业NCC】
 * @date 2020-11-23
 * @version with NCC V2005
 */
@SuppressWarnings("unchecked")
public class NC63F5ToNCCF3PullBillProcess {
	
	public static List<CMPPaybillVO_NC63> queryHead(BaseDAO nc63Dao,BaseDAO nccDao,LinkedHashMap<String, Object> paraMap) throws BusinessException {
		Object settledate = paraMap.get("settledate");
		StringBuffer sqlstr = new StringBuffer("");
		/** 1.查询中间表ts **/
		sqlstr.append("select max(nc63ts) nc63ts from nc63_cmp_paybill");
		Object nc633ts = nccDao.executeQuery(sqlstr.toString(), new ColumnProcessor());
		/** 2.NC63表头 **/
		sqlstr = new StringBuffer("");
		sqlstr.append("select h.pk_paybill,").append("\r\n");
		sqlstr.append("       org.code pk_org,").append("\r\n");
		sqlstr.append("       'F3-Cxx-D01' pk_tradetypeid,").append("\r\n");
		sqlstr.append("       h.bill_no,").append("\r\n");
		sqlstr.append("       h.bill_status,").append("\r\n");
		sqlstr.append("       h.effect_flag,").append("\r\n");
		sqlstr.append("       sh.settledate,").append("\r\n");
		sqlstr.append("       h.bill_date,").append("\r\n");
		sqlstr.append("       h.local_rate,").append("\r\n");
		sqlstr.append("       h.def29,").append("\r\n");
		sqlstr.append("       h.ts nc63ts,").append("\r\n");
		sqlstr.append("       'N' state").append("\r\n");
		sqlstr.append("  from cmp_paybill h").append("\r\n");
		sqlstr.append(" inner join cmp_settlement sh").append("\r\n");
		sqlstr.append("    on h.pk_paybill = sh.pk_busibill").append("\r\n");
		sqlstr.append(" inner join org_orgs org").append("\r\n");
		sqlstr.append("    on org.pk_org = h.pk_org").append("\r\n");
		sqlstr.append(" where h.trade_type = 'F5-Cxx-2'").append("\r\n");
		sqlstr.append("   and nvl(h.dr, 0) = 0").append("\r\n");
		sqlstr.append("   and nvl(sh.dr, 0) = 0").append("\r\n");
		sqlstr.append("   and h.effect_flag = 10").append("\r\n");
		sqlstr.append("   and sh.settlestatus = 5").append("\r\n");
		sqlstr.append("   and org.def56 = 'Y'").append("\r\n");
		sqlstr.append("   and sh.settledate > ?");
		if(nc633ts!=null) {
			sqlstr.append("   and h.ts >?").append("\r\n");
		}
		
		SQLParameter parameter = new SQLParameter();
		parameter.addParam(settledate);
		if(nc633ts!=null) {
			parameter.addParam(nc633ts);
		}
		List<CMPPaybillVO_NC63> nc63HeadVOList = (List<CMPPaybillVO_NC63>)nc63Dao.executeQuery(sqlstr.toString(), parameter, new BeanListProcessor(CMPPaybillVO_NC63.class));
		return nc63HeadVOList;
	}
	
	public static List<CMPPayBillDetailVO_NC63> queryBody(BaseDAO nc63Dao,BaseDAO nccDao,LinkedHashMap<String, Object> paraMap,List<CMPPaybillVO_NC63> nc63HeadVOList) throws BusinessException {
		List<String> nc63HidList = nc63HeadVOList.stream().map(CMPPaybillVO_NC63::getPk_paybill).collect(Collectors.toList());
		String wheresql = SQLUtil.buildSqlForIn("b.pk_paybill", nc63HidList.toArray(new String[0]));
		//表体
		StringBuffer sqlstr = new StringBuffer("");
		sqlstr.append("select b.pk_paybill,").append("\r\n");
		sqlstr.append("       b.pk_paybill_detail,").append("\r\n");
		sqlstr.append("       b.memo,").append("\r\n");
		sqlstr.append("       ioc.code            pk_recproject,").append("\r\n");
		sqlstr.append("       fp.code             bankroll_projet,").append("\r\n");
		sqlstr.append("       cf.code             cash_item,").append("\r\n");
		sqlstr.append("       b.pay_primal,").append("\r\n");
		sqlstr.append("       b.local_rate,").append("\r\n");
		sqlstr.append("       b.pay_local,").append("\r\n");
		sqlstr.append("       b.objecttype,").append("\r\n");
		sqlstr.append("       st.code             pk_supplier,").append("\r\n");
		sqlstr.append("       pjn.project_code    zyx26").append("\r\n");
		sqlstr.append("  from cmp_paybilldetail b").append("\r\n");
		sqlstr.append("  left join bd_inoutbusiclass ioc").append("\r\n");
		sqlstr.append("    on b.pk_recproject = ioc.pk_inoutbusiclass").append("\r\n");
		sqlstr.append("  left join bd_fundplan fp").append("\r\n");
		sqlstr.append("    on fp.pk_fundplan = b.bankroll_projet").append("\r\n");
		sqlstr.append("  left join bd_cashflow cf").append("\r\n");
		sqlstr.append("    on cf.pk_cashflow = b.cash_item").append("\r\n");
		sqlstr.append("  left join bd_supplier st").append("\r\n");
		sqlstr.append("    on st.pk_supplier = b.pk_supplier").append("\r\n");
		sqlstr.append("  left join bd_project pjn").append("\r\n");
		sqlstr.append("    on pjn.pk_project = b.zyx26").append("\r\n");
		sqlstr.append(" where nvl(b.dr, 0) = 0").append("\r\n");
		sqlstr.append("   and b.trade_type = 'F5-Cxx-2'"); 
		sqlstr.append("   and "+wheresql).append("\r\n");
		List<CMPPayBillDetailVO_NC63> nc63BodyList = (List<CMPPayBillDetailVO_NC63>)nc63Dao.executeQuery(sqlstr.toString(), new BeanListProcessor(CMPPayBillDetailVO_NC63.class));
		return nc63BodyList;
	}
	
}
