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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package com.changhong.epc.form.execute.impl;
 
import java.util.*;
 
import javax.annotation.Resource;
 
import com.changhong.autoform.entity.ProcessBudget;
import com.changhong.epc.bean.form.FormType;
import com.changhong.epc.form.execute.BudgetRecord;
import com.changhong.epc.form.execute.IFormOthersOperation;
import com.changhong.epc.form.execute.IProcessBudgetBack;
import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
 
import lombok.Data;
 
@Data
public class MainLogic {
    
    @Resource
    private IFormOthersOperation formOthersOperation = SpringUtil.getBean(IFormOthersOperation.class);
    
    // @Resource
    private IProcessBudgetBack processBudgetBack;
    
    private ProcessBudget processBudget;
    
    private Map<FormType, List<Integer>> pbs;
    
    private List<Double> pzMoney;
    private List<Double> pzMoneynew;
    private List<BudgetRecord> pdrs;
    
    private Double xyMoney = 0D;
    
    private Boolean flog = true;
    
    private List<BudgetRecord> ySs;
    private List<BudgetRecord> budgetRecords;
    
    private FormType billType;
    
    
    
    public void initData(ProcessBudget processBudget){
        pbs = new HashMap<>();
        for(FormType type : FormType.values()){
            pbs.put(type, new ArrayList<>());
        }
        pdrs = new ArrayList<>();
        pzMoney = new ArrayList<>();
        this.processBudget = processBudget;
    }
    
    public boolean addOfType(FormType formType, List<Integer> pds){
        if (pbs.get(formType).size() > 0){
            return false;
        }
        pbs.get(formType).addAll(pds);
        
        return true;
    }
    
    /**
     * 查找
     * @param formType
     * @return
     */
    public Integer findOfTypeEques(FormType formType){
        if(FormType.YS == formType){
            if(pbs.get(formType).size() > 0){
                Integer processBudget = pbs.get(formType).get(0);
                pbs.get(FormType.YS).remove(processBudget);
                return processBudget;
            }
        }
        return null;
    }
    
    public List<Integer> getYsDataRowNumbers(){
        return Optional.ofNullable(pbs.get(FormType.YS)).orElse(Collections.EMPTY_LIST);
    }
    
    public void exeLogic(){
        // 1. 预运行一次
        try {
            executePrimary(processBudget);
        } catch (Exception e) {
        }
        double  sum = 0D;
        pzMoneynew = new ArrayList<>(pzMoney);
        for(double s : pzMoney){
            sum = sum + s;
        }
//        if(xyMoney != sum){
//            throw new IEMRuntimeException("E001", "配置错误,配置金额与报销金额不符合!");
//        }
        
        splittingData(); 
        
        
        // 3. 没有一次出现异常,执行 真正的回写预算
        for (BudgetRecord budgetRecord : pdrs) {
            updateForm(budgetRecord);
        }
            
    }
    
    
    
    
 
    /**
     * 
     */
    private void splittingData() {
        // budgetRecords
        int i = 0;
        for(BudgetRecord brys : ySs){
            budgetRecords.remove(budgetRecords.size()-1);
            Double money = pzMoneynew.get(i);
            BudgetRecord budgetRecord = budgetRecords.get(budgetRecords.size()-1);
            budgetRecord.getMoney().money = money;
            budgetRecords.add(brys);
            
            executeProcessBudget(budgetRecords);
            
            i ++;
        }
    }
 
    public void setXyMoney(Double money) {
        if(flog){
            xyMoney = money;
        }
    }
    
    public void setPzMoney(List<String> obs){
        if(flog){
            obs.forEach(s->pzMoney.add(Double.parseDouble(s)));
        }
        flog = false;
    }
    
    public Integer loopNumber(){
        return pbs.get(FormType.YS).size();
    }
    
    public Double getEachMoney(){
        if(pzMoneynew == null || pzMoneynew.size() == 0){
            return 0D;
        }
        Double double1 = pzMoneynew.get(0);
        pzMoneynew.remove(0);
        return double1;
    }
    
    /**
     * 预运行一次
     * @param processBudget
     */
    public void executePrimary(ProcessBudget processBudget) {
        // formOthersOperation.getMoneyCpnMdCode();
        
        budgetRecords = new ArrayList<>();
        processBudgetBack = new ProcessBudgetBack(this);
    
        /**
         * FormType.BX
         * formOthersOperation.getBillType(processBudget)
         */
        billType = formOthersOperation.getBillType(processBudget);
        processBudget.setCostType(billType.toString());
        switch (billType) {
        // A.预算 
        case YS:
            processBudgetBack.exeBudget(budgetRecords, processBudget);
            
            // System.out.println(budgetRecords);
            break;
        
        // B.申请
        case SQ:
            processBudgetBack.exeApply(budgetRecords, processBudget);
            break;
            
        // 报销
        case BX:
            processBudgetBack.exeExpense(budgetRecords, processBudget);
            break;
        
        default:
            break;
        }    
        ySs = new ArrayList<>();
        ProcessBudget atProcessBudget = budgetRecords.get(budgetRecords.size() -1).getAtProcessBudget();
        
        for(Integer dataRowNumber : getYsDataRowNumbers()){
            ProcessBudget obj = JSONTool.toObj(JSONTool.toJson(atProcessBudget),ProcessBudget.class );
            obj.setDataRowNUm(dataRowNumber);
            processBudgetBack.exeBasics(ySs, obj);
        }
        
    }
    
 
    
    /**
     * 真正开始执行业务逻辑
     * @param budgetRecords2
     */
    public void executeProcessBudget(List<BudgetRecord> bRs) {
        List<BudgetRecord> budgetRecords = JSONTool.toList(JSONTool.toJson(bRs), BudgetRecord.class);
        switch (billType) {
        
        // A.预算 
        case YS:
 
            if(budgetRecords.size() == 1){
                throw new IEMRuntimeException("C001", "没有上级预算!");
            }
            BudgetRecord budgetRecord = budgetRecords.get(0);
            // System.out.println("预算A的金额:" + budgetRecord.getMoney().money);
            BudgetRecord budgetRecord2 = budgetRecords.get(1);
            // System.out.println("预算B的金额:" + budgetRecord2.getMoney().money);
            budgetRecord2.getMoney().money = budgetRecord2.getMoney().money - budgetRecord.getMoney().money;
            if(budgetRecord2.getMoney().money < 0){
                throw new IEMRuntimeException("C001", "上级预算余额不足!");
            }
            setUpdateForm(budgetRecord2,budgetRecord2.getMoney().money );
            break;
        
        // B.申请
        case SQ:
            if(budgetRecords.size() == 1){
                // 没有上级报销
            }
            BudgetRecord b1 = budgetRecords.get(0);
            BudgetRecord b2 = budgetRecords.get(1);
            b2.getMoney().money = b2.getMoney().money - b1.getMoney().money;
            if(b2.getMoney().money < 0){
                throw new IEMRuntimeException("C001", "申请金额失败, 预算余额不足!");
            }
            // 正常
            setUpdateForm(b2, b2.getMoney().money);
            break;
            
        // 报销
        case BX:
            Double money;
            if(budgetRecords.size() == 1){
                // 没有上级报销
                throw new IEMRuntimeException("C001", "没有上级预算!");
            }
            BudgetRecord b01 = budgetRecords.get(0);
            BudgetRecord b02 = budgetRecords.get(1);
            BudgetRecord b03 = null;
            try {
                b03 = budgetRecords.get(2);
                if(b03 == null){
                    throw new IEMRuntimeException("C002", "其他异常请找程序员询问。");
                }
                money = (b03.getMoney().money + b02.getMoney().money)- b01.getMoney().money;
                if(money < 0){
                    throw new IEMRuntimeException("C001", "预算余额不足!");
                }
                setUpdateForm(b03,money);
                break;
            } catch (Exception e) {
            }
            
//            if(b02.getType() == FormType.SQ){
                
                // 正常
                
    //        }
            
            money = b02.getMoney().money - b01.getMoney().money;
            if(money < 0){
                throw new IEMRuntimeException("C001", "预算余额不足!");
            }
            // 正常
            setUpdateForm(b02,money);
            break;
        
        default:
            break;
        }
    }
    
    
    
    public void  setUpdateForm(BudgetRecord budgetRecord, Double money){
        //budgetRecord.getAtProcessBudget().setFormID("80b0627f825ef169x9da7646eb0847c0");
//        if(budgetRecord.getMoney().alias == null){
//            budgetRecord.getMoney().alias = "预算金额";
//        }
        budgetRecord.getMoney().money = money;
        pdrs.add(budgetRecord);
    }
    
    public void updateForm(BudgetRecord budgetRecord){
        // System.out.println(budgetRecord.getAtProcessBudget().getDataRowNUm());
        formOthersOperation.updateFormFieldProperty(budgetRecord.getAtProcessBudget(), budgetRecord.getMoney());            
    }
    
    
}