package com.changhong.epc.count.service.count.logic.impl; import com.changhong.epc.bean.count.msg.AppStanderMsg; import com.changhong.epc.bean.tenant.bill.IOCRBill; import com.changhong.epc.bean.tenant.norm.extend.AppStanderExtend; import com.changhong.epc.bean.tenant.norm.extend.SuppStanderExtend; import com.changhong.epc.bean.tenant.norm.way.EleType; import com.changhong.epc.bean.tenant.norm.way.Way; import com.changhong.epc.constter.parsing.bill.ConstBill; import com.changhong.epc.count.service.count.data.condition.ICountConditionIter; import com.changhong.epc.count.service.count.data.paramiter.IParamIter; import com.changhong.epc.count.service.count.execution.ICounter; import com.changhong.epc.count.service.count.logic.ICountLogic; import com.changhong.epc.count.service.count.model.CountNormErrStart; import com.changhong.epc.count.service.count.model.CountResult; import com.changhong.epc.count.service.count.model.UnitInfo; import com.changhong.epc.count.service.exception.IEMRuntimeMasterException; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Objects; /** * 计算费用 逻辑类 * * @ClassName: CountLogicImpl * @author [九鼎联合科技] * @date 2017年9月1日 下午9:48:26 */ @Slf4j public class CountLogicImpl implements ICountLogic, ConstBill { @Getter private CountNormErrStart countNormErrStart; // 补助标准金额 private Double recordNormMoney = 0D; /** * 标准金额 */ private Double sAmount; /** * 补助标准迭代器 */ private ICountConditionIter icountCondition; protected IParamIter iparml; private SuppStanderExtend currGroup; /** * 计算返回值 */ private CountResult countResult; /** * 当前计算的票据信息 */ protected IOCRBill iocrBill; /** * 是否计算成功 */ private boolean success; public CountLogicImpl(){ this.success = false; } protected void initData(IOCRBill iocrBill){ this.success = false; this.iocrBill = iocrBill; } /** * 逻辑 */ @Override public CountResult countLogic(IParamIter iparm, ICountConditionIter icountCondition){ this.countNormErrStart = new CountNormErrStart(); this.iparml = iparm; this.icountCondition = icountCondition; this.currGroup = icountCondition.getCurrGroup(); // 定额包干 if(currGroup.getWay() == Way.DEBG){ initData(null); return setAllBillMoney(countGroup()); }else{ List bills = iparm.current().getBillInfo(); // 无票据 if(ObjectUtil.empty(bills)){ initData(null); return getEmptyResult(); } CountResult result = null; for(IOCRBill bill : bills){ // 票据类型,与标准不同 if(currGroup.getOverlookExpendType() == 0 && !bill.getBillByType(currGroup.getType()) && iparm.getBillType() != null){ continue; } initData(bill); // 多票据计算累加 if(result != null){ result = merge(countGroup(), result); }else { result = countGroup(); } } if(result == null){ return getEmptyResult(); } return result; } } protected CountResult countGroup(){ icountCondition.initRow(); // 移动到下一行 while(icountCondition.hasRowNext()) { icountCondition.rowNext(); countMoney(); if(isSuccess()){ return this.countResult; } } return getEmptyResult(); } /** * 合并两个计算结果集 * @param arg1 * @param arg2 * @return */ protected CountResult merge(CountResult arg1, CountResult arg2){ log.debug("合并计算结果:{},\n {}", JSONTool.toJson(arg1), JSONTool.toJson(arg2)); // 计算标准金额 arg1.setJpAmount(arg1.getJpAmount() + arg2.getJpAmount()); // 计算错误信息 List asmList = new ArrayList<>(); asmList.addAll(arg1.getErrorMsg()); asmList.addAll(arg2.getErrorMsg()); arg1.setErrorMsg(asmList); // 票据信息 if(arg1.getBillCode() != null && arg2.getBillCode() != null){ arg1.setBillCode(String.format("%s,%s", arg1.getBillCode(), arg2.getBillCode())); arg1.setBillMoney(arg1.getBillMoney() + arg2.getBillMoney()); }else if(arg2.getBillCode() != null && arg1.getBillCode() == null){ arg1.setBillCode(arg2.getBillCode()); arg1.setBillMoney(arg2.getBillMoney()); } return arg1; } // 票据之外的金额 计算后不进行累加 // 开始计算补助金额 protected void countMoney() { // 3.2 进行每一行的 维度匹配 ***** Object result = countMoneyCol(); // System.out.println("计算结果:"+result); if (result instanceof String) { setSuccess(); // return this.countResult; return; } boolean success = (boolean) result; /** * 条件、标准 都满足,没有金额,则 * */ if(success){ /* 清空错误信息 */ countNormErrStart.clearErrProp(); resultMoney( currGroup.getWay().getExpenseMoney().expense(getBillMoney(), 0D)); setSuccess(); // return this.countResult; return; } // if(countNormErrStart.isExistErr() && !icountCondition.hasRowNext()){ // getEmptyResult(); // setSuccess(); // return this.countResult; // return; // } // 3.4 没有匹配成功 封装结果 金额填充为0 // return getEmptyResult(); } private Object countMoneyCol() { Boolean success = false; /** * 2.循环一个标准-列 */ List appStanderTJ = new ArrayList<>(); List appStanderBZ = new ArrayList<>(); List appStanderJE = new ArrayList<>(); while (icountCondition.hasColNext()) { AppStanderExtend row = icountCondition.colNext(); switch (row.getEleType()) { case TJ: appStanderTJ.add(row); break; case BZ: appStanderBZ.add(row); break; case JE: appStanderJE.add(row); break; default: break; } } try { // 计算条件 if(currGroup.getWay().isContain(EleType.TJ)) { Boolean r1 = (Boolean) countMoneyColTJ(appStanderTJ); log.debug("计算条件:{}, 结果:{}", JSONTool.toJson(appStanderTJ), r1); if (r1 == false) return false; } // 计算标准 if(currGroup.getWay().isContain(EleType.BZ)) { Boolean r2 = (Boolean) countMoneyColTJ(appStanderBZ); log.debug("计算条件:{}, 结果:{}", JSONTool.toJson(appStanderBZ), r2); if (r2 == false) { countNormErrStart.notStandard(appStanderBZ, this.currGroup); return false; } } Object je = countMoneyColMoney(appStanderJE); log.debug("计算条件:{}, 结果:{}", JSONTool.toJson(appStanderJE), je); return je; } catch (Exception e) { if (e instanceof IEMRuntimeMasterException) { IEMRuntimeMasterException master = (IEMRuntimeMasterException)e; countNormErrStart.setErrType(CountNormErrStart.NOT_MASTER_BILL); countNormErrStart.addBillErr(master.getErrSystemMsg()); } } return success; } /** * 条件 标准运算 @Title: countMoneyColTJ @param @param all @param @param * sbi @param @return 设定文件 @return Object 返回类型 @throws */ private Object countMoneyColTJ(List asd) { Boolean res = true; // 上一个结果 Boolean eachTempSuccess = null; // 上一个条件 and | or Integer eachTempAsRelation = null; if (asd.size() <= 0) { return true; } for (AppStanderExtend row : asd) { //如果当前条件不满足补助标准相对应条件时,下一步 try { // res = count(row, all); /** * 公式计算 */ res = icountCondition.getExeCounter(row).exeCount(row, iparml); if (Objects.equals(row.getInverse(), INVERSE_OFF) == true) { res = !res; } } catch (Exception e) { log.error(e.getMessage(), e); res = false; } if (eachTempAsRelation != null) { if (eachTempAsRelation == RELATION_AND) res = res && eachTempSuccess; else if (eachTempAsRelation == RELATION_OR) { res = res || eachTempSuccess; } else { return res; } } eachTempSuccess = res; eachTempAsRelation = row.getRelation(); } return res; } private Object countMoneyColMoney(List asd) { if(!currGroup.getWay().isContain(EleType.JE)) { /* 清空错误信息 */ countNormErrStart.clearErrProp(); resultMoney( currGroup.getWay().getExpenseMoney().expense(getBillMoney(), 0)); return ""; } Boolean res = false; for (AppStanderExtend row : asd) { ICounter iCounter; if ((iCounter = icountCondition.getExeCounter()).exeCount(row, iparml)) { /* 清空错误信息 */ countNormErrStart.clearErrProp(); Double money; // 金额为公式 if (row.getDefType() == AS_SYSTEM_MATH) { money = iCounter.gainMoney(); } else { money = row.getMoney(); } if(currGroup.getWay() == Way.BZNXEBX){ sAmount = money; } resultMoney( currGroup.getWay().getExpenseMoney().expense(getBillMoney(), money)); /** * 退出当前补助标准 */ return ""; } else { res = false; if (row.getDefType() == MONEY_TYPE) { continue; } } } return res; } protected CountResult getNewCountResult(){ return new CountResult(); } /** * 封装结果 * @param * @return */ protected CountResult resultMoney(double money) { List asmList = new ArrayList<>(); if(countNormErrStart.isExistErr()){ asmList.add(new AppStanderMsg(currGroup.getType(), "" , new HashSet<>(countNormErrStart.getPropInfo()), countNormErrStart.getMsg())); } Way way = currGroup.getWay(); countResult = getNewCountResult(); // 报销方式 countResult.setWay(way); countResult.setRecordNormMoney(recordNormMoney); countResult.setName(currGroup.getMdName()); countResult.setBillMust(currGroup.getSubType()); countResult.setJpCdCode(currGroup.getType()); countResult.setJpAmount(money); countResult.setSAmount(sAmount); if(this.iocrBill != null) { countResult.setBillMoney(this.iocrBill.getBillMoney()); countResult.setBillCode(this.iocrBill.getBillCode()); } countResult.setErrorMsg(asmList); // countResult.setFormDay(this.iparml.current().getVal(FROM_DATE)); countResult.setSsId(String.valueOf(currGroup.getId())); return countResult; } /** * 获得空的返回值 * @return */ public CountResult getEmptyResult(){ return resultMoney(0); } // 获得票据 上的金额 protected double getBillMoney() { if(this.iocrBill == null){ return 0D; } try{ return this.iocrBill.getBillMoney(); }catch (Exception e) { return 0D; } } private Double getAllBillMoney() { List bills = iparml.current().getBillInfo(); double money = 0; if(ObjectUtil.empty(bills)){ return null; } boolean noBill = true; for (IOCRBill bill: bills) { if(currGroup != null && currGroup.getOverlookExpendType() == 0 && bill != null && bill.getBillByType(currGroup.getType())) { money += bill.getBillMoney(); noBill = false; } } if(noBill){ return null; } return money; } protected CountResult setAllBillMoney(CountResult countResult){ Double money = getAllBillMoney(); if(money != null){ countResult.setBillMoney(money); } return countResult; } protected void setSuccess(){ // System.out.println("计算成功"); this.success = true; } public boolean isSuccess() { return this.success; } }