package com.changhong.epc.form.service.budget.tool.impl; import com.changhong.epc.bean.form.Budget; import com.changhong.epc.form.service.budget.tool.entity.UpdateBudgetEntity; import com.iemsoft.framework.cloud.core.tools.Assert; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.extern.slf4j.Slf4j; import java.util.*; /** * 占用预算抽象类 */ @Slf4j public abstract class AbstractOccupyUpdateBudget extends AbstractUpdateBudget{ private List BudgetList = new ArrayList<>(); private Boolean flag; /** * 循环预算和上级预算 * @param updateBudgetEntity * @param budgetForEachFunction */ protected void forEach(UpdateBudgetEntity updateBudgetEntity, String parentFormId, BudgetForEachFunction budgetForEachFunction){ Integer parentId = getParentId(getFormInfo(updateBudgetEntity)); if(parentId == null){ log.debug("{}:{}没找到上级预算", updateBudgetEntity.getFormId(), updateBudgetEntity.getDataRowNum()); return; } List> childInfos = getFormChildInfos(updateBudgetEntity.getFormId() , BUDGET_SUB_FROM, updateBudgetEntity.getDataRowNum()); List> parentChildInfos = getFormChildInfos(parentFormId , BUDGET_SUB_FROM, parentId); Assert.condition(parentChildInfos == null, BUDGET_INVALID); // 子表单数量不同,抛出异常 // Assert.condition(childInfos.size() != parentChildInfos.size(), BUDGET_CHILD_SIZE); for (int i = 0, j = childInfos.size(); i < j; i++) { Map childInfo = childInfos.get(i); Map parentChildInfo = parentChildInfos.get(i); // 没有余额,忽略 if(childInfo.get(BUDGET_ITEM_BALANCE) == null || parentChildInfo.get(BUDGET_ITEM_BALANCE) == null){ continue; } budgetForEachFunction.each(childInfo, parentChildInfo); } } /** * 循环(追加|扩展)预算、预算、上级预算 * @param updateBudgetEntity * @param parentFormId * @param budgetForEach3Function */ protected void forEach(Boolean flag,UpdateBudgetEntity updateBudgetEntity, String parentFormId, BudgetForEach3Function budgetForEach3Function){ //判断是追加和回收,true为回收 this.flag = flag; Integer parentId = getParentId(getFormInfo(updateBudgetEntity)); if(parentId == null){ log.debug("{}:{}没找到上级预算", updateBudgetEntity.getFormId(), updateBudgetEntity.getDataRowNum()); return; } List> toolInfos = getFormChildInfos(updateBudgetEntity.getFormId() , BUDGET_SUB_FROM, updateBudgetEntity.getDataRowNum()); List> childInfos = getFormChildInfos(parentFormId , BUDGET_SUB_FROM, parentId); Assert.condition(childInfos == null, BUDGET_INVALID); Integer budgetParentId = getParentId(getFormInfo(new UpdateBudgetEntity(parentFormId, parentId))); List> parentChildInfos = Collections.EMPTY_LIST; if(budgetParentId != null){ parentChildInfos = getFormChildInfos(parentFormId , BUDGET_SUB_FROM, budgetParentId); } // 子表单数量不同,抛出异常 // Assert.condition(toolInfos.size() != childInfos.size() || (ObjectUtil.notEmpty(parentChildInfos) && childInfos.size() != parentChildInfos.size()), BUDGET_CHILD_SIZE); for (int i = 0, j = childInfos.size(); i < j; i++) { Map toolInfo = toolInfos.get(i); Map childInfo = childInfos.get(i); Map parentChildInfo = null; //当前占用金额 Double money = toDouble(toolInfo.get(BUDGET_ADDITIONALMONEY)); //业务类型 Object cType = childInfo.get(COST_TYPE); //上级预算部门 Object superDept = childInfo.get(BUDGET_SUPER_DEPARTEMENT); //取到当前上级预算 if(ObjectUtil.notEmpty(parentChildInfos)) { parentChildInfo = parentChildInfos .stream() .filter(fi-> Objects.equals(cType,fi.get(COST_TYPE)) && Objects.equals(superDept,fi.get(BUDGET_ITEM_DEPARTMENT))) .findFirst().orElse(null); // parentChildInfo = parentChildInfos.get(i); Object balance = isExist(cType,superDept,money,toDouble(parentChildInfo.get(BUDGET_ITEM_BALANCE))); Map currParentChildInfo = new HashMap<>(parentChildInfo); BudgetList.stream() .filter(b->balance!=null && Objects.equals(b.getCType(),cType) && Objects.equals(b.getDept(),superDept)) .forEach(o->{ currParentChildInfo.put(BUDGET_ITEM_BALANCE,balance); if(!this.flag) { o.setMoney(toDouble(balance) - money); }else{ o.setMoney(toDouble(balance) + money); } }); parentChildInfo = currParentChildInfo; } log.debug("当前计算余额:"+ JSONTool.toJson(this.BudgetList)); // 没有余额,忽略 if(childInfo.get(BUDGET_ITEM_BALANCE) == null || (parentChildInfo != null && parentChildInfo.get(BUDGET_ITEM_BALANCE) == null) || toolInfo.get(BUDGET_ADDITIONALMONEY) == null){ continue; } budgetForEach3Function.each(toolInfo, childInfo, parentChildInfo); } } @FunctionalInterface public interface BudgetForEachFunction{ void each(Map childInfo, Map parentChildInfo); } @FunctionalInterface public interface BudgetForEach3Function{ void each(Map toolInfo, Map childInfo, Map parentChildInfo); } public Object isExist(Object cType,Object dept,Double money,Double superMoney){ for(Budget b:BudgetList){ if(Objects.equals(b.getCType(),cType) && Objects.equals(b.getDept(),dept)){ return b.getMoney(); } } this.BudgetList.add(new Budget(cType,dept,!this.flag?superMoney-money:superMoney+money)); return null; } }