package nccloud.web.hrwa.ref.basic;

import java.util.Map;

import nc.bs.logging.Logger;
import nc.hr.utils.SQLHelper;
import nc.vo.pub.BusinessException;
import nc.vo.util.VisibleUtil;
import nc.vo.wa.grade.WaGradeVO;
import nccloud.framework.web.processor.refgrid.RefQueryInfo;
import nccloud.framework.web.ui.meta.RefMeta;
import nccloud.framework.web.ui.meta.TreeRefMeta;
import nccloud.pubitf.platform.db.SqlParameterCollection;
import nccloud.web.refer.DefaultGridRefAction;

/**
 * 薪资类别标准列表参照
 * 
 * @author YuCheng
 *
 */
public class WaGradeVerExGridRef extends DefaultGridRefAction {

	@Override
	public RefMeta getRefMeta(RefQueryInfo refQueryInfo) {
		TreeRefMeta meta = new TreeRefMeta();
		meta.setTableName(" (select wa_grade_ver.pk_wa_gradever, \r\n" + 
				"               wa_grade.code gradever_num, \r\n" + 
				"               wa_grade.name gradever_name, \r\n" + 
				"               wa_grade.pk_org, \r\n" + 
				"               wa_grade.pk_group,  \r\n" + 
				"               wa_grade.standard_type,  \r\n" + 
				"               wa_grade_ver.gradever_name name, \r\n" + 
				"               wa_grade_ver.gradever_num code, \r\n" + 
				"               wa_grade_ver.effect_flag \r\n" + 
				"               from wa_grade_ver   \r\n" + 
				"               left join wa_grade \r\n" + 
				"                    on wa_grade.pk_wa_grd = wa_grade_ver.pk_wa_grd\r\n" + 
				"               inner join (select distinct ver.pk_wa_gradever,wg.code,wg.name from wa_gradeorg org \r\n" + 
				"                   left join wa_grade wg\r\n" + 
				"                        on org.pk_wa_grade = wg.pk_wa_grd\r\n" + 
				"                   left join wa_grade_ver ver\r\n" + 
				"                        on wg.pk_wa_grd = ver.pk_wa_grd\r\n" + 
				"                 where ver.effect_flag = 'Y') orgtemp\r\n" + 
				"                    on orgtemp.pk_wa_gradever = wa_grade_ver.pk_wa_gradever and wa_grade.code = orgtemp.code \r\n" + 
				"                    and orgtemp.name = wa_grade.name\r\n" + 
				"               where nvl(wa_grade_ver.dr,0) = 0 and nvl(wa_grade.dr,0) = 0) temp");
		meta.setExtraFields(new String[] { "code", "name", "pk_wa_gradever", "pk_group", "pk_org","standard_type","gradever_name","gradever_num","effect_flag"});
		meta.setPkField("pk_wa_gradever");
		meta.setCodeField("code");
		meta.setNameField("name");
		return meta;
	}

	@Override
	public String getExtraSql(RefQueryInfo refQueryInfo, RefMeta refMeta) {
		Map<String, String> param = refQueryInfo.getQueryCondition();
		if (param == null)
			return null;
		StringBuffer sbWhere = new StringBuffer(
				" and effect_flag = 'Y' ");
		if (param.get("pk_group") != null) {
			sbWhere.append(" and pk_org ='"+param.get("pk_group")+"' and pk_group ='"+param.get("pk_group")+"'");
		}
		if (param.get("standard_type") != null) {
			sbWhere.append(" and decode(standard_type,'~',0,standard_type) ='"+param.get("standard_type")+"'");
		}
		if (param.get("code") != null) {
			sbWhere.append(" and gradever_num ='"+param.get("code")+"'");
		}
		if (param.get("name") != null) {
			sbWhere.append(" and gradever_name ='"+param.get("name")+"'");
		}
//		try {
//			if (param.get("pk_group") != null && param.get("pk_org") != null) {
//				sbWhere.append(" and " + VisibleUtil.getRefVisibleCondition(param.get("pk_group"), param.get("pk_org"),
//						"92054d71-866d-4d82-9085-75ad2f63d4c1"));
//			}
//		} catch (BusinessException e) {
//			Logger.error(e.getMessage());
//		}
		return sbWhere.toString();
	}
	
	@Override
	public SqlParameterCollection getExtraSqlParameter(RefQueryInfo refQueryInfo, RefMeta refMeta) {
		return super.getExtraSqlParameter(refQueryInfo, refMeta);
	}
}
