/*b7jri8/jUxDwJdgq6kJknLX2/5MEkwJH608bq2vTkyGl5S59BzvfzvLkey5TPkQy*/
package nc.impl.sscpfa.pfareport.bp;

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

import nc.bs.dao.BaseDAO;
import nc.bs.logging.Logger;
import nc.bs.pf.pub.PfDataCache;
import nc.impl.sscpfa.pfareport.AbstractSSCTaskReqportQueryBP;
import nc.impl.sscpfa.pfareport.util.GetDatabaseUtil;
import nc.impl.sscpfa.pfareport.util.TimestampUtils;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.util.DBConsts;
import nc.pub.smart.context.SmartContext;
import nc.pub.smart.data.DataSet;
import nc.pubitf.bd.accessor.GeneralAccessorFactory;
import nc.pubitf.bd.accessor.IGeneralAccessor;
import nc.vo.bd.accessor.IBDData;
import nc.vo.ml.MultiLangUtil;
import nc.vo.pf.pub.util.SQLUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.pub.billtype.BilltypeVO;
import nc.vo.sscpfa.pfareport.report.ReportQueryConditionVO;
import nc.vo.sscpfa.pfareport.report.SSCTaskReportForRejectDetailVO;
import nc.vo.ssctp.sscbd.constant.SSCWorkFlowConst;
import nccloud.commons.lang.StringUtils;

/**  

* @ClassName:RejectDetailQueryBP   

* @Description:驳回统计明细表   

* @author gaoymf  

* @date:2019年5月30日 下午1:49:08 

*/  
public class RejectDetailQueryBP extends AbstractSSCTaskReqportQueryBP{
	@Override
	public DataSet queryData(ReportQueryConditionVO reportQueryConditionVO, SmartContext context) {

		DataSet ds = new DataSet();

		try {
			// 1.查询数据
			List<SSCTaskReportForRejectDetailVO> result = this.queryQualifiedBills(reportQueryConditionVO);
			
			Object[][] datas = SSCTaskReportForRejectDetailVO.toDataSet(result);
			ds.setMetaData(SSCTaskReportForRejectDetailVO.getMetaData());
			ds.setDatas(datas);
		} catch (BusinessException e) {
			Logger.error(e.getMessage());
			throw new BusinessRuntimeException(e.getMessage(),e);
		}
		return ds;
	}

	@SuppressWarnings("unchecked")
	public List<SSCTaskReportForRejectDetailVO> queryQualifiedBills(ReportQueryConditionVO reportQueryConditionVO) throws BusinessException {
		String sql = this.getExcuteSql(reportQueryConditionVO);
		List<SSCTaskReportForRejectDetailVO> rejectResult = (List<SSCTaskReportForRejectDetailVO>) new BaseDAO()
				.executeQuery(sql.toString(), new BeanListProcessor(SSCTaskReportForRejectDetailVO.class));
		
		Map<String,BilltypeVO> pk2billtypevo = new HashMap<String,BilltypeVO>();
		HashMap<String, BilltypeVO> billtypes = PfDataCache.getBilltypes();
		for (BilltypeVO billtypevo : billtypes.values()) {
			pk2billtypevo.put(billtypevo.getPk_billtypeid(), billtypevo);
		}
		
		IGeneralAccessor orgAccessor = GeneralAccessorFactory.getAccessor("985be8a4-3a36-4778-8afe-2d8ed3902659");
		if(rejectResult != null){
			for (SSCTaskReportForRejectDetailVO sscTaskReportForRejectDetailVO : rejectResult) {
				BilltypeVO billtypevo = pk2billtypevo.get(sscTaskReportForRejectDetailVO.getPk_tradetype());
				IBDData orgdoc = orgAccessor.getDocByPk(sscTaskReportForRejectDetailVO.getPk_org());
				if(orgdoc != null){
					sscTaskReportForRejectDetailVO.setOrg_code(orgdoc.getCode());
					sscTaskReportForRejectDetailVO.setOrg_name(orgdoc.getName().toString());
				}
				if(billtypevo != null){
					sscTaskReportForRejectDetailVO.setTradetype_name(billtypevo.getBilltypenameOfCurrLang());
					sscTaskReportForRejectDetailVO.setTradetype_code(billtypevo.getPk_billtypecode());
					sscTaskReportForRejectDetailVO.setPk_billtypecode(billtypevo.getIstransaction().booleanValue() ? billtypevo.getParentbilltype() : billtypevo.getPk_billtypecode());
				}
				
			}
		}
		
		return rejectResult;
	}
	
	/**
	 * 构建查询sql
	 * @param reportQueryConditionVO
	 * @return
	 * @throws BusinessException
	 */
	private String getExcuteSql(ReportQueryConditionVO reportQueryConditionVO){
		
		//add by zhaoyangm 查询提交的子查询需要增加基础条件，减少查询结果
		StringBuilder commitpathWhere = new StringBuilder();
		String pk_sscunit = reportQueryConditionVO.getPk_sscunit();
		String pk_org = reportQueryConditionVO.getPk_org();
		String pk_transtype = reportQueryConditionVO.getPk_transtype();
		commitpathWhere.append(" and  pk_sscunit='"+ pk_sscunit + "'");
		if(StringUtils.isNotEmpty(pk_org)){
			commitpathWhere.append("and "+SQLUtil.buildSqlForIn("pk_org", pk_org.split(",")));
		}
		if(StringUtils.isNotEmpty(pk_transtype)){
			commitpathWhere.append("and "+SQLUtil.buildSqlForIn("pk_tradetype", pk_transtype.split(",")));
		}
		
		// 构建where条件
		String whereCondition = this.getWhereConditionSql(reportQueryConditionVO);
		// 多语标识
		String suffix = MultiLangUtil.getCurrentLangSeqSuffix();
		StringBuilder sb = new StringBuilder();
		sb.append("	select /*+ use_hash(flow,commitpath)*/ flow.pk_org,")
		.append("	       '' org_name,")
		.append("	       flow.busiid,")
		.append("	       flow.pk_tradetype,")
		.append("	       '' tradetype_name,")
		.append("	       flow.billdate bill_createtime,")
		.append("	       flow.billno,")
		.append("	       flow.amount bill_jsamount,")
		.append("	       isnull(usr.user_name"+suffix+",usr.user_name)  bill_bxr,")
		.append("	       max(commitpath.operatetime) bill_committime,")
		.append("	       isnull(node.name"+suffix+",node.name) reject_type,")
		.append("	       flow.operater,")
		.append("	       isnull(ps.user_name"+suffix+",ps.user_name)  reject_user,")
		.append("	       flow.operatetime reject_time,")
		.append("	       flow.reason reject_reason,")
		.append("	       flow.pk_group,")
		.append("	       '' tradetype_code,")
		.append("	       '' pk_billtypecode")
		.append("	  from ssctp_flowpath flow")
		//.append("	 left join (select billdate,billno,amount,busiid,billmaker,pk_tradetype, billtype.pk_billtypecode billtypecode,tradetype.pk_billtypecode transtypecode from ssctp_flowpath flow left join bd_billtype billtype on flow.pk_billtype = billtype.pk_billtypeid left join bd_billtype tradetype  on flow.pk_tradetype = tradetype.pk_billtypeid where " + whereCondition + "  ) bill on flow.busiid = bill.busiid")
		//.append("	 left join bd_billtype b on bill.pk_tradetype = b.pk_billtypeid")
		//.append("	 left join org_orgs o on flow.pk_org  = o.pk_org ")
		.append("	 left join sm_user ps on  ps.cuserid  = flow.operater  ")
		.append("	 left join sm_user usr on usr.cuserid = flow.billmaker  ")
		.append("	 left join  ssctp_node node on  flow.pk_sscnode = node.pk_sscnode  ")
		.append("	 left join (select "+(GetDatabaseUtil.getDatabaseType()==DBConsts.SQLSERVER?" top 100 percent ":"")+" operatetime, busiid from ssctp_flowpath  where actiontype = 'makebill' " + commitpathWhere + " ) commitpath on commitpath.busiid =  flow.busiid and  flow.operatetime > commitpath.operatetime ")
		.append("	 where 1=1").append(" and ").append(whereCondition)
		.append("   group by flow.pk_org,flow.busiid,flow.pk_tradetype,flow.billdate,flow.billno,flow.amount,isnull(usr.user_name"+suffix+",usr.user_name),isnull(node.name"+suffix+",node.name),flow.operater,isnull(ps.user_name"+suffix+",ps.user_name),flow.operatetime,flow.reason, flow.pk_group")
        .append("   order by flow.pk_org,flow.busiid,flow.pk_tradetype,flow.billdate,flow.billno,flow.amount,isnull(usr.user_name"+suffix+",usr.user_name),isnull(node.name"+suffix+",node.name),flow.operater,isnull(ps.user_name"+suffix+",ps.user_name),flow.operatetime,flow.reason, flow.pk_group");

		return sb.toString();
	}
	
	/**
	 * 构建where条件:初审/复审取值范围为进入共享的单据
	 * 			       影响驳回发生在共享中心外,统计的取进入过共享中心流程历史的单据
	 * @param reportQueryConditionVO
	 * @return
	 * @throws BusinessException
	 */
	private String getWhereConditionSql(ReportQueryConditionVO reportQueryConditionVO){
		String pk_sscunit = reportQueryConditionVO.getPk_sscunit();
		String pk_org = reportQueryConditionVO.getPk_org();
		String pk_billtype = reportQueryConditionVO.getPk_billtype();
	    String pk_sscnode = reportQueryConditionVO.getPk_sscnode();
		String pk_transtype = reportQueryConditionVO.getPk_transtype();
		String startdate = reportQueryConditionVO.getStartdate();
		String enddate = reportQueryConditionVO.getEnddate();
		StringBuilder flowpathWhere = new StringBuilder();
		if(pk_sscnode.equals(SSCWorkFlowConst.ImagePK)){// 影像驳回先查影响驳回再查进入过指定共享中心
			flowpathWhere.append(" flow.actiontype ='imagereject' and flow.taskstatus = 'reject'");
			flowpathWhere.append(" and flow.busiid in (select busiid from ssctp_flowpath  where  flow.busiid = ssctp_flowpath.busiid and pk_sscunit='"+ pk_sscunit + "' ) ");

		} else { // 初审/复审驳回
			flowpathWhere.append(" flow.actiontype ='sscreject'   and flow.taskstatus = 'reject' ");
			flowpathWhere.append(" and  pk_sscunit='"+ pk_sscunit + "'");
		}
		if(StringUtils.isNotEmpty(pk_org)){
			flowpathWhere.append("and "+SQLUtil.buildSqlForIn("flow.pk_org", pk_org.split(",")));
		}
		//delete by zhaoyangm 单据类型都会展开交易类型条件，所以没有必要拼
//		if(StringUtils.isNotEmpty(pk_billtype)){
//			flowpathWhere.append(" and "+SQLUtil.buildSqlForIn("pk_billtype", pk_billtype.split(",")));
//		}
		if(StringUtils.isNotEmpty(pk_sscnode)){
			flowpathWhere.append("and flow.pk_sscnode='"+ pk_sscnode + "' ");
		}
		if(StringUtils.isNotEmpty(pk_transtype)){
			flowpathWhere.append("and "+SQLUtil.buildSqlForIn("flow.pk_tradetype", pk_transtype.split(",")));
		}
		if(StringUtils.isNotEmpty(startdate) && StringUtils.isNotEmpty(enddate)){
			flowpathWhere.append("and flow.operatetime ");
			flowpathWhere.append("  between  '" + TimestampUtils.getFormatDate(startdate) + "'  ");
			flowpathWhere.append("  and      '" + TimestampUtils.getFormatDate(enddate)  + "' ");
		}
		// add by wenfx start
		String billno = reportQueryConditionVO.getBillno();
		if (StringUtils.isNotEmpty(billno)) {
			flowpathWhere.append("and flow.billno like '%" + billno + "%' ");
		}

		String bill_bxr = reportQueryConditionVO.getBill_bxr();
		if (StringUtils.isNotEmpty(bill_bxr)) {
			flowpathWhere.append("and " + SQLUtil.buildSqlForIn("flow.billmaker", bill_bxr.split(",")));
		}
		// add by wenfx end
		// add by dujunlong start
		String bill_createtime_startdate = reportQueryConditionVO.getBill_createtime_startdate();
		String bill_createtime_enddate = reportQueryConditionVO.getBill_createtime_enddate();
		if(StringUtils.isNotEmpty(bill_createtime_startdate) && StringUtils.isNotEmpty(bill_createtime_enddate)){
			flowpathWhere.append("and flow.billdate ");
			flowpathWhere.append("  between  '" + TimestampUtils.getFormatDate(bill_createtime_startdate) + "'  ");
			flowpathWhere.append("  and      '" + TimestampUtils.getFormatDate(bill_createtime_enddate)  + "' ");
		}
		String bill_committime_startdate = reportQueryConditionVO.getBill_committime_startdate();
		String bill_committime_enddate = reportQueryConditionVO.getBill_committime_enddate();
		if(StringUtils.isNotEmpty(bill_committime_startdate) && StringUtils.isNotEmpty(bill_committime_enddate)){
			flowpathWhere.append("and commitpath.operatetime ");
			flowpathWhere.append("  between  '" + TimestampUtils.getFormatDate(bill_committime_startdate) + "'  ");
			flowpathWhere.append("  and      '" + TimestampUtils.getFormatDate(bill_committime_enddate)  + "' ");
		}
		// add by dujunlong end
		return flowpathWhere.toString();	
	}
}

/*b7jri8/jUxDwJdgq6kJknLX2/5MEkwJH608bq2vTkyGl5S59BzvfzvLkey5TPkQy*/