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 lombok.extern.slf4j.Slf4j;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
|
/**
|
* 占用预算抽象类
|
*/
|
@Slf4j
|
public abstract class AbstractGrepUpdateBudget extends AbstractUpdateBudget{
|
|
private List<Budget> BudgetList = new ArrayList<>();
|
|
/**
|
* 分组循环预算和上级预算
|
* @param updateBudgetEntity
|
* @param
|
*/
|
protected void forEach(UpdateBudgetEntity updateBudgetEntity, String parentFormId, AbstractGrepUpdateBudget.BudgetGrepForEachFunction budgetGrepForEachFunction){
|
Integer parentId = getParentId(getFormInfo(updateBudgetEntity));
|
if(parentId == null){
|
log.debug("{}:{}预算占用,没找到上级预算", updateBudgetEntity.getFormId(), updateBudgetEntity.getDataRowNum());
|
return;
|
}
|
List<Map<String, Object>> childInfos = getFormChildInfos(updateBudgetEntity.getFormId() , BUDGET_SUB_FROM, updateBudgetEntity.getDataRowNum());
|
List<Map<String, Object>> parentChildInfos = getFormChildInfos(parentFormId , BUDGET_SUB_FROM, parentId);
|
Assert.condition(parentChildInfos == null, BUDGET_INVALID);
|
for (int i = 0, j = childInfos.size(); i < j; i++) {
|
Map<String, Object> childInfo = childInfos.get(i);
|
//当前占用金额
|
Double money = toDouble(childInfo.get(BUDGET_ITEM_BALANCE));
|
//业务类型
|
Object cType = childInfo.get(COST_TYPE);
|
//上级预算部门
|
Object superDept = childInfo.get(BUDGET_SUPER_DEPARTEMENT);
|
Map<String, Object> parentChildInfo = parentChildInfos
|
.stream()
|
.filter(fi-> Objects.equals(cType,fi.get(COST_TYPE)) && Objects.equals(superDept,fi.get(BUDGET_ITEM_DEPARTMENT)))
|
.findFirst().orElse(null);
|
|
// 没有余额,忽略
|
if(childInfo.get(BUDGET_ITEM_BALANCE) == null || parentChildInfo.get(BUDGET_ITEM_BALANCE) == null){
|
continue;
|
}
|
Object balance = isExist(cType,superDept,money,toDouble(parentChildInfo.get(BUDGET_ITEM_BALANCE)));
|
BudgetList.stream()
|
.filter(b->
|
balance!=null
|
&& Objects.equals(b.getCType(),cType)
|
&& Objects.equals(b.getDept(),superDept))
|
.forEach(o->{
|
parentChildInfo.put(BUDGET_ITEM_BALANCE,balance);
|
o.setMoney(toDouble(balance)-money);
|
});
|
log.debug("当前计算余额:"+ JSONTool.toJson(this.BudgetList));
|
budgetGrepForEachFunction.each(childInfo, parentChildInfo);
|
}
|
}
|
|
|
@FunctionalInterface
|
public interface BudgetGrepForEachFunction{
|
|
void each(Map<String, Object> childInfo, Map<String, Object> 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,superMoney-money));
|
return null;
|
}
|
|
}
|