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
package com.changhong.epc.count.service.system.impl;
 
import com.changhong.epc.bean.count.format.CpnInfo;
import com.changhong.epc.bean.count.format.FormInfo;
import com.changhong.epc.bean.count.format.ItemInfo;
import com.changhong.epc.bean.count.format.SystemFunFormInfo;
import com.changhong.epc.bean.tenant.system.ErrSystemMsg;
import com.changhong.epc.count.service.count.data.paramiter.impl.ParamIter;
import com.changhong.epc.count.service.count.model.CountParamMax;
import com.changhong.epc.count.service.count.model.CountResultLast;
import com.changhong.epc.count.service.format.FormatFormInfo;
import com.changhong.epc.count.service.system.IExpendSystemCount;
import com.changhong.epc.count.service.system.performer.IFormulaHandle;
import com.changhong.epc.count.service.system.performer.impl.FunctionParam;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
 
/**
 * 系统变量计算
 */
@Service
@Slf4j
public class IExpendSystemCountImpl implements IExpendSystemCount {
 
    @Resource
    private FormatFormInfo<CountParamMax, CountResultLast> formatFormInfoCount;
 
    @Override
    public FormInfo systemCount(SystemFunFormInfo systemFunFormInfo) {
        //格式化参数
        CountParamMax p = formatFormInfoCount.formatParam(systemFunFormInfo.getFormInfo());
        ParamIter paramIter = SpringUtil.getBean(ParamIter.class).setDataSource(p, Collections.EMPTY_LIST);
        List<ErrSystemMsg> errSystemMsgs = new ArrayList<>();
        // 循环计算系统变量
        systemFunFormInfo.getData().stream().forEach(data -> {
            if(ObjectUtil.empty(data)){
                data.setVal(null);
                return;
            }
            if(data.getJourneyIndex() != null && data.getJourneyIndex() < paramIter.getOldUnitInfos().size()) {
                paramIter.setIndex(data.getJourneyIndex());
            }
            FunctionParam functionParam = new FunctionParam();
            functionParam.initData(paramIter);
            functionParam.prepare(data.getId());
            if(functionParam.getSystemMath() == null){
                return;
            }
            IFormulaHandle iFormulaHandle = SpringUtil.getBean(IFormulaHandle.class);
            try{
                data.setVal(iFormulaHandle.execute(functionParam));
            }catch (Exception e){
                log.error("系统变量计算错误, id:{}", data.getId());
                log.error(e.getMessage(), e);
            }
            // 记录主数据缺失错误
            if(ObjectUtil.notEmpty(iFormulaHandle.getErrSystemMsgs())) {
                errSystemMsgs.addAll(iFormulaHandle.getErrSystemMsgs());
            }
        });
        // 赋值给表单信息
        systemFunFormInfo.getData().stream().forEach(data -> {
            if(ObjectUtil.empty(data) || data.getVal() == null){
                return;
            }
            CpnInfo cpnInfo;
            if(isSubCpnInfo(data.getFieldKey())){
                cpnInfo = getCpnInfo(data.getFieldKey(), systemFunFormInfo.getFormInfo(), data.getJourneyIndex());
            }else{
                cpnInfo = getCpnInfo(data.getFieldKey(), systemFunFormInfo.getFormInfo());
            }
            if(cpnInfo == null)
                cpnInfo.setValue(data.getVal());
        });
        // 添加主数据缺失信息
        systemFunFormInfo.getFormInfo().setErrSystemMsgs(errSystemMsgs);
        return systemFunFormInfo.getFormInfo();
    }
 
    protected CpnInfo getCpnInfo(String fieldKey, FormInfo formInfo){
        return getCpnInfo(fieldKey, formInfo.getData());
    }
 
    protected CpnInfo getCpnInfo(String fieldKey, List<CpnInfo> cpnInfoList){
        if(ObjectUtil.empty(cpnInfoList)){
            return null;
        }
        for (CpnInfo cpnInfo: cpnInfoList) {
            if(Objects.equals(cpnInfo.getFieldKey(), fieldKey)){
                return cpnInfo;
            }
        }
        return null;
    }
 
    protected boolean isSubCpnInfo(String fieldKey){
        return fieldKey.indexOf('.') >= 0;
    }
 
    protected CpnInfo getCpnInfo(String fieldKey, FormInfo formInfo, Integer index){
        String[] fieldKeys = fieldKey.split("[.]");
        if(fieldKeys.length != 2){
            return null;
        }
        CpnInfo subCpnInfo = getCpnInfo(fieldKeys[0], formInfo);
        ItemInfo itemInfo = subCpnInfo.getChild().get(index);
        return getCpnInfo(fieldKeys[1], itemInfo.getItems());
    }
 
}