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
package com.changhong.epc.form.service.budget.tool.impl;
 
import com.changhong.epc.bean.admin.Account;
import com.changhong.epc.bean.form.FormBaseEntity;
import com.changhong.epc.bean.form.FormDataEntity;
import com.changhong.epc.bean.form.FormKey;
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.constter.admin.AdminUrlConst;
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.base.Context;
import com.changhong.epc.constter.system.SystemClients;
import com.changhong.epc.constter.system.businesscode.BudgetBusinessMeaningCode;
import com.changhong.epc.constter.system.run.EpcRestInterface;
import com.changhong.epc.form.mapper.tenant.FormDataMapper;
import com.changhong.epc.form.service.budget.impl.GetConstant;
import com.changhong.epc.form.service.budget.tool.entity.UpdateBudgetEntity;
import com.changhong.epc.form.service.data.FormDataService;
import com.changhong.epc.form.service.field.FormFieldService;
import com.changhong.epc.form.tool.FormNameTool;
import com.iemsoft.framework.cloud.core.base.ResMsg;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.*;
import com.iemsoft.framework.cloud.ribbon.RestInterface;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
 
import javax.annotation.Resource;
 
 
 
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * 回写预算抽象类
 */
@Slf4j
public abstract class AbstractUpdateBudget extends AbstractBudgetLog implements SystemClients,BaseConst,BudgetBusinessMeaningCode {
 
    protected List<CostFromMapping> costFromMappings;
 
    protected Map<SystemForm, String> systemConfigs;
 
    @Resource
    protected FormDataService formDataService;
 
    @Resource
    private FormDataMapper formDataMapper;
 
    @Resource(name="formFieldService")
    private FormFieldService formFieldService;
 
 
    @Override
    public void setCostFromMappings(List<CostFromMapping> costFromMappings) {
        this.costFromMappings = costFromMappings;
    }
 
    /**
     * 获得主表数据
     * @param updateBudgetEntity
     * @return
     */
    protected Map<String, Object> getFormInfo(UpdateBudgetEntity updateBudgetEntity){
        return getFormInfo(updateBudgetEntity.getFormId(), null, updateBudgetEntity.getDataRowNum());
    }
    protected Map<String, Object> getFormInfo(String formId, String childAlias, Integer dataRowNum){
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormId(formId);
        formDataEntity.setTenantId(StringUtil.toString(Context.getTenantId(), ""));
        formDataEntity.setDataRowNum(dataRowNum);
        String formName;
        if(childAlias != null)
            formName = FormNameTool.getSubFormTableName(formDataEntity.getTenantId(), formDataEntity.getFormId(), childAlias);
        else
            formName = FormNameTool.getFormTableName(formDataEntity.getTenantId(), formDataEntity.getFormId());
        formDataEntity.setFormName(formName);
        return formDataMapper.searchFormData(formDataEntity);
    }
 
    /**
     * 获得子表单数据
     * @param formId
     * @param childAlias
     * @param dataRowNum
     * @return
     */
    protected List<Map<String, Object>> getFormChildInfos(String formId, String childAlias, Integer dataRowNum){
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormId(formId);
        formDataEntity.setTenantId(StringUtil.toString(Context.getTenantId(), ""));
        formDataEntity.setDataRowNum(dataRowNum);
        formDataEntity.setChildAlias(childAlias);
        String formName;
        if(formDataEntity.getChildAlias() != null)
            formName = FormNameTool.getSubFormTableName(formDataEntity.getTenantId(), formDataEntity.getFormId(), formDataEntity.getChildAlias());
        else
            formName = FormNameTool.getFormTableName(formDataEntity.getTenantId(), formDataEntity.getFormId());
        formDataEntity.setFormName(formName);
        return formDataMapper.searchFormDataList(formDataEntity);
    }
 
 
    /**
     * 修改数据
     * @param formId
     * @param childAlias
     * @param dataRowNum
     * @param data
     */
    protected void updateData(String formId, String childAlias, Integer dataRowNum, Map<String, Object> data){
        FormDataEntity formDataEntity = new FormDataEntity();
        formDataEntity.setFormId(formId);
        if(ObjectUtil.notEmpty(childAlias)) {
            formDataEntity.setChildAlias(childAlias);
        }
        formDataEntity.setTenantId(Objects.toString(Context.getTenantId(), ""));
        formDataEntity.setDataRowNum(dataRowNum);
        formDataEntity.setMap(data);
        formDataService.updateForm(formDataEntity);
    }
 
    @Override
    public void setSystemFormInfo(List<SystemConfig> systemFormInfo) {
        Map<SystemForm, String> map = new HashMap<>();
        systemFormInfo.stream().forEach(systemConfig -> {
            map.put(SystemForm.valueOf(systemConfig.getCkey()), systemConfig.getCvalue());
        });
        this.systemConfigs = map;
    }
 
    protected Map<SystemForm, String> getSystemFormInfo(){
        return this.systemConfigs;
    }
 
    protected String getFormId(SystemForm systemForm){
        return getSystemFormInfo().get(systemForm);
    }
 
    protected void checkMoney(double money){
        Assert.condition(money < 0, BUDGET_MONEY_NOT_ENOUGH);
    }
 
    private Map<String,FormBaseEntity> formMap=new HashedMap<>();
 
    public FormBaseEntity getFormBase(String formId){
//        if(formMap.get(formId)!=null){
//            log.debug("回写预算返回表单缓存数据{}", formMap.size());
//            return formMap.get(formId);
//        }
        FormKey fk = new FormKey();
        fk.setFormId(formId);
        fk.setTenantId(ThreadData.get(Keys.TENANT_ID));
        FormBaseEntity fb=  formFieldService.selectFormFieldProperty(fk);
//        log.debug("回写预算返回表单实时数据:{}", JSONTool.toJson(fb));
//        formMap.put(formId,fb);
        return fb;
 
    }
 
    protected  String format(double money,FormBaseEntity fieldSet,String field){
        String doubleStr =formatDouble(fieldSet.getFieldset(),field);
        DecimalFormat df = new DecimalFormat(doubleStr);
        String result = df.format(money);
//        result = result.replaceAll("0*$", "");
        result = result.replaceAll("[.]$", "");
        log.debug("小数格式化信息{}, 格式前数据:{}, 格式后数据:{}", doubleStr, money, result);
        return result;
    }
 
    protected  String formatDouble(String str,String field){
       // String str=fieldSet;
        Map<String,Object> map=JSONTool.toObj(str,Map.class);
        final StringBuffer sb =new StringBuffer("#0.");
        map.keySet()
            .stream().forEach(o->{
                Map<String,Object> item = JSONTool.toObj(JSONTool.toJson(map.get(o)),Map.class);
                Map<String,Object> tagAttribute = JSONTool.toObj(JSONTool.toJson(item.get(TAG_ATTRIBUTE)),Map.class);
                if(Objects.equals(tagAttribute.get(ALIAS),field)){
                    String decimalplace = Objects.toString(tagAttribute.get(DECIMALPLACE),"0");
                    if(decimalplace!=null && !Objects.equals("",decimalplace )){
                        Integer s = Integer.parseInt(decimalplace);
                        if(s != 0) {
                            for (int i = 0; i < s; i++) {
                                sb.append("0");
                            }
                        }else{
                            sb.deleteCharAt(sb.length()-1);
                        }
                    }else{
                        sb.deleteCharAt(sb.length()-1);
                    }
                }
            });
        return sb.toString();
    }
 
    public void account(String formId,String tenantId,String dataRowNum){
        //请求表单数据判断是否需要记账
        Map<String,Object> formInfo = getFormInfo(formId,null,Integer.parseInt(dataRowNum));
        String flag = Objects.toString(formInfo.get(ACCOUNT_FLAG),"");
        log.debug("{}-{}表单数据记账标示是:{}", formId, dataRowNum, flag);
        if(Objects.equals(GetConstant.getConstant(ACCOUNT_FLAG),flag) || ObjectUtil.empty(flag)){
            //请求表单接口
            FormBaseEntity formBaseEntity = getFormBase(formId);
            //判断当前表单是否记账
            log.debug("{}表单记账标示是:{}", formId, ("1".equals(formBaseEntity.getAccountingRule()) ? "记账" : "不记账"));
            if ("1".equals(formBaseEntity.getAccountingRule())) {
                Account account = new Account();
                account.setFormId(formId);
                account.setTenantId(tenantId);
                account.setDataRowNum(dataRowNum);
                account.setCompanyId(ThreadData.get(Keys.COMPANY_ID));
                account.setOpenId(ThreadData.get(OPEN_ID));
                account.setFieldSet(formBaseEntity.getFieldset());
                account.setDataStart(0);
                account.initParam();
                RestInterface restInterface = SpringUtil.getBean(SERVER_ADMIN.getBeanName(), RestInterface.class);
                Object data = restInterface.post(AdminUrlConst.ACCOUNT_ADD, account, ResMsg.class, EpcRestInterface.getEpcHeads()).getData();
 
            }
        }
 
    }
 
}