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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com.changhong.epc.form.service.budget.impl;
 
import com.changhong.autoform.entity.WorkFlowStateEntity;
import com.changhong.epc.bean.form.BorrowType;
import com.changhong.epc.bean.form.FormDataEntity;
import com.changhong.epc.bean.tenant.cost.CostFromMapping;
import com.changhong.epc.bean.tenant.system.SystemConfig;
import com.changhong.epc.bean.tenant.system.SystemForm;
import com.changhong.epc.config.tool.ContextEnvironment;
import com.changhong.epc.constter.system.SystemClients;
import com.changhong.epc.constter.system.businesscode.BudgetBusinessMeaningCode;
import com.changhong.epc.form.mapper.tenant.FormDataMapper;
import com.changhong.epc.form.service.budget.CheckFormDataServes;
import com.changhong.epc.form.service.budget.department.model.BudgetExtend;
import com.changhong.epc.form.tool.FormNameTool;
import com.changhong.epc.rely.api.epc.tenant.SystemConfigApi;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
 
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service("checkFormDataServes")
public class CheckFormDataServesImpl  implements CheckFormDataServes , BudgetBusinessMeaningCode,SystemClients{
 
    @Resource
    private  FormDataMapper formDataMapper;
    
    //@Resource
    //private IFormOthersOperation formOthersOperation;
    
    @Override
    public boolean checkData(WorkFlowStateEntity workFlowStateEntity) {
        //FormType type = GetConstant.getFormType(workFlowStateEntity.getFormId()); 
        
        String type = GetConstant.getMeaningByMdCode(workFlowStateEntity.getFormId()+"_type");
        
        // System.out.println("表单类型"+type);
        switch (type) {
        case YS:
            return this.checkYS(workFlowStateEntity);//预算验证上级预算余额 ok
        case SQ:
            return this.checkSQ(workFlowStateEntity);//申请验证预算单余额  ok
        case BX:
            return this.checkBX(workFlowStateEntity);//报销 验证申请单 余额 ok
        case ZJYS:
            return this.checkZJYS(workFlowStateEntity);//追加预算验证上级预算余额  ok
        case HSYS:
            return this.checkTHYS(workFlowStateEntity);//回收预算验证本条预算余额  ok
        case HTBX:
            return this.checkHT(workFlowStateEntity);//合同报销验证预算 ok
        case GDZCBX:
            return this.checkGDZCBX(workFlowStateEntity);//固定资产报销单 
        }
        return true;
    }
    
    /**
     *验证 固定资产报销单  
     * 
     */
    public boolean checkGDZCBX(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("固定资产报销单:+++"+JSONTool.toJson(workFlowStateEntity));
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        String formId = GetConstant.getFormIdGetConstant(SystemForm.BUDGET_FORM_ID.name());//预算单id
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId,BUDGET_SUB_FROM);
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(Integer.parseInt(Objects.toString(selfData.get(TO_WRITE_BUDGET))));
        Map<String ,Object> father = formDataMapper.searchFormData(formDataEntity);
        //报销金额合计 expendUseMoney    《= 预算余额  budget_itemBalance 
        // System.out.println("固定资产报销单金额:"+Integer.parseInt(Objects.toString(selfData.get(EXPEND_USE_MONEY))));
        // System.out.println("选择预算单余额:"+Integer.parseInt(Objects.toString(father.get(BUDGET_ITEM_BALANCE))));
        if(Integer.parseInt(Objects.toString(father.get(BUDGET_ITEM_BALANCE))) < Integer.parseInt(Objects.toString(selfData.get(EXPEND_USE_MONEY)))){
            return false;
        }
        return true;
    }
    
    
    /**
     *验证预算 
     * 
     */
    public boolean checkYS(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("预算:+++"+JSONTool.toJson(workFlowStateEntity));
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        //获取本条预算单数据
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        //如果有上级预算 判断  没有  返回true
        if(selfData.get(TO_WRITE_BUDGET) != null && !"".equals(selfData.get(TO_WRITE_BUDGET))){
            formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId(),BUDGET_SUB_FROM);
            formDataEntity.setFormName(formName);
            formDataEntity.setParentDataRowNum(Integer.parseInt(Objects.toString(selfData.get(TO_WRITE_BUDGET))));
            List<Map<String,Object>> father = formDataMapper.searchSubFormDataList(formDataEntity);
            formDataEntity.setParentDataRowNum(workFlowStateEntity.getDataRowNum());
            List<Map<String,Object>> self = formDataMapper.searchSubFormDataList(formDataEntity);
            for(Map se : self){
                for(Map fa : father){
                    if (se.get(BUDGET_ITEM_COST_TYPE).equals(fa.get(BUDGET_ITEM_COST_TYPE)) &&  se.get(BUDGET_SUPER_DEPARTEMENT).equals(fa.get(BUDGET_ITEM_DEPARTMENT))) {   //第二个判断是后追加的
                        // System.out.println("预算单预算金额:"+Integer.parseInt(Objects.toString(se.get(BUDGET_ITEM_MONEY))));
                        // System.out.println("上级预算单余额:"+Integer.parseInt(Objects.toString(fa.get(BUDGET_ITEM_BALANCE))));
                        
                        if (Integer.parseInt(Objects.toString(fa.get(BUDGET_ITEM_BALANCE))) < Integer.parseInt(Objects.toString(se.get(BUDGET_ITEM_MONEY)))) {
                            return false;
                        }
                    }
                }
            }    
        }
        return true;
    }
    
    /**
     *验证预算追加 
     * 
     */
    public boolean checkZJYS(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("追加预算:+++"+JSONTool.toJson(workFlowStateEntity));
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        //获取本条追加预算单数据  为了拿到预算单id
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        //获取到预算单   
        String formId = GetConstant.getFormIdGetConstant(SystemForm.BUDGET_FORM_ID.name());//预算单id
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId);
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(Integer.parseInt(Objects.toString(selfData.get(CHOOSE_SUPER_BUDGET))));
        //获取预算单
        Map<String ,Object> father = formDataMapper.searchFormData(formDataEntity);
        //获取上级预算单
        //如果有上级预算 判断  没有  返回true
        if(father.get(TO_WRITE_BUDGET) != null && !"".equals(father.get(TO_WRITE_BUDGET))){
            formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId,BUDGET_SUB_FROM);
            formDataEntity.setFormName(formName);
            formDataEntity.setParentDataRowNum(Integer.parseInt(Objects.toString(selfData.get(CHOOSE_SUPER_BUDGET))));
            //本级预算单的子表单
            List<Map<String,Object>> fatherData = formDataMapper.searchSubFormDataList(formDataEntity);
            
            
            
            
            formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId,BUDGET_SUB_FROM);
            formDataEntity.setFormName(formName);
            formDataEntity.setParentDataRowNum(Integer.parseInt(Objects.toString(father.get(TO_WRITE_BUDGET))));
            //上级预算单的子表单
            List<Map<String,Object>> grFather = formDataMapper.searchSubFormDataList(formDataEntity);
            //预算追加的子表单数据
            formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId(),BUDGET_SUB_FROM);
            formDataEntity.setFormName(formName);
            formDataEntity.setParentDataRowNum(workFlowStateEntity.getDataRowNum());
            List<Map<String,Object>> self = formDataMapper.searchSubFormDataList(formDataEntity);
            for(Map se : self){
                for(Map fa : fatherData){
                    if(se.get(BUDGET_ITEM_DEPARTMENT).equals(fa.get(BUDGET_ITEM_DEPARTMENT)) && se.get(BUDGET_ITEM_COST_TYPE).equals(fa.get(BUDGET_ITEM_COST_TYPE))){
                        for(Map gfa : grFather){
                            if (fa.get(BUDGET_ITEM_COST_TYPE).equals(gfa.get(BUDGET_ITEM_COST_TYPE)) &&  fa.get(BUDGET_SUPER_DEPARTEMENT).equals(gfa.get(BUDGET_ITEM_DEPARTMENT))) {
                                // System.out.println("预算追加金额:"+Integer.parseInt(Objects.toString(se.get(BUDGET_ADDITIONALMONEY))));
                                // System.out.println("上级预算单余额:"+Integer.parseInt(Objects.toString(gfa.get(BUDGET_ITEM_BALANCE))));
                                if (Integer.parseInt(Objects.toString(gfa.get(BUDGET_ITEM_BALANCE))) < Integer.parseInt(Objects.toString(se.get(BUDGET_ADDITIONALMONEY)))) {
                                    return false;
                                }
                            }
                        }
                    }
                }
                
            }    
        }else{
            // System.out.println("没有上级预算,不需要验证");
        }
        return true;
    }
    
    /**
     *验证退回回收 
     * 
     */
    public boolean checkTHYS(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("退回预算:+++"+JSONTool.toJson(workFlowStateEntity));
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        //获取本条回收预算单数据  为了拿到预算单id
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        
        //获取本条回收子表单数据
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId(),BUDGET_SUB_FROM);
        formDataEntity.setFormName(formName);
        formDataEntity.setParentDataRowNum(workFlowStateEntity.getDataRowNum());
        List<Map<String,Object>> self = formDataMapper.searchSubFormDataList(formDataEntity);//追加数据
        
        String formId = GetConstant.getFormIdGetConstant(SystemForm.BUDGET_FORM_ID.name());//预算单id
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId,BUDGET_SUB_FROM);
        formDataEntity.setFormName(formName);
        formDataEntity.setParentDataRowNum(Integer.parseInt(Objects.toString(selfData.get(CHOOSE_SUPER_BUDGET))));
        
        List<Map<String,Object>> father = formDataMapper.searchSubFormDataList(formDataEntity);//选择的预算单数据(子表单)
        for(Map se : self){
            for(Map fa : father){
                if (se.get(BUDGET_ITEM_COST_TYPE).equals(fa.get(BUDGET_ITEM_COST_TYPE)) &&  se.get(BUDGET_ITEM_DEPARTMENT).equals(fa.get(BUDGET_ITEM_DEPARTMENT))) {
                    // System.out.println("回收预算金额:"+Integer.parseInt(Objects.toString(se.get(BUDGET_ADDITIONALMONEY))));
                    // System.out.println("预算单余额:"+Integer.parseInt(Objects.toString(fa.get(BUDGET_ITEM_BALANCE))) );
                    if (Integer.parseInt(Objects.toString(fa.get(BUDGET_ITEM_BALANCE))) < Integer.parseInt(Objects.toString(se.get(BUDGET_ADDITIONALMONEY)))) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
    
    /**
     *验证 申请使用预算 
     * 
     */
    public boolean checkSQ(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("申请:+++"+JSONTool.toJson(workFlowStateEntity));
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        //获取本条申请单数据  为了拿到预算单子表单 datarownum
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        String formId = GetConstant.getFormIdGetConstant(SystemForm.BUDGET_FORM_ID.name());//预算单id
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId,BUDGET_SUB_FROM);
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(Integer.parseInt(Objects.toString(selfData.get(TO_WRITE_BUDGET))));
        Map<String ,Object> father = formDataMapper.searchFormData(formDataEntity);
        
        // System.out.println("申请使用金额:"+Integer.parseInt(Objects.toString(selfData.get(APPLY_USE_MONEY))));
        // System.out.println("预算单余额:"+Integer.parseInt(Objects.toString(father.get(BUDGET_ITEM_BALANCE))));
        if(Integer.parseInt(Objects.toString(father.get(BUDGET_ITEM_BALANCE))) < Integer.parseInt(Objects.toString(selfData.get(APPLY_USE_MONEY)))){
            return false;
        }
        return true;
    }
    
 
    /**
     *验证 报销 使用 申请 
     * 
     */
    public boolean checkBX(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("报销:+++"+JSONTool.toJson(workFlowStateEntity));
        
        //获取本条数据
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        
        //获取申请单数据
        String formId = GetConstant.getFormIdGetConstant(SQ_FORMID);//申请单id
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId);
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(Integer.parseInt(Objects.toString(selfData.get(TO_WRITE_BUDGET))));
        Map<String ,Object> fatherData = formDataMapper.searchFormData(formDataEntity);
        //比较   申请金额 小于 报销金额   返回false 
        // System.out.println("报销金额:"+Integer.parseInt(Objects.toString(selfData.get(EXPEND_USE_MONEY))));
        // System.out.println("申请单余额:"+Integer.parseInt(Objects.toString(fatherData.get(APPLY_USE_MONEY))));
        if(Integer.parseInt(Objects.toString(fatherData.get(APPLY_USE_MONEY))) <  Integer.parseInt(Objects.toString(selfData.get(EXPEND_USE_MONEY)))){
            return false;
        }
        return true;
    }
    
    /**
     *验证合同报销单 使用预算 
     */
    private boolean checkHT(WorkFlowStateEntity workFlowStateEntity){
        // System.out.println("合同报销单:+++"+JSONTool.toJson(workFlowStateEntity));
        String formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),workFlowStateEntity.getFormId());
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(workFlowStateEntity.getDataRowNum());
        Map<String ,Object> selfData = formDataMapper.searchFormData(formDataEntity);
        
        String formId = GetConstant.getFormIdGetConstant(SystemForm.BUDGET_FORM_ID.name());//预算单id
        formName = FormNameTool.getSubFormTableName(workFlowStateEntity.getTenantId(),formId,BUDGET_SUB_FROM);
        formDataEntity.setFormName(formName);
        formDataEntity.setDataRowNum(Integer.parseInt(Objects.toString(selfData.get(TO_WRITE_BUDGET))));
        Map<String ,Object> father = formDataMapper.searchFormData(formDataEntity);
        //报销金额合计 expendUseMoney    《= 预算余额  budget_itemBalance 
        // System.out.println("合同报销金额:"+ Integer.parseInt(Objects.toString(selfData.get(EXPEND_USE_MONEY))));
        // System.out.println("预算单余额:"+Integer.parseInt(Objects.toString(father.get(BUDGET_ITEM_BALANCE))));
        if(Integer.parseInt(Objects.toString(father.get(BUDGET_ITEM_BALANCE))) < Integer.parseInt(Objects.toString(selfData.get(EXPEND_USE_MONEY)))){
            return false;
        }
        return true;
    }
 
    @Override
    public List<Map<String, Object>> getSumAlreadyPaid(BudgetExtend budgetExtend) {
        //取出合同报销单Id
        List<SystemConfig> config = SpringUtil.getBean(SystemConfigApi.class)
                .getSystemConfigs(SystemConfig.of(FORM_INFO,null));
        String formId = null;
        for(SystemConfig con :config){
            if(SystemForm.CONTRACT_EXPEND_FORM_ID.toString().equals(con.getCkey())){
                formId = con.getCvalue();
                break;
            }
        }
        String formName = "formdata_"+budgetExtend.getTenantID()+"_"+formId;
        List<Integer> list = formDataMapper.selectDataRows(formName, budgetExtend.getDatarowNum(),budgetExtend.getCurrDataRowNum());
        if(list ==null || list.size()<=0){
            return new ArrayList<>();
        }
        String subFormName = formName+"_subform";
        return formDataMapper.getSumAlreadyPaid(subFormName,list);
        
    }
 
    @Override
    public Integer checkReimbursement(String borrowMoneyUser, BorrowType borrowType) {
        // 查询备用金还款
        ContextEnvironment context = new ContextEnvironment();
        // 备用金借款
        if(borrowType == BorrowType.estimateRepaymentReserve) {
            String day = context.getSystemConfig("estimateRepayment", borrowType.name());
            if(!NumberUtils.isNumber(day)){
                return 0;
            }
            // 还款
            String repaymentFormId = context.getSystemConfig(FORM_INFO, SystemForm.REPAYMENT_FORM_ID.name());
            // 借款
            String loanFormId = context.getSystemConfig(FORM_INFO, SystemForm.LOAN_FORM_ID.name());
 
            return formDataMapper.selectRepayment(borrowMoneyUser, repaymentFormId, loanFormId, Integer.valueOf(day));
        }
        // 费用借款
        else {
            String day = context.getSystemConfig("estimateRepayment", borrowType.name());
            if(!NumberUtils.isNumber(day)){
                return 0;
            }
            // 查询费用类还款
            List<CostFromMapping> applyFormIds = formDataMapper.selectApplyFormIds();
            if (ObjectUtil.empty(applyFormIds)) {
                return 0;
            }
            List<String> formIds = applyFormIds.stream().map(CostFromMapping::getFId).collect(Collectors.toList());
            return formDataMapper.selectApplyRepayment(formIds, borrowMoneyUser);
        }
    }
 
}