zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package com.changhong.epc.count.service.system.impl;
Z 2
3 import com.changhong.epc.bean.count.format.CpnInfo;
4 import com.changhong.epc.bean.count.format.FormInfo;
5 import com.changhong.epc.bean.count.format.ItemInfo;
6 import com.changhong.epc.bean.count.format.SystemFunFormInfo;
7 import com.changhong.epc.bean.tenant.system.ErrSystemMsg;
8 import com.changhong.epc.count.service.count.data.paramiter.impl.ParamIter;
9 import com.changhong.epc.count.service.count.model.CountParamMax;
10 import com.changhong.epc.count.service.count.model.CountResultLast;
11 import com.changhong.epc.count.service.format.FormatFormInfo;
12 import com.changhong.epc.count.service.system.IExpendSystemCount;
13 import com.changhong.epc.count.service.system.performer.IFormulaHandle;
14 import com.changhong.epc.count.service.system.performer.impl.FunctionParam;
15 import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
16 import com.iemsoft.framework.cloud.core.tools.SpringUtil;
17 import lombok.extern.slf4j.Slf4j;
18 import org.springframework.stereotype.Service;
19
20 import javax.annotation.Resource;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Objects;
25
26 /**
27  * 系统变量计算
28  */
29 @Service
30 @Slf4j
31 public class IExpendSystemCountImpl implements IExpendSystemCount {
32
33     @Resource
34     private FormatFormInfo<CountParamMax, CountResultLast> formatFormInfoCount;
35
36     @Override
37     public FormInfo systemCount(SystemFunFormInfo systemFunFormInfo) {
38         //格式化参数
39         CountParamMax p = formatFormInfoCount.formatParam(systemFunFormInfo.getFormInfo());
40         ParamIter paramIter = SpringUtil.getBean(ParamIter.class).setDataSource(p, Collections.EMPTY_LIST);
41         List<ErrSystemMsg> errSystemMsgs = new ArrayList<>();
42         // 循环计算系统变量
43         systemFunFormInfo.getData().stream().forEach(data -> {
44             if(ObjectUtil.empty(data)){
45                 data.setVal(null);
46                 return;
47             }
48             if(data.getJourneyIndex() != null && data.getJourneyIndex() < paramIter.getOldUnitInfos().size()) {
49                 paramIter.setIndex(data.getJourneyIndex());
50             }
51             FunctionParam functionParam = new FunctionParam();
52             functionParam.initData(paramIter);
53             functionParam.prepare(data.getId());
54             if(functionParam.getSystemMath() == null){
55                 return;
56             }
57             IFormulaHandle iFormulaHandle = SpringUtil.getBean(IFormulaHandle.class);
58             try{
59                 data.setVal(iFormulaHandle.execute(functionParam));
60             }catch (Exception e){
61                 log.error("系统变量计算错误, id:{}", data.getId());
62                 log.error(e.getMessage(), e);
63             }
64             // 记录主数据缺失错误
65             if(ObjectUtil.notEmpty(iFormulaHandle.getErrSystemMsgs())) {
66                 errSystemMsgs.addAll(iFormulaHandle.getErrSystemMsgs());
67             }
68         });
69         // 赋值给表单信息
70         systemFunFormInfo.getData().stream().forEach(data -> {
71             if(ObjectUtil.empty(data) || data.getVal() == null){
72                 return;
73             }
74             CpnInfo cpnInfo;
75             if(isSubCpnInfo(data.getFieldKey())){
76                 cpnInfo = getCpnInfo(data.getFieldKey(), systemFunFormInfo.getFormInfo(), data.getJourneyIndex());
77             }else{
78                 cpnInfo = getCpnInfo(data.getFieldKey(), systemFunFormInfo.getFormInfo());
79             }
80             if(cpnInfo == null)
81                 cpnInfo.setValue(data.getVal());
82         });
83         // 添加主数据缺失信息
84         systemFunFormInfo.getFormInfo().setErrSystemMsgs(errSystemMsgs);
85         return systemFunFormInfo.getFormInfo();
86     }
87
88     protected CpnInfo getCpnInfo(String fieldKey, FormInfo formInfo){
89         return getCpnInfo(fieldKey, formInfo.getData());
90     }
91
92     protected CpnInfo getCpnInfo(String fieldKey, List<CpnInfo> cpnInfoList){
93         if(ObjectUtil.empty(cpnInfoList)){
94             return null;
95         }
96         for (CpnInfo cpnInfo: cpnInfoList) {
97             if(Objects.equals(cpnInfo.getFieldKey(), fieldKey)){
98                 return cpnInfo;
99             }
100         }
101         return null;
102     }
103
104     protected boolean isSubCpnInfo(String fieldKey){
105         return fieldKey.indexOf('.') >= 0;
106     }
107
108     protected CpnInfo getCpnInfo(String fieldKey, FormInfo formInfo, Integer index){
109         String[] fieldKeys = fieldKey.split("[.]");
110         if(fieldKeys.length != 2){
111             return null;
112         }
113         CpnInfo subCpnInfo = getCpnInfo(fieldKeys[0], formInfo);
114         ItemInfo itemInfo = subCpnInfo.getChild().get(index);
115         return getCpnInfo(fieldKeys[1], itemInfo.getItems());
116     }
117
118 }