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
package com.changhong.epc.count.service.system.performer.impl;
 
import com.changhong.epc.bean.count.MeInfo;
import com.changhong.epc.bean.count.system.SystemMathExtend;
import com.changhong.epc.bean.count.system.SystemMathParam;
import com.changhong.epc.count.mapper.tenant.system.SystemMathMapper;
import com.changhong.epc.count.service.count.data.paramiter.IParamIter;
import com.changhong.epc.count.service.count.model.UnitInfo;
import com.changhong.epc.count.service.system.model.SystemFunContextType;
import com.changhong.epc.count.service.system.performer.IFormulaHandle;
import com.changhong.epc.count.service.system.performer.verify.FuncFormatVerify;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import java.util.*;
 
@Data
@Slf4j
public class FunctionParam {
 
    /**
     * 函数参数名前缀
     */
    public static final String FUNC_ENV_NAME = "FUNPT_";
 
    private IParamIter<UnitInfo> unitInfos;
 
    private SystemMathMapper systemMathMapper;
 
    private IFormulaHandle formulaHandle;
 
    private String expression;
    
    private SystemMathExtend systemMath;
    private Map<String,Object> originalEnv;
    
    private Map<String,Object> env;
 
    private Map<String,Object> val;
 
    public FunctionParam(){
        systemMathMapper = SpringUtil.getBean(SystemMathMapper.class);
        formulaHandle = SpringUtil.getBean(IFormulaHandle.class);
    }
    public FunctionParam(Map<String, Object> val){
        this();
        this.val = val;
    }
 
    private Map<String,Object> getEnvCopy(){
        Map<String,Object> copy = new HashMap<>();
        for(Map.Entry<String,Object> entry: this.originalEnv.entrySet()){
            copy.put(entry.getKey(), entry.getValue());
        }
        return copy;
    }
    
    /**
     * 初始化数据  
     *     ·计算费用 参数迭代器进行初始化    
     *     
     * @param unitInfos
     */
    public void initData(IParamIter<UnitInfo> unitInfos){
        originalEnv = new HashMap<>();
        if(unitInfos == null)
            return ;
        this.unitInfos = unitInfos;
        
        originalEnv.put(SystemFunContextType.ALL_JOURNEY.toString(), unitInfos.getOldUnitInfos());
        originalEnv.put(SystemFunContextType.JOURNEY.toString(), unitInfos.getEachOldUnitInfo());
    }
    
    /**
     * 
     * @param mid
     */
    public void prepare(int mid){
        env = getEnvCopy();
        systemMath = systemMathMapper.selectSystemSystemMathExtendById(mid);
 
        if(systemMath == null){
            return;
        }
 
        expression = FuncFormatVerify.pretreatmentFun(systemMath.getCount());
 
        List<SystemMathParam> systemMathParam = systemMath.getSystemMathParam();
 
        if(ObjectUtil.empty(systemMathParam)){
            return;
        }
 
        for (SystemMathParam param : systemMathParam) {
            Object temp;
            // 引用类型
            if(param.getPType() == 20){
                FunctionParam functionParam = new FunctionParam();
                functionParam.setOriginalEnv(this.getOriginalEnv());
                functionParam.prepare(param.getJoinMathId());
                temp = formulaHandle.execute(functionParam);
            }
            // 普通类型
            else{
 
                MeInfo meInfo = new MeInfo(
                        param.getMdCode()
                        , param.getMeCode()
                        , Optional.ofNullable(val)
                                .map(v->v.get(Objects.toString(param.getPIndex())))
                                .map(Object::toString)
                            .orElse(param.getDefVal()));
                meInfo.setMdName(param.getMdName());
                meInfo.setMeName(param.getMeName());
                temp = meInfo;
//                temp = Optional.ofNullable(val)
//                        .map(v->v.get(Objects.toString(param.getPIndex())))
//                        .map(Object::toString)
//                        .orElse(param.getDefVal());
            }
            
            String name = FUNC_ENV_NAME+param.getPIndex();
            env.put(name, temp);
            expression = expression.replace("{"+param.getPIndex()+"}",name);
        }
        
    }
 
    public void setVal(String s) {
    }
}