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<Budget> 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<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);
|
// 子表单数量不同,抛出异常
|
// Assert.condition(childInfos.size() != parentChildInfos.size(), BUDGET_CHILD_SIZE);
|
for (int i = 0, j = childInfos.size(); i < j; i++) {
|
Map<String, Object> childInfo = childInfos.get(i);
|
Map<String, Object> 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<Map<String, Object>> toolInfos = getFormChildInfos(updateBudgetEntity.getFormId() , BUDGET_SUB_FROM, updateBudgetEntity.getDataRowNum());
|
List<Map<String, Object>> childInfos = getFormChildInfos(parentFormId , BUDGET_SUB_FROM, parentId);
|
Assert.condition(childInfos == null, BUDGET_INVALID);
|
Integer budgetParentId = getParentId(getFormInfo(new UpdateBudgetEntity(parentFormId, parentId)));
|
List<Map<String, Object>> 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<String, Object> toolInfo = toolInfos.get(i);
|
Map<String, Object> childInfo = childInfos.get(i);
|
Map<String, Object> 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<String, Object> 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<String, Object> childInfo, Map<String, Object> parentChildInfo);
|
|
}
|
|
@FunctionalInterface
|
public interface BudgetForEach3Function{
|
|
void each(Map<String, Object> toolInfo, 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,!this.flag?superMoney-money:superMoney+money));
|
return null;
|
}
|
|
}
|