package nccloud.web.hrwa.wagrade.action;

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.IItemQueryService;
import nc.itf.hr.wa.IWaGradeQueryService;
import nc.itf.hr.wa.IWaGradeService;
import nc.lightapp.pubapp.web.template.ref.util.StringUtils;
import nc.vo.bd.pub.NODE_TYPE;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVOUtil;
import nc.vo.uif2.LoginContext;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.CrtVO;
import nc.vo.wa.grade.IWaGradeCommonDef;
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.item.WaItemVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.grid.Grid;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hr.pub.HRNccUtils;
import nccloud.web.hrwa.pub.IWATempletConst;
import nccloud.web.hrwa.wagrade.action.util.WaGradeGrid;
import nccloud.web.hrwa.wagrade.action.util.WaGradeUtil;
import nccloud.web.hrwa.wagrade.query.PrmlvSeclvPorpHelper;
import nccloud.web.hrwa.wagrade.query.WaStaTableHelper;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 
 * 下午4:20:50 
 * msw： 
 * 返回薪资标准版本信息
 *
 */
public class WaGradeGetVerDataAction extends HRCommonAction {
	private String pkWaGrd;
	private String pkWaGradever;
	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		String pkWaGrdWeb = param.getString("pk_wa_grd", null);
		String pkWaGradeverWeb = param.getString("pk_wa_gradever", null);
		String areaGradeVer=param.getString("areaGradeVer", "gradever_info");
		LoginContext context = HRNccUtils.getLoginContext(param);
		String appCode = NODE_TYPE.ORG_NODE.equals(context.getNodeType()) ? IWATempletConst.APPCODE_WAGRADE_ORG
				: IWATempletConst.APPCODE_WAGRADE_GRP;
		this.pkWaGrd = pkWaGrdWeb;
		this.pkWaGradever = pkWaGradeverWeb;
		return getResultMap(appCode,areaGradeVer);
	}
	/**
	 * 
	 * 2019年6月26日下午3:56:30
	 * msw
	 * @param appCode
	 * @param areaGradeVer
	 * @return
	 * @throws BusinessException：
	 *
	 */
	protected Map<String, Object> getResultMap(String appCode,String areaGradeVer) throws BusinessException{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		AggWaGradeVO aggGradeVo = (AggWaGradeVO) ServiceLocator.find(IWaGradeQueryService.class)
				.queryAggWagradeByGrdPK(pkWaGrd);

		WaPrmlvVO[] waPrmlvVOArr = (WaPrmlvVO[]) aggGradeVo.getTableVO(IWaGradeCommonDef.WA_PRMLV);
		// 原薪资标准档别数组
		WaSeclvVO[] waSeclvVOArr = (WaSeclvVO[]) aggGradeVo.getTableVO(IWaGradeCommonDef.WA_SECLV);
		
		SuperVOUtil.sortByAttributeName(waPrmlvVOArr, "displayindex", true);//按displayindex字段升序排列
		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);
			//add by@chenzf 添加下限和上限金额值waSeclvVOArr 2021-3-1 16:25:18 end
		} else {
			//add by@chenzf 添加下限和上限金额值waSeclvVOArr 2021-3-1 16:25:18 begin
//			List<WaSeclvVO> waSeclvVOList = new ArrayList();
//			int displayindex = 0;
//			String pk_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);
		}
		resultMap.put("seclv", seclvList);
		
		WaGradeVerVO[] gradeVerVo = ServiceLocator.find(IWaGradeService.class).queryGradeVerByGradePK(pkWaGrd);
		Grid gradeVerGrid = (ArrayUtils.isEmpty(gradeVerVo)) ? null : WaGradeGrid.getGridWithArea2WaGradeVer(appCode, areaGradeVer, gradeVerVo);
		resultMap.put("gradeVerVo", gradeVerGrid);
		
		ensurResultMap(resultMap,aggGradeVo,prmlvList,seclvList);
		return resultMap;
	}

	/**
	 * 
	 * 2019年6月26日下午3:56:45 msw
	 * @param resultMap
	 * @param aggGradeVo
	 * @param prmlvList
	 * @param seclvList
	 * @throws BusinessException：
	 * 补充薪资项目精度和薪资标准表数据
	 */
	private void ensurResultMap(Map<String, Object> resultMap,AggWaGradeVO aggGradeVo,List<String> prmlvList,List<String> seclvList) throws BusinessException{
		WaGradeVO gradeVo = aggGradeVo.getParentVO();
		WaItemVO itemvo =ServiceLocator.find(IItemQueryService.class).queryWaItemVOByPk(gradeVo.getPk_wa_item());
		int decimalPlaces = (itemvo == null) ? 0 : itemvo.getIflddecimal();
		Map<String, Object> tableDataMap = new HashMap<String, Object>();
		WaStaTableHelper handleData=new WaStaTableHelper();
		if (StringUtils.isEmpty(pkWaGradever)) {// 新增时，表需要清空
			setResultMap(resultMap, null, decimalPlaces);
			return;
		}
		CrtVO[] crtVOs = ServiceLocator.find(IWaGradeQueryService.class).queryCriterionByClassid(pkWaGrd, pkWaGradever,
				WaGradeUtil.isTrue(gradeVo.getIsmultsec()));
		if (ArrayUtils.isEmpty(crtVOs)) {
			tableDataMap = handleData.getWaStaTableData(prmlvList, seclvList, null, decimalPlaces);
			setResultMap(resultMap, tableDataMap, decimalPlaces);
			return;
		}
		/* 薪资标准表数据 */
		tableDataMap = handleData.getWaStaTableData(prmlvList, seclvList, crtVOs, decimalPlaces);
		setResultMap(resultMap, tableDataMap, decimalPlaces);
	}
	/**
	 * 
	 * 2019年6月26日下午3:57:29
	 * msw
	 * @param resultMap
	 * @param tableDataMap
	 * @param decimalPlaces：
	 * 根据薪资项目查出的精度
	 */
	private void setResultMap (Map<String, Object> resultMap,Map<String, Object> tableDataMap,int decimalPlaces){
		resultMap.put("decimalPlaces", decimalPlaces);
		resultMap.put("staTable", tableDataMap);
	}
}
