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
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.CheckFormDataServes;
import com.changhong.epc.form.service.budget.department.model.BudgetExtend;
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.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
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 org.apache.commons.lang3.math.NumberUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * 合同扣减预算
 */
@Service(UpdateBudgetFactory.CONTRACT_TYPE)
@Slf4j
@Scope("prototype")
public class ContractBudgetUpdate extends AbstractUpdateBudget{
 
    private Map<String, Object> contractInfo;
 
    private Map<String, Object> budgetChildInfo;
 
    @Resource
    private CheckFormDataServes checkFormDataServes;
 
    @Override
    public void checkBudget(UpdateBudgetEntity updateBudgetEntity) {
        this.contractInfo = getFormInfo(updateBudgetEntity);
        Integer budgetId = getParentId(contractInfo);
        if(budgetId == null){
            return;
        }
        this.budgetChildInfo = getFormInfo(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId);
        Assert.condition(budgetChildInfo == null, BUDGET_INVALID);
        if(contractInfo.get(EXPEND_USE_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
            return;
        }
        addLock(budgetId);
        // 验证预算
        Assert.condition(
                toDouble(contractInfo.get(EXPEND_USE_MONEY)) > toDouble(getBudgetBalance(budgetChildInfo))
                , BUDGET_MONEY_NOT_ENOUGH);
 
        // 验证合同
        forEach(updateBudgetEntity, getFormId(SystemForm.CONTRACT_FORM_ID), (contract, contractExpend)->{
            Assert.condition(
                    toDouble(contract.get(PAYMENT_MONEY)) <
                            toDouble(contract.get(ALREADYPAID)) + toDouble(contractExpend.get(PAID_MONDY))
                , OVERTOP_CONTRACT_MONEY);
        });
    }
 
    @Override
    @TxTransaction
    @Transactional
    public void updateBudget(UpdateBudgetEntity updateBudgetEntity) {
        super.updateBudget(updateBudgetEntity);
        checkBudget(updateBudgetEntity);
        if(ObjectUtil.empty(this.budgetChildInfo) || contractInfo.get(EXPEND_USE_MONEY) == null){
            account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),""));
            return;
        }
        double money = toDouble(getBudgetBalance(budgetChildInfo)) - toDouble(contractInfo.get(EXPEND_USE_MONEY));
        checkMoney(money);
 
        addBudgetLogMoney(
                this.budgetChildInfo
                , money
                , updateBudgetEntity.getFormId()
                , this.contractInfo
                , toDouble(contractInfo.get(EXPEND_USE_MONEY))
        );
        String budgetFormId = getFormId(SystemForm.BUDGET_FORM_ID);
        // 修改预算
        updateData(budgetFormId, BUDGET_SUB_FROM
                , getDataRowNum(budgetChildInfo)
                , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(budgetFormId),BUDGET_ITEM_BALANCE)));
 
        // 修改合同
        forEach(updateBudgetEntity, getFormId(SystemForm.CONTRACT_FORM_ID), (contract, contractExpend)->{
            double alreadyPaid = toDouble(contract.get(ALREADYPAID)) + toDouble(contractExpend.get(PAID_MONDY));
            Assert.condition(
                    toDouble(contract.get(PAYMENT_MONEY)) < alreadyPaid
                    , OVERTOP_CONTRACT_MONEY);
            checkMoney(alreadyPaid);
 
            updateData(updateBudgetEntity.getFormId(), SUBFORM  , getDataRowNum(contract)         , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
            updateData(updateBudgetEntity.getFormId(), SUBFORM  , getDataRowNum(contractExpend)   , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
        });
        account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),""));
    }
 
    @Override
    public void flowAfterCall(UpdateBudgetEntity updateBudgetEntity) {
        this.contractInfo = getFormInfo(updateBudgetEntity);
        Integer budgetId = getParentId(contractInfo);
        if(budgetId == null){
            return;
        }
        delLock(budgetId);
    }
 
    @Override
    public void rollback(UpdateBudgetEntity updateBudgetEntity) {
        super.rollback(updateBudgetEntity);
//        this.contractInfo = getFormInfo(updateBudgetEntity);
//        Integer budgetId = getParentId(contractInfo);
//        if(budgetId == null){
//            return;
//        }
//        this.budgetChildInfo = getFormInfo(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId);
//        Assert.condition(budgetChildInfo == null, BUDGET_INVALID);
//        if(contractInfo.get(EXPEND_USE_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
//            return;
//        }
//        double money = toDouble(getBudgetBalance(budgetChildInfo)) + toDouble(contractInfo.get(EXPEND_USE_MONEY));
//        String budgetFormId = getFormId(SystemForm.BUDGET_FORM_ID);
//        // 修改预算
//        updateData(budgetFormId, BUDGET_SUB_FROM
//                , getDataRowNum(budgetChildInfo)
//                , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(budgetFormId),BUDGET_ITEM_BALANCE)));
//
//        // 修改合同
//        forEach(updateBudgetEntity, getFormId(SystemForm.CONTRACT_FORM_ID), (contract, contractExpend)->{
//            double alreadyPaid = toDouble(contractExpend.get(PAID_MONDY)) - toDouble(contract.get(ALREADYPAID));
//            Assert.condition(
//                    toDouble(contract.get(PAYMENT_MONEY)) < alreadyPaid
//                    , OVERTOP_CONTRACT_MONEY);
//            checkMoney(alreadyPaid);
//
//            updateData(updateBudgetEntity.getFormId(), SUBFORM  , getDataRowNum(contract)         , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
//            updateData(updateBudgetEntity.getFormId(), SUBFORM  , getDataRowNum(contractExpend)   , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
//        });
    }
 
    /**
     * 循环合同报销和合同信息
     * @param updateBudgetEntity
     * @param budgetForEachFunction
     */
    protected void forEach(UpdateBudgetEntity updateBudgetEntity, String parentFormId, AbstractOccupyUpdateBudget.BudgetForEachFunction budgetForEachFunction){
        Integer parentId = getContractId(getFormInfo(updateBudgetEntity));
        if(parentId == null){
            log.debug("{}:{}没找到合同信息", updateBudgetEntity.getFormId(), updateBudgetEntity.getDataRowNum());
            return;
        }
        List<Map<String, Object>> contractExpends   = getFormChildInfos(updateBudgetEntity.getFormId()  , SUBFORM    , updateBudgetEntity.getDataRowNum());
        List<Map<String, Object>> contracts         = getFormChildInfos(parentFormId                    , SUBFORM    , parentId);
        //实时合同付款金额
        List<Map<String,Object>> sunAlreadys = checkFormDataServes.getSumAlreadyPaid(new BudgetExtend(parentFormId,parentId,ThreadData.get(Keys.TENANT_ID),updateBudgetEntity.getDataRowNum()));
        log.debug("实时付款金额:"+ JSONTool.toJson(sunAlreadys));
        contracts.stream().forEach(o-> {
                    Map<String,Object> sunAlready = sunAlreadys
                        .stream().filter(fi -> Objects.equals(fi.get(PAYMENTSTAGE), o.get(PAYMENTSTAGE)))
                            .findFirst().orElse(null);
                    Object money = sunAlready!=null?sunAlready.get(ALREADYPAID):"0";
                    log.debug("第"+o.get(PAYMENTSTAGE)+"阶段付款金额:"+money);
                    o.put(ALREADYPAID, money);
                }
        );
        log.debug("金额处理后数据:"+ JSONTool.toJson(contracts));
        // 子表单数量不同,抛出异常
//        Assert.condition(contracts.size() != contractExpends.size(), CONTRACT_CHILD_SIZE);
        for (int i = 0, j = contractExpends.size(); i < j; i++) {
            Map<String, Object> contract = new HashMap<>();
            Map<String, Object> contractExpend = contractExpends.get(i);
            boolean flag = true;
            for(Map<String,Object> m:contracts){
                if(Objects.equals(contractExpend.get(PAYMENTSTAGE),m.get(PAYMENTSTAGE))){
                    contract = m;
                    flag = false;
                    break;
                }
            }
            // 没有余额,忽略
            if(flag || contract.get(ALREADYPAID) == null
                    || contract.get(PAYMENT_MONEY) == null
                    || contractExpend.get(PAID_MONDY) == null){
                continue;
            }
            budgetForEachFunction.each(contract, contractExpend);
        }
    }
 
    /**
     * 获得关联合同信息控件id
     * @param formInfo
     * @return
     */
    protected Integer getContractId(Map<String, Object> formInfo){
        Object parentId = formInfo.get(SELECT_CONTRACTS);
        if(ObjectUtil.empty(parentId)){
            return null;
        }
        return NumberUtils.createInteger(parentId.toString());
    }
 
    @Override
    public FormType getFormType() {
        return FormType.HT;
    }
 
}