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
package com.yeshi.buwan.job;
 
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yeshi.buwan.domain.system.DetailSystemConfig;
import com.yeshi.buwan.domain.system.DetailSystem;
import com.yeshi.buwan.service.imp.DetailSystemConfigService;
import com.yeshi.buwan.service.imp.SystemService;
import com.yeshi.buwan.util.EHCacheManager;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import org.yeshi.utils.AppMarketUtil;
import org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.util.Map;
 
//10409568
@Component
public class AdJob {
    @Resource
    private DetailSystemConfigService configService;
    @Resource
    private SystemService systemService;
    @Resource
    private EHCacheManager ehCacheManager;
 
 
    //打开华为应用市场的广告
    @XxlJob("ad-hw-check")
    public ReturnT<String> openHWAd(String params) throws Exception {
        JSONObject paramsJson = JSONObject.fromObject(params);
        String appId = paramsJson.optString("appId");
        String systemId = paramsJson.optString("detailSystemId");
        //应用市场的版本
        String onLineVersion = AppMarketUtil.getHWLatestVersion(appId);
        if (StringUtil.isNullOrEmpty(onLineVersion)) {
            throw new Exception("应用市场版本获取失败:" + appId);
        }
 
        //获取正在上线的版本
        DetailSystem detailSystem = systemService.getDetailSystemById(systemId);
        Map<String, String> map = configService.getConfigAsMap(detailSystem, 1);
        //正在上线的版本
        String onLiningVersionInfo = map.get("ad_hw_online_version_info");
        JSONObject onLiningVersionInfoJson = JSONObject.fromObject(onLiningVersionInfo);
        String onLiningVersionName = onLiningVersionInfoJson.optString("versionName");
        int onLiningVersionCode = onLiningVersionInfoJson.optInt("versionCode");
        //是否已经上线完成
        if (Integer.parseInt(onLineVersion.replace(".", "")) >= Integer.parseInt(onLiningVersionName.replace(".", ""))) {
            //已经上线,设置
            showAd("huawei", detailSystem, onLiningVersionCode + 1);
        }
        return ReturnT.SUCCESS;
    }
 
    //打开vivo应用市场广告
 
    //打开华为应用市场的广告
    @XxlJob("ad-vivo-check")
    public ReturnT<String> openVIVOAd(String params) throws Exception {
        JSONObject paramsJson = JSONObject.fromObject(params);
        String appId = paramsJson.optString("appId");
        String systemId = paramsJson.optString("detailSystemId");
        //应用市场的版本
        Integer onLineVersion = AppMarketUtil.getVIVOLatestVersionCode(appId);
        if (onLineVersion == null) {
            throw new Exception("应用市场版本获取失败:" + appId);
        }
 
        //获取正在上线的版本
        DetailSystem detailSystem = systemService.getDetailSystemById(systemId);
        Map<String, String> map = configService.getConfigAsMap(detailSystem, 1);
        //正在上线的版本
        String onLiningVersionCode = map.get("ad_click_download_version");
        //是否已经上线完成
        if (onLineVersion >= Integer.parseInt(onLiningVersionCode)) {
            //已经上线,设置
            showAd("vivo", detailSystem, onLineVersion + 1);
        }
        return ReturnT.SUCCESS;
    }
 
 
    /**
     * 打开广告
     *
     * @param detailSystem
     * @param version
     */
    public void showAd(String channel, DetailSystem detailSystem, int version) {
        DetailSystemConfig config = configService.getConfigByKey("video_detail_full_video_version_channel", detailSystem, version);
        updateVersionConfig(channel, config, version);
        config = configService.getConfigByKey("ad_splash_config", detailSystem, version);
        updateVersionConfig(channel, config, version);
        if (!channel.equalsIgnoreCase("vivo")) {//VIVO屏蔽其他广告位
            config = configService.getConfigByKey("ad_other_open_settings", detailSystem, version);
            updateVersionConfig(channel, config, version);
        }
        config = configService.getConfigByKey("ad_play_video_pre", detailSystem, version);
        updateVersionConfig(channel, config, version);
 
        config = configService.getConfigByKey("ad_exit_app", detailSystem, version);
        updateVersionConfig(channel, config, version);
 
        config = configService.getConfigByKey("ad_video_detail_full_video", detailSystem, version);
        updateVersionConfig(channel, config, version);
 
        config = configService.getConfigByKey("ad_video_search", detailSystem, version);
        updateVersionConfig(channel, config, version);
 
        //删除缓存
        ehCacheManager.clearCacheByCacheName("configCache");
    }
 
    /**
     * 获取正在上线的版本
     *
     * @return
     */
    public int getOnliningVersionCode(DetailSystem detailSystem) {
        DetailSystemConfig config = configService.getConfigByKey("ad_click_download_version", detailSystem, 1);
        return Integer.parseInt(config.getValue());
    }
 
    public void setOnliningVersionCode(int versionCode, DetailSystem detailSystem) {
        DetailSystemConfig config = configService.getConfigByKey("ad_click_download_version", detailSystem, 1);
        config.setValue(versionCode + "");
        configService.updateConfig(config);
    }
 
    /**
     * 更新version
     *
     * @param config
     * @param version
     */
    private void updateVersionConfig(String channel, DetailSystemConfig config, int version) {
        channel = channel.toLowerCase();
        if (config == null)
            return;
        if (StringUtil.isNullOrEmpty(config.getValue()))
            return;
        JSONObject json = JSONObject.fromObject(config.getValue());
        if (json.opt(channel) == null)
            return;
 
        if (json.opt(channel) instanceof Integer) {
            if (json.optInt(channel) < version)
                json.put(channel, version);
        } else {
            JSONObject item = json.optJSONObject(channel);
            if (item != null && !StringUtil.isNullOrEmpty(item.optString("version"))) {
                if (Integer.parseInt(item.optString("version")) < version)
                    item.put("version", version);
            }
            json.put(channel, item);
        }
        config.setValue(json.toString());
        configService.updateConfig(config);
    }
 
 
}