//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.vo.fbm.pub.util;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import nc.bs.framework.common.NCLocator;
import nc.itf.fbm.gather.IGatherQueryService;
import nc.md.model.MetaDataException;
import nc.md.persist.framework.IMDPersistenceQueryService;
import nc.pub.templet.converter.util.helper.ExceptionUtils;
import nc.pubitf.uapbd.IRateTypePubService;
import nc.vo.bd.notetype.NotetypeVO;
import nc.vo.bd.ratetype.RateTypeVO;
import nc.vo.fbm.register.AggRegisterVO;
import nc.vo.fbm.register.RegisterVO;
import nc.vo.fbm.util.BillRangeTools;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.SuperVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.vo.tmpub.util.StringUtil;
import nc.vo.tmpub.util.TMCurrencyUtil;
import nccloud.itf.fbm.gather.GatherConstant;
import nccloud.itf.fbm.pub.IFBMCommonService4NCC;
import nccloud.pubitf.tmpub.pub.ITMPrecisionServiceForNCC;
import nccloud.vo.tmpub.precison.PrecisionField;
import org.apache.commons.codec.binary.StringUtils;

public class FbmCommonTools {
	private static final IFBMCommonService4NCC commonService = (IFBMCommonService4NCC) NCLocator.getInstance()
			.lookup(IFBMCommonService4NCC.class);

	public FbmCommonTools() {
	}

	public static boolean isEBill(String pk_fbmBilltype) {
		return commonService.isEBill(pk_fbmBilltype);
	}

	public static boolean isEBillByPk_register(String pk_register) {
		try {
			return commonService.isEcdNoteClass(pk_register);
		} catch (BusinessException var2) {
			BusinessException e = var2;
			ExceptionUtils.wrapException(e);
			return false;
		}
	}

	public static boolean isBankType(String pk_fbmBilltype) {
		return commonService.isBankType(pk_fbmBilltype);
	}

	public static boolean isBusType(String pk_fbmBilltype) {
		return commonService.isBusType(pk_fbmBilltype);
	}

	public static String getfbmbilltypeByNo(String fbmbillno) throws BusinessException {
		String flag = null;
		String type = null;
		boolean isEbill = true;
		if (fbmbillno.length() == 16) {
			flag = String.valueOf(fbmbillno.charAt(6));
			isEbill = false;
		} else if (fbmbillno.length() == 30) {
			flag = String.valueOf(fbmbillno.charAt(0));
		}

		Objects.requireNonNull(flag);
		if (isEbill) {
			switch (flag) {
			case "5" -> type = "FBMTZ6E0000000000003";
			case "6" -> type = "FBMTZ6E0000000000004";
			case "1" -> type = "FBMTZ6E0000000000003";
			case "2" -> type = "FBMTZ6E0000000000004";
			}
		} else {
			switch (flag) {
			case "5" -> type = "FBMTZ6E0000000000001";
			case "6" -> type = "FBMTZ6E0000000000002";
			}
		}

		return type;
	}

	public static void setSubcontractcirculation(RegisterVO registerVO, String fbmbilltype) throws MetaDataException {
		if (!StringUtil.isNull(fbmbilltype)) {
			String condtion = "PK_NOTETYPE = '" + fbmbilltype + "'";
			Collection<NotetypeVO> collection = ((IMDPersistenceQueryService) NCLocator.getInstance()
					.lookup(IMDPersistenceQueryService.class)).queryBillOfVOByCond(NotetypeVO.class, condtion, false);
			if (collection != null && collection.size() > 0) {
				NotetypeVO notetypeVO = (NotetypeVO) collection.iterator().next();
				if (notetypeVO.getNoteclass() == 9) {
					registerVO.setSubcontractcirculation(UFBoolean.TRUE);
					registerVO.setBillrangestart("000000000000");
					registerVO.setBillrangeend("000000000000");
				} else if (notetypeVO.getNoteclass() == 2) {
					registerVO.setSubcontractcirculation(UFBoolean.FALSE);
				}
			}
		}

	}

	public static String getPKBilltypeByCode(String billtypeCode) throws Exception {
		return commonService.getPKBilltypeByCode(billtypeCode);
	}

	public static String getCodeByPKBilltype(String pk_billtype) throws BusinessException {
		return commonService.getCodeByPKBilltype(pk_billtype);
	}

	public static void billNoValication(String fbmbillno) throws BusinessException {
		if (fbmbillno != null && fbmbillno.length() != 0) {
			Pattern blankCheck = Pattern.compile("\\s*|\t|\r|\n");
			if (blankCheck.matcher(fbmbillno) == null) {
				throw new BusinessException(
						fbmbillno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--186"));
			} else {

				// zhangxinah 去掉收票登记导入时票据长度校验
				// checkFbmbillnoLength(fbmbillno);
				if (fbmbillno.length() > 16) {
					eBillNoCheck(fbmbillno);
				} else {
					paperBillCheck(fbmbillno);
				}

			}
		} else {
			throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--187"));
		}
	}

	public static void checkFbmbillnoLength(String fbmbillno) throws BusinessException {
		if (fbmbillno.length() != 30 && fbmbillno.length() != 16) {
			throw new BusinessException(
					fbmbillno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--185"));
		}
	}

	public static void checkFbmbillnoByIsEbill(String fbmbillno, boolean isEbill) throws BusinessException {
		if (isEbill) {
			if (fbmbillno != null && fbmbillno.length() != 30) {
				throw new BusinessException(
						NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--185"));
			}
		} else if (fbmbillno != null && fbmbillno.length() != 16) {
			throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--185"));
		}

	}

	private static void paperBillCheck(String fbmbillno) throws BusinessException {
		Pattern pattern = Pattern.compile("\\d{16}");
		Boolean ismatch = pattern.matcher(fbmbillno).matches();
		if (!ismatch) {
			throw new BusinessException(
					fbmbillno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--184"));
		} else {
			String bankCode = fbmbillno.substring(0, 3);
			String provinceCode = fbmbillno.substring(4, 6);
			String billtypeCode = fbmbillno.substring(6, 7);
			String printCode = fbmbillno.substring(7, 8);
			if (!"00".equals(provinceCode)) {
				throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--183",
						(String) null, new String[] { String.valueOf(fbmbillno) }));
			} else if (!"5".equals(billtypeCode) && !"6".equals(billtypeCode)) {
				throw new BusinessException(
						fbmbillno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--182"));
			} else if ("6".equals(billtypeCode) && !"001".equals(bankCode)) {
				throw new BusinessException(
						fbmbillno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--181"));
			} else if ("7".equals(printCode) || "8".equals(printCode) || "9".equals(printCode)) {
				throw new BusinessException(
						fbmbillno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--180"));
			}
		}
	}

	private static void eBillNoCheck(String fbmno) throws BusinessException {
		Pattern pattern = Pattern.compile("\\d{30}");
		Boolean ismatch = pattern.matcher(fbmno).matches();
		// 电票不校验票据号长度
		ismatch = true;
		if (!ismatch) {
			throw new BusinessException(
					fbmno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--179"));
		} else {
			String firstNum = fbmno.substring(0, 1);
			if (!"2".equals(firstNum) && !"1".equals(firstNum) && !"5".equals(firstNum) && !"6".equals(firstNum)) {
				throw new BusinessException(
						fbmno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd--178"));
			} else {
				int year = Integer.parseInt(fbmno.substring(13, 17));
				int month = Integer.parseInt(fbmno.substring(17, 19));
				int day = Integer.parseInt(fbmno.substring(19, 21));
				if (month > 0 && month <= 12) {
					if (year % 4 == 0 && month == 2 && (day <= 0 || day > 29)) {
						throw new BusinessException(
								fbmno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd-0243"));
					} else if (year % 4 == 0 || month != 2 || day > 0 && day <= 28) {
						Integer[] bigMonthArray = new Integer[] { 1, 3, 5, 7, 8, 10, 12 };
						Integer[] smallMonthArray = new Integer[] { 4, 6, 9, 11 };
						List<Integer> bigMonth = Arrays.asList(bigMonthArray);
						List<Integer> smallMonth = Arrays.asList(smallMonthArray);
						if (!bigMonth.contains(month) || day > 0 && day <= 31) {
							if (smallMonth.contains(month) && (day <= 0 || day > 30)) {
								throw new BusinessException(fbmno
										+ NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd-0243"));
							}
						} else {
							throw new BusinessException(fbmno
									+ NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd-0243"));
						}
					} else {
						throw new BusinessException(
								fbmno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd-0243"));
					}
				} else {
					throw new BusinessException(
							fbmno + NCLangRes4VoTransl.getNCLangRes().getStrByID("3618nccd_0", "03618nccd-0243"));
				}
			}
		}
	}

	public String getBankdocPkByAccessorAndName(String pk_org, String name) throws BusinessException {
		return commonService.getBankdocPkByAccessorAndName(pk_org, name);
	}

	public String getAssLineNumPkByAccessorAndName(String pk_org, String name) throws BusinessException {
		return commonService.getAssLineNumPkByAccessorAndName(pk_org, name);
	}

	public String getBankaccsubPkByAccessorAndName(String name, String pk_curr) throws BusinessException {
		return commonService.getBankaccsubPkByAccessorAndName(name, pk_curr);
	}

	public static AbstractBill resetProcessAmounts(AggregatedValueObject aggregvo, AbstractBill destAggvo,
			List<PrecisionField> headPrecisionFields,
			Map<Class<? extends ISuperVO>, List<PrecisionField>> bodyPrecisionFields, String errmsg)
			throws BusinessException {
		if (destAggvo == null) {
			return null;
		} else {
			new Object();
			new Object();
			new Object();
			new Object();
			SuperVO destHeadVO = (SuperVO) destAggvo.getParentVO();
			Object olcratecategoryObj;
			Object olcratedate;
			Object olcratetype;
			Object olcrate;
			if (aggregvo != null) {
				SuperVO srcHeadVO = (SuperVO) aggregvo.getParentVO();
				olcratecategoryObj = srcHeadVO.getAttributeValue("olcratecategory");
				olcratedate = srcHeadVO.getAttributeValue("olcratedate");
				olcratetype = srcHeadVO.getAttributeValue("olcratetype");
				olcrate = srcHeadVO.getAttributeValue("olcrate");
			} else {
				olcratecategoryObj = destHeadVO.getAttributeValue("olcratecategory");
				olcratedate = destHeadVO.getAttributeValue("olcratedate");
				olcratetype = destHeadVO.getAttributeValue("olcratetype");
				olcrate = destHeadVO.getAttributeValue("olcrate");
			}

			String billtype = (String) destHeadVO.getAttributeValue("pk_billtypecode");
			Integer olcratecategory = null;
			if (olcratecategoryObj != null) {
				olcratecategory = (Integer) olcratecategoryObj;
			} else if (olcratecategoryObj == null && olcratetype != null) {
				IRateTypePubService service = (IRateTypePubService) NCLocator.getInstance()
						.lookup(IRateTypePubService.class);
				RateTypeVO[] rateTypeVOs = service.queryRateTypeByPKs(new String[] { (String) olcratetype });
				if (rateTypeVOs != null && rateTypeVOs.length != 0) {
					olcratecategory = rateTypeVOs[0].getRate_category();
				}
			}

			destHeadVO.setAttributeValue("olcratetype", olcratetype);
			boolean isResetRate = true;
			if (olcratecategory != null && TMCurrencyUtil.judgeRatecategory(olcratecategory)) {
				destHeadVO.setAttributeValue("olcratedate", olcratedate);
				destHeadVO.setAttributeValue("olcratecategory", olcratecategoryObj);
				destHeadVO.setAttributeValue("olcrate", olcrate);
				if (!StringUtil.isNull(billtype) && (billtype.equals("36HA") || billtype.equals("36HK"))) {
					destHeadVO.setAttributeValue("olcbrate", olcrate);
				}

				FbmPublicUtil.copyRateTyefieldsHead2Body(destAggvo);
				isResetRate = false;
			}

			AbstractBill[] returnAggvos = ((ITMPrecisionServiceForNCC) NCLocator.getInstance()
					.lookup(ITMPrecisionServiceForNCC.class)).processAggVOPrecision(new AbstractBill[] { destAggvo },
							headPrecisionFields, bodyPrecisionFields, isResetRate, true);
			boolean notHasOlcrate = false;
			if (!StringUtil.isNull(billtype) && (billtype.equals("36HA") || billtype.equals("36HK"))) {
				if (StringUtil.isNull(destAggvo.getParent().getAttributeValue("olcbrate"))) {
					notHasOlcrate = true;
				}
			} else if (StringUtil.isNull(destAggvo.getParent().getAttributeValue("olcrate"))) {
				notHasOlcrate = true;
			}

			if (notHasOlcrate) {
				throw new BusinessException(errmsg);
			} else {
				return returnAggvos[0];
			}
		}
	}

	public static void validRepeatBill(RegisterVO registerVO) throws BusinessException {
		String fbmBillNo = registerVO.getFbmbillno();
		if (registerVO.getSubcontractcirculation() != null
				&& !registerVO.getSubcontractcirculation().equals(UFBoolean.FALSE)
				&& registerVO.getPk_register() == null) {
			Integer billrangestart = BillRangeTools.handleBillRange(registerVO.getBillrangestart());
			Integer billrangeend = BillRangeTools.handleBillRange(registerVO.getBillrangeend());
			String condition = "FBMBILLNO = '" + fbmBillNo + "'";
			Collection<AggRegisterVO> collections = ((IMDPersistenceQueryService) NCLocator.getInstance()
					.lookup(IMDPersistenceQueryService.class))
					.queryBillOfVOByCond(AggRegisterVO.class, condition, false);
			if (collections != null && collections.size() > 0) {
				Iterator var6 = collections.iterator();

				while (var6.hasNext()) {
					AggRegisterVO aggAllocateAgreeVO = (AggRegisterVO) var6.next();
					RegisterVO register = aggAllocateAgreeVO.getparentVO();
					if (billrangestart != null && billrangeend != null && register.getBillrangeend() != null
							&& register.getBillrangestart() != null
							&& (BillRangeTools.handleBillRange(register.getBillrangeend()) <= billrangeend
									&& BillRangeTools.handleBillRange(register.getBillrangeend()) >= billrangestart
									|| BillRangeTools.handleBillRange(register.getBillrangestart()) >= billrangestart
											&& BillRangeTools
													.handleBillRange(register.getBillrangestart()) <= billrangeend)) {
						throw new BusinessException(
								"票据号码" + fbmBillNo + "子票区间" + billrangestart + "-" + billrangeend + "已存在");
					}
				}
			}

		}
	}

	public static void validateAmount(RegisterVO registerVO) throws BusinessException {
		if (registerVO.getSubcontractcirculation() != null
				&& !registerVO.getSubcontractcirculation().equals(UFBoolean.FALSE)) {
			Integer billrangestart = BillRangeTools.handleBillRange(registerVO.getBillrangestart());
			Integer billrangeend = BillRangeTools.handleBillRange(registerVO.getBillrangeend());
			UFDouble money = registerVO.getMoney();
			if (billrangestart != null && billrangeend != null) {
				Integer total = billrangeend - billrangestart + 1;
				UFDouble count = (new UFDouble("0.01")).multiply((double) total);
				if (count.compareTo(money) != 0) {
					throw new BusinessException("票据号码" + registerVO.getFbmbillno() + "子票区间" + billrangestart + "-"
							+ billrangeend + "的金额" + count.toString() + "与票据金额不相等，请修改");
				}
			}

		}
	}

	public static void setBillRange(RegisterVO registerVO) {
		Boolean isSplitAllowed = registerVO.getSubcontractcirculation() != null
				&& registerVO.getSubcontractcirculation().booleanValue();
		if (isSplitAllowed) {
			String var10001 = registerVO.getBillrangestart();
			registerVO.setBillrange(var10001 + "-" + registerVO.getBillrangeend());
		} else if (ifBelongNewBillSystem(registerVO.getFbmbillno())) {
			registerVO.setBillrange("0");
		}

	}

	private static ITMPrecisionServiceForNCC getPrecisionService() {
		return (ITMPrecisionServiceForNCC) NCLocator.getInstance().lookup(ITMPrecisionServiceForNCC.class);
	}

	private static RegisterVO calcMoneyPrecision(RegisterVO registerVO, UFDouble money) throws BusinessException {
		RegisterVO newRegisterVO = new RegisterVO();
		newRegisterVO.setOlcmoney(registerVO.getOlcmoney());
		newRegisterVO.setOlcrate(registerVO.getOlcrate());
		newRegisterVO.setGlcmoney(registerVO.getGlcmoney());
		newRegisterVO.setGlcrate(registerVO.getGlcrate());
		newRegisterVO.setGllcmoney(registerVO.getGllcmoney());
		newRegisterVO.setGllcrate(registerVO.getGllcrate());
		newRegisterVO.setGatherdate(registerVO.getGatherdate());
		newRegisterVO.setPk_curr(registerVO.getPk_curr());
		newRegisterVO.setPaybillunit(registerVO.getPaybillunit());
		newRegisterVO.setMoney(money);
		getPrecisionService().processHeadPrecision(newRegisterVO,
				GatherConstant.getHeadPrecisionFieldsWithFieldRateType(), true);
		return newRegisterVO;
	}

	public static void validBillRange(RegisterVO registerVO, boolean isEbill) throws BusinessException {
		UFBoolean subcontractCirculation = registerVO.getSubcontractcirculation();
		if (subcontractCirculation != null && subcontractCirculation.booleanValue()) {
			label101: {
				String billRangeStart = registerVO.getBillrangestart();
				String billRangeEnd = registerVO.getBillrangeend();
				String firstNum = registerVO.getFbmbillno().substring(0, 1);
				if (isEbill && !"5".equals(firstNum) && !"6".equals(firstNum)) {
					throw new BusinessException("票据" + registerVO.getFbmbillno() + "非新一代票据，不可进行分包流转！");
				}

				if (StringUtil.isNull(billRangeStart) || StringUtil.isNull(billRangeEnd)) {
					throw new BusinessException("票据可分包流转状态下，票据子票区间开始和截止值不能为空！");
				}

				if (Long.valueOf(billRangeStart) > 0L && Long.valueOf(billRangeEnd) > 0L) {
					if (billRangeStart.length() >= 12 && billRangeEnd.length() >= 12) {
						if (Long.parseLong(billRangeEnd) < Long.parseLong(billRangeStart)) {
							throw new BusinessException("子票区间截止值不能小于开始值！");
						}

						checkMoneyFormatCorrect(registerVO.getMoney());
						Long rangeDiff = Long.parseLong(registerVO.getBillrangeend())
								- Long.parseLong(registerVO.getBillrangestart());
						UFDouble diffMoney = (new UFDouble(rangeDiff)).add(1.0).multiply(0.01);
						RegisterVO newRegisterVO = calcMoneyPrecision(registerVO, diffMoney);
						if (newRegisterVO.getMoney().doubleValue() != registerVO.getMoney().doubleValue()) {
							throw new BusinessException("票据号码" + registerVO.getFbmbillno() + "，子票区间" + billRangeStart
									+ "-" + billRangeEnd + "的金额" + newRegisterVO.getMoney().toString()
									+ "（根据公式自动计算出的金额）与票据金额不相等，请修改！");
						}

						if (!isEbill) {
							throw new BusinessException("纸票不能进行分包流转！");
						}

						if (Long.parseLong(billRangeEnd) < Long.parseLong(billRangeStart)) {
							throw new BusinessException("子票区间截止值不能小于开始值！");
						}
						break label101;
					}

					throw new BusinessException("子票区间开始值和截止值长度都必须为12位！");
				}

				throw new BusinessException("子票区间开始值和截止值都必须大于0！");
			}
		}

		boolean isBelongNewBillSys = ifBelongNewBillSystem(registerVO.getFbmbillno());
		if (!isBelongNewBillSys || subcontractCirculation != null && subcontractCirculation.booleanValue()
				|| "0".equals(registerVO.getBillrangestart()) && "0".equals(registerVO.getBillrangeend())) {
			if (isBelongNewBillSys || !StringUtil.isNotNull(registerVO.getBillrangestart())
					&& !StringUtil.isNotNull(registerVO.getBillrangeend())) {
				AggRegisterVO[] registers = ((IGatherQueryService) NCLocator.getInstance()
						.lookup(IGatherQueryService.class))
						.queryRegisterByFbmBillnoAndPkorg(registerVO.getFbmbillno(), registerVO.getPk_org());
				if (null != registers && registers.length > 0) {
					checkBillRangeRepeat(registers, registerVO);
				}

			} else {
				throw new BusinessException("非新一代票据不可进行分包流转，不可输入子票区间！");
			}
		} else {
			throw new BusinessException("票据[" + registerVO.getFbmbillno() + "]为新一代票且不可分包流转，子票区间开始和结束应为0！");
		}
	}

	public static String checkBillRangeRepeat(AggRegisterVO[] registers, RegisterVO newRegisterVO)
			throws BusinessException {
		int var4;
		String var10002;
		if (StringUtil.isNull(newRegisterVO.getPrimaryKey())) {
			if (registers != null && registers.length == 1) {
				AggRegisterVO register = registers[0];
				RegisterVO vo = register.getparentVO();
				if (!StringUtil.isNull(vo.getBillrangestart()) && !StringUtil.isNull(vo.getBillrangeend())) {
					if (!StringUtil.isNull(newRegisterVO.getBillrangestart())
							&& !StringUtil.isNull(newRegisterVO.getBillrangeend())) {
						if (ifIncludesInterval(register, newRegisterVO)) {
							var10002 = register.getparentVO().getFbmbillno();
							throw new BusinessException(
									"票据号码" + var10002 + "，子票区间" + register.getparentVO().getBillrangestart() + "-"
											+ register.getparentVO().getBillrangeend()
											+ "已存在，请勿重复录入，请在票据台账节点查询该票据号对应单据是否处理完成！");
						}

						return null;
					}

					throw new BusinessException("票据号[" + newRegisterVO.getFbmbillno() + "]已存在，且为等分票据，不可再登记非等分票据信息！");
				}

				if (!StringUtil.isNull(newRegisterVO.getBillrangestart())
						&& !StringUtil.isNull(newRegisterVO.getBillrangeend())) {
					return "票据号[" + newRegisterVO.getFbmbillno() + "]已存在，且为非等分票据，不可再登记等分票据信息！";
				}

				throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("3618fbm_0", "03618fbm-0598",
						(String) null, new String[] { newRegisterVO.getFbmbillno() }));
			} else if (registers != null && registers.length > 1) {
				if (StringUtil.isNull(newRegisterVO.getBillrangestart())
						|| StringUtil.isNull(newRegisterVO.getBillrangeend())) {
					throw new BusinessException("票据号[" + newRegisterVO.getFbmbillno() + "]已存在，且为等分票据，不可再登记非等分票据信息！");
				}

				AggRegisterVO[] var7 = registers;
				int var9 = registers.length;

				for (var4 = 0; var4 < var9; ++var4) {
					AggRegisterVO vo = var7[var4];
					if (ifIncludesInterval(vo, newRegisterVO)) {
						var10002 = vo.getparentVO().getFbmbillno();
						throw new BusinessException("票据号码" + var10002 + "，子票区间" + vo.getparentVO().getBillrangestart()
								+ "-" + vo.getparentVO().getBillrangeend() + "已存在，请勿重复录入，请在票据台账节点查询该票据号对应单据是否处理完成！");
					}
				}
			}
		} else {
			if (registers == null || registers.length == 0) {
				throw new BusinessException("修改失败，票据" + newRegisterVO.getFbmbillno() + "数据不存在！");
			}

			if (registers != null && registers.length > 1) {
				if (newRegisterVO.getSubcontractcirculation() != null
						&& !newRegisterVO.getSubcontractcirculation().booleanValue()) {
					throw new BusinessException("票据号码 " + newRegisterVO.getFbmbillno() + "作为等分票据已拆包，无法再变更为非等分票据！");
				}

				boolean intervalRepeat = false;
				AggRegisterVO[] var3 = registers;
				var4 = registers.length;

				for (int var5 = 0; var5 < var4; ++var5) {
					AggRegisterVO vo = var3[var5];
					if (StringUtil.isNull(vo.getparentVO().getBillrangestart())
							|| StringUtil.isNull(vo.getparentVO().getBillrangeend())) {
						throw new BusinessException(
								"修改失败，票据 " + newRegisterVO.getFbmbillno() + "既存在等分票据数据，又存在非等分票据数据！");
					}

					if (vo.getparentVO().getPk_register().equals(newRegisterVO.getPk_register())) {
						if (vo.getparentVO().getBillrangestart().equals(newRegisterVO.getBillrangestart())
								&& vo.getparentVO().getBillrangeend().equals(newRegisterVO.getBillrangeend())) {
							intervalRepeat = false;
						}
					} else if (ifIncludesInterval(vo, newRegisterVO)) {
						intervalRepeat = true;
					}
				}

				if (intervalRepeat) {
					var10002 = newRegisterVO.getFbmbillno();
					throw new BusinessException("票据号码" + var10002 + "，子票区间" + newRegisterVO.getBillrangestart() + "-"
							+ newRegisterVO.getBillrangeend() + "已存在，请勿重复录入，请在票据台账节点查询该票据号对应单据是否处理完成！");
				}
			}
		}

		return null;
	}

	private static boolean ifIncludesInterval(AggRegisterVO aggRegisterVO, RegisterVO newRegisterVO) {
		boolean flag = false;
		if (Long.parseLong(aggRegisterVO.getparentVO().getBillrangestart()) <= Long
				.parseLong(newRegisterVO.getBillrangeend())
				&& Long.parseLong(aggRegisterVO.getparentVO().getBillrangeend()) >= Long
						.parseLong(newRegisterVO.getBillrangeend())) {
			flag = true;
		}

		if (Long.parseLong(aggRegisterVO.getparentVO().getBillrangestart()) <= Long
				.parseLong(newRegisterVO.getBillrangestart())
				&& Long.parseLong(aggRegisterVO.getparentVO().getBillrangeend()) >= Long
						.parseLong(newRegisterVO.getBillrangestart())) {
			flag = true;
		}

		if (Long.parseLong(newRegisterVO.getBillrangestart()) <= Long
				.parseLong(aggRegisterVO.getparentVO().getBillrangeend())
				&& Long.parseLong(newRegisterVO.getBillrangeend()) >= Long
						.parseLong(aggRegisterVO.getparentVO().getBillrangeend())) {
			flag = true;
		}

		if (Long.parseLong(newRegisterVO.getBillrangestart()) <= Long
				.parseLong(aggRegisterVO.getparentVO().getBillrangestart())
				&& Long.parseLong(newRegisterVO.getBillrangeend()) >= Long
						.parseLong(aggRegisterVO.getparentVO().getBillrangestart())) {
			flag = true;
		}

		if (Long.parseLong(aggRegisterVO.getparentVO().getBillrangestart()) >= Long
				.parseLong(newRegisterVO.getBillrangestart())
				&& Long.parseLong(aggRegisterVO.getparentVO().getBillrangeend()) <= Long
						.parseLong(newRegisterVO.getBillrangeend())) {
			flag = true;
		}

		if (Long.parseLong(newRegisterVO.getBillrangestart()) >= Long
				.parseLong(aggRegisterVO.getparentVO().getBillrangestart())
				&& Long.parseLong(newRegisterVO.getBillrangeend()) <= Long
						.parseLong(aggRegisterVO.getparentVO().getBillrangeend())) {
			flag = true;
		}

		return flag;
	}

	public static Long checkMoneyFormatCorrect(UFDouble money) throws BusinessException {
		if (money == null) {
			return 0L;
		} else {
			UFDouble standardAmountMultiplier = money.multiply(100.0);
			long roundmum = standardAmountMultiplier.longValue();
			if (!(standardAmountMultiplier.sub(new UFDouble(roundmum)).doubleValue() > 0.0)
					&& !(standardAmountMultiplier.sub(new UFDouble(roundmum)).doubleValue() < 0.0)) {
				return roundmum;
			} else {
				throw new BusinessException("等分票据的票据金额要等于标准金额的整数倍，请修改！");
			}
		}
	}

	public static String formatBillRangeValue(String rangeValue) {
		if (StringUtil.isNull(rangeValue)) {
			return "000000000001";
		} else if (rangeValue.length() != 12 && rangeValue.length() <= 12) {
			long needSupplyCharacterLeng = (long) (12 - rangeValue.length());

			StringBuilder builder;
			for (builder = new StringBuilder(); needSupplyCharacterLeng > 0L; --needSupplyCharacterLeng) {
				builder.append("0");
			}

			builder.append(rangeValue);
			return builder.toString();
		} else {
			return rangeValue;
		}
	}

	public static String getBillSysTypeByFbmBillNo(String fbmBillNo) {
		if (StringUtil.isNull(fbmBillNo)) {
			return null;
		} else {
			String firstNum = fbmBillNo.substring(0, 1);
			return ("5".equals(firstNum) || "6".equals(firstNum)) && fbmBillNo.length() == 30 ? "CS02" : "CS01";
		}
	}

	public static boolean ifBelongNewBillSystem(String fbmBillNo) {
		String billsystype = getBillSysTypeByFbmBillNo(fbmBillNo);
		return "CS02".equals(billsystype);
	}

	public static boolean ifRangeMatched(CircularlyAccessibleValueObject one, CircularlyAccessibleValueObject other) {
		String oneStart = (String) one.getAttributeValue("billrangestart");
		String oneEnd = (String) one.getAttributeValue("billrangeend");
		String otherStart = (String) other.getAttributeValue("billrangestart");
		String otherEnd = (String) other.getAttributeValue("billrangeend");
		return StringUtils.equals(oneStart, otherStart) && StringUtils.equals(oneEnd, otherEnd);
	}
}
