package nc.impl.wa.rule;

import nc.bs.bd.cache.CacheProxy;
import nc.bs.dao.DAOException;
import nc.bs.logging.Logger;
import nc.hr.frame.persistence.BaseDAOManager;
import nc.hr.frame.persistence.SimpleDocServiceTemplate;
import nc.hr.utils.ResHelper;
import nc.itf.hr.wa.IWaRuleExService;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.hrwa.waruleexorg.WageformExOrgVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.util.AuditInfoUtil;
import nc.vo.wa.rule.WageformVO;
import nc.vo.wabm.util.WaCacheUtils;

/**
 * WaRuleAppImpl
 *
 * @author: xuhw
 * @date: 2009-11-12 下午01:56:16
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleExAppImpl implements IWaRuleExService
{
	private final String DOC_NAME = "waruleex";
	private SimpleDocServiceTemplate serviceTemplate;
	private BaseDAOManager basedao = null;

	/**
	 * 删除薪资规则表<BR>
	 * 删除之前要进行是否被应用得校验，如果被引用则不能被删除<BR>
	 *
	 * @author xuhw on 2009-11-12
	 */
	@Override
	public void deleteVO(AggWageformExVO vo) throws BusinessException
	{
		// 删除之前要进行是否被应用得校验，如果被引用则不能被删除
		if (gradeHaveItemReference(vo.getParentVO().getPrimaryKey()))
		{
			Logger.debug("删除失败，该薪资标准已经被公共项目或者薪资发放项目引用!");
			throw new BusinessException(ResHelper.getString("60130payrule","060130payrule0014")/*@res "删除失败，该薪资标准已经被公共项目或者薪资发放项目引用!"*/);
		}
		getServiceTemplate().delete(vo);
		CacheProxy.fireDataDeleted(WageformExVO.getDefaultTableName(), vo.getParentVO().getPrimaryKey());
	}

	/**
	 * 新增薪资规则表
	 *
	 * @author xuhw on 2009-11-12
	 */
	@Override
	public AggWageformExVO insertVO(AggWageformExVO billVO) throws BusinessException
	{
		WaCacheUtils.synCache(WageformExVO.getDefaultTableName());
		return sortVO(getServiceTemplate().insert(billVO));
	}

	/**
	 * 更新薪资规则表
	 *
	 * @author xuhw on 2009-11-12
	 */
	@Override
	public AggWageformExVO updateVO(AggWageformExVO billVO) throws BusinessException
	{
		WaCacheUtils.synCache(WageformExVO.getDefaultTableName());
		return sortVO(getServiceTemplate().update(billVO,true));
	}

	/**
	 * 复制薪资规则表
	 *
	 * @author xuhw on 2009-11-12
	 */
	@Override
	public Object copyTable(AggWageformExVO billVO) throws BusinessException
	{
		// 设置主表修改状态
		billVO.getParentVO().setStatus(VOStatus.NEW);
		// 重置主表主键
		billVO.getParentVO().setPrimaryKey(null);
		// 重置子表主键
		for (int i = 0; i < billVO.getChildrenVO().length; i++)
		{
			((WageformdetExVO) billVO.getChildrenVO()[i]).setPk_wa_wageformdetex(null);
			((WageformdetExVO) billVO.getChildrenVO()[i]).setStatus(VOStatus.NEW);
		}
		// 重置审计信息
		resetAudit((WageformExVO) billVO.getParentVO());
		WaCacheUtils.synCache(WageformVO.getDefaultTableName());
		return sortVO(getServiceTemplate().insert(billVO));
	}

	/**
	 * 对子表vo进行排序
	 */
	private AggWageformExVO sortVO(AggWageformExVO billVO)
	{
		WageformdetExVO[] wageFormDetVOs = (WageformdetExVO[]) billVO.getChildrenVO();
		if (wageFormDetVOs != null)
		{
			//NCdp205566113 20150727 shenliangc 薪资规则子表保存后无法按照序号排序，原因是Arrays.sort方法失效。 jdk1.7出现此问题。
			SuperVOUtil.sortByAttributeName(wageFormDetVOs, WageformdetExVO.ILEVEL, true);
//			Arrays.sort(wageFormDetVOs, new WaRuleComparator());
		}
		billVO.setChildrenVO(wageFormDetVOs);
		return billVO;
	}

	/**
	 * 复制时重置审计信息
	 *
	 * @author xuhw on 2009-11-12
	 * @param vo
	 */
	private void resetAudit(WageformExVO vo)
	{
		// 创建人
		vo.setAttributeValue(WageformExVO.CREATOR, AuditInfoUtil.getCurrentUser());
		// 创建时间
		vo.setAttributeValue(WageformExVO.CREATIONTIME, AuditInfoUtil.getCurrentTime());
		// 修改人
		vo.setAttributeValue(WageformExVO.MODIFIER, null);
		// 修改时间
		vo.setAttributeValue(WageformExVO.MODIFIEDTIME, null);
	}

	/**
	 * 判断薪资规则表是否被薪资公共项目、发放项目引用
	 *
	 * @author xuhw on 2009-11-28
	 * @param strGrdPk
	 * @return
	 * @throws DAOException
	 */
	private boolean gradeHaveItemReference(String strRulePk) throws DAOException
	{
		// ifromflag=1 表示，数据来源是薪资规则表
		StringBuffer sbsql = new StringBuffer();
		sbsql.append(" select 3 ");
		sbsql.append("   from wa_item item ");
		sbsql.append("  where (item.ifromflag = '1' and ");
		sbsql.append("        item.vformula like  '%" + strRulePk + "%' ) ");
		sbsql.append(" union ");
		sbsql.append(" select 2 ");
		sbsql.append("   from wa_classitem classitem ");
		sbsql.append("  where ");
		sbsql.append("  (classitem.ifromflag = '1' and ");
		sbsql.append("  classitem.vformula like '%" + strRulePk + "%' )");
		return getBaseDaoManager().isValueExist(sbsql.toString());
	}

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

	private BaseDAOManager getBaseDaoManager()
	{
		if (basedao == null)
		{
			basedao = new BaseDAOManager();
		}
		return basedao;
	}

	@Override
	public void instertWageformExOrgVO(WageformExOrgVO[] vos) throws BusinessException {
		getBaseDaoManager().insertVOArrayReturnVOArray(vos);
	}

	@Override
	public int updateEnable(String pkWageformEx, Boolean isenable) throws BusinessException {
		String enable = isenable ? "Y" : "N";
		String updateSql = "update WA_WAGEFORMEX set ISENABLE = '"+enable+"' where pk_wa_wageformex = '"+pkWageformEx+"' and dr = 0";
		int rst = getBaseDaoManager().getBaseDao().executeUpdate(updateSql);
		return rst;
	}
}