wuxw
2019-02-02 9454b49eeabd56894550f1419f14e96f9d10c2ef
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
package com.java110.rule.dao.impl;
 
import com.java110.common.constant.RuleDomain;
import com.java110.common.util.SerializeUtil;
import com.java110.core.base.dao.BaseServiceDao;
import com.java110.entity.rule.Rule;
import com.java110.entity.rule.RuleCondCfg;
import com.java110.entity.rule.RuleEntrance;
import com.java110.rule.dao.IRuleDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
 
import java.util.*;
 
/**
 * Created by wuxw on 2017/7/23.
 */
 
@Service("ruleDaoImpl")
@Transactional
public class RuleDaoImpl extends BaseServiceDao implements IRuleDao {
    private final static Logger logger = LoggerFactory.getLogger(RuleDaoImpl.class);
 
 
    @Override
    public List<Map<String, String>> executeSql(String sql) throws Exception {
        return sqlSessionTemplate.selectList("ruleDaoImpl.executeSql",sql);
    }
 
    /**
     *
     * @param transactionId
     * @param paramIn
     * @param procName
     * @return
     * @throws Exception
     */
    @Override
    public String executeProc(String transactionId, String paramIn, String procName) throws Exception {
 
        Map<String,String> paramInMap = new HashMap<String,String>();
        paramInMap.put("transactionId",transactionId);
 
        paramInMap.put("procName",procName);
 
        paramInMap.put("paramIn",paramIn);
 
        long updateFlag = sqlSessionTemplate.update("ruleDaoImpl.executeProc",paramInMap);
 
        if (updateFlag > 0){
            return paramInMap.get("paramOut");
        }
        return null;
    }
 
    /**
     * 查询 rule_entrance表信息
     *
     * 这里逻辑,首先从ehCache 中获取数据,如果没有缓存,则校验redis中是否存在,如果不存在从数据库中查询,然后保存至redis中,
     * 自动缓存到ehcache 中(默认是10分钟)
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(key=RuleDomain.REDIS_KEY_RULE_ENTRANCE)
    public Map<String, RuleEntrance> getRuleEntranceMap() throws Exception {
 
        Jedis jedis = getJedis();
 
        Map map = new HashMap();
 
        List<RuleEntrance> list = null;
 
        if(jedis.exists(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes())){
            list =  SerializeUtil.unserializeList(jedis.get(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes()),RuleEntrance.class);
        }else{
            list = sqlSessionTemplate.selectList("ruleDaoImpl.getRuleEntranceMap");
            //将 数据缓存至redis中
 
            jedis.set(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes(),SerializeUtil.serializeList(list));
        }
 
        if (null != list && !list.isEmpty()) {
            RuleEntrance ruleEntrance = null;
            for (int ruleEntranceIndex = 0;ruleEntranceIndex< list.size();ruleEntranceIndex++) {
                ruleEntrance =  list.get(ruleEntranceIndex);
                map.put(String.valueOf(ruleEntranceIndex), ruleEntrance);
            }
        }
        return map;
    }
 
    /**
     * 初始化业务规则分组和编码映射关联关系信息
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(key=RuleDomain.REDIS_KEY_RULE_GROUP)
    public List getRuleGroupRelaList() throws Exception {
        List saopRuleGroupRelaList = new ArrayList();
 
        Jedis jedis = getJedis();
 
        if(jedis.exists(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes())){
            saopRuleGroupRelaList =  SerializeUtil.unserializeList(jedis.get(RuleDomain.REDIS_KEY_RULE_GROUP.getBytes()),Map.class);
        }else {
            //查询全部业务规则分组信息
            List allSaopRuleGroupInfoList = sqlSessionTemplate.selectList("ruleDaoImpl.querySaopRuleGroupMap");
 
            //业务分组和编码映射关系集合
            List allsaopRuleGroupRelaInfoList = sqlSessionTemplate.selectList("ruleDaoImpl.querySaopRuleGroupRelaMap");
 
            int allSaopRuleGroupInfoListSize = allSaopRuleGroupInfoList.size();
            int allsaopRuleGroupRelaInfoListSize = allsaopRuleGroupRelaInfoList.size();
 
            if (allSaopRuleGroupInfoListSize > 0 && allsaopRuleGroupRelaInfoListSize > 0) {
                for (int i = 0; i < allSaopRuleGroupInfoListSize; i++) {
                    //每个业务规则分组项
                    Map saopRuleGroupMap = (Map) allSaopRuleGroupInfoList.get(i);
                    if (null == saopRuleGroupMap || null == saopRuleGroupMap.get("groupId")) {
                        continue;
                    }
 
                    //当前规则分组编码
                    String curRuleGroupId = String.valueOf(saopRuleGroupMap.get("groupId"));
 
                    //当前业务规则分组下的规则编码集合
                    List ruleIdList = new ArrayList();
                    saopRuleGroupMap.put("ruleIdList", ruleIdList);
 
                    for (int j = 0; j < allsaopRuleGroupRelaInfoListSize; j++) {
                        Map saopRuleGroupRelaMap = (Map) allsaopRuleGroupRelaInfoList.get(j);
                        if (null == saopRuleGroupRelaMap || null == saopRuleGroupRelaMap.get("groupId")) {
                            continue;
                        }
 
                        //当前规则分组编码
                        String ruleRelaGroupId = String.valueOf(saopRuleGroupRelaMap.get("groupId"));
 
                        if (curRuleGroupId.equals(ruleRelaGroupId)) {
                            //获取规则编码
                            String ruleId = "";
                            if (null != saopRuleGroupRelaMap.get("rule_id")) {
                                ruleId = String.valueOf(saopRuleGroupRelaMap.get("rule_id"));
                            }
 
                            if (!StringUtils.isEmpty(ruleId)) {
                                ruleIdList.add(ruleId);
                            }
                        }
                    }
                }
 
                saopRuleGroupRelaList = allSaopRuleGroupInfoList;
 
                jedis.set(RuleDomain.REDIS_KEY_RULE_ENTRANCE.getBytes(),SerializeUtil.serializeList(saopRuleGroupRelaList));
            }
        }
 
 
        return saopRuleGroupRelaList;
    }
 
    @Override
    @Cacheable(key=RuleDomain.REDIS_KEY_RULE)
    public Map<String, Rule> getRuleMap() throws Exception {
 
        Jedis jedis = getJedis();
 
        List<Rule> ruleList = null;
 
        Map map = new HashMap();
 
        if(jedis.exists(RuleDomain.REDIS_KEY_RULE.getBytes())){
            ruleList =  SerializeUtil.unserializeList(jedis.get(RuleDomain.REDIS_KEY_RULE.getBytes()),Rule.class);
        }else{
            ruleList = sqlSessionTemplate.selectList("ruleDaoImpl.queryRule");
            for (Rule rule : ruleList) {
                String ruleId = rule.getRule_id();
                List<RuleCondCfg> ruleCondCfgList = sqlSessionTemplate.selectList("ruleDaoImpl.queryRuleCondCfg", ruleId);
                rule.setRuleCondCfgs(ruleCondCfgList);
            }
            //将 数据缓存至redis中
 
            jedis.set(RuleDomain.REDIS_KEY_RULE.getBytes(),SerializeUtil.serializeList(ruleList));
        }
 
        if (null != ruleList && !ruleList.isEmpty()) {
            Rule rule = null;
            for (Iterator i = ruleList.iterator(); i.hasNext();) {
                rule = (Rule) i.next();
                map.put(rule.getRule_id(), rule);
            }
        }
 
        return map;
    }
}