package nccloud.web.hrwa.wagrade.validation;

import java.util.ArrayList;
import java.util.List;

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

import nc.bs.logging.Logger;
import nc.hr.utils.ResHelper;
import nc.hr.utils.StringHelper;
import nc.hr.utils.StringPiecer;
import nc.itf.hr.wa.IWaGradeService;
import nc.vo.pub.BusinessException;
import nc.vo.wa.grade.AggWaGradeVO;
import nc.vo.wa.grade.CrtVO;
import nc.vo.wa.grade.IWaGradeCommonDef;
import nc.vo.wa.grade.WaCriterionVO;
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 nccloud.framework.service.ServiceLocator;

public class WaGradeValidate {

	public void validateDataDuplication(AggWaGradeVO aggvo) throws BusinessException {
		// AggWaGradeVO aggvo = (AggWaGradeVO) obj;
		WaPrmlvVO[] prmvo = (WaPrmlvVO[]) aggvo.getTableVO(IWaGradeCommonDef.WA_PRMLV);
		WaSeclvVO[] secvo = (WaSeclvVO[]) aggvo.getTableVO(IWaGradeCommonDef.WA_SECLV);
		StringBuffer errMessage = new StringBuffer();
		// 统一类别下的级别名称重复的校验
		String gradeName = validatePrmName(prmvo);
		if (StringUtils.isNotBlank(gradeName)) {
			errMessage.append(ResHelper.getString("60130paystd", "060130paystd0247")/* 以下信息存在重复 */ + "\n");
			errMessage.append(ResHelper.getString("60130paystd", "060130paystd0248")
					/* 位置 */ + ResHelper.getString("60130paystd", "060130paystd0171")/* 薪级页签 */ + "\n");
			errMessage.append(gradeName);
		}
		if (aggvo.getParentVO().getIsmultsec().booleanValue()) {
			if (null != secvo && secvo.length > 0) {
				if (StringUtils.isNotBlank(errMessage.toString())) {
					errMessage.append(ResHelper.getString("60130paystd", "060130paystd0247")/* 以下信息存在重复 */ + "\n");
				}
				// 统一类别下的档别名称重复的校验
				String dangName = validateSecName(secvo);
				if (StringUtils.isNotBlank(dangName)) {
					errMessage.append(ResHelper.getString("60130paystd", "060130paystd0248")
							/* 位置 */ + ResHelper.getString("60130paystd", "060130paystd0173")/* 薪档页签 */ + "\n");
					errMessage.append(dangName);
				}
			} else {
				errMessage.append(ResHelper.getString("60130paystd",
						"060130paystd0221")/* @res "你选择了多档，请填写相关数据！ " */);
			}
		}
		if (StringUtils.isBlank(errMessage.toString())) {
			return;
		} else {
			throw new BusinessException(errMessage.toString());
		}
	}

	/**
	 * 统一类别下的级别名称重复的校验
	 *
	 * @author xuhw on 2009-12-17
	 * @param prmvo
	 * @param sbErrMessage
	 */
	private String validatePrmName(WaPrmlvVO[] prmvo) {
		if (ArrayUtils.isEmpty(prmvo)) {
			return null;
		}
		// prmNames用来存储全部的级别名称
		String[] prmNames = new String[prmvo.length];
		prmNames = StringPiecer.getStrArray(prmvo, WaPrmlvVO.LEVELNAME);
		List<String> listname = new ArrayList<String>();

		StringBuffer message = new StringBuffer();
		// 找出级别名称全部相同的行
		for (int i = 0; i < prmvo.length - 1; i++) {
			// 对于已经检验过的不再检验
			if (listname.contains(prmNames[i])) {
				continue;
			} else {
				listname.add(prmNames[i]);
			}
			// 记录相同的行号
			StringBuffer linename = new StringBuffer();
			for (int j = i + 1; j < prmvo.length; j++) {
				if (prmvo[j].getStatus() == 3) {
					continue;
				}
				if (prmNames[i].equals(prmNames[j])) {
					linename.append(j + 1 + ResHelper.getString("60130paystd", "060130paystd0251"));
				}
			}
			if (StringUtils.isNotBlank(linename.toString())) {
				message.append(ResHelper.getString("60130paystd", "060130paystd0246")/* 行号 */ + (i + 1)
						+ ResHelper.getString("60130paystd", "060130paystd0251")
						+ linename.toString().substring(0, linename.toString().length() - 1)
						+ ResHelper.getString("60130paystd", "060130paystd0252")
						+ ResHelper.getString("60130paystd", "160130paystd0012")/* 级别名称 */
						+ ResHelper.getString("60130paystd", "060130paystd0253") + prmNames[i] + "\n");
			}
		}
		return message.toString();
	}

	/**
	 * 统一类别下的档别名称重复的校验
	 *
	 * @author xuhw on 2009-12-17
	 * @param secvo
	 * @param sbErrMessage
	 */
	private String validateSecName(WaSeclvVO[] secvo) {
		if (ArrayUtils.isEmpty(secvo)) {
			return null;
		}
		// 记录已经校验过的
		List<String> lisSecNames = new ArrayList<String>();
		// 记录薪档名称
		String[] secNames = new String[secvo.length];
		secNames = StringPiecer.getStrArray(secvo, WaSeclvVO.LEVELNAME);

		// 返回信息，返回所有的重复行号
		StringBuffer message = new StringBuffer();
		for (int i = 0; i < secvo.length - 1; i++) {
			if (lisSecNames.contains(secNames[i])) {
				continue;
			} else {
				lisSecNames.add(secNames[i]);
			}
			StringBuffer line = new StringBuffer();
			for (int j = i + 1; j < secvo.length; j++) {
				if (secvo[j].getStatus() == 3) {
					continue;
				}
				if (secNames[i].equals(secNames[j])) {
					line.append(j + 1 + ResHelper.getString("60130paystd", "060130paystd0251"));
				}
			}
			if (StringUtils.isNotBlank(line.toString())) {
				message.append(ResHelper.getString("60130paystd", "060130paystd0246")/* 行号 */ + (i + 1)
						+ ResHelper.getString("60130paystd", "060130paystd0251")
						+ line.toString().substring(0, line.toString().length() - 1)
						+ ResHelper.getString("60130paystd", "060130paystd0252")
						+ ResHelper.getString("60130paystd", "160130paystd0011")/* 档别名称 */
						+ ResHelper.getString("60130paystd", "060130paystd0253") + secNames[i] + "\n");
			}
		}
		return message.toString();
	}

	/**
	 * 
	 * 下午3:32:10 
	 * msw
	 * @param gradeverVO
	 * @param waGradeVO
	 * @return：
	 * @throws BusinessException
	 *
	 */
	public void busiValidate(WaGradeVerVO gradeverVO, WaGradeVO waGradeVO) throws BusinessException {
		// 业务验证
		StringBuffer sbErrMessage = new StringBuffer();
		CrtVO[] crtVOs = null;
		validateGradeVerInfo(gradeverVO, sbErrMessage);
		crtVOs = gradeverVO.getCrtVOs();
		if (ArrayUtils.isEmpty(crtVOs)) {
			return;
		}
		// 宽带薪酬的场合，校验宽带的上限，下限，基准值的关系
		if (waGradeVO.getIsrange().booleanValue()) {
			validateCrtRange(crtVOs, sbErrMessage);
		}
	}
	/**
	 * 版本信息非空项目校验
	 *
	 * @param gradeverVO
	 * @param sbErrMessage
	 * @return
	 */
	private String validateGradeVerInfo(WaGradeVerVO gradeverVO, StringBuffer sbErrMessage) {
		if (gradeverVO == null) {
			Logger.debug("请正确输入版本相关数据.");
			sbErrMessage.append(ResHelper.getString("60130paystd",
					"060130paystd0149")/* @res "请正确输入版本相关数据." */);
			return sbErrMessage.toString();
		}
		if (gradeverVO.getGradever_num() == null) {
			Logger.debug("[版本号]");
			sbErrMessage.append(ResHelper.getString("60130paystd",
					"060130paystd0150")/* @res "[版本号]" */);
		}
		if (StringUtils.isBlank(gradeverVO.getGradever_name())) {
			Logger.debug("[版本名称]");
			sbErrMessage.append(ResHelper.getString("60130paystd",
					"060130paystd0151")/* @res "[版本名称]" */);
		}
		if (gradeverVO.getVer_create_date() == null
				|| StringUtils.isBlank(gradeverVO.getVer_create_date().toStdString())) {
			Logger.debug("[版本创建日期]");
			sbErrMessage.append(ResHelper.getString("60130paystd",
					"060130paystd0152")/* @res "[版本创建日期]" */);
		}
		if (StringUtils.isBlank(sbErrMessage.toString())) {
			return null;
		}
		sbErrMessage.append(ResHelper.getString("60130paystd",
				"060130paystd0153")/* @res "不能为空!" */);
		return sbErrMessage.toString();
	}

	/**
	 * 校验薪资标准类别值的合法性<BR>
	 * 宽带薪酬的场合，下限<=基准值<=上限的校验<BR>
	 *
	 * @return ValidationFailure
	 * @throws BusinessException
	 */
	private String validateCrtRange(CrtVO[] crtVOs, StringBuffer sbErrMessage) throws BusinessException {
		double dblMin = 0;
		double dblBasic = 0;
		double dblMax = 0;
		// 业务验证
		for (int i = 0; i < crtVOs.length; i++) {
			WaCriterionVO[] criterionvos = (crtVOs[i].getVecData().toArray(new WaCriterionVO[0]));
			for (int j = 0; j < criterionvos.length; j++) {
				dblMin = criterionvos[j].getMin_value().doubleValue();
				dblBasic = criterionvos[j].getCriterionvalue().doubleValue();
				dblMax = criterionvos[j].getMax_value().doubleValue();

				if (dblMin > dblBasic) {
					Logger.debug((i + 1) + "行 " + "第" + (j + 1) + "列的下限值" + StringHelper.convertDoubleValue(dblMin, 3)
							+ " 不能大于 基准值 " + StringHelper.convertDoubleValue(dblBasic, 3));
					sbErrMessage.append((i + 1)
							+ ResHelper.getString("60130paystd",
									"060130paystd0154")/* @res "行/ " */
							+ (j + 1)
							+ ResHelper.getString("60130paystd",
									"060130paystd0155")/* @res "列 ：[下限值" */
							+ StringHelper.convertDoubleValue(dblMin, 3)
							+ ResHelper.getString("60130paystd",
									"060130paystd0156")/* @res "] 不能大于 [基准值 " */
							+ StringHelper.convertDoubleValue(dblBasic, 3) + "]  \n");
				} else if (dblBasic > dblMax) {
					Logger.debug((i + 1) + "行 " + "第" + (j + 1) + "列的基准值" + StringHelper.convertDoubleValue(dblBasic, 3)
							+ " 不能大于 上限值 " + StringHelper.convertDoubleValue(dblMax, 3));
					sbErrMessage.append((i + 1)
							+ ResHelper.getString("60130paystd",
									"060130paystd0157")/* @res "行/" */
							+ (j + 1)
							+ ResHelper.getString("60130paystd",
									"060130paystd0158")/* @res "列 ：[基准值" */
							+ StringHelper.convertDoubleValue(dblBasic, 3)
							+ ResHelper.getString("60130paystd",
									"060130paystd0159")/* @res "] 不能大于 [上限值 " */
							+ StringHelper.convertDoubleValue(dblMax, 3) + "]  \n");
				}
			}
		}
		if (StringUtils.isBlank(sbErrMessage.toString())) {
			return null;
		} else {
			throw new BusinessException(sbErrMessage.toString());
		}
	}

	/**
	 * 薪资标准版本的生效日期的校验
	 *
	 * @param vervos
	 * @throws BusinessException
	 */
	public void validateEffectFlag(WaGradeVerVO[] gradeVerVOs,WaGradeVerVO gradeVer, String pkWaGrd) throws BusinessException {
		if (gradeVer == null || pkWaGrd == null || gradeVer.getEffect_flag() == null) {
			return;
		}
		int intErrorFlag = 0;
		for (WaGradeVerVO vervo : gradeVerVOs) {
			if (vervo.getEffect_flag() != null && vervo.getEffect_flag().booleanValue()) {
				intErrorFlag++;
			}
		}
		if (intErrorFlag == 1) {
			return;
		} else if (intErrorFlag > 1) {
			throw new BusinessException(ResHelper.getString("60130paystd",
					"060130paystd0206")/* @res "一个薪资标准中不能有多个生效标记被选中！" */);
		} else {
			IWaGradeService service = ServiceLocator.find(IWaGradeService.class);
			String msg = service.validateGradeHaveReferenceByBusiness(pkWaGrd);
			if (msg != null && !"".equals(msg)) {
				throw new BusinessException(msg);
			}
		}
	}

	/**
	 * 
	 * 下午12:42:07 
	 * msw
	 * @param aggvo
	 * @throws BusinessException：
	 *
	 */
	public void validatePrvSecIsNull(AggWaGradeVO aggvo) throws BusinessException {
		WaPrmlvVO[] prmvo = (WaPrmlvVO[]) aggvo.getTableVO(IWaGradeCommonDef.WA_PRMLV);
		WaSeclvVO[] secvo = (WaSeclvVO[]) aggvo.getTableVO(IWaGradeCommonDef.WA_SECLV);
		String prvSheet = ResHelper.getString("60130paystd",
				"160130paystd0016")/* @res "薪级" */;
		String sevSheet = ResHelper.getString("60130paystd",
				"160130paystd0018")/* @res "薪档" */;
		String msg = "";
		String sheetName = ResHelper.getString("_Bill", "UPP_Bill-000003");// 页签
		if (prmvo != null) {
			for (int i = 0; i < prmvo.length; i++) {
				if (prmvo[i].getLevelname() == null) {
					String rowText = ResHelper.getString("6013bnkitf", "06013bnkitf0133") + prmvo[i].getDisplayindex()
							+ ResHelper.getString("60130paystd", "060130paystd0160")/* @res"第{0}行:" */;
					msg += prvSheet + "(" + sheetName + ")" + rowText + "["
							+ ResHelper.getString("60130paystd", "160130paystd0012")/* 级别名称 */ + "] ";
				}
			}
		}
		if (!"".equals(msg)) {
			throw new BusinessException(ResHelper.getString("_template", "1-systemplet-00001") + msg);
		}
		if (secvo != null) {
			for (int i = 0; i < secvo.length; i++) {
				if (secvo[i].getLevelname() == null) {
					String rowText = ResHelper.getString("6013bnkitf", "06013bnkitf0133") + secvo[i].getDisplayindex()
							+ ResHelper.getString("60130paystd", "060130paystd0160")/* @res"第{0}行:" */;
					msg += sevSheet + "(" + sheetName + ")" + rowText + "["
							+ ResHelper.getString("60130paystd", "160130paystd0011")/* 级别名称 */ + "] ";
				}
			}
		}
		if (!"".equals(msg)) {
			throw new BusinessException(ResHelper.getString("_template", "1-systemplet-00001") + msg);
		}
	}
}
