package nc.impl.wa.rule;

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

import nc.bs.dao.BaseDAO;
import nc.hr.frame.persistence.BaseDAOManager;
import nc.hr.frame.persistence.SimpleDocServiceTemplate;
import nc.itf.hr.wa.IWaRuleExQueryService;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.uif2.LoginContext;
import nc.vo.wa.paydata.DataVO;
import nc.vo.wa.pub.WaLoginContext;

/**
 * WaRuleImpl<BR>
 * <BR>
 *
 * @author: xuhw
 * @date: 2009-11-10
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleExQueryImpl implements IWaRuleExQueryService
{
	private BaseDAOManager baseDao;
	private final String DOC_NAME = "waruleex";
	private SimpleDocServiceTemplate serviceTemplate;
	private BaseDAOManager getBaseDao()
	{
		if (baseDao == null)
		{
			baseDao = new BaseDAOManager();
		}
		return baseDao;
	}

	private SimpleDocServiceTemplate getServiceTemplate()
	{
		if (serviceTemplate == null)
		{
			serviceTemplate = new SimpleDocServiceTemplate(DOC_NAME);
		}
		return serviceTemplate;
	}

	/**
	 * 查询薪资规则by薪资规则PK
	 * @author xuhw on 2010-4-21
	 * @param strPkWageform
	 * @return
	 * @throws BusinessException
	 */
	public WageformdetExVO[] queryWageformdetVOsByPK(String strPkWageform) throws BusinessException
	{
		StringBuffer sbSql = new StringBuffer();
		sbSql.append(" select ");
		sbSql.append(" 	formdet.*  ");
		sbSql.append(" from ");
		sbSql.append(" 	wa_wageformdetex formdet  ");
		sbSql.append(" where ");
		sbSql.append(" 	formdet.pk_wa_wageformex = '"+strPkWageform +"'");
		sbSql.append(" order by ");
		sbSql.append(" 	ilevel desc");
		return getBaseDao().executeQueryVOs(sbSql.toString(), WageformdetExVO.class);
	}

	@Override
	public Object[] queryWageFormVO(LoginContext context, String strCondition) throws BusinessException
	{
		//对薪资规则表主表根据所属组织和编码进行排序
	     String strOrderBy = " pk_org , code ";
	     AggWageformExVO[] billVOs = getServiceTemplate().queryByCondition(context, AggWageformExVO.class, strCondition, strOrderBy);
		if (billVOs == null)
		{
			return null;
		}
		//对薪资规则表字表根据序号进行排序
		WageformdetExVO[] wageFormDetVOs = null;
		for (int i = 0; i < billVOs.length; i++)
		{
			wageFormDetVOs = (WageformdetExVO[])billVOs[i].getChildrenVO();
			if (wageFormDetVOs != null)
			{
				//NCdp205566113 20150727 shenliangc 薪资规则子表保存后无法按照序号排序，原因是Arrays.sort方法失效。 jdk1.7出现此问题。
				SuperVOUtil.sortByAttributeName(wageFormDetVOs, WageformdetExVO.ILEVEL, true);
//				Arrays.sort(wageFormDetVOs, new WaRuleComparator());
			}

			billVOs[i].setChildrenVO(wageFormDetVOs);
		}
		return billVOs;
	}

	@Override
	public List<String> queryOrgsByPkWageformEx(String pkWageformEx) throws BusinessException {
		String sql = "select pk_org from wa_wageformex_org where pk_wa_wageformex = '"+pkWageformEx+"' and nvl(dr,0) = 0";
		List<String> list = (List<String>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());
		return list;
	}

	@Override
	public List<Map<String, Object>> queryWageformExContral(WaLoginContext loginContext) throws BusinessException {
//		String sql =  "select wa_wageformex.pk_waitem,\n" +
//			"       wa_item.itemkey,\n" + 
//			"       wa_item.code,\n" + 
//			"       wa_item.name,\n" + 
//			"       wa_wageformex.iscontrol,\n" + 
//			"       wa_wageformdetex.vformula,\n" + 
//			"       wa_wageformdetex.vformulastr,\n" + 
//			"       wa_wageformdetex.upperlimit\n" + 
//			"  from wa_wageformex\n" + 
//			"  left join wa_item\n" + 
//			"    on wa_item.pk_wa_item = wa_wageformex.pk_waitem\n" + 
//			"  left join wa_wageformdetex\n" + 
//			"    on wa_wageformex.pk_wa_wageformex = wa_wageformdetex.pk_wa_wageformex\n" + 
//			" where wa_wageformex.isenable = 'Y' and \n" + 
//			"       wa_wageformex.pk_org = '"+loginContext.getPk_org()+"' and nvl(wa_wageformex.dr,0) = 0\n" + 
//			"       and nvl(wa_wageformdetex.dr,0) = 0 and nvl(wa_wageformex.pk_waitem,'~') <> '~'";
		String sql =  "select wa_wageformex.pk_waitem,\n" +
						"             wa_item.itemkey,\n" + 
						"             wa_item.code,\n" + 
						"             wa_item.name,\n" + 
						"             wa_wageformex.iscontrol,\n" + 
						"             wa_wageformdetex.vformula,\n" + 
						"             wa_wageformdetex.vformulastr,\n" + 
						"             wa_wageformdetex.upperlimit\n" + 
						"        from wa_wageformex\n" + 
						"        left join wa_item\n" + 
						"          on wa_item.pk_wa_item = wa_wageformex.pk_waitem\n" + 
						"        left join wa_wageformdetex\n" + 
						"          on wa_wageformex.pk_wa_wageformex = wa_wageformdetex.pk_wa_wageformex\n" + 
						"        left join wa_wageformex_org\n" + 
						"          on wa_wageformex.pk_wa_wageformex = wa_wageformex_org.pk_wa_wageformex\n" + 
						"       where wa_wageformex.isenable = 'Y'\n" + 
						"             and wa_wageformex_org.pk_org = '"+loginContext.getPk_org()+"'\n" + 
						"             and nvl(wa_wageformex.dr,0) = 0 and nvl(wa_wageformdetex.dr,0) = 0\n" + 
						"             and nvl(wa_wageformex.pk_waitem,'~') <> '~';";

		List<Map<String, Object>> listMap = (List<Map<String, Object>>) new BaseDAO().executeQuery(sql, new MapListProcessor());
		return listMap;
	}

	@Override
	public List<String> queryAllOrgs() throws BusinessException {
		String sql = "select pk_hrorg from org_hrorg where dr = 0 and enablestate = 2";
		List<String> list = (List<String>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());
		return list;
	}

	@Override
	public UFBoolean checkData(String pk_wa_data, String context) throws BusinessException {
		String sql =
			"select * from wa_data\n" +
			"       left join bd_psndoc\n" + 
			"            on bd_psndoc.pk_psndoc = wa_data.pk_psndoc\n" + 
			"       left join hi_psnjob\n" + 
			"            on hi_psnjob.pk_psnjob = wa_data.pk_psnjob\n" + 
			"       where wa_data.pk_wa_data = '"+pk_wa_data+"'";
		if(context.indexOf("bd_psndoc") >= 0 || context.indexOf("hi_psnjob") >= 0) {
			context = context.replaceAll("\"", "'").replaceAll("\\|\\|", "or").replaceAll("\\&\\&", "and");
			sql = sql + "and "+ context;
		}
		DataVO dataVO = (DataVO) getBaseDao().getBaseDao().executeQuery(sql, new BeanProcessor(DataVO.class));
		if(dataVO != null) {
			return UFBoolean.TRUE;
		}
		return UFBoolean.FALSE;
	}
}
