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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
    }
 
}