Authored by zhengwen.ge

广点通需求

1 -/**  
2 - *  
3 - */  
4 -package com.yoho.unions.server.service.impl;  
5 -  
6 -import javax.annotation.Resource;  
7 -  
8 -import org.apache.commons.lang.StringUtils;  
9 -import org.apache.commons.lang3.tuple.Pair;  
10 -import org.apache.http.HttpStatus;  
11 -import org.slf4j.Logger;  
12 -import org.slf4j.LoggerFactory;  
13 -import org.springframework.stereotype.Service;  
14 -  
15 -import com.alibaba.fastjson.JSONObject;  
16 -import com.netflix.config.DynamicIntProperty;  
17 -import com.netflix.config.DynamicPropertyFactory;  
18 -import com.yoho.core.cache.CacheClient;  
19 -import com.yoho.service.model.union.request.ActivateDingdangRequestBO;  
20 -import com.yoho.service.model.union.request.AddDingdangRequestBO;  
21 -import com.yoho.service.model.union.request.MainUnionRequestBO;  
22 -import com.yoho.service.model.union.response.ActivateDingdangResponseBO;  
23 -import com.yoho.service.model.union.response.AddDingdangResponseBO;  
24 -import com.yoho.service.model.union.response.MainUnionResponseBO;  
25 -import com.yoho.unions.common.enums.SourceEnum;  
26 -import com.yoho.unions.common.utils.DateUtil;  
27 -import com.yoho.unions.common.utils.HttpUtils;  
28 -import com.yoho.unions.dal.IUnionsActiveRecordDAO;  
29 -import com.yoho.unions.dal.model.UnionsActiveRecord;  
30 -import com.yoho.unions.server.service.MainUnionService;  
31 -  
32 -/**  
33 - * 描述:  
34 - * 顶当联盟实现  
35 - * @author ping.huang  
36 - * 2016年3月10日  
37 - */  
38 -@Service  
39 -public class DingdangServiceImpl2 implements MainUnionService {  
40 -  
41 - static Logger log = LoggerFactory.getLogger(DingdangServiceImpl2.class);  
42 -  
43 - static Logger addDingdang = LoggerFactory.getLogger("addDingdang");  
44 - static Logger activeDingdang = LoggerFactory.getLogger("activeDingdang");  
45 - private static final String unions_KEY = "yh:unions:dingdang_";  
46 -  
47 - @Resource  
48 - IUnionsActiveRecordDAO unionsActiveRecordDAO;  
49 -  
50 - @Resource  
51 - CacheClient cacheClient;  
52 -  
53 - @Override  
54 - public MainUnionResponseBO addUnionCheck(MainUnionRequestBO request) {  
55 - AddDingdangRequestBO requestBO = (AddDingdangRequestBO) request;  
56 - if (StringUtils.isEmpty(requestBO.getApp())) {  
57 - log.warn("addunions error app is null with param is {}", request);  
58 - return new AddDingdangResponseBO(false, "APP IS NULL");  
59 - }  
60 - if (StringUtils.isEmpty(requestBO.getUdid())) {  
61 - log.warn("addunions error udid is null with param is {}", request);  
62 - return new AddDingdangResponseBO(false, "UDID IS NULL");  
63 - }  
64 - if (StringUtils.isEmpty(requestBO.getCallbackurl())) {  
65 - log.warn("addunions error callbackurl is null with param is {}", request);  
66 - return new AddDingdangResponseBO(false, "CALLBACKURL IS NULL");  
67 - }  
68 - return new AddDingdangResponseBO();  
69 - }  
70 -  
71 - @Override  
72 - public MainUnionResponseBO addUnion(MainUnionRequestBO request) {  
73 - AddDingdangRequestBO requestBO = (AddDingdangRequestBO) request;  
74 - log.debug("addunions with param is {}", request);  
75 -  
76 - //检查memcached中是否已经有该udid  
77 - String cacheUdid = cacheClient.get(unions_KEY + requestBO.getApp() + "_" + requestBO.getUdid(), String.class);  
78 - log.info("addunions get cache key={}, cacheUdid={}", unions_KEY + requestBO.getApp() + "_" + requestBO.getUdid(), cacheUdid);  
79 - if (StringUtils.isNotEmpty(cacheUdid)) {  
80 - log.warn("addunions error app and udid is added with param is {}", request);  
81 - return new AddDingdangResponseBO(false, "APP UDID IS EXISTS");  
82 - }  
83 -  
84 - //保存到memcached,时间,一个小时  
85 - DynamicIntProperty activeTime = DynamicPropertyFactory.getInstance().getIntProperty("activeTime", 60 * 60);  
86 - cacheClient.set(unions_KEY + requestBO.getApp() + "_" + requestBO.getUdid(), activeTime.get(), requestBO.getUdid());  
87 - log.debug("addunions set cache success");  
88 - //插入数据库  
89 - try {  
90 - UnionsActiveRecord unions = new UnionsActiveRecord();  
91 - unions.setApp(requestBO.getApp());  
92 - unions.setUdid(requestBO.getUdid());  
93 - unions.setCallbackurl(requestBO.getCallbackurl());  
94 - unions.setType(requestBO.getType());  
95 - log.debug("add to unionsLog db with param is {}", unions);  
96 - unionsActiveRecordDAO.insert(unions);  
97 - } catch (Exception e) {  
98 - log.error("", e);  
99 - }  
100 - log.info("addunions success with param is {}", request);  
101 - addDingdang.info("addunions success with param is {}", request);  
102 - return new AddDingdangResponseBO(true, "成功");  
103 - }  
104 -  
105 - @Override  
106 - public MainUnionResponseBO activateUnionCheck(MainUnionRequestBO request) {  
107 - ActivateDingdangRequestBO requestBO = (ActivateDingdangRequestBO) request;  
108 - if (StringUtils.isEmpty(requestBO.getAppid())) {  
109 - log.warn("activeunions error app is null with param is {}", request);  
110 - return new ActivateDingdangResponseBO(false, "APP IS NULL");  
111 - }  
112 - if (StringUtils.isEmpty(requestBO.getUdid())) {  
113 - log.warn("activeunions error udid is null with param is {}", request);  
114 - return new ActivateDingdangResponseBO(false, "UDID IS NULL");  
115 - }  
116 - return new ActivateDingdangResponseBO();  
117 - }  
118 -  
119 - @Override  
120 - public MainUnionResponseBO activateUnion(MainUnionRequestBO request) {  
121 - ActivateDingdangRequestBO requestBO = (ActivateDingdangRequestBO) request;  
122 - log.debug("activeunions with param is {}", requestBO);  
123 -  
124 - String memKey = unions_KEY + requestBO.getAppid() + "_" + requestBO.getUdid();  
125 -  
126 - //检查memcached中是否已经有该udid  
127 - String cacheUdid = cacheClient.get(memKey, String.class);  
128 - log.info("activeUnion get cache key={}, cacheUdid={}", memKey, cacheUdid);  
129 - if (StringUtils.isEmpty(cacheUdid)) {  
130 - log.warn("activeunions error app and udid is not exists with param is {}", request);  
131 - return new ActivateDingdangResponseBO(false, "APP UDID IS NOT EXISTS IN CACHE");  
132 - }  
133 -  
134 - //检查该app和udid是否已经激活  
135 - UnionsActiveRecord unions = new UnionsActiveRecord();  
136 - unions.setApp(requestBO.getAppid());  
137 - unions.setUdid(requestBO.getUdid());  
138 - unions.setType(requestBO.getApptype());  
139 - UnionsActiveRecord u = unionsActiveRecordDAO.selectByUdidAndApp(unions);  
140 -  
141 - if (u == null) {  
142 - log.warn("activeunions error app and udid is not exists with param is {}", request);  
143 - return new ActivateDingdangResponseBO(false, "database has not exists this udid but cache exists");  
144 - }  
145 -  
146 - //已经激活  
147 - if ("1".equals(u.getIsActive())) {  
148 - log.warn("activeunions error app is actived with param is {}", request);  
149 - return new ActivateDingdangResponseBO(false, "app udid had actived");  
150 - }  
151 -  
152 - //调用联盟激活接口  
153 - int repeatCount = 0;  
154 - Pair<Integer, String> pair = null;  
155 - JSONObject json = null;  
156 - boolean isSuccess = false;  
157 - String url = "";  
158 - while (true) {  
159 - //如果已经成功,或者重试次数大于3次,则不再调用  
160 - if (isSuccess || repeatCount >= 3) {  
161 - break;  
162 - }  
163 - try {  
164 - url = u.getCallbackurl();  
165 - pair = HttpUtils.httpGet(url);  
166 - if (pair.getLeft() == HttpStatus.SC_OK) {  
167 - log.debug("call " + u.getCallbackurl() + " url success return message is {}", pair.getRight());  
168 - json = JSONObject.parseObject(pair.getRight());  
169 - isSuccess = json.getBooleanValue("isSuccess");  
170 - }  
171 - } catch (Exception e) {  
172 - log.error("", e);  
173 - }  
174 - repeatCount++;  
175 - }  
176 - log.info("call " + u.getCallbackurl() + " url return message is {}", pair.getRight());  
177 -  
178 - MainUnionResponseBO response = null;  
179 - //更新数据库  
180 - if (isSuccess) {  
181 - unions.setIsActive("1");  
182 - unions.setId(u.getId());  
183 - unionsActiveRecordDAO.updateByPrimaryKey(unions);  
184 - //激活成功,从memcached中删除  
185 - cacheClient.delete(memKey);  
186 - log.debug("activeUnion delete cache success key={}", memKey);  
187 - response = new ActivateDingdangResponseBO(true, "激活成功");  
188 - } else {  
189 - unions.setIsActive("0");  
190 - unions.setId(u.getId());  
191 - unionsActiveRecordDAO.updateByPrimaryKey(unions);  
192 - response = new ActivateDingdangResponseBO(false, "激活失败");  
193 - }  
194 -  
195 - try {  
196 - //组装大数据分析的日志  
197 - JSONObject j = new JSONObject();  
198 - j.put("apptype", requestBO.getApptype());  
199 - j.put("appid", requestBO.getAppid());  
200 - j.put("udid", requestBO.getUdid());  
201 - j.put("dateid", DateUtil.getcurrentDateTime());  
202 - SourceEnum e = SourceEnum.getSourceEnumByValue(u.getType());  
203 - j.put("source", e == null ? "" : e.getName());  
204 - j.put("ip", requestBO.getClientIP());  
205 - j.put("collect_ip", "");  
206 - activeDingdang.info(j.toString());  
207 - } catch (Exception e) {  
208 - log.error("", e);  
209 - }  
210 - return response;  
211 - }  
212 -  
213 -  
214 -}