package nccloud.web.hrwa.psndocwadoc;

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

import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;

import nc.itf.hr.wa.IWaGradeQueryService;
import nc.itf.uap.IUAPQueryBS;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.wa.grade.CrtVO;
import nc.vo.wa.grade.WaCriterionVO;
import nc.vo.wa.grade.WaGradeVerVO;
import nc.vo.wa.item.WaItemVO;
import nccloud.base.exception.ExceptionUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.web.hr.pub.HRCommonAction;
import nccloud.web.hrwa.pub.ErrorContent;
import nccloud.web.uapbd.commons.web.ParamUtils;

/**
 * 定调薪信息维护 查询薪资标准表
 * 
 * @author lenovo
 *
 */
public class PsndocWaGradeAction extends HRCommonAction {
	
	private String levelHeadName = "一档";

	@Override
	public <T> Object execute(IRequest request, T para) throws Exception {
		ParamUtils param = new ParamUtils(request);
		// 是否多档
		String isMultsec = param.getString("isMultsec", null);
		// 是否宽带
		String is_range = param.getString("is_range", null);
		// 薪资标准主键
		String strPkWaGrd = param.getString("strPkWaGrd", null);
		// 薪资项目主键
		String pk_wa_item = param.getString("pk_wa_item", null);
		if (StringUtils.isBlank(isMultsec) || StringUtils.isBlank(strPkWaGrd) || StringUtils.isBlank(is_range)) {
			ExceptionUtils.wrapBusinessException(ErrorContent.QUERYMETHODNULL);
		}
		// 获取service进行查询
		IWaGradeQueryService service = ServiceLocator.find(IWaGradeQueryService.class);
		WaGradeVerVO waGradeVerVO = service.queryEffectOrCurrentGradeVerVO(strPkWaGrd, null,
				UFBoolean.valueOf(isMultsec).booleanValue());
		if (null == waGradeVerVO) {
			return null;
		}
		CrtVO[] crtVOs = waGradeVerVO.getCrtVOs();
		if (ArrayUtils.isEmpty(crtVOs)) {
			return null;
		}
		
		//根据薪资标准设置数据的精度  by liuboz 20181203 ---------------
		WaItemVO itemVO = (WaItemVO) ServiceLocator.find(IUAPQueryBS.class).retrieveByPK(WaItemVO.class, pk_wa_item);
		Integer iflddecimal = null;
		if(itemVO != null){
			iflddecimal = itemVO.getIflddecimal();
		}
		for (CrtVO crtVO : crtVOs) {
			List<Object> vecData = crtVO.getVecData();
			for (Object obj : vecData) {
				if(iflddecimal != null){
					WaCriterionVO waObj = (WaCriterionVO) obj;
					if(waObj.getCriterionvalue() != null)
						waObj.setCriterionvalue(waObj.getCriterionvalue().setScale(iflddecimal, UFDouble.ROUND_HALF_UP));
					if(waObj.getMax_value() != null)
						waObj.setMax_value(waObj.getMax_value().setScale(iflddecimal, UFDouble.ROUND_HALF_UP));
					if(waObj.getMin_value() != null)
						waObj.setMin_value(waObj.getMin_value().setScale(iflddecimal, UFDouble.ROUND_HALF_UP));
				}
			}
		}
		//------------------------------------------------------
		
		if (isMultsec.equals(UFBoolean.TRUE.toString()) || is_range.equals(UFBoolean.TRUE.toString())) {
			// 将数据转换为二维数组的table
			Set<String> levelHead = new LinkedHashSet<>();// 横列表头
			Set<String> verticalHead = new LinkedHashSet<>();// 竖列表头
			Map<String, Object> dataMap = new HashMap<>();// 数据
			for (CrtVO crtVO : crtVOs) {
				List<Object> vecData = crtVO.getVecData();
				for (Object obj : vecData) {
					WaCriterionVO waObj = (WaCriterionVO) obj;
					if("lower000000000000001".equals(waObj.getPk_wa_seclv()) ||  "upper000000000000001".equals(waObj.getPk_wa_seclv())) {
						continue;
					}
					if (is_range.equals(UFBoolean.TRUE.toString())
							&& isMultsec.equals(UFBoolean.FALSE.toString())) {
						// 横列表头
						levelHead.add(levelHeadName);
						// 竖列表头
						verticalHead.add(waObj.getPrmlvName());
						// 数据
						if(!dataMap.containsKey(levelHeadName + waObj.getPrmlvName())){
							dataMap.put(levelHeadName + waObj.getPrmlvName(), obj);
						}
					}else{
						// 横列表头
						levelHead.add(waObj.getSeclvName());
						// 竖列表头
						verticalHead.add(waObj.getPrmlvName());
						// 数据
						if(!dataMap.containsKey(waObj.getSeclvName() + waObj.getPrmlvName())){
							dataMap.put(waObj.getSeclvName() + waObj.getPrmlvName(), obj);
						}
					}
				}
			}
			Map<String, Object> map = new HashMap<>();
			map.put("levelHead", levelHead);
			map.put("verticalHead", verticalHead);
			map.put("dataMap", dataMap);
			return map;
		}
		if (isMultsec.equals(UFBoolean.FALSE.toString())) {
			List<Object> vecData = new ArrayList<>();
			// 不是多档，直接返回数据
			for (CrtVO crtVO : crtVOs) {
				vecData.addAll(crtVO.getVecData());
			}
			return vecData;
		}
		return null;
	}

}
