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
package com.changhong.epc.form.service.budget.tool.impl;
 
import com.changhong.epc.bean.form.FormType;
import com.changhong.epc.bean.tenant.system.SystemForm;
import com.changhong.epc.form.service.budget.tool.UpdateBudgetFactory;
import com.changhong.epc.form.service.budget.tool.entity.UpdateBudgetEntity;
import com.codingapi.tx.annotation.TxTransaction;
import com.iemsoft.framework.cloud.core.tools.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 回收预算
 */
@Service(UpdateBudgetFactory.YS_DEL_TYPE)
@Slf4j
@Scope("prototype")
public class RecoveryBudgetUpdate extends AbstractOccupyUpdateBudget {
 
 
 
    @Override
    public void checkBudget(UpdateBudgetEntity updateBudgetEntity) {
        forEach(true,updateBudgetEntity, getFormId(SystemForm.BUDGET_FORM_ID), (toolInfo, childInfo, parentChildInfo)->{
            addLock(getDataRowNum(childInfo));
            if(parentChildInfo != null) {
                addLock(getDataRowNum(parentChildInfo));
            }
            double childMoney = toDouble(childInfo.get(BUDGET_ITEM_BALANCE));
            double toolMoney = toDouble(toolInfo.get(BUDGET_ADDITIONALMONEY));
            // 回收金额 不能大于 预算余额
            Assert.condition(toolMoney > childMoney, RECOVERY_BUDGET_MONEY_NOT_ENOUGH);
        });
    }
 
    @Override
    @TxTransaction
    @Transactional
    public void updateBudget(UpdateBudgetEntity updateBudgetEntity) {
        super.updateBudget(updateBudgetEntity);
        forEach(true,updateBudgetEntity, getFormId(SystemForm.BUDGET_FORM_ID), (toolInfo, childInfo, parentChildInfo)->{
            double childMoney = toDouble(childInfo.get(BUDGET_ITEM_BALANCE));
            double toolMoney  = toDouble(toolInfo.get(BUDGET_ADDITIONALMONEY));
            Double parentMoney = null;
            if(parentChildInfo != null) {
                parentMoney = toDouble(parentChildInfo.get(BUDGET_ITEM_BALANCE));
            }
            Assert.condition(toolMoney > childMoney, RECOVERY_BUDGET_MONEY_NOT_ENOUGH);
            Double parentResidueMoney = parentMoney == null ? null : parentMoney + toolMoney;
            if(parentResidueMoney != null) {
                checkMoney(parentResidueMoney);
 
                addParentBudgetLogMoney(
                        parentChildInfo
                        , parentResidueMoney
                        , updateBudgetEntity.getFormId()
                        , childInfo
                        , toolMoney
                );
 
                // 上级预算金额+回收预算金额
                updateData(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM
                        , getDataRowNum(parentChildInfo)
                        , new ModelMap(BUDGET_ITEM_BALANCE, format(parentResidueMoney,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_BALANCE)));
            }
 
            double childResidueMoney = childMoney - toolMoney;
            double childBudgetMoney = toDouble(childInfo.get(BUDGET_ITEM_MONEY)) - toolMoney;
            checkMoney(childResidueMoney);
            Map<String, Object> val = new ModelMap(BUDGET_ITEM_BALANCE, format(childResidueMoney,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_BALANCE))
                    .addAttribute(BUDGET_ITEM_MONEY, format(childBudgetMoney,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_MONEY));
            if(parentResidueMoney != null) {
                val.put(BUDGET_SUPER_MONEY, format(parentResidueMoney,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_SUPER_MONEY));
            }
            // 预算金额-回收预算金额
            updateData(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM
                    , getDataRowNum(childInfo)
                    , val);
 
            addParentBudgetLogMoney(
                    childInfo
                    , childResidueMoney
                    , updateBudgetEntity.getFormId()
                    , toolInfo
                    , toolMoney
            );
 
            // 预算余额 回写
            updateData(updateBudgetEntity.getFormId(), BUDGET_SUB_FROM
                    , getDataRowNum(toolInfo)
                    , new ModelMap(BUDGET_ITEM_BALANCE, format(childResidueMoney,getFormBase(updateBudgetEntity.getFormId()),BUDGET_ITEM_BALANCE))
                            .addAttribute(BUDGET_ITEM_MONEY, format(childBudgetMoney,getFormBase(updateBudgetEntity.getFormId()),BUDGET_ITEM_MONEY)));
        });
    }
 
    @Override
    public void flowAfterCall(UpdateBudgetEntity updateBudgetEntity) {
        forEach(true,updateBudgetEntity, getFormId(SystemForm.BUDGET_FORM_ID), (toolInfo, childInfo, parentChildInfo)-> {
            delLock(getDataRowNum(childInfo));
            if(parentChildInfo != null) {
                delLock(getDataRowNum(parentChildInfo));
            }
        });
    }
 
    @Override
    public FormType getFormType() {
        return FormType.TH_YS;
    }
}