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
package com.changhong.epc.form.service.budget.tool.impl;
 
import com.changhong.epc.bean.form.FormType;
import com.changhong.epc.bean.tenant.system.SystemConfig;
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.impl.GetConstant;
import com.changhong.epc.form.service.budget.tool.UpdateBudgetFactory;
import com.changhong.epc.form.service.budget.tool.entity.UpdateBudgetEntity;
import com.changhong.epc.rely.api.epc.tenant.SystemConfigApi;
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.ObjectUtil;
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 javax.annotation.Resource;
 
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
@Service(UpdateBudgetFactory.CONTRACT_INFO_TYPE)
@Slf4j
@Scope("prototype")
public class ContractInfoUpdate extends AbstractUpdateBudget{
 
    private Map<String, Object> contractInfo;
 
    private Map<String, Object> budgetChildInfo;
 
    @Resource
    private CheckFormDataServes checkFormDataServes;
    
    @Resource
    private SystemConfigApi systemConfigApi;
 
    @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(CONTRACT_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
            return;
        }
//        addLock(budgetId);
        // 验证预算
        Assert.condition(
                toDouble(contractInfo.get(CONTRACT_MONEY)) > toDouble(getBudgetBalance(budgetChildInfo))
                , BUDGET_MONEY_NOT_ENOUGH);
    }
 
    @Override
    @TxTransaction
    @Transactional
    public void updateBudget(UpdateBudgetEntity updateBudgetEntity) {
        super.updateBudget(updateBudgetEntity);
        checkBudget(updateBudgetEntity);
        if(ObjectUtil.empty(this.budgetChildInfo) || contractInfo.get(CONTRACT_MONEY) == null){
            account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),""));
            return;
        }
        double money = toDouble(getBudgetBalance(budgetChildInfo)) - toDouble(contractInfo.get(CONTRACT_MONEY));
        checkMoney(money);
 
        addBudgetLogMoney(
                this.budgetChildInfo
                , money
                , updateBudgetEntity.getFormId()
                , this.contractInfo
                , toDouble(contractInfo.get(CONTRACT_MONEY))
        );
        // 修改预算
        updateData(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM
                , getDataRowNum(budgetChildInfo)
                , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_BALANCE)));
    }
 
 
    /**
     * 预算回写
     */
    public Integer budgetWirteBack(BudgetExtend budgetExtend){
        this.contractInfo = getFormInfo(new UpdateBudgetEntity(budgetExtend.getFormId(),budgetExtend.getDatarowNum()));
        log.debug("当前执行状态:"+contractInfo.get(STATE_EXECUTION));
        Assert.condition(contractInfo.get(STATE_EXECUTION)!=null&& !Objects.equals("",contractInfo.get(STATE_EXECUTION)), CONTRACT_OVER);
        log.debug("开始合同信息预算回写");
        //判断合同执行状态,修改控件值
        log.debug("当前合同执行状态:"+budgetExtend.getData());
        if(ObjectUtil.notEmpty(budgetExtend.getData())
                && !Objects.equals(Objects.toString(budgetExtend.getData().get(STATE_EXECUTION),null)
                , GetConstant.getConstant(STATE_EXECUTION))) {
            updateData(budgetExtend.getFormId(), null
                    , budgetExtend.getDatarowNum()
                    , budgetExtend.getData());
            //取出实时已付金额
            Double sum = 0d;
            for(Map<String,Object> map:checkFormDataServes.getSumAlreadyPaid(budgetExtend)){
                if(map.get(ALREADYPAID)!=null){
                    sum+=toDouble(map.get(ALREADYPAID));
                }
            }
            log.debug("当前合同已付金额为:"+sum);
            //回写预算
            Assert.condition((toDouble(this.contractInfo.get(CONTRACT_MONEY))-sum)<0, OVERTOP_CONTRACT_MONEY);
            Integer budgetId = getParentId(contractInfo);
            if(budgetId == null){
                return 0;
            }
            this.budgetChildInfo = getFormInfo(this.getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId);
            Assert.condition(budgetChildInfo == null, BUDGET_INVALID);
            if(contractInfo.get(CONTRACT_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
                return 0;
            }
            log.debug("当前预算余额:"+this.budgetChildInfo.get(BUDGET_ITEM_BALANCE));
            Double budgetbl = toDouble(this.budgetChildInfo.get(BUDGET_ITEM_BALANCE))+(toDouble(this.contractInfo.get(CONTRACT_MONEY))-sum);
            log.debug("预算回写后余额:"+budgetbl);
            //回写预算
            updateData(this.getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, getDataRowNum(this.budgetChildInfo),
                    new ModelMap(BUDGET_ITEM_BALANCE, format(budgetbl,getFormBase(budgetExtend.getFormId()),BUDGET_ITEM_BALANCE)));
        }
        return 1;
    }
 
    @Override
    public void flowAfterCall(UpdateBudgetEntity updateBudgetEntity) {
        account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),""));
    }
 
    @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(CONTRACT_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
            return;
        }
        double money = toDouble(getBudgetBalance(budgetChildInfo)) + toDouble(contractInfo.get(CONTRACT_MONEY));
 
        addBudgetLogMoney(
                this.budgetChildInfo
                , money
                , updateBudgetEntity.getFormId()
                , this.contractInfo
                , toDouble(contractInfo.get(CONTRACT_MONEY))
        );
        // 修改预算
        updateData(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM
                , getDataRowNum(budgetChildInfo)
                , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_BALANCE)));
    }
 
    @Override
    public FormType getFormType() {
        return FormType.HTXX;
    }
    List<SystemConfig> config = null;
    public String getFormId(SystemForm systemForm){
        if(this.config == null){
            this.config = systemConfigApi.getSystemConfigs(new SystemConfig("formInfo"));
        }
        SystemConfig sys = config.stream().filter(fi->Objects.equals(fi.getCkey(),systemForm.toString())).findFirst().orElse(null);
        return sys!=null?sys.getCvalue():null;
    }
 
}