java110
2021-11-22 4065251a85956f6a32f7fd35317446dfed7cbdcd
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
package com.java110.core.factory;
 
import com.java110.dto.communitySetting.CommunitySettingDto;
import com.java110.intf.community.ICommunitySettingInnerServiceSMO;
import com.java110.utils.cache.BaseCache;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.SerializeUtil;
import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import redis.clients.jedis.Jedis;
 
import java.util.List;
 
public class CommunitySettingFactory extends BaseCache {
 
    //日志
    private static Logger logger = LoggerFactory.getLogger(CommunitySettingFactory.class);
 
    public static final String KEY_FEE_SCALE = "SCALE";
    public static final int DEFAULE_FEE_SCALE = 2;
 
 
    /**
     * 查询设置值
     *
     * @param communityId
     * @param key
     * @return
     */
    public static String getValue(String communityId, String key) {
        Jedis redis = null;
        CommunitySettingDto communitySettingDto = null;
        try {
            redis = getJedis();
            Object object = SerializeUtil.unserialize(redis.get((communityId + "_" + key + "_community_setting").getBytes()));
            if (object == null) {//这里存在并发问题,但是 等于查询了多次 然后多次写缓存,作者认为 这种应该比加全局锁效率高些
                communitySettingDto = getCommunitySettingFromDb(communityId, key, redis);
            } else {
                communitySettingDto = (CommunitySettingDto) object;
            }
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
 
        if (communitySettingDto == null) {
            return null;
        }
 
        return communitySettingDto.getSettingValue();
    }
 
    /**
     * 查询设置值
     *
     * @param communityId
     * @param key
     * @return
     */
    public static String getRemark(String communityId, String key) {
        Jedis redis = null;
        CommunitySettingDto communitySettingDto = null;
        try {
            redis = getJedis();
            Object object = SerializeUtil.unserialize(redis.get((communityId + "_" + key + "_community_setting").getBytes()));
            if (object == null) {//这里存在并发问题,但是 等于查询了多次 然后多次写缓存,作者认为 这种应该比加全局锁效率高些
                communitySettingDto = getCommunitySettingFromDb(communityId, key, redis);
            } else {
                communitySettingDto = (CommunitySettingDto) object;
            }
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
 
        if (communitySettingDto == null) {
            return null;
        }
 
        return communitySettingDto.getRemark();
    }
 
    public static CommunitySettingDto getCommunitySettingFromDb(String communityId, String key) {
        Jedis redis = null;
        try {
            redis = getJedis();
            return getCommunitySettingFromDb(communityId, key, redis);
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
    }
 
    private static CommunitySettingDto getCommunitySettingFromDb(String communityId, String key, Jedis redis) {
        ICommunitySettingInnerServiceSMO communitySettingInnerServiceSMOImpl = null;
        try {
            communitySettingInnerServiceSMOImpl = ApplicationContextFactory.getBean(ICommunitySettingInnerServiceSMO.class.getName(), ICommunitySettingInnerServiceSMO.class);
        } catch (NoSuchBeanDefinitionException e) {
            communitySettingInnerServiceSMOImpl = ApplicationContextFactory.getBean("communitySettingInnerServiceSMOImpl", ICommunitySettingInnerServiceSMO.class);
        }
        CommunitySettingDto communitySettingDto = new CommunitySettingDto();
        communitySettingDto.setCommunityId(communityId);
        communitySettingDto.setSettingKey(key);
        List<CommunitySettingDto> communitySettingDtos = communitySettingInnerServiceSMOImpl.queryCommunitySettings(communitySettingDto);
        if (communitySettingDtos == null || communitySettingDtos.size() < 1) {
            return null;
        }
 
        redis.set((communityId + "_" + key + "_community_setting").getBytes(), SerializeUtil.serialize(communitySettingDtos.get(0)));
        return communitySettingDtos.get(0);
    }
 
    /**
     * 手工保存数据
     *
     * @param communitySettingDto
     */
    public static void saveCommunitySetting(CommunitySettingDto communitySettingDto) {
        Jedis redis = null;
        try {
            redis = getJedis();
            redis.set((communitySettingDto.getCommunityId() + "_" + communitySettingDto.getSettingKey() + "_community_setting").getBytes(), SerializeUtil.serialize(communitySettingDto));
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
    }
 
    /**
     * 查询设置值
     *
     * @param communityId
     * @param key
     * @return
     */
    public static CommunitySettingDto getCommunitySetting(String communityId, String key) {
        Jedis redis = null;
        CommunitySettingDto communitySettingDto = null;
        try {
            redis = getJedis();
            Object object = SerializeUtil.unserialize(redis.get((communityId + "_" + key + "_community_setting").getBytes()));
            if (object == null) { //这里存在并发问题,但是 等于查询了多次 然后多次写缓存,作者认为 这种应该比加全局锁效率高些
                communitySettingDto = getCommunitySettingFromDb(communityId, key, redis);
            } else {
                communitySettingDto = (CommunitySettingDto) object;
            }
        } finally {
            if (redis != null) {
                redis.close();
            }
        }
 
        if (communitySettingDto == null) {
            return null;
        }
 
        return communitySettingDto;
    }
 
    /**
     * 查询小数点 位数
     *
     * @param communityId
     * @return
     */
    public static int getFeeScale(String communityId) {
        String scale = getValue(communityId, KEY_FEE_SCALE);
        if (StringUtil.isEmpty(scale)) {
            //防止每次都需要 查询数据库 增加 压力,这里像缓存中写入默认值
            CommunitySettingDto communitySettingDto = new CommunitySettingDto();
            communitySettingDto.setCommunityId(communityId);
            communitySettingDto.setSettingKey(KEY_FEE_SCALE);
            communitySettingDto.setSettingName("小数点位数");
            communitySettingDto.setCsId("-1");
            communitySettingDto.setSettingValue(DEFAULE_FEE_SCALE + "");
            communitySettingDto.setSettingType(CommunitySettingDto.SETTING_TYPE_FEE);
            communitySettingDto.setRemark("费用计算小数点位数,0至4整数");
            saveCommunitySetting(communitySettingDto);
            return DEFAULE_FEE_SCALE;
        }
 
        if (!StringUtil.isInteger(scale)) {
            return DEFAULE_FEE_SCALE;
        }
 
        int scaleInt = Integer.parseInt(scale);
        if (scaleInt > 4 || scaleInt < 0) {
            return DEFAULE_FEE_SCALE;
        }
        return scaleInt;
    }
}