package nc.bs.cmp.report.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.pa.PreAlertReturnType;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.jdbc.framework.JdbcSession;
import nc.jdbc.framework.PersistenceManager;
import nc.jdbc.framework.exception.DbException;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDate;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nccloud.commons.lang.StringUtils;

/**
 * 
 * @Description: 资金执行明细（分公司）根据结算日期抽数到临时表
 * 
 * @author 吴斌勇 【碧桂园-物业NCC-资金执行明细（分公司）抽数任务】
 * @date 2021-10-11
 * @version with NCC V2005
 */
public class FundExecutionDetailGenPlugin implements IBackgroundWorkPlugin {
	@Override
	public PreAlertObject executeTask(BgWorkingContext bg) throws BusinessException {
		String beginDate = "";
		String endDate = "";
		
		//获取配置的参数值
		Map paraMap = bg.getKeyMap();
		if (paraMap.containsKey("beginDate")) {
			if (paraMap.get("beginDate") != null)
				beginDate = paraMap.get("beginDate").toString();
			else
				throw new BusinessException("结算日期开始时间不能为空！");
		} else {
			throw new BusinessException("请先设置beginData参数！");
		}
		
		if (paraMap.containsKey("endDate")) {
			if (paraMap.get("endDate") != null)
				endDate = paraMap.get("endDate").toString();
			else
				throw new BusinessException("结算日期结束时间不能为空！!");
		} else {
			throw new BusinessException("请先设置endData参数！");
		}
		
		//生成数据
		genData(beginDate, endDate);
		
		// 返回后台任务消息
		PreAlertObject retObj = new PreAlertObject();
		retObj.setReturnType(PreAlertReturnType.RETURNMESSAGE);
		retObj.setMsgTitle("资金执行明细（分公司）抽数到临时表");
		retObj.setReturnObj("");
		return retObj;
	}
	
	/**
	 * 
	* @Title: query数据查询方法
	* @Description:
	* @author: 吴斌勇
	* @date 2021年10月11日 下午19:49:49
	* @param allVOList
	* @throws BusinessException void
	* @version
	 */
	private void genData(String beginDate, String endDate) throws BusinessException {
		BaseDAO dao = new BaseDAO();
		List<String> sqlList = new ArrayList<String>();
		//抽取数据到临时表
		//查询条件
		String whereSql = "where SETTLEDATE between '" + beginDate + "' and '" + new UFDate(endDate).getDateAfter(1).toString() + "'; ";
		String cols = StringUtils.join(ncccols,",") + ",to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') TS,null ID";
		sqlList.add("truncate table tmp_ncc_view_zjjh");
		//1 view_zjjh_sk 收款单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_sk " + whereSql);
		
		//2 view_zjjh_fk 付款单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_fk " + whereSql);
		
		//3 view_zjjh_jk 借款单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_jk " + whereSql);
		
		//4 view_zjjh_hk 还款单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_hk " + whereSql);
		
		//5 view_zjjh_bx 报销单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_bx " + whereSql);
		
		//6 view_zjjh_fkjs 付款结算单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_fkjs " + whereSql);
		
		//7 view_zjjh_hzjs 划账结算
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_hzjs " + whereSql);
		
		//8 view_zjjh_yhts 银行托收单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_yhts " + whereSql);
		
		//9 view_zjjh_txbl 贴现办理单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_txbl " + whereSql);
		
		//10 view_zjjh_pjqf 票据签发单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_pjqf " + whereSql);
		
		//11 view_zjjh_pjfk 票据付款单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_pjfk " + whereSql);
		
		//12 view_zjjh_yfpjtx 应付票据贴现单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_yfpjtx " + whereSql);
		
		//13 view_zjjh_lccpdj 理财产品登记
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_lccpdj " + whereSql);
		
		//14 view_zjjh_lccpsh 理财产品赎回
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_lccpsh " + whereSql);
		
		//15 view_zjjh_lccpsy 理财产品收益
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_lccpsy " + whereSql);
		
		//15 view_zjjh_skjs 收款结算单
		sqlList.add(" insert into tmp_ncc_view_zjjh select " + cols + " from view_zjjh_skjs " + whereSql);
		
		PersistenceManager persist = null;
	    try {
	      //将业务数据插入中间表
	      persist = PersistenceManager.getInstance();
	      JdbcSession jdbc = persist.getJdbcSession();
	      for(String sql : sqlList) {
	        jdbc.addBatch(sql);
	        jdbc.addBatch("commit");
	      }
	      int res = jdbc.executeBatch();
	    } catch (DbException e) {
	    	ExceptionUtils.wrappBusinessException("执行sql异常" + e.getMessage() + "！");
	    } finally {
	      if (persist != null)
	        persist.release();
	    }
	}
	
    
	/**
	 * 定义抽到数据的视图
	 */
	private Map<String,String> billTypeViewNameMap = new HashMap<String,String>(){
		private static final long serialVersionUID = 1L;
		{
			put("天问收款单（物业）", "view_zjjh_sk");//天问收款单（物业）
			put("收款单（内转）", "view_zjjh_sk");//收款单（内转）
			put("付款单", "view_zjjh_fk");//付款单
			put("采购付款单", "view_zjjh_fk");//采购付款单
			put("物业付款单（明源）", "view_zjjh_fk");//物业付款单（明源）
			put("天问付款单（物业）", "view_zjjh_fk");//天问付款单（物业）
			put("报账付款单（物业）", "view_zjjh_fk");//报账付款单（物业）
			put("账扣付款单（物业）", "view_zjjh_fk");//账扣付款单（物业）
			put("付款单（内转）", "view_zjjh_fk");//付款单（内转）
			put("借款单（物业）", "view_zjjh_jk");//借款单（物业）
			put("还款单（物业）", "view_zjjh_hk");//还款单（物业）
			put("公务出差（市内交通）报销单", "view_zjjh_bx");//公务出差（市内交通）报销单
			put("对外公务业务费报销单", "view_zjjh_bx");//对外公务业务费报销单（物业）
			put("限额费用报销单", "view_zjjh_bx");//限额费用报销单（物业）
			put("员工费用报销单", "view_zjjh_bx");//员工费用报销单（物业）
			//put("上收回单", "view_zjjh_sshd");//上收回单
			//put("下拨回单", "view_zjjh_xbhd");//下拨回单
			put("内部单位转账（付方）", "view_zjjh_fkjs");//内部单位转账（付方）
			put("付款结算", "view_zjjh_fkjs");//付款结算单
			put("付款结算单（上收）", "view_zjjh_fkjs");//付款结算单（上收）
			put("付款结算单（协同）", "view_zjjh_fkjs");//付款结算单（协同）
			put("内部单位转账（收方）", "view_zjjh_skjs");//内部单位转账（收方）
			put("收款结算", "view_zjjh_skjs");//收款结算单
			put("收款结算单（下拨）", "view_zjjh_skjs");//收款结算单（下拨）
			put("收款结算单（协同）", "view_zjjh_skjs");//收款结算单（协同）
			put("划账结算", "view_zjjh_hzjs");//划账结算
			put("银行托收单", "view_zjjh_yhts");//银行托收单
			put("贴现办理单", "view_zjjh_txbl");//贴现办理单
			put("票据签发单", "view_zjjh_pjqf");//票据签发单
			put("票据付款单", "view_zjjh_pjfk");//票据付款单
			put("应付票据贴现单", "view_zjjh_yfpjtx");//应付票据贴现单
			put("理财产品登记", "view_zjjh_lccpdj");//理财产品登记
			put("理财产品赎回", "view_zjjh_lccpsh");//理财产品赎回
			put("理财产品收益", "view_zjjh_lccpsy");//理财产品收益
		}
	};
	
	 /**定义中间列名
     * 
     */
	private String[] ncccols = new String[]{
			"PK_ORG","BILLNAME","PK_ACCOUNT","BILL_DATE","SETTLEDATE",
		    "MEMO","BILL_NO","PK_CUST_SUPPLIER","CP_CODE","CP_NAME",
		    "DFZT_CODE","DFZT_NAME","PK_DEPT","REC_TYPE","balatype_name",
		    "CHARGE_TYPE","EXPENSE_TYPE","PK_RECPROJECT_63","BANKROLL_PROJET_63","CASH_ITEM_63",
		    "PK_RECPROJECT","BANKROLL_PROJET","CASH_ITEM","RECPROJECT_ISOK","PLANITEM_ISOK",
		    "CASH_ITEM_ISOK","PK_PROJECT","REC_PRIMAL","PAY_PRIMAL","REC_LOCAL",
		    "PAY_LOCAL","PK_CURRTYPE","LOCAL_RATE","STATE"};
}
