package nccloud.web.hrwa.wagrade.validation;

import nccloud.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;

import nc.hr.utils.ResHelper;
import nc.itf.hr.wa.IItemQueryService;
import nc.itf.hr.wa.IWaGradeService;
import nc.ui.hr.comp.sort.UFDoubleCompare;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.CrtVO;
import nc.vo.wa.grade.GradeSortEnum;
import nc.vo.wa.grade.WaCriterionVO;
import nc.vo.wa.grade.WaGradeVO;
import nc.vo.wa.grade.WaGradeVerVO;
import nc.vo.wa.item.WaItemVO;
import nccloud.framework.service.ServiceLocator;

public class WaGradeVerValidateHelper {
	UFDoubleCompare compare = new UFDoubleCompare();
	/** 同一行的前一个值 */
	UFDouble dblBefBasicValue = new UFDouble(0);
	/** 同一行的后一个值 */
	UFDouble dblAftBasicValue = new UFDouble(0);
	/** 同一列的前一个值 */
	UFDouble dblUpBasicValues = new UFDouble(0);
	/** 同一列的后一个值 */
	UFDouble dblDownBasicValues = new UFDouble(0);

	Integer iflddecimal = Integer.valueOf(0);
	/**
	 * 2019年6月12日下午6:05:39 msw
	 * @param wagradevo
	 * @param gradeVerVO
	 * @param gradeVerVOs
	 * @throws BusinessException：
	 *
	 */
	public void validate(WaGradeVO wagradeVO, WaGradeVerVO gradeVerVO, WaGradeVerVO[] gradeVerVOs)
			throws BusinessException {
		WaGradeValidate validate = new WaGradeValidate();
		validateEffectFlag(gradeVerVOs, gradeVerVO);
		validate.busiValidate(gradeVerVO, wagradeVO);
	}
	/**
	 * 2019年4月23日下午2:46:38 msw
	 * @param gradeVerVO
	 * @throws BusinessException：
	 *
	 */
	private void validateEffectFlag(WaGradeVerVO[] gradeVerVOs, WaGradeVerVO gradeVerVO) throws BusinessException {
		WaGradeVerVO gradeVerVoOld = new WaGradeVerVO();
		WaGradeValidate validate = new WaGradeValidate();
		if (gradeVerVO.getPk_wa_gradever() != null) {
			WaGradeVerVO[] gradeVerVosOld = ServiceLocator.find(IWaGradeService.class)
					.queryGradeVerByGradePK(gradeVerVO.getPk_wa_grd());
			for (WaGradeVerVO verVO : gradeVerVosOld) {
				if (verVO.getPk_wa_gradever().equals(gradeVerVO.getPk_wa_gradever())) {
					gradeVerVoOld = verVO;
					break;
				}
			}
			if (gradeVerIsChange(gradeVerVoOld, gradeVerVO)) {
				validate.validateEffectFlag(gradeVerVOs, gradeVerVO, gradeVerVO.getPk_wa_grd());
			}
		}
	}
	/**
	 * 2019年4月23日下午2:21:07 msw
	 * @param gradeVerVOOld
	 * @param gradeVerVONew
	 * @return： 
	 * 判断薪资标准版本主表数据是否发生改变
	 *
	 */
	private boolean gradeVerIsChange(WaGradeVerVO gradeVerVOOld, WaGradeVerVO gradeVerVONew) {
		boolean effectFlagIsChange = !gradeVerVOOld.getEffect_flag().equals(gradeVerVONew.getEffect_flag());
		boolean gradeverNameIsChange = !gradeVerVOOld.getGradever_name().equals(gradeVerVONew.getGradever_name());
		boolean createDateIsChange = !gradeVerVOOld.getVer_create_date().isSameDate(gradeVerVONew.getVer_create_date());

		return (effectFlagIsChange || gradeverNameIsChange || createDateIsChange) ? true : false;
	}
	/**
	 * 下午6:04:53 msw
	 * @param gradeverVO
	 * @param waGradeVO
	 * @param aggvo
	 * @return：
	 * @throws BusinessException
	 *
	 */
	public void setValidataWaStandTableMsg(JSONObject resultJson, WaGradeVerVO gradeverVO, WaGradeVO waGradeVO,
			AggWaGradeVO aggvo) throws BusinessException {
		resetValue();
		// 业务验证
		StringBuffer sbErrMessage = new StringBuffer();
		CrtVO[] waStandDatas = gradeverVO.getCrtVOs();
		validataWaGradeCrtValue(sbErrMessage, waGradeVO, aggvo, waStandDatas);

		WaItemVO itemvo = ServiceLocator.find(IItemQueryService.class).queryWaItemVOByPk(waGradeVO.getPk_wa_item());
		int decimalPlaces = (itemvo == null) ? 0 : itemvo.getIflddecimal();

		String msg = StringUtils.isBlank(sbErrMessage.toString()) ? null : sbErrMessage.toString();
		boolean val = StringUtils.isEmpty(msg) ? true : false;
		resultJson.put("message", msg);
		resultJson.put("success", val);
		resultJson.put("decimalPlaces", decimalPlaces);
		resultJson.put("dataMap", gradeverVO.getPk_wa_gradever());
	}
	/**
	 * 2019年6月12日下午6:29:46 msw
	 * @param sbErrMessage
	 * @param waGradeVO
	 * @param aggvo
	 * @param waStandDatas：
	 */
	private void validataWaGradeCrtValue(StringBuffer sbErrMessage, WaGradeVO waGradeVO, AggWaGradeVO aggvo,
			CrtVO[] waStandDatas) {
		if (waStandDatas.length > 0) {
			WaCriterionVO[] criterionvos = (waStandDatas[0].getVecData().toArray(new WaCriterionVO[0]));
			// 保留上一个级别的第一个档别基准值
			dblUpBasicValues = criterionvos[0].getCriterionvalue();
		}
		if (aggvo != null && aggvo.getParentVO() != null && aggvo.getParentVO().getIflddecimal() != null) {// 无数据时，默认为0精度
			iflddecimal = aggvo.getParentVO().getIflddecimal();
		}
		// 级别升序，档别升序
		if (GradeSortEnum.MoneyUpType == waGradeVO.getPrmlv_money_sort()
				&& GradeSortEnum.MoneyUpType == waGradeVO.getSeclv_money_sort()) {
			validatePrmUpSecUp(waStandDatas, sbErrMessage);
		}
		// 级别升序，档别降序
		else if (GradeSortEnum.MoneyUpType == waGradeVO.getPrmlv_money_sort()
				&& GradeSortEnum.MoneyDownType == waGradeVO.getSeclv_money_sort()) {
			validatePrmUpSecDown(waStandDatas, sbErrMessage);
		}
		// 级别降序，档别降序
		else if (GradeSortEnum.MoneyDownType == waGradeVO.getPrmlv_money_sort()
				&& GradeSortEnum.MoneyDownType == waGradeVO.getSeclv_money_sort()) {
			validatePrmDownSecDown(waStandDatas, sbErrMessage);
		}
		// 级别降序，档别升序
		else if (GradeSortEnum.MoneyDownType == waGradeVO.getPrmlv_money_sort()
				&& GradeSortEnum.MoneyUpType == waGradeVO.getSeclv_money_sort()) {
			validatePrmDownSecUp(waStandDatas, sbErrMessage);
		}
	}
	/**
	 * xuhw on 2010-1-19
	 * @param crtVOs
	 * @param sbErrMessage
	 * @return： 
	 * 级别升序，档别降序
	 */
	private void validatePrmUpSecDown(CrtVO[] crtVOs, StringBuffer sbErrMessage) {
		WaCriterionVO[][] criterionvos = new WaCriterionVO[crtVOs.length][];
		for (int i = 0; i < crtVOs.length; i++) {
			criterionvos[i] = (crtVOs[i].getVecData().toArray(new WaCriterionVO[0]));
		}
		for (int i = 0; i < criterionvos.length; i++) {
			for (int j = 0; j < criterionvos[i].length; j++) {
				dblAftBasicValue = dblDownBasicValues = criterionvos[i][j].getCriterionvalue();
				if (i != 0) {
					dblUpBasicValues = criterionvos[i - 1][j].getCriterionvalue();
					if (compare.lessThan(dblDownBasicValues, dblUpBasicValues)) {
						sbErrMessage.append((j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0218")/* @res "列 ：[" */
								+ (i)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行 " */
								+ dblUpBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行" */
								+ dblDownBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0168")
								/* @res "级别没有按照升序排列。" */ + "  \n");
					}
				}
				if (j != 0) {
					dblBefBasicValue = criterionvos[i][j - 1].getCriterionvalue();
					if (compare.lessThan(dblBefBasicValue, dblAftBasicValue)) {
						sbErrMessage.append((i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0164")/* @res "行 ：[" */
								+ j
								+ ResHelper.getString("60130paystd",
										"060130paystd0165")/* @res " 列 " */
								+ dblBefBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0166")/* @res " 列  " */
								+ dblAftBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0169")
								/* @res " 档别没有按照降序排列。" */ + "  \n");
					}
				}
			}
		}
	}
	/**
	 * 级别降序，档别降序的情况<BR>
	 * 9 | 8 | 7 |<BR>
	 * ----|----|----|---<BR>
	 * 6 | 5 | 4 |<BR>
	 * ----|----|----|---<BR>
	 * 3 | 2 | 1 |<BR>
	 * ----|----|----|---<BR>
	 *
	 * @author xuhw on 2010-1-19
	 */
	private void validatePrmDownSecDown(CrtVO[] crtVOs, StringBuffer sbErrMessage) {
		WaCriterionVO[][] criterionvos = new WaCriterionVO[crtVOs.length][];
		for (int i = 0; i < crtVOs.length; i++) {
			criterionvos[i] = (crtVOs[i].getVecData().toArray(new WaCriterionVO[0]));
		}
		for (int i = 0; i < criterionvos.length; i++) {
			for (int j = 0; j < criterionvos[i].length; j++) {
				dblAftBasicValue = dblDownBasicValues = criterionvos[i][j].getCriterionvalue();
				if (i != 0) {
					dblUpBasicValues = criterionvos[i - 1][j].getCriterionvalue();
					if (compare.lessThan(dblUpBasicValues, dblDownBasicValues)) {
						sbErrMessage.append((j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0218")/* @res "列 ：[" */
								+ (i)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行 " */
								+ dblUpBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行" */
								+ dblDownBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0170")
								/* @res "级别没有按照降序排列。" */ + "  \n");
					}
				}
				if (j != 0) {
					dblBefBasicValue = criterionvos[i][j - 1].getCriterionvalue();
					if (compare.lessThan(dblBefBasicValue, dblAftBasicValue)) {
						sbErrMessage.append((i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0164")/* @res "行 ：[" */
								+ j
								+ ResHelper.getString("60130paystd",
										"060130paystd0165")/* @res " 列 " */
								+ dblBefBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0166")/* @res " 列  " */
								+ dblAftBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0169")
								/* @res " 档别没有按照降序排列。" */ + " \n ");
					}
				}
			}
		}
	}
	/**
	 * 级别降序，档别升序的情况<BR>
	 * 5 | 6 | 7 |<BR>
	 * ----|----|----|---<BR>
	 * 4 | 5 | 6 |<BR>
	 * ----|----|----|---<BR>
	 * 3 | 4 | 5 |<BR>
	 * ----|----|----|---<BR>
	 *
	 * @author xuhw on 2010-1-19
	 */
	private void validatePrmDownSecUp(CrtVO[] crtVOs, StringBuffer sbErrMessage) {
		WaCriterionVO[][] criterionvos = new WaCriterionVO[crtVOs.length][];
		for (int i = 0; i < crtVOs.length; i++) {
			criterionvos[i] = (crtVOs[i].getVecData().toArray(new WaCriterionVO[0]));
		}
		for (int i = 0; i < criterionvos.length; i++) {
			for (int j = 0; j < criterionvos[i].length; j++) {
				dblAftBasicValue = dblDownBasicValues = criterionvos[i][j].getCriterionvalue();
				if (i != 0) {
					dblUpBasicValues = criterionvos[i - 1][j].getCriterionvalue();
					if (compare.lessThan(dblUpBasicValues, dblDownBasicValues)) {
						sbErrMessage.append((j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0218")/* @res "列 ：[" */
								+ (i)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行 " */
								+ dblUpBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行 " */
								+ dblDownBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0170")
								/* @res "级别没有按照降序排列。" */ + " \n ");
					}
				}
				if (j != 0) {
					dblBefBasicValue = criterionvos[i][j - 1].getCriterionvalue();
					if (compare.lessThan(dblAftBasicValue, dblBefBasicValue)) {
						sbErrMessage.append((i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0164")/* @res "行 ：[" */
								+ j
								+ ResHelper.getString("60130paystd",
										"060130paystd0165")/* @res " 列 " */
								+ dblBefBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0166")/* @res " 列  " */
								+ dblAftBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0167")
								/* @res " 档别没有按照升序排列。" */ + " \n ");
					}
				}
			}
		}
	}
	/** 重新初始化 */
	private void resetValue() {
		/** 同一行的前一个值 */
		dblBefBasicValue = new UFDouble(0).setScale(iflddecimal, UFDouble.ROUND_HALF_UP);
		/** 同一行的后一个值 */
		dblAftBasicValue = new UFDouble(0).setScale(iflddecimal, UFDouble.ROUND_HALF_UP);
		/** 同一列的前一个值 */
		dblUpBasicValues = new UFDouble(0).setScale(iflddecimal, UFDouble.ROUND_HALF_UP);
		/** 同一列的后一个值 */
		dblDownBasicValues = new UFDouble(0).setScale(iflddecimal, UFDouble.ROUND_HALF_UP);
	}
	/**
	 * 级别升序，档别升序的情况<BR>
	 * 1 | 2 | 3 |<BR>
	 * ----|----|----|---<BR>
	 * 4 | 5 | 6 |<BR>
	 * ----|----|----|---<BR>
	 * 5 | 6 | 7 |<BR>
	 * ----|----|----|---<BR>
	 *
	 * @author xuhw on 2010-1-19
	 */
	private void validatePrmUpSecUp(CrtVO[] crtVOs, StringBuffer sbErrMessage) {
		WaCriterionVO[][] criterionvos = new WaCriterionVO[crtVOs.length][];
		for (int i = 0; i < crtVOs.length; i++) {
			criterionvos[i] = (crtVOs[i].getVecData().toArray(new WaCriterionVO[0]));
		}
		for (int i = 0; i < criterionvos.length; i++) {
			for (int j = 0; j < criterionvos[i].length; j++) {
				dblAftBasicValue = dblDownBasicValues = criterionvos[i][j].getCriterionvalue();
				if (i != 0) {
					if(criterionvos[i - 1].length == j) {
						dblUpBasicValues = criterionvos[i - 1][j-1].getCriterionvalue();
					}else {
						dblUpBasicValues = criterionvos[i - 1][j].getCriterionvalue();
					}
					if (compare.lessThan(dblDownBasicValues, dblUpBasicValues)) {
						sbErrMessage.append((j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0218")/* @res "列 ：[" */
								+ (i)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行 " */
								+ dblUpBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0160")/* @res "行" */
								+ dblDownBasicValues.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0163")
								/* @res " 级别没有按照升序排列。" */ + " \n ");
					}
				}
				if (j != 0) {
					dblBefBasicValue = criterionvos[i][j - 1].getCriterionvalue();
					if (compare.lessThan(dblAftBasicValue, dblBefBasicValue)) {
						sbErrMessage.append((i + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0164")/* @res "行 ：[" */
								+ j
								+ ResHelper.getString("60130paystd",
										"060130paystd0165")/* @res " 列 " */
								+ dblBefBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP)
								+ ResHelper.getString("60130paystd",
										"060130paystd0161")/* @res "] 与  [" */
								+ (j + 1)
								+ ResHelper.getString("60130paystd",
										"060130paystd0166")/* @res " 列  " */
								+ dblAftBasicValue.setScale(iflddecimal, UFDouble.ROUND_HALF_UP) + "]");
						sbErrMessage.append(ResHelper.getString("60130paystd", "060130paystd0167")
								/* @res " 档别没有按照升序排列。" */ + " \n ");
					}
				}
			}
		}
	}
}
