/*cyt75ZmxLzKV1H2j7I+7W+iEApOWgqBuI4lV6nNfMRQEf0K49DGYlE0amUOBsAz+*/
package nc.pub.sscpfa.pfareport.processor;

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

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.sscrp.tools.SqlUtils;
import nc.itf.iufo.freereport.extend.IQueryCondition;
import nc.itf.sscpfa.pfareport.ISSCWorkGroupService;
import nc.jdbc.framework.processor.ArrayListProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.pub.smart.data.IRowData;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.pf.pub.util.SQLUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.BusinessRuntimeException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.sscpfa.pfareport.report.ReportQueryConditionVO;
import nccloud.commons.lang.StringUtils;

import com.ufida.dataset.IContext;
import com.ufida.iufo.table.drill.SimpleRowDataParam;
import com.ufida.report.anareport.FreeReportContextKey;
import com.ufida.report.anareport.base.BaseQueryCondition;
import com.ufida.report.anareport.base.BaseSearchTemplateCondition;
import com.ufida.report.anareport.base.BaseSearchTemplateConditionItem;
import com.ufida.report.anareport.base.DefaultConditionProcessor;
import com.ufida.report.anareport.base.FreeReportDrillParam;
import com.ufida.report.anareport.model.AbsAnaReportModel;


public class SSCTaskProcessor extends DefaultConditionProcessor{
	
    @Override
	public IQueryCondition doQueryByWebScheme(IContext context,AbsAnaReportModel reportModel, IQueryScheme queryScheme) {
		BaseSearchTemplateCondition templateCondition = (BaseSearchTemplateCondition) context.getAttribute(FreeReportContextKey.KEY_ALLCONDITION);
		List<BaseSearchTemplateConditionItem> conditionItems = templateCondition.getRealSearchConditionItems();
		
		BaseQueryCondition queryCondition = (BaseQueryCondition) super.doQueryByWebScheme(context, reportModel, queryScheme);
		ReportQueryConditionVO condVO = new ReportQueryConditionVO(); 
		
		for(int i = 0;i<conditionItems.size();i++){
			BaseSearchTemplateConditionItem item = conditionItems.get(i);
			if(item.getField().equals(ReportQueryConditionVO.PK_SSCUNIT)&&item.getOpertype().equals("=")){
				condVO.setPk_sscunit(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PK_ORG)&&item.getOpertype().equals("=")){
				condVO.setPk_org(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PK_DEPT)&&item.getOpertype().equals("=")){
				condVO.setPk_dept(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PK_BUSIUNIT)&&item.getOpertype().equals("=")){
				condVO.setPk_busiunit(item.getValue());
				//根据业务范围设置单据类型条件
				if(item.getValue() != null && item.getValue().length()>0){
					try {
						List<?> pk_billtypes = (List<?>) new BaseDAO().executeQuery("SELECT PK_BILLTYPE FROM ORG_SSCBUSIUNITCLIENTAGE WHERE " + SqlUtils.getInStr("PK_BUSIUNIT", item.getValue().trim().split(","), true), new ColumnListProcessor());
						if(pk_billtypes != null && pk_billtypes.size()>0){
							condVO.setPk_billtype(StringUtils.join(pk_billtypes.toArray(new String[0]),","));
						}
					} catch (Exception e) {
						Logger.error(e.getMessage(), e);
					}
				}
				
			}else if(item.getField().equals(ReportQueryConditionVO.PK_TRANSTYPE)&&item.getOpertype().equals("=")){
				condVO.setPk_transtype(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PERIOD)&&item.getOpertype().equals(">=")){
				condVO.setStartdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PERIOD)&&item.getOpertype().equals("<=")){
				condVO.setEnddate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.CUR_PERIOD)&&item.getOpertype().equals(">=")){
				condVO.setCur_startdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.CUR_PERIOD)&&item.getOpertype().equals("<=")){
				condVO.setCur_enddate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PRE_PERIOD)&&item.getOpertype().equals(">=")){
				condVO.setPre_startdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PRE_PERIOD)&&item.getOpertype().equals("<=")){
				condVO.setPre_enddate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.APPROVEDATE)&&item.getOpertype().equals(">=")){
				condVO.setIap_startdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.APPROVEDATE)&&item.getOpertype().equals("<=")){
				condVO.setIap_enddate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.APPROVERESULT)&&item.getOpertype().equals("=")){
				condVO.setIap_result(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.REPORTTYPE)&&item.getOpertype().equals("=")){
				condVO.setReportType(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.TIMEUNIT)&&item.getOpertype().equals("=")){
				condVO.setTimeUnit(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.INCLUDEAUTOAPPROVE)&&item.getOpertype().equals("=")){
				condVO.setIncludeAutoApprove(UFBoolean.valueOf(item.getValue()));
			}else if(item.getField().equals(ReportQueryConditionVO.INCLUDEAUTOSIGN)&&item.getOpertype().equals("=")){
				condVO.setIncludeAutoSign(UFBoolean.valueOf(item.getValue()));
			}else if(item.getField().equals(ReportQueryConditionVO.INCLUDEAUTOSETTLE)&&item.getOpertype().equals("=")){
				condVO.setIncludeAutoSettle(UFBoolean.valueOf(item.getValue()));
			}else if(item.getField().equals(ReportQueryConditionVO.PK_WORKINGGROUP)&&item.getOpertype().equals("=")){
				condVO.setPk_workinggroup(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PK_WORKINGGROUP_USER)&&item.getOpertype().equals("=")){
				condVO.setPk_workinggroup_user(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PK_SSCNODE)&&item.getOpertype().equals("=")){
				condVO.setPk_sscnode(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.IMAGESCANFINISHTIME)&&item.getOpertype().equals(">=")){
				condVO.setStartdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.IMAGESCANFINISHTIME)&&item.getOpertype().equals("<=")){
				condVO.setEnddate(item.getValue());
			}   
			// add by wenfx start
			else if(item.getField().equals(ReportQueryConditionVO.BILLNO)&&item.getOpertype().equals("like")) {
			  condVO.setBillno(item.getValue());
			} else if(item.getField().equals(ReportQueryConditionVO.BILL_BXR)&&item.getOpertype().equals("=")) {
			  condVO.setBill_bxr(item.getValue());	
			}
			// add by wenfx end
			// add by dujunlong start
			else if(item.getField().equals(ReportQueryConditionVO.BILL_CREATETIME)&&item.getOpertype().equals(">=")){
				condVO.setBill_createtime_startdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.BILL_CREATETIME)&&item.getOpertype().equals("<=")){
				condVO.setBill_createtime_enddate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.BILL_COMMITTIME)&&item.getOpertype().equals(">=")){
				condVO.setBill_committime_startdate(item.getValue());			
			}else if(item.getField().equals(ReportQueryConditionVO.BILL_COMMITTIME)&&item.getOpertype().equals("<=")){
				condVO.setBill_committime_enddate(item.getValue());
			}
			// add by dujunlong end
			
			//add by sdan 增加查询条件20220416
			else if(item.getField().equals(ReportQueryConditionVO.OPERATETIM)&&item.getOpertype().equals(">=")){//单据提交时间
				condVO.setPre_creationtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.OPERATETIM)&&item.getOpertype().equals("<=")){
				condVO.setEnd_creationtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.BILLMAKER)&&item.getOpertype().equals("=")) {//报账人
				  condVO.setBillmaker(item.getValue());	
			}else if(item.getField().equals(ReportQueryConditionVO.BILLSTATUS)&&item.getOpertype().equals("=")) {//单据状态
				  condVO.setBillstatus(item.getValue());	
			}else if(item.getField().equals(ReportQueryConditionVO.SSC_STARTTIMR)&&item.getOpertype().equals(">=")){//入池时间
				condVO.setPressc_starttime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.SSC_STARTTIMR)&&item.getOpertype().equals("<=")){
				condVO.setEndssc_starttime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GETBILL_TIME)&&item.getOpertype().equals(">=")){//单据到手时间
				condVO.setPregetbill_time(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GETBILL_TIME)&&item.getOpertype().equals("<=")){
				condVO.setEndgetbill_time(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GXSH_ENDTIME)&&item.getOpertype().equals(">=")){//共享审核时间
				condVO.setPregxsh_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GXSH_ENDTIME)&&item.getOpertype().equals("<=")){
				condVO.setEndgxsh_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GXSH_USER)&&item.getOpertype().equals("=")) {//共享审核人
				  condVO.setGxsh_user(item.getValue());	
			}else if(item.getField().equals(ReportQueryConditionVO.GXFH_ENDTIME)&&item.getOpertype().equals(">=")){//共享复核时间
				condVO.setPregxfh_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GXFH_ENDTIME)&&item.getOpertype().equals("<=")){
				condVO.setEndgxfh_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.GXFH_USER)&&item.getOpertype().equals("=")) {//共享复核人
				  condVO.setGxfh_user(item.getValue());	
			}else if(item.getField().equals(ReportQueryConditionVO.RECBILL_USER)&&item.getOpertype().equals("=")) {//收单处理人
				  condVO.setRecbill_user(item.getValue());	
			}else if(item.getField().equals(ReportQueryConditionVO.RECBILL_TIME)&&item.getOpertype().equals(">=")){//收单时间
				condVO.setPrerecbill_time(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.RECBILL_TIME)&&item.getOpertype().equals("<=")){
				condVO.setEndrecbill_time(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.BUSI_AUDITDATE)&&item.getOpertype().equals(">=")){//签字开始时间
				condVO.setPrebusi_auditdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.BUSI_AUDITDATE)&&item.getOpertype().equals("<=")){
				condVO.setEndbusi_auditdate(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.QZ_ENDTIME)&&item.getOpertype().equals(">=")){//签字时间
				condVO.setPreqz_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.QZ_ENDTIME)&&item.getOpertype().equals("<=")){
				condVO.setEndqz_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.QZ_USER)&&item.getOpertype().equals("=")) {//签字人
				  condVO.setQz_user(item.getValue());	
			}else if(item.getField().equals(ReportQueryConditionVO.PAYCOMFIRM_TIME)&&item.getOpertype().equals(">=")){//支付确认单提交时间
				condVO.setPrepaycomfirm_time(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.PAYCOMFIRM_TIME)&&item.getOpertype().equals("<=")){
				condVO.setEndpaycomfirm_time(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.JS_ENDTIME)&&item.getOpertype().equals(">=")){//结算时间
				condVO.setPrejs_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.JS_ENDTIME)&&item.getOpertype().equals("<=")){
				condVO.setEndjs_endtime(item.getValue());
			}else if(item.getField().equals(ReportQueryConditionVO.JS_USER)&&item.getOpertype().equals("=")) {//结算人
				  condVO.setJs_user(item.getValue());	
			}
			// add by sdan end
		}
		String pk_transtype = condVO.getPk_transtype();//多选，逗号隔开
		String pk_billtype = condVO.getPk_billtype();//单据类型
		if(StringUtils.isNotEmpty(pk_transtype)){
			
		} else {
			if(StringUtils.isNotEmpty(pk_billtype)){
				try {
					String[] pk_transtypes = this.getTradeTypeByBillType(pk_billtype);
					condVO.setPk_transtype(StringUtils.join(pk_transtypes,","));
				} catch (BusinessException e) {
					Logger.error(e.getMessage(), e);
				}
				
			}
		}
		queryCondition.setUserObject(condVO);
		return queryCondition;
	}
	

    @Override
    public IQueryCondition doQueryByWebDrill(IContext context, AbsAnaReportModel reportModel, FreeReportDrillParam drillParam) {

    	IQueryCondition srcCondition = drillParam.getSrcCondition();
        BaseQueryCondition result = (BaseQueryCondition) srcCondition;
        ReportQueryConditionVO rqcvo = (ReportQueryConditionVO)result.getUserObject();
        String reportType = rqcvo.getReportType();
        if(reportType!=null){
        	if(reportType.equals(ReportQueryConditionVO.REPORTTYPE_INTELLIGENTAPPROVE)){
        		rqcvo.setReportType(ReportQueryConditionVO.REPORTTYPE_INTELLIGENTAPPROVEDETAIL);
           	 	SimpleRowDataParam[] rowDatas = drillParam.getTraceDatas();
                IRowData rowData = rowDatas[0].getRowData();
                String tradetype = (String)rowData.getData("pk_tradetype");
                rqcvo.setPk_transtype(tradetype);
                String fieldName = rowDatas[0].getFieldname();
                if(fieldName.equals("taskTotal")){
               	 rqcvo.setIap_result("all");
                } else if(fieldName.equals("successNum")){
               	 rqcvo.setIap_result("Y");
                }else if(fieldName.equals("failNum")){
               	 rqcvo.setIap_result("N");
                }
        	}else if(reportType.equals(ReportQueryConditionVO.REPORTTYPE_REJECT)||reportType.equals(ReportQueryConditionVO.REPORTTYPE_REJECT_DETAIL)){
        		rqcvo.setReportType(ReportQueryConditionVO.REPORTTYPE_REJECT_DETAIL);
        		SimpleRowDataParam[] rowDatas = drillParam.getTraceDatas();
                IRowData rowData = rowDatas[0].getRowData();
                rqcvo.setPk_org(rowData.getData("pk_org").toString());
                String tradetype = (String)rowData.getData("pk_tradetype");
                rqcvo.setPk_transtype(tradetype);
                String fieldName = rowDatas[0].getFieldname();
                if(fieldName.equals("reject_quantity_cur_period")){
                	rqcvo.setStartdate(rqcvo.getCur_startdate());;
        			rqcvo.setEnddate(rqcvo.getCur_enddate());
        		}else if(fieldName.equals("reject_quantity_pre_period")){
        			rqcvo.setStartdate(rqcvo.getPre_startdate());
        			rqcvo.setEnddate(rqcvo.getPre_enddate());
        		}
        	}else if(reportType.equals(ReportQueryConditionVO.REPORTTYPE_WORKINGGROUP)){
        		
        		SimpleRowDataParam[] rowDatas = drillParam.getTraceDatas();
                IRowData rowData = rowDatas[0].getRowData();
                String group = (String)rowData.getData("pk_workinggroup");
        		
        	
        	    String[] leafgroup = new String[100];
        	    try {
                    leafgroup = NCLocator.getInstance().lookup(ISSCWorkGroupService.class)
                            .listLeafPkWorkGroups(new String[]{group});
                } catch (BusinessException e) {
                    Logger.error(e);
                    throw new BusinessRuntimeException(e.getMessage());                    
                }
        	    
        	    String pk_workinggroup = String.join(",", leafgroup);
        	    rqcvo.setPk_workinggroup(pk_workinggroup);
        	    rqcvo.setTimeUnit("hour");
        	    String fieldName = rowDatas[0].getFieldname();
        	    if("pre_quantity".equals(fieldName)){
        	        rqcvo.setCur_enddate(null);
        	        rqcvo.setCur_startdate(null);
        	    }else if("cur_quantity".equals(fieldName)) {
        	        rqcvo.setPre_enddate(null);
        	        rqcvo.setPre_startdate(null);
        	    }
                
        	}else if(reportType.equals(ReportQueryConditionVO.REPORTTYPE_USEROPRATION)){
        	    rqcvo.setPk_workinggroup(null);
        	    rqcvo.setPre_enddate(null);
        	    rqcvo.setPre_startdate(null);
                rqcvo.setReportType(ReportQueryConditionVO.REPORTTYPE_TASKDETAIL);
            }else {
            	if(rqcvo.getReportType_src()==null){
            		rqcvo.setReportType_src(reportType);
            	}
        		rqcvo.setReportType(ReportQueryConditionVO.REPORTTYPE_TASKDETAIL);
        		SimpleRowDataParam[] rowDatas = drillParam.getTraceDatas();
        		String fieldname = rowDatas[0].getFieldname();
        		if(fieldname.equals("cur_quantity") || !fieldname.equals("pre_quantity")){
        			rqcvo.setStartdate(rqcvo.getCur_startdate());
        			rqcvo.setEnddate(rqcvo.getCur_enddate());
        			rqcvo.setIsCurrentPeriod("Y");
        		}else if(fieldname.equals("pre_quantity")){
        			rqcvo.setStartdate(rqcvo.getPre_startdate());
        			rqcvo.setEnddate(rqcvo.getPre_enddate());
        			rqcvo.setIsCurrentPeriod("N");
        		}
                IRowData rowData = rowDatas[0].getRowData();
                rqcvo.setPk_org(rowData.getData("pk_org").toString());
                rqcvo.setPk_transtype(rowData.getData("pk_tradetype")!=null?rowData.getData("pk_tradetype").toString():null);
        	}
        }
        drillParam.setSrcCondition(result);

        return result;
    }
    
    /**
	 * 根据单据类型主键查询所有交易类型
	 * @param pk_billtypes  单据类型
	 * @return 交易类型主键数组
	 * @throws BusinessException
	 */
	public String[] getTradeTypeByBillType(String pk_billtypes) throws BusinessException{
		if(StringUtils.isEmpty(pk_billtypes)){
			return null;
		}
		StringBuilder sb = new StringBuilder("select pk_billtypeid from bd_billtype where parentbilltype in  (");
		sb.append("select pk_billtypecode from bd_billtype where " + SQLUtil.buildSqlForIn("pk_billtypeid", pk_billtypes.split(",")) + ")");
		@SuppressWarnings("unchecked")
		List<Object[]> pks = (List<Object[]>) new BaseDAO().
				 executeQuery(sb.toString().toString(), new ArrayListProcessor());
		List<String> pk_tradetype = new ArrayList<>();
		for(Object[] pk : pks){
			pk_tradetype.add((String)pk[0]);
		}
		//处理没有交易类型的情况
		String[] billtypes = pk_billtypes.split(",");
		for(String pk : billtypes){
			pk_tradetype.add(pk);
		}
		return pk_tradetype.toArray(new String[pk_tradetype.size()]);
	}
}

/*cyt75ZmxLzKV1H2j7I+7W+iEApOWgqBuI4lV6nNfMRQEf0K49DGYlE0amUOBsAz+*/