package nccloud.web.hrwa.wagrade.save;

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

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDouble;
import nc.vo.wa.grade.CrtVO;
import nc.vo.wa.grade.WaCriterionVO;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.grade.WaGradeVerVO;
import nccloud.commons.collections.MapUtils;
import nccloud.framework.service.ServiceLocator;
import nccloud.impl.platform.common.util.CollectionUtils;

public class WaGradeVerHelper {

	/**
	 * 2019年5月16日下午6:29:55 msw
	 * @param gradeVerVOs
	 * @return：
	 * 获取选中的版本VO
	 */
	public WaGradeVerVO getSelectVerVo(WaGradeVerVO[] gradeVerVOs) {
		for (WaGradeVerVO verVO : gradeVerVOs) {
			if (verVO.getStatus() != VOStatus.UNCHANGED) {
				return verVO;
			}
		}
		return null;
	}
	/**
	 * 下午2:40:27 msw
	 * @param request
	 * @return： 
	 * 封装WaGradeVerVO
	 * @throws BusinessException
	 */
	public void setWaGradeVerVO(WaGradeVerVO gradeVerVO, WaGradeVO waGradeVO, String waStaTableData)
			throws BusinessException {
		List<Map<String, Object>> sevList = getPrmlvOrSeclvVal(2, gradeVerVO.getPk_wa_grd());
		List<Map<String, Object>> prvList = getPrmlvOrSeclvVal(1, gradeVerVO.getPk_wa_grd());
		
		if(sevList.size() > 0) {
			Map<String, Object> map01 = new HashMap();
			map01.put("display", "级别金额下限(含下限)");
			map01.put("val", "lower000000000000001");
			Map<String, Object> map02 = new HashMap();
			map02.put("display", "级别金额上限(含上限)");
			map02.put("val", "upper000000000000001");
			sevList.add(map01);
			sevList.add(map02);
		}
		Map<String, String> prvMap = new HashMap<String, String>();
		convert2PrmlvMap(prvMap, prvList);

		Map<String, String> map = getExitWaCriterionPKs(gradeVerVO.getPk_wa_gradever(), gradeVerVO.getPk_wa_grd());
		waStaTableData = waStaTableData.substring(1, waStaTableData.length() - 1);
		String tables[] = waStaTableData.split("}},");
		List<WaCriterionVO> list = new ArrayList<WaCriterionVO>();
		WaCriterionVO vo;
		for (int i = 0; i < tables.length - 1; i++) {
			String[] tabs = getStandTableRowValsFromWeb(tables[i].toString());
			if (CollectionUtils.isEmpty(sevList)) {// 如果没有档别时，添加默认值
				sevList = new ArrayList<Map<String, Object>>();
				setDefaultSeclvVal(sevList);
			}
			for (int k = 0; k < sevList.size(); k++) {
				vo = new WaCriterionVO();
				for (int p = 0; p < tabs.length; p++) {
					Object[] key = tabs[p].split(":");
					if(key.length == 1) {
						tabs[p] = tabs[p]+"0.00";
					}
					Map<String, String> valueMap = new Gson().fromJson("{" + tabs[p] + "}",
							new TypeToken<HashMap<String, String>>() {
							}.getType());
					convertWaStandTableRowVal2WaCriterionVO(vo, valueMap, sevList.get(k), prvMap, map);
					ensureWaCriterionVO(gradeVerVO, waGradeVO, vo, sevList.get(k));
				}
				list.add(vo);
			}
		}
		gradeVerVO.setCrtVOs(listCrtVOs(prvList, list));
	}
	/**
	 * 上午11:00:43 msw
	 * @param classType
	 * @param pkWaGrd
	 * @return
	 * @throws BusinessException：
	 */
	@SuppressWarnings("unchecked")
	private List<Map<String, Object>> getPrmlvOrSeclvVal(int classType, String pkWaGrd) throws BusinessException {
		String sql = null;
		if (classType == 1) {
			sql = "select levelname display,pk_wa_prmlv val from wa_prmlv  where pK_wa_grd = '" + pkWaGrd + "'";
		} else {
			sql = " select levelname display,pk_wa_seclv val from wa_seclv where pK_wa_grd = '" + pkWaGrd + "'";
		}
		List<Map<String, Object>> list = (List<Map<String, Object>>) ServiceLocator.find(IUAPQueryBS.class)
				.executeQuery(sql, new MapListProcessor());
		if (CollectionUtils.isEmpty(list)) {
			return new ArrayList<Map<String, Object>>();
		}
		return list;
	}
	/**
	 * 2019年6月13日下午12:02:52 msw
	 * @param prvMap
	 * @param prvList
	 * @throws BusinessException：
	 */
	private void convert2PrmlvMap(Map<String, String> prvMap, List<Map<String, Object>> prvList)
			throws BusinessException {
		// display与val一一对应，display中数据不重复
		for (int j = 0; j < prvList.size(); j++) {
			prvMap.put(prvList.get(j).get("display").toString(), prvList.get(j).get("val").toString());
		}
	}
	/**
	 * 上午10:59:07 msw
	 * @param pkWaGradever
	 * @param pkWaGrd
	 * @return：
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	private Map<String, String> getExitWaCriterionPKs(String pkWaGradever, String pkWaGrd) throws BusinessException {
		StringBuffer sb = new StringBuffer(" select pk_wa_prmlv, pk_wa_seclv,pk_wa_crt from wa_criterion c where");
		sb.append(" pk_wa_gradever='" + pkWaGradever + "' and pk_wa_grd='" + pkWaGrd + "'");
		List<Map<String, Object>> list = (List<Map<String, Object>>) ServiceLocator.find(IUAPQueryBS.class)
				.executeQuery(sb.toString(), new MapListProcessor());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		Map<String, String> resultMap = new HashMap<String, String>();
		String name = null;
		String val = null;
		for (int i = 0; i < list.size(); i++) {
			name = list.get(i).get("pk_wa_prmlv") + "@" + list.get(i).get("pk_wa_seclv");
			val = list.get(i).get("pk_wa_crt").toString();
			resultMap.put(name, val);
		}
		return resultMap;
	}

	/**
	 * 2019年6月12日下午7:11:10 msw
	 * @param gradeVerVO
	 * @param waGradeVO
	 * @param vo
	 * @param sevMap：
	 *
	 */
	private void ensureWaCriterionVO(WaGradeVerVO gradeVerVO, WaGradeVO waGradeVO, WaCriterionVO vo,
			Map<String, Object> sevMap) {
		if (vo.getSeclvName() == null) {
			if (waGradeVO.getIsrange().booleanValue()) {
				vo.setSeclvName(sevMap.get("display").toString());
			} else {// 如果不是宽带，如果有薪档，则取原薪档名字，如果没有档别，只有薪级，薪档名字默认为 数值
				String secName = "".equals(sevMap.get("display").toString()) ? "数值" : sevMap.get("display").toString();
				vo.setSeclvName(secName);
				vo.setLevName(secName);
			}
			vo.setPk_wa_seclv(sevMap.get("val") == null ? null : sevMap.get("val").toString());
		}
		if (vo.getPk_wa_grd() == null) {
			vo.setPk_wa_grd(gradeVerVO.getPk_wa_grd());
		}
		if (vo.getPk_wa_gradever() == null) {
			vo.setPk_wa_gradever(gradeVerVO.getPk_wa_gradever());
		}
	}

	/**
	 * 2019年6月12日下午7:11:17 msw
	 * @param vo
	 * @param valueMap
	 * @param sevMap
	 * @param prvList
	 * @param map：
	 *
	 */
	private void convertWaStandTableRowVal2WaCriterionVO(WaCriterionVO vo, Map<String, String> valueMap,
			Map<String, Object> sevMap, Map<String, String> prvMap, Map<String, String> map) {
		if (valueMap.get(sevMap.get("display") + "min") != null) {
			String minVal = valueMap.get(sevMap.get("display") + "min");
			vo.setMin_value(new UFDouble(minVal));
		}
		if (valueMap.get(sevMap.get("display") + "criterion") != null) {
			String cirVal = valueMap.get(sevMap.get("display") + "criterion");
			vo.setCriterionvalue(new UFDouble(cirVal));
		}
		if (valueMap.get(sevMap.get("display") + "max") != null) {
			String maxVal = valueMap.get(sevMap.get("display") + "max");
			vo.setMax_value(new UFDouble(maxVal));
		}
		if (valueMap.get("prmlv") != null && vo.getPk_wa_prmlv() == null) {
			String prmlv = valueMap.get("prmlv");
			vo.setPrmlvName(prmlv);
			vo.setPk_wa_prmlv(prvMap.get(prmlv));
		}
		if (vo.getPk_wa_crt() == null) {
			String pkWaCrt = MapUtils.isEmpty(map) ? null : map.get(vo.getPk_wa_prmlv() + "@" + vo.getPk_wa_seclv());
			vo.setPk_wa_crt(pkWaCrt);
		}
	}

	/**
	 * 2019年6月12日下午6:47:21 msw
	 * @param sevList：
	 *
	 */
	private void setDefaultSeclvVal(List<Map<String, Object>> sevList) {
		Map<String, Object> sevMap = new HashMap<String, Object>();
		sevMap.put("display", "");
		sevMap.put("val", null);
		sevList.add(sevMap);
	}

	/**
	 * 2019年6月12日下午6:44:56 msw
	 * @param standTableValstr
	 * @return：
	 *
	 */
	private String[] getStandTableRowValsFromWeb(String standTableValstr) {
		int position = standTableValstr.indexOf("values");
		position = (position == 1) ? 9 : position + 8;
		standTableValstr = standTableValstr.substring(position, standTableValstr.length());
		standTableValstr = standTableValstr.replace("{", "");
		standTableValstr = standTableValstr.replace("value=", "");
		standTableValstr = standTableValstr.replace("}", "");
		standTableValstr = standTableValstr.replace("=", ":");
		return standTableValstr.split(",");
	}

	/**
	 * 下午4:20:37 msw
	 * @param dataJson
	 * @return
	 * @throws BusinessException：
	 * 封装CrtVO[]
	 *
	 */
	private CrtVO[] listCrtVOs(List<Map<String, Object>> prvList, List<WaCriterionVO> criterions)
			throws BusinessException {
		CrtVO[] crtvo = null;
		String strOrdGrdpk = "";
		CrtVO one = null;
		Vector<CrtVO> v = new Vector<CrtVO>();
		int intCriterion = 0;
		if (CollectionUtils.isEmpty(criterions)) {
			return null;
		}
		intCriterion = criterions.size();
		for (int j = 0; j < prvList.size(); j++) {
			strOrdGrdpk = prvList.get(j).get("val").toString();
			one = new CrtVO();
			for (int i = 0; i < intCriterion; i++) {
				if (strOrdGrdpk.equals(criterions.get(i).getPk_wa_prmlv())) {// 同一个级别
					one.add(criterions.get(i));
				}
			}
			v.addElement(one);
		}
		if (v.size() > 0) {
			crtvo = new CrtVO[v.size()];
			v.copyInto(crtvo);
		}
		return crtvo;
	}
}
