admin
2019-05-16 4114e871bcb3dce771b6aed64a1027d0bbb95ca6
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.yeshi.fanli.controller.client;
 
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.common.JumpDetailV2;
import com.yeshi.fanli.entity.goods.CommonGoods;
import com.yeshi.fanli.entity.push.DeviceActive;
import com.yeshi.fanli.entity.push.PushGoods;
import com.yeshi.fanli.entity.push.PushGoodsGroup;
import com.yeshi.fanli.entity.push.PushGoodsRecord;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.exception.push.PushGoodsGroupException;
import com.yeshi.fanli.service.inter.common.JumpDetailV2Service;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.push.DeviceActiveService;
import com.yeshi.fanli.service.inter.push.DeviceTokenHWService;
import com.yeshi.fanli.service.inter.push.IOSPushService;
import com.yeshi.fanli.service.inter.push.PushGoodsGroupService;
import com.yeshi.fanli.service.inter.push.PushGoodsRecordService;
import com.yeshi.fanli.service.inter.push.PushGoodsService;
import com.yeshi.fanli.service.inter.push.PushRecordService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.factory.JumpDetailParamsFactory;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping(value = "api/v1/push")
public class PushController {
 
    @Resource
    private BusinessSystemService businessSystemService;
 
    @Resource
    private PushRecordService pushRecordService;
 
    @Resource
    private IOSPushService iosPushService;
 
    @Resource
    private DeviceTokenHWService deviceTokenHWService;
 
    @Resource
    private DeviceActiveService deviceActiveService;
 
    @Resource
    private PushService pushService;
 
    @Resource
    private PushGoodsService pushGoodsService;
 
    @Resource
    private PushGoodsGroupService pushGoodsGroupService;
 
    @Resource
    private PushGoodsRecordService pushGoodsRecordService;
 
    @Resource
    private JumpDetailV2Service jumpDetailV2Service;
 
    @Resource
    private ConfigService configService;
 
    @Resource(name = "taskExecutor")
    private TaskExecutor executor;
 
    @RequestMapping(value = "callback", method = RequestMethod.POST)
    public void callback(AcceptData acceptData, String pushId, PrintWriter out) {
        BusinessSystem system = businessSystemService.getBusinessSystemCache(acceptData.getPlatform(),
                acceptData.getPackages());
        if (system == null) {
            out.print(JsonUtil.loadFalseResult("不存在该系统"));
            return;
        }
        int platform = system.getPlatform();
        if (platform == 1) {
            pushRecordService.increaseByAndroid(pushId);
        } else {
            pushRecordService.increaseByIOS(pushId);
        }
        out.print(JsonUtil.loadTrueResult("回调成功"));
    }
 
    /**
     * 
     * 插入苹果推送的deviceToken
     * 
     * @author mawurui createTime 2018年5月8日 下午4:15:44
     * @param deviceToken
     */
    @RequestMapping(value = "/insertDeviceToken", method = RequestMethod.POST)
    public void insertIOSDeviceToken(AcceptData acceptData, String deviceToken, PrintWriter out) {
        if (!StringUtil.isNullOrEmpty(acceptData.getDevice()) && !StringUtil.isNullOrEmpty(deviceToken)) {
            // 添加设备活跃记录
            iosPushService.addDeviceToken(null, Integer.parseInt(acceptData.getVersion()), deviceToken,
                    acceptData.getDevice());
            out.print(JsonUtil.loadTrue(0, null, "成功"));
        }
    }
 
    /**
     * IOS推送 方法说明: 将用户id和deviceToken绑定
     * 
     * @author mawurui createTime 2018年5月8日 下午4:29:45
     * @param uid
     * @param deviceToken
     */
 
    @RequestMapping(value = "/uidBindDeviceToken", method = RequestMethod.POST)
    public void uidBindIOSDeviceToken(AcceptData acceptData, Long uid, String deviceToken, PrintWriter out) {
        if (uid != null && uid != 0 && !StringUtil.isNullOrEmpty(deviceToken)) {
            // 添加token
            iosPushService.addDeviceToken(uid, Integer.parseInt(acceptData.getVersion()), deviceToken,
                    acceptData.getDevice());
        }
    }
 
    /**
     * IOS推送 方法说明: 解绑
     * 
     * @author mawurui createTime 2018年5月9日 上午9:49:58
     * @param deviceToken
     */
    @RequestMapping(value = "/unBind", method = RequestMethod.POST)
    public void unBind(AcceptData acceptData, String deviceToken, PrintWriter out) {
        if (!StringUtil.isNullOrEmpty(acceptData.getDevice())) {
            iosPushService.unBindUidAndDevice(acceptData.getDevice());
            out.print(JsonUtil.loadTrue(0, null, "解绑成功"));
        }
    }
 
    /**
     * 绑定华为推送
     * 
     * @param acceptData
     * @param token
     *            -华为token
     * @param uid
     *            -用户ID
     * @param out
     */
    @RequestMapping(value = "/bindHWPush", method = RequestMethod.POST)
    public void bindHWDeviceToken(AcceptData acceptData, String token, Long uid, PrintWriter out) {
        deviceTokenHWService.addDeviceToken(token, acceptData.getDevice(), uid);
        out.print(JsonUtil.loadTrueResult("成功"));
    }
 
    /**
     * 解绑推送
     * 
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "/unBindHWPush", method = RequestMethod.POST)
    public void unBindHWDeviceToken(AcceptData acceptData, PrintWriter out) {
        deviceTokenHWService.unBindDeviceToken(acceptData.getDevice());
        out.print(JsonUtil.loadTrueResult("成功"));
    }
 
    /**
     * 商品推送记录
     * 
     * @param acceptData
     * @param out
     */
    @RequestMapping(value = "/gethistory", method = RequestMethod.POST)
    public void getHistory(AcceptData acceptData, Long uid, String deviceToken, int page, PrintWriter out) {
 
        long count = 0;
        List<PushGoods> list = null;
        JSONArray resultList = new JSONArray();
 
        DeviceActive deviceActive = deviceActiveService.getDeviceByDeviceAndPlatform(acceptData.getDevice(),
                deviceToken, acceptData.getPlatform());
 
        if (deviceActive != null) {
 
            // 设备注册时间
            Date createTime = deviceActive.getCreateTime();
            count = pushGoodsService.countHistoryByPushTime(uid, createTime);
 
            int pageSize = Constant.PAGE_SIZE;
 
            list = pushGoodsService.listHistoryByPushTime((page - 1) * pageSize, pageSize, uid, createTime);
            if (list != null && list.size() > 0) {
                for (PushGoods pushGoods : list) {
                    JSONObject result = new JSONObject();
 
                    Long pushId = pushGoods.getId();
                    String picture = pushGoods.getPicture();
                    String alertContent = pushGoods.getAlertContent();
                    Date pushTime = pushGoods.getPushTime();
 
                    result.put("content", alertContent);
                    result.put("pushtime", pushTime.getTime());
 
                    // 统计商品数量
                    long totalgoods = 0;
                    // 商品id
                    Long auctionId = null;
                    if (StringUtil.isNullOrEmpty(picture)) {
                        try {
                            List<PushGoodsGroup> listGroup = pushGoodsGroupService.getAllInfoByPushId(pushId);
 
                            if (listGroup != null && listGroup.size() > 0) {
 
                                totalgoods = listGroup.size();
 
                                PushGoodsGroup pushGoodsGroup = listGroup.get(0);
                                if (pushGoodsGroup != null) {
                                    CommonGoods commonGoods = pushGoodsGroup.getCommonGoods();
                                    if (commonGoods != null) {
                                        picture = commonGoods.getPicture();
                                        auctionId = commonGoods.getGoodsId();
                                    }
                                }
                            }
                        } catch (PushGoodsGroupException e) {
                            e.printStackTrace();
                        }
                    }
 
                    result.put("picture", picture);
 
                    String fontColor1 = "#666666";
                    String fontColor2 = "#E5005C";
                    JSONArray array = new JSONArray();
 
                    JSONObject contentJson1 = new JSONObject();
                    contentJson1.put("color", fontColor1);
                    contentJson1.put("content", "本次推荐共");
 
                    JSONObject contentJson2 = new JSONObject();
                    contentJson2.put("color", fontColor2);
                    contentJson2.put("content", totalgoods);
 
                    JSONObject contentJson3 = new JSONObject();
                    contentJson3.put("color", fontColor1);
                    contentJson3.put("content", "个商品");
 
                    array.add(contentJson1);
                    array.add(contentJson2);
                    array.add(contentJson3);
 
                    result.put("totalwords", array);
 
                    String params = "";
 
                    JumpDetailV2 jumpDetail = null;
                    if (totalgoods == 1) {
 
                        params = JumpDetailParamsFactory.createGoodsParams(auctionId);
                        // 单个商品跳转商品详情
                        jumpDetail = jumpDetailV2Service.getByTypeCache("goodsdetail");
 
                    } else {
                        String url = configService.get("push_goods_details");
                        if (url == null) {
                            url = "";
                        }
                        url = url + "?id=" + pushId;
 
                        params = JumpDetailParamsFactory.createWEBParams(url);
 
                        // 跳转推荐详情
                        jumpDetail = jumpDetailV2Service.getByTypeCache("web");
                    }
                    result.put("params", params);
                    result.put("jumpDetail", jumpDetail);
 
                    resultList.add(result);
                }
            }
        }
 
        JSONObject resultJson = new JSONObject();
        resultJson.put("count", count);
        resultJson.put("result_list", resultList);
 
        out.print(JsonUtil.loadTrueResult(resultJson));
 
        // 列表参数、设备参数信息不为空
        if (list != null && list.size() > 0 && deviceActive != null) {
 
            final List<PushGoods> listPush = list;
            // 记录访问信息
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Long deviceId = deviceActive.getId();
 
                        for (PushGoods pushGoods : listPush) {
                            List<PushGoodsRecord> listRecord = pushGoodsRecordService
                                    .listByPushIdAndDeviceId(pushGoods.getId(), deviceId);
                            if (listRecord != null && listRecord.size() > 0) {
                                continue; // 已被记录
                            }
 
                            PushGoodsRecord pushGoodsRecord = new PushGoodsRecord();
 
                            pushGoodsRecord.setCreateTime(new Date());
                            pushGoodsRecord.setPushGoods(pushGoods);
                            pushGoodsRecord.setDeviceActive(deviceActive);
                            pushGoodsRecordService.insert(pushGoodsRecord);
                        }
 
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
 
                }
            });
        }
    }
 
}