package nccloud.web.hrwa.wagrade.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nccloud.commons.lang.ArrayUtils;

import nc.itf.hr.wa.IWaGradeQueryService;
import nc.lightapp.pubapp.web.template.ref.util.StringUtils;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.IWaGradeCommonDef;
import nc.vo.wa.grade.PsnhiHeaderVO;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.grade.WaGradeVerVO;
import nc.vo.wa.grade.WaPrmlvVO;
import nc.vo.wa.grade.WaSeclvVO;
import nc.vo.wa.grade.WaStdHiSetVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.ui.pattern.form.Form;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.hrwa.wagrade.action.util.WaGradeConsts;
import nccloud.web.hrwa.wagrade.action.util.WaGradeGrid;
import nccloud.web.hrwa.wagrade.action.util.WaGradePersnPropTempletUtils;

public class GetHandleDataHelper {
	private String appCode;
	private String pkOrg;

	public GetHandleDataHelper(String appCode, String pkOrg) {
		this.appCode = appCode;
		this.pkOrg = pkOrg;
	}

	/**
	 * 
	 * 2019年6月26日下午3:54:04 msw
	 * @param parentVO
	 * @param tableHead
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws BusinessException：
	 *
	 */
	public Grid getStdHisetSGrid(WaGradeVO parentVO, PsnhiHeaderVO[] tableHead)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException, BusinessException {
		WaStdHiSetVO[] stdHiSetVosS = ServiceLocator.find(IWaGradeQueryService.class)
				.queryStdHiByGrdPk(parentVO.getPk_wa_grd(), 2);
		// 档别人员属性设置table
		Grid stdHisetSGrid = null;
		if (!ArrayUtils.isEmpty(stdHiSetVosS)) {
			stdHisetSGrid = WaGradeGrid.getGrid(pkOrg, tableHead, stdHiSetVosS, 2,
					WaGradePersnPropTempletUtils.AREA_SUB_SECLV);
		}
		return stdHisetSGrid;
	}

	/**
	 * 
	 * 2019年6月26日下午3:54:11 msw
	 * @param parentVO
	 * @param tableHead
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws BusinessException：
	 *
	 */
	public Grid getStdHisetPGrid(WaGradeVO parentVO, PsnhiHeaderVO[] tableHead)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException, BusinessException {
		WaStdHiSetVO[] stdHiSetVosP = ServiceLocator.find(IWaGradeQueryService.class)
				.queryStdHiByGrdPk(parentVO.getPk_wa_grd(), 1);
		// 级别人员属性设置table
		Grid stdHisetPGrid = null;
		if (!ArrayUtils.isEmpty(stdHiSetVosP)) {
			stdHisetPGrid = WaGradeGrid.getGrid(pkOrg, tableHead, stdHiSetVosP, 1,
					WaGradePersnPropTempletUtils.AREA_SUB_PRMLV);
		}
		return stdHisetPGrid;
	}

	/**
	 * 
	 * 2019年6月26日下午3:54:18 msw
	 * @param gradeVo
	 * @return：
	 *
	 */
	public Grid getSeclvSubGrid(AggWaGradeVO gradeVo) {
		// 薪级table
		WaSeclvVO[] waSeclvVOArr = (WaSeclvVO[]) gradeVo.getTableVO(IWaGradeCommonDef.WA_SECLV);
		SuperVOUtil.sortByAttributeName(waSeclvVOArr, "displayindex", true);// 按displayindex字段升序排列
		Grid seclvSubGrid = null;
		if (!ArrayUtils.isEmpty(waSeclvVOArr)) {
			seclvSubGrid = WaGradeGrid.getGridWithArea(appCode, WaGradeConsts.AREA_CODE_SECLV_SUB, waSeclvVOArr);
		}
		return seclvSubGrid;
	}

	/**
	 * 
	 * 2019年6月26日下午3:54:23 msw
	 * @param gradeVo
	 * @return：
	 *
	 */
	public Grid getPrmlvSubGrid(AggWaGradeVO gradeVo) {
		// 薪级table
		WaPrmlvVO[] waPrmlvVOArr = (WaPrmlvVO[]) gradeVo.getTableVO(IWaGradeCommonDef.WA_PRMLV);
		SuperVOUtil.sortByAttributeName(waPrmlvVOArr, "displayindex", true);// 按displayindex字段升序排列
		Grid prmlvSubGrid = null;
		if (!ArrayUtils.isEmpty(waPrmlvVOArr)) {
			prmlvSubGrid = WaGradeGrid.getGridWithArea(appCode, WaGradeConsts.AREA_CODE_PRMLV_SUB, waPrmlvVOArr);
		}
		return prmlvSubGrid;
	}

	/**
	 * 
	 * 2019年6月26日下午3:54:33 msw 
	 * @param parentVO
	 * @return
	 * @throws BusinessException：
	 *
	 */
	public Form getGradeVerForm(WaGradeVO parentVO) throws BusinessException {
		boolean isMultsec = ((UFBoolean) parentVO.getIsmultsec()).booleanValue();
		WaGradeVerVO gradevervo = ServiceLocator.find(IWaGradeQueryService.class)
				.queryEffectOrCurrentGradeVerVO(parentVO.getPk_wa_grd(), null, isMultsec);
		Form gradeVerForm = null;
		if (gradevervo != null) {
			gradeVerForm = WaGradeGrid.getFormWithArea(appCode, WaGradeConsts.AREA_CODE_GRADE_VER, gradevervo);
		}
		return gradeVerForm;
	}
	/**
	 * 
	 * 2019年6月26日下午3:55:21
	 * msw
	 * @param pkWaGrd
	 * @return
	 * @throws BusinessException：
	 * 获取薪资标准表数据
	 */
	public Map<String, Object> getWaStaTable(String pkWaGrd) throws BusinessException {
		if (StringUtils.isEmpty(pkWaGrd)) {
			return null;
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AggWaGradeVO aggWaGradeVO = (AggWaGradeVO) ServiceLocator.find(IWaGradeQueryService.class)
				.queryAggWagradeByGrdPK(pkWaGrd);
		WaGradeVO waGradeVO = aggWaGradeVO.getParentVO();
		WaPrmlvVO[] waPrmlvVOArr = (WaPrmlvVO[]) aggWaGradeVO.getTableVO(IWaGradeCommonDef.WA_PRMLV);
		SuperVOUtil.sortByAttributeName(waPrmlvVOArr, "displayindex", true);//按displayindex字段升序排列
		// 原薪资标准档别数组
		WaSeclvVO[] waSeclvVOArr = (WaSeclvVO[]) aggWaGradeVO.getTableVO(IWaGradeCommonDef.WA_SECLV);
		SuperVOUtil.sortByAttributeName(waSeclvVOArr, "displayindex", true);//按displayindex字段升序排列
		// 原薪资标准人员属性数组
		List<String> prmlvList = new ArrayList<String>();
		if (!ArrayUtils.isEmpty(waPrmlvVOArr)) {
			new PrmlvSeclvPorpHelper().setPrmlvPropList(waPrmlvVOArr,prmlvList);
		}
		resultMap.put("prmlv", prmlvList);
		
		List<String> seclvList = new ArrayList<String>();
		if(!ArrayUtils.isEmpty(waSeclvVOArr)) {
			
			//add by@chenzf 添加下限和上限金额值waSeclvVOArr 2021-3-1 16:25:18 begin
			List<WaSeclvVO> waSeclvVOList = new ArrayList();
			int displayindex = 0;
			String pk_wa_grd = "";
			for(WaSeclvVO waSeclvVO : waSeclvVOArr) {
				waSeclvVOList.add(waSeclvVO);
				displayindex = waSeclvVO.getDisplayindex() + 1;
				pk_wa_grd = waSeclvVO.getPk_wa_grd();
			}
			//级别金额下限(含下限)
			WaSeclvVO lowerVO = new WaSeclvVO();
			lowerVO.setPk_wa_grd(pk_wa_grd);
			lowerVO.setDisplayindex(displayindex);
			lowerVO.setPk_wa_seclv("lower000000000000001");
			lowerVO.setLevelname("级别金额下限(含下限)");
			lowerVO.setDr(0);
			waSeclvVOList.add(lowerVO);
			
			//级别金额上限(含上限)
			WaSeclvVO upperVO = new WaSeclvVO();
			upperVO.setPk_wa_grd(pk_wa_grd);
			upperVO.setDisplayindex(displayindex+1);
			upperVO.setPk_wa_seclv("upper000000000000001");
			upperVO.setLevelname("级别金额上限(含上限)");
			upperVO.setDr(0);
			waSeclvVOList.add(upperVO);
			
			new PrmlvSeclvPorpHelper().setSeclvPropList(waSeclvVOList.toArray(new WaSeclvVO[0]), seclvList);
//			new PrmlvSeclvPorpHelper().setSeclvPropList(waSeclvVOArr, seclvList);
		}
		resultMap.put("seclv", seclvList);
		new WaStaTableHelper().setStaTable(resultMap, prmlvList,  seclvList,pkWaGrd, waGradeVO);
		return resultMap;
	}
}
