admin
2019-09-11 032eddac4b4627f855905e8e846b0388483e3979
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.yeshi.fanli.service.impl.config;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.entity.ProxyIP;
 
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.ConfigMapper;
import com.yeshi.fanli.entity.common.Config;
import com.yeshi.fanli.entity.config.AppHomeFloatImg;
import com.yeshi.fanli.entity.xcx.XCXSettingConfig;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.dataoke.DaTaoKeApiUtil;
 
import net.sf.json.JSONArray;
 
@Service
public class ConfigServiceImpl implements ConfigService {
 
    @Resource
    private ConfigMapper configMapper;
 
    @Cacheable(value = "config")
    public List<Config> getAllList() {
        return configMapper.listAll();
    }
 
    @Override
    public List<Config> listObjects(String key, int page) {
        int start = (page - 1) * Constant.PAGE_SIZE;
        return configMapper.listSearchByName(key, start, Constant.PAGE_SIZE);
    }
 
    @Override
    public int getCount(String key) {
        return (int) configMapper.countSearchByName(key);
    }
 
    @CacheEvict(value = "config", allEntries = true)
    @Transactional
    public void update(List<Config> list) {
        for (Config config : list) {
            config.setCreatetime(new Date().getTime() + "");
            configMapper.updateByPrimaryKeySelective(config);
        }
    }
 
    @CacheEvict(value = "config", allEntries = true)
    public void update(Config config) {
        config.setCreatetime(new Date().getTime() + "");
        configMapper.updateByPrimaryKeySelective(config);
    }
 
    @Cacheable(value = "config", key = "#p0+'Str'")
    public String get(String key) {
        List<Config> list = configMapper.listByKey(key, null, null);
        if (list.size() == 0) {
            return null;
        }
        String value = list.get(0).getValue();
        return value;
    }
 
    @Cacheable(value = "config", key = "'getByVersion'+'-'+#key+'-'+#platform+'-'+#version ")
    public String getByVersion(String key, String platform, int version) {
        Integer minAndroidVersion = null;
        Integer minIosVersion = null;
        if ("android".equalsIgnoreCase(platform)) {
            minAndroidVersion = version;
        } else
            minIosVersion = version;
 
        List<Config> list = configMapper.listByKey(key, minAndroidVersion, minIosVersion);
        if (list.size() == 0) {
            return null;
        }
        String value = list.get(0).getValue();
        return value;
    }
 
    @Cacheable(value = "config", key = "#p0")
    public Config getConfig(String key) {
        List<Config> list = configMapper.listByKey(key, null, null);
        if (list.size() == 0) {
            return null;
        }
        return list.get(0);
    }
 
    @Override
    public boolean xcxShow(String appId, Integer version) {
        if (version == null)
            return false;
 
        String value = get("xcx_setting");
        if (StringUtil.isNullOrEmpty(value))
            return false;
        JSONArray array = JSONArray.fromObject(value);
        for (int i = 0; i < array.size(); i++) {
            XCXSettingConfig config = new Gson().fromJson(array.optJSONObject(i).toString(), XCXSettingConfig.class);
            if (config.getAppId().equalsIgnoreCase(appId)) {
                if (version > config.getVersion())
                    return config.isShow();
                else
                    return true;
            }
        }
        return false;
    }
 
    @Override
    public XCXSettingConfig getXCXInfoByGhId(String ghId) {
        String value = get("xcx_setting");
        JSONArray array = JSONArray.fromObject(value);
        for (int i = 0; i < array.size(); i++) {
            XCXSettingConfig config = new Gson().fromJson(array.optJSONObject(i).toString(), XCXSettingConfig.class);
            if (config.getGhId().equalsIgnoreCase(ghId)) {
                return config;
            }
        }
        return null;
    }
 
    @Override
    public String getH5Host() {
        String value = get("h5_url");
        String[] sts = value.split(",");
        value = sts[(int) (sts.length * Math.random())];
        return value.trim();
    }
 
    @Cacheable(value = "config", key = "'iosOnLining'+#version")
    @Override
    public boolean iosOnLining(int version) {
        String value = get("ios_onling_version");
        if (StringUtil.isNullOrEmpty(value))
            return false;
        return version >= Integer.parseInt(value);
    }
 
    @Cacheable(value = "config", key = "'isConvertTaoBaoLinkInServer'")
    @Override
    public boolean isConvertTaoBaoLinkInServer() {
        String value = get("convert_taobao_link_in_server");
        if (StringUtil.isNullOrEmpty(value))
            return false;
        if ("1".equalsIgnoreCase(value.trim()))
            return true;
        else
            return false;
    }
 
    @Cacheable(value = "config", key = "'getAppHomeFloatImg'")
    @Override
    public AppHomeFloatImg getAppHomeFloatImg() {
        String value = get("app_float_img");
        if (!StringUtil.isNullOrEmpty(value)) {
            Gson gson = new Gson();
            AppHomeFloatImg appHomeFloatImg = gson.fromJson(value, AppHomeFloatImg.class);
            if (appHomeFloatImg.getShow())
                return appHomeFloatImg;
        }
 
        return null;
    }
 
    @Cacheable(value = "config", key = "'getHomeWEEXUrl'")
    @Override
    public String getHomeWEEXUrl() {
        String value = get("home_weex_url");
        return value;
    }
 
    @Override
    public Config getConfig(long id) {
        return configMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public ProxyIP getTaoBaoProxyIP() {
        String value = get("taobao_proxy_ip");
        try {
            String[] sts = value.split(":");
            return new ProxyIP(sts[0], Integer.parseInt(sts[1]));
        } catch (Exception e) {
        }
        return null;
    }
 
    @Override
    public String getAppHomeFloatNotifyImg() {
 
        return get("home_float_notify_img");
    }
 
    @Override
    public void save(Config config) {
        List<Config> list = configMapper.listByKey(config.getKey(), null, null);
        if (list == null || list.size() == 0) {
            configMapper.insertSelective(config);
        }
    }
 
    @Cacheable(value = "config", key = "'getSearchDiscoveryKeys'")
    @Override
    public String getSearchDiscoveryKeys() {
        List<Config> list = configMapper.listByKey("search_discovery_keys", null, null);
        if (list == null || list.size() == 0)
            return null;
 
        Config config = list.get(0);
        if (config == null)
            return null;
 
        // 更新
        updateSearchDiscoveryKeys(config);
 
        String value = config.getValue();
        return value;
    }
 
    /**
     * 更新搜索发现词
     * 
     * @param config
     */
    @Async
    private void updateSearchDiscoveryKeys(Config config) {
        long currentTime = java.lang.System.currentTimeMillis();
 
        String createtime = config.getCreatetime();
        if (!StringUtil.isNullOrEmpty(createtime)) {
            long diff = currentTime - Long.parseLong(createtime);
            if (diff < 1000 * 60 * 60)
                return; // 超过一个小时更新
        }
 
        String hotWords = DaTaoKeApiUtil.getHotWords();
        if (StringUtil.isNullOrEmpty(hotWords))
            return;
 
        config.setValue(hotWords);
        config.setCreatetime(currentTime + "");
        configMapper.updateByPrimaryKeySelective(config);
    }
}