package nc.ui.wa.ruleex.model;

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

import nc.bs.framework.common.NCLocator;
import nc.itf.hr.wa.IWaRuleExQueryService;
import nc.ui.uif2.model.BillManageModel;
import nc.vo.hrwa.waruleex.AggWageformExVO;
import nc.vo.hrwa.waruleex.WageformdetExVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.uif2.LoginContext;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.item.WaItemVO;

/**
 * WaRuleModel<BR>
 * <BR>
 * 
 * @author: xuhw
 * @date: 2009-11-10
 * @since: eHR V6.0
 * @走查人:
 * @走查日期:
 * @修改人:
 * @修改日期:
 */
public class WaRuleModel extends BillManageModel
{
	public final static String EVENT_COPY = "event_copy";
	/**
	 * 存储删除的字表VO
	 */
	private List<WageformdetExVO> lisDelSubVos = new ArrayList<WageformdetExVO>();

	/**
	 * 判断是否是复制表状态
	 */
	private boolean copyStatus = false;

	/**
	 * 公式编辑器使用
	 */
	private AggWaGradeVO[] aggWaGradeVos = null;

	/**
	 * 复制<BR>
	 * <BR>
	 * 
	 * @author xuhw on 2009-11-12
	 * @param object
	 * @return Object
	 * @throws BusinessException
	 */
	public Object copyTable(Object object) throws Exception
	{
		Object obj = ((WaRuleModelService) getService()).copyTable(object);
		directlyAdd(obj);
		return obj;
	}

	/**
	 * 重新整合聚合VO
	 * 
	 * @author xuhw on 2009-12-16
	 * @param value
	 * @param wageRuleVOArr
	 */
	public void reMakeAggVO(Object value, WageformdetExVO[] wageRuleVOArr)
	{
		AggWageformExVO aggVo = (AggWageformExVO) value;
		List<WageformdetExVO> lisDelSubVOs = this.lisDelSubVos;
		List<WageformdetExVO> lisWageFormDetvos = getWageRuleVOArr(wageRuleVOArr, lisDelSubVOs);

		if (lisWageFormDetvos != null && lisWageFormDetvos.size() > 0)
		{
			aggVo.setChildrenVO(lisWageFormDetvos.toArray(new WageformdetExVO[lisWageFormDetvos.size()]));
		}
	}

	/**
	 * 取得界面上的和删除的VO
	 * 
	 * @author xuhw on 2009-11-13
	 * @param wageRuleVOArr
	 * @param valueChangedData
	 * @return
	 */
	private List<WageformdetExVO> getWageRuleVOArr(WageformdetExVO[] wageRuleVOArr, List<WageformdetExVO> lisDelSubVOs)
	{
		List<WageformdetExVO> list = new ArrayList<WageformdetExVO>();
		for (int i = 0; i < wageRuleVOArr.length; i++)
		{
			WageformdetExVO wageFormDetVO = wageRuleVOArr[i];
			if (VOStatus.UNCHANGED == wageFormDetVO.getStatus())
			{
				wageFormDetVO.setStatus(VOStatus.UPDATED);
			}
			if (VOStatus.NEW == wageFormDetVO.getStatus() && wageFormDetVO.getPk_wa_wageformdetex() != null)
			{
				wageFormDetVO.setStatus(VOStatus.UPDATED);
			}
			list.add(wageFormDetVO);
		}

		// 取得删除的部分
		for (int i = 0; i < lisDelSubVOs.size(); i++)
		{
			WageformdetExVO ageFormDetVO = lisDelSubVOs.get(i);
			if(ageFormDetVO.getPk_wa_wageformex()!=null)
				list.add(ageFormDetVO);
		}
		return list;
	}

	/**
	 * 查询公共薪资项目
	 * 
	 * @author xuhw on 2010-4-14
	 * @param context
	 * @return
	 * @throws BusinessException
	 */
	public WaItemVO[] queryAllItemVOs(LoginContext context) throws BusinessException
	{
		return ((WaRuleModelService) getService()).getItemQueryService().queryWaItemVOsByCondition(context, null);
	}

	/**
	 * 查询公共薪资项目
	 * 
	 * @author xuhw on 2010-4-14
	 * @param context
	 * @return
	 * @throws BusinessException
	 */
	public AggWaGradeVO[] queryWagradeVOs(LoginContext context) throws BusinessException
	{
		if (aggWaGradeVos == null)
		{
			aggWaGradeVos = (AggWaGradeVO[]) ((WaRuleModelService) getService()).getGradeQueryService().queryWaGradeVO(context, null);
		}
		return aggWaGradeVos;
	}

	/**
	 * 查询符合条件的所有薪资标准类别VO
	 * 
	 * @author xuhw on 2010-4-14
	 * @return
	 * @throws BusinessException
	 */
	public AggWageformExVO[] queryAggWageFormVO() throws BusinessException
	{
		return (AggWageformExVO[]) NCLocator.getInstance().lookup(IWaRuleExQueryService.class).queryWageFormVO(this.getContext(), null);
	}

	public List<WageformdetExVO> getLisDelSubVos()
	{
		return lisDelSubVos;
	}

	public void setLisDelSubVos(List<WageformdetExVO> lisDelSubVos)
	{
		this.lisDelSubVos = lisDelSubVos;
	}

	public void addDelSubVo(WageformdetExVO wageFormDetVO)
	{
		lisDelSubVos.add(wageFormDetVO);
	}

	public void setCopyStatus(boolean blnIsCopyStatus)
	{
		this.copyStatus = blnIsCopyStatus;
	}

	public boolean isCopyStatus()
	{
		return copyStatus;
	}

	public AggWaGradeVO[] getAggWaGradeVos()
	{
		return aggWaGradeVos;
	}

	public void setAggWaGradeVos(AggWaGradeVO[] aggWaGradeVos)
	{
		this.aggWaGradeVos = aggWaGradeVos;
	}

	/**
	 * 清空缓存<BR>
	 * 删除子表缓存<BR>
	 * 
	 * @author xuhw on 2009-11-15
	 */
	public void clear()
	{
		this.lisDelSubVos.clear();
		aggWaGradeVos = null;
	}
}
