zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
    }
 
}