package nccloud.web.hrwa.ref.basic;

import java.util.Map;

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 WaPrmlvExGridRef extends DefaultGridRefAction {

	@Override
	public RefMeta getRefMeta(RefQueryInfo refQueryInfo) {
		TreeRefMeta meta = new TreeRefMeta();
		meta.setTableName("wa_grade inner join wa_prmlv on wa_grade.pk_wa_grd = wa_prmlv.pk_wa_grd");
		meta.setExtraFields(new String[]{"wa_prmlv.displayindex", "wa_grade.name","wa_prmlv.levelname","wa_grade.pk_wa_grd","wa_prmlv.pk_wa_prmlv"});
		meta.setPkField("wa_prmlv.pk_wa_prmlv");
		meta.setCodeField("wa_prmlv.levelname");
		meta.setNameField("wa_prmlv.levelname");
		return meta;
	}
	
	@Override
	public String getExtraSql(RefQueryInfo refQueryInfo, RefMeta refMeta) {
		//获取前端传递过来的参数 
		String sqlWhere = " and wa_prmlv.isgroup = 'Y' ";	
		Map<String, String> param = refQueryInfo.getQueryCondition();
		if(param == null)
			return sqlWhere;
		if (param.get("pk_wa_grd")!=null)
		{
			sqlWhere += " and wa_prmlv.pK_wa_grd = '" + param.get("pk_wa_grd") +  "'  ";
//			select pk_wa_grd from wa_grade where code = (select code from wa_grade where pk_wa_grd = '1001ZZ1000000002WEHX') and pk_org = '0001N210000000000E4X'
//			sqlWhere += " and wa_prmlv.pK_wa_grd = (select pk_wa_grd from wa_grade where code = (select code from wa_grade where pk_wa_grd = '"+param.get("pk_wa_grd")+"') and pk_org = '"+param.get("pk_org")+"') ";
		}else if(param.get("pk_group")!=null){
			sqlWhere += " and wa_grade.pk_group = '"+param.get("pk_group")+"' and (wa_grade.pk_org = '"+param.get("pk_org")+
					"' or wa_grade.pk_org = '"+param.get("pk_group")+"')"
					+ "";
		}else if(param.get("pk_org")!=null) {
//			sqlWhere += " and wa_grade.pk_org = '"+param.get("pk_org")+"'";
//					+ " and wa_grade.pk_wa_grd in (select pk_wa_grd from wa_grade_ver where effect_flag = 'Y') ";
		}
		return sqlWhere;
	}

	@Override
	public String getOrderSql(RefQueryInfo paramRefQueryInfo, RefMeta paramRefMeta) {
		return "order by wa_prmlv.displayindex";
	}
	
	@Override
	public SqlParameterCollection getExtraSqlParameter(RefQueryInfo refQueryInfo, RefMeta refMeta) {
		return super.getExtraSqlParameter(refQueryInfo, refMeta);
	}
}
