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
package com.changhong.epc.form.service.budget.department.impl;
 
import com.changhong.autoform.entity.ProcessBudget;
import com.changhong.epc.bean.form.BugetData;
import com.changhong.epc.bean.form.BugetSubForm;
import com.changhong.epc.bean.form.FormType;
import com.changhong.epc.bean.form.budget.BudgetUserDefined;
import com.changhong.epc.bean.form.budget.BudgetUserDefined.Dimension;
import com.changhong.epc.bean.form.budget.filtrate.BudgetclassifyKey;
import com.changhong.epc.bean.form.budget.filtrate.BudgetclassifyValue;
import com.changhong.epc.bean.tenant.cost.budgetlog.MoneyBudgetLog;
import com.changhong.epc.form.execute.IFormOthersOperation;
import com.changhong.epc.form.service.budget.department.SuperBudgt;
import com.changhong.epc.form.service.budget.department.model.BudgetRow;
import com.changhong.epc.form.service.budget.department.model.BudgetRowUpdateRecord;
import com.changhong.epc.form.service.budget.impl.GetConstant;
import com.iemsoft.framework.cloud.core.exception.IEMRestException;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import com.iemsoft.framework.cloud.core.tools.StringUtil;
import lombok.extern.slf4j.Slf4j;
 
import java.math.BigDecimal;
import java.util.*;
 
/** 
 * 上级预算部门
 */
@SuppressWarnings("unchecked")
@Slf4j
public class SuperBudgtDepartments extends SuperBudgt{
 
    private IFormOthersOperation formOthersOperation = SpringUtil.getBean(IFormOthersOperation.class);
    
    //当前预算
    private static final String NOWBUDGET = "nowBudget";
    //上级预算
    private static final String SUPERBUDGET = "superBudget";
    
     
    
    public static void main(String[] args) {
        String str = "{\"formID\":\"a60c5a8c444c34f1aab44a2fd8c8f076\",\"data\":{\"budget_itemCostType\":\"2017\",\"数字2\":\"0\",\"下拉框3\":[\"aaa\"],\"多行文本6\":\"fdsafasdfsdafdsfasd\",\"supperBudgetRows\":[{\"DATAROWNUM\":\"123\",\"日历7\":\"2017\"},{\"DATAROWNUM\":\"\",\"日历7\":\"2017\"},{\"DATAROWNUM\":\"\",\"日历7\":\"2018\"},{\"DATAROWNUM\":\"\",\"日历7\":\"2019\"}]},\"tenantID\":\"001\",\"userName\":\"WangYX\"}";
        // System.out.println(str);
        Map<String,Object> obj = (Map<String, Object>) JSONTool.toObj(str, Map.class).get("data");
        BudgetRow budgetRow = new BudgetRow(obj);
        // System.out.println(budgetRow);
    }
    
    /**
     * 获得可用预算
     */
    @Override
    public List<Map<String,Object>>  getUsableBudgetList() {
        
        List<Map<String,Object>> datas = new ArrayList<>();
         Collection<BudgetclassifyValue> values = data.values();
        values.forEach(o->datas.addAll(o.dispose()));
        return datas;
    }
 
    private MoneyBudgetLog moneyBudgetLog = new MoneyBudgetLog();
    
    @Override
    public boolean executeWriteBack(Map<String, Object> fromInfo, ProcessBudget processBudget,String formID) {
        List<Map<String,Object>> subData = (List<Map<String, Object>>) fromInfo.get(BUDGET_SUB_FROM);
        
        this.moneyBudgetLog.setAlterAlias(BUDGET_ITEM_BALANCE);
        this.moneyBudgetLog.setFormId(processBudget.getFormId());
        this.moneyBudgetLog.setQFormId(processBudget.getFormId());
        
        log.debug("subData: "+JSONTool.toJson(subData));
        List<BudgetRow> budgetRows =  new ArrayList<>();
        subData.forEach(o->budgetRows.add(new BudgetRow(o)));
        budgetItemBalanceType = Integer.parseInt(fromInfo.get("budgetItemBalanceType").toString());
        log.debug("List<BudgetRow>: "+JSONTool.toJson(budgetRows));
        
        // 验证每一条费用够不够扣
        List<BudgetRowUpdateRecord> verify = verify(budgetRows);
        
        
        log.debug("List<BudgetRowUpdateRecord>: "+JSONTool.toJson(verify));
        
        verify.forEach(o->{
            formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData() ,o.getFormType(),o.getIBillCostLog());
        });
        
        return true;
    }
    
    
    private List<MoneyBudgetLog> moneyBudgetLogs = new LinkedList<>();
    
    public List<BudgetRowUpdateRecord> verify(List<BudgetRow> budgetRows){
        
        log.debug("开始算钱了");
        
        List<BudgetRowUpdateRecord> budgetRowRecord = new ArrayList<>();
        if(ObjectUtil.empty(budgetRows)){
            return budgetRowRecord;
        }
        for (BudgetRow budgetRow : budgetRows) {
            //序列化复制一个对象
            MoneyBudgetLog budgetlog = JSONTool.toObj(JSONTool.toJson(this.moneyBudgetLog), MoneyBudgetLog.class);
            budgetlog.setQRowId(budgetRow.getDataRowNumber());
            BigDecimal applyMoney = new BigDecimal(budgetRow.getMoney());
            for(BudgetRow supperbudgetRow : budgetRow.getSupperBudgetRows()){
                BigDecimal balance =new BigDecimal(supperbudgetRow.getBalance());
                BigDecimal money = balance.subtract(applyMoney);
                
                log.debug("上级预算" + balance);
                log.debug("当前预算" + applyMoney);
                log.debug("计算后" + money);
                
                //记录本地log中
                budgetlog.setRowId(supperbudgetRow.getDataRowNumber());
                budgetlog.setOperatorMsg("预算占用上级预算---存子表单dataRowNum");
                budgetlog.setAlterContentOld(balance.toEngineeringString());
                budgetlog.setAlterContentNew(money.toEngineeringString());
                budgetlog.initParam();
//                this.moneyBudgetLogs.add(budgetlog);
                
                if(money.compareTo(BigDecimal.ZERO) == -1){
                    //当上级预算余额不足时,一条也不让扣
                    throw new IEMRestException("预算不足");
                    //12.7 yuezhw 注释
//                    applyMoney = applyMoney.subtract(balance);
//                    budgetRowRecord.add(new BudgetRowUpdateRecord(
//                            new ProcessBudget(
//                                    GetConstant.getFormIdGetConstant(YS_FORMID)
//                                    , supperbudgetRow.getDataRowNumber(), BUDGET_SUB_FROM)
//                            , BUDGET_ITEM_BALANCE, "0"));
                }else{
                    BudgetRowUpdateRecord param= new BudgetRowUpdateRecord(
                            new ProcessBudget(GetConstant.getFormIdGetConstant(YS_FORMID)
                                    , supperbudgetRow.getDataRowNumber(), BUDGET_SUB_FROM)
                            , BUDGET_ITEM_BALANCE, getValueByBalanceType(money) + "",budgetRow.getTenantId());
                    
                    param.setFormType(FormType.YS);
                    param.setIBillCostLog(budgetlog);
                    budgetRowRecord.add(param);
                    applyMoney = BigDecimal.ZERO;
                }
            }
            if(applyMoney.compareTo(BigDecimal.ZERO) == -1){
                throw new IEMRestException("预算不足");
            }
            
        }
        log.debug("算完了");
        return budgetRowRecord;
    }
    
    public List<BudgetRowUpdateRecord> refunds(Map<String, List<BugetSubForm>> buget){
        List<BudgetRowUpdateRecord> budgetRowRecord = new ArrayList<>();
        buget.get(NOWBUDGET).stream().forEach(now ->{
            buget.get(SUPERBUDGET).stream().forEach(suPer ->{
                if(suPer.getC_Type().equals(now.getC_Type())){
                    //将金额相加 
                    budgetRowRecord.add(new BudgetRowUpdateRecord(
                            new ProcessBudget(superFormId
                                    , Integer.parseInt(suPer.getDATAROWNUM()), BUDGET_SUB_FROM)
                                     , BUDGET_ITEM_BALANCE
                                      , getValueByBalanceType(new BigDecimal(suPer.getBudget_itemBalance()).add(new BigDecimal(now.getBudget_itemBalance()))) + ""
                                      ,"13"));
                }
            });
        });
        return budgetRowRecord;
    }
    @Override
    public void setDataSource(BudgetUserDefined budgetUserDefined,
            Map<BudgetclassifyKey,BudgetclassifyValue> data) {
        this.budgetUserDefined = budgetUserDefined;
        this.data = data;
    }
 
 
    @Override
    public boolean isMereg() {
        return false;
    }
 
    @Override
    public boolean isFilter(Map<String, Object> data) {
        return false;
    }
    
    @Override
    public BudgetUserDefined getBudgetUserDefined() {
        BudgetUserDefined budgetUserDefined = new BudgetUserDefined();
        budgetUserDefined.setFilterSubForm(false);
        budgetUserDefined.setMergeSubForm(false);
        budgetUserDefined.setKey("SuperBudgtDepartments");
        budgetUserDefined.setVal("上级部门");
        List<Dimension> mainDimension = new ArrayList<>();
        List<Dimension> subDimension = new ArrayList<>();
        
        mainDimension.add(new Dimension(BUDGET_START_DATE));
        mainDimension.add(new Dimension(BUDGET_END_DATE));
        mainDimension.add(new Dimension(CURRENCY));
        
        subDimension.add(new Dimension(BUDGET_ITEM_COST_TYPE));
        subDimension.add(new Dimension(BUDGET_ITEM_DEPARTMENT));
        
        budgetUserDefined.setMainDimension(mainDimension);
        budgetUserDefined.setSubDimension(subDimension);
        
        return budgetUserDefined;
    }
 
    @Override
    public Boolean excuteRefunds(Map<String, Object> formInfo) {
        budgetItemBalanceType = Integer.parseInt(StringUtil.toString(formInfo.get(BUDGETITEMBALANCETYPE)));
        BugetData bugetData = JSONTool.toObj(JSONTool.toJson(formInfo),BugetData.class);
        HashSet<String> costType = new HashSet<>();
        //获得业务类型
        superFormId = formInfo.get(YS_FORMID).toString();
        bugetData.getBudget_subFrom().stream().forEach(sub ->{
            costType.add(sub.getC_Type());
        });
        
        List<BugetSubForm> supersubBudget = formOthersOperation
                                                    .queryBudgetByCostTypeAndParentId(new ProcessBudget(formInfo.get("formID").toString()
                                                                ,Integer.parseInt(bugetData.getToWriteBudget())
                                                                    ,BUDGET_SUB_FROM), costType);
        if(supersubBudget.isEmpty())
            return false;
        Map<String, List<BugetSubForm>> nowAndSuper = new HashMap<>();
        nowAndSuper.put(NOWBUDGET, bugetData.getBudget_subFrom());
        nowAndSuper.put(SUPERBUDGET, supersubBudget);
        // System.err.println(JSONTool.toJson(bugetData));
        refunds(nowAndSuper).stream().forEach( o ->{
            formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData() ,o.getFormType(),o.getIBillCostLog());
        });
        return true;
    }
    
    public BudgetUserDefined getBudgetUserDefined(String fun) {
        BudgetUserDefined budgetUserDefined = new BudgetUserDefined();
        budgetUserDefined.setFilterSubForm(false);
        budgetUserDefined.setMergeSubForm(false);
        budgetUserDefined.setKey("SuperBudgtDepartments");
        budgetUserDefined.setVal("上级部门");
        List<Dimension> mainDimension = new ArrayList<>();
        List<Dimension> subDimension = new ArrayList<>();
        
        mainDimension.add(new Dimension(BUDGET_START_DATE));
        mainDimension.add(new Dimension(BUDGET_END_DATE));
        mainDimension.add(new Dimension(CURRENCY));
        
        subDimension.add(new Dimension(BUDGET_ITEM_COST_TYPE));
        subDimension.add(new Dimension(BUDGET_ITEM_DEPARTMENT));
        
        budgetUserDefined.setMainDimension(mainDimension);
        budgetUserDefined.setSubDimension(subDimension);
        
        return budgetUserDefined;
    }
 
    
}