admin
2018-12-15 6a4d58a65e916323b1ea11b36b27e47943482f18
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
package com.yeshi.fanli.service.impl.push;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.controller.admin.PushController;
import com.yeshi.fanli.entity.bus.user.AccountMessage;
import com.yeshi.fanli.entity.push.DeviceTokenHW;
import com.yeshi.fanli.entity.system.SystemZnx;
import com.yeshi.fanli.entity.xinge.MessageInfo;
import com.yeshi.fanli.entity.xinge.PushRecord;
import com.yeshi.fanli.exception.PushException;
import com.yeshi.fanli.log.PushLogHelper;
import com.yeshi.fanli.service.inter.push.DeviceTokenHWService;
import com.yeshi.fanli.service.inter.push.HWPushService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.push.HWPushUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class HWPushServiceImpl implements HWPushService {
 
    @Resource
    private DeviceTokenHWService deviceTokenHWService;
 
    @Override
    public void pushGoods(Long uid, Long auctionId, String title, String content) throws PushException {
        String gid = auctionId + "";
        JSONObject json = new JSONObject();
        json.put("url", "");
        json.put("id", gid);
        json.put("type", "goodsdetail");
        json.put("miPushUrl", String.format("%s.ui.recommend.GoodsBrowserActivity",
                Constant.systemCommonConfig.getAndroidBaseactivityName()));
        if (uid == null || uid == 0) {
            // 华为全推送 --暂时设置推送100000用户
            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenList(1, 100000);
            ThreadUtil.run(new Runnable() {
                @Override
                public void run() {
                    while (hwDeviceList != null && hwDeviceList.size() > 0) {
                        List<String> deviceTokens = new ArrayList<>();
                        if (hwDeviceList.size() > 10000) {
                            List<DeviceTokenHW> pushList = hwDeviceList.subList(0, 1000);
                            for (DeviceTokenHW DeviceTokenHW : pushList) {
                                deviceTokens.add(DeviceTokenHW.getDeviceToken());
                                hwDeviceList.remove(DeviceTokenHW);
                            }
                        } else {
                            for (DeviceTokenHW DeviceTokenHW : hwDeviceList) {
                                deviceTokens.add(DeviceTokenHW.getDeviceToken());
                                hwDeviceList.remove(DeviceTokenHW);
                            }
                        }
                        try {
                            HWPushUtil.pushGoods(deviceTokens, title, content, 1, Long.parseLong(gid));
                        } catch (Exception e) {
                            PushLogHelper.hwError(e);
                        }
                    }
                }
            });
        } else {
            // 华为单推
            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(uid);
            if (hwDeviceList != null && hwDeviceList.size() > 0) {
                List<String> deviceList = new ArrayList<>();
                for (DeviceTokenHW dt : hwDeviceList)
                    deviceList.add(dt.getDeviceToken());
                try {
                    HWPushUtil.pushGoods(deviceList, title, content, 1, Long.parseLong(gid));
                } catch (Exception e) {
                    PushLogHelper.hwError(e);
                }
            }
        }
    }
 
    @Override
    public void pushUrl(Long uid, String url, String title, String content) throws PushException {
        JSONObject json = new JSONObject();
        json.put("url", url);
        json.put("type", "url");
        json.put("webUrl", url);
        json.put("miPushUrl",
                String.format("%s.ui.BrowserActivity", Constant.systemCommonConfig.getAndroidBaseactivityName()));
 
        MessageInfo info = new MessageInfo();
        info.setTitle(title);
        info.setContent(content);
        info.setDescription(content);
        // 小米推送网页
        info.setPackageName(Constant.systemCommonConfig.getAndroidPackageName());
        info.setActivty(
                String.format("%s.ui.BrowserActivity", Constant.systemCommonConfig.getAndroidBaseactivityName()));
 
        PushRecord pushRecord = new PushRecord();
        pushRecord.setState(1);
        pushRecord.setType(PushController.URL);
        pushRecord.setUrl(url);
        pushRecord.setTitle(title);
        pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
 
        // 华为全推送 --暂时设置推送100000用户
        if (uid == null || uid == 0) {
            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenList(1, 100000);
            ThreadUtil.run(new Runnable() {
                @Override
                public void run() {
                    while (hwDeviceList != null && hwDeviceList.size() > 0) {
                        List<String> deviceTokens = new ArrayList<>();
                        if (hwDeviceList.size() > 1000) {
                            List<DeviceTokenHW> pushList = hwDeviceList.subList(0, 1000);
                            for (DeviceTokenHW DeviceTokenHW : pushList) {
                                deviceTokens.add(DeviceTokenHW.getDeviceToken());
                                hwDeviceList.remove(DeviceTokenHW);
                            }
                        } else {
                            for (DeviceTokenHW DeviceTokenHW : hwDeviceList) {
                                deviceTokens.add(DeviceTokenHW.getDeviceToken());
                                hwDeviceList.remove(DeviceTokenHW);
                            }
                        }
                        try {
                            HWPushUtil.pushWeb(deviceTokens, title, content, url);
                        } catch (Exception e) {
                            PushLogHelper.hwError(e);
                        }
                    }
                }
            });
        } else {
            // 华为单推
            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(uid);
            if (hwDeviceList != null && hwDeviceList.size() > 0) {
                List<String> deviceList = new ArrayList<>();
                for (DeviceTokenHW dt : hwDeviceList)
                    deviceList.add(dt.getDeviceToken());
                try {
                    HWPushUtil.pushWeb(deviceList, title, content, url);
                } catch (Exception e) {
                    PushLogHelper.hwError(e);
                }
            }
        }
    }
 
    @Override
    public void pushZNX(Long uid, AccountMessage msg, SystemZnx systemZNX) throws PushException {
 
        JSONObject contentJson = new JSONObject();
        contentJson.put("isOpen", 0);
        contentJson.put("createTime", java.lang.System.currentTimeMillis());
 
        if (systemZNX != null) {
            // 华为全推送 --暂时设置推送10000用户
            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenList(1, 10000);
            ThreadUtil.run(new Runnable() {
                @Override
                public void run() {
                    while (hwDeviceList != null && hwDeviceList.size() > 0) {
                        List<String> deviceTokens = new ArrayList<>();
                        if (hwDeviceList.size() > 1000) {
                            List<DeviceTokenHW> pushList = hwDeviceList.subList(0, 1000);
                            for (DeviceTokenHW DeviceTokenHW : pushList) {
                                deviceTokens.add(DeviceTokenHW.getDeviceToken());
                                hwDeviceList.remove(DeviceTokenHW);
                            }
                        } else {
                            for (DeviceTokenHW DeviceTokenHW : hwDeviceList) {
                                deviceTokens.add(DeviceTokenHW.getDeviceToken());
                                hwDeviceList.remove(DeviceTokenHW);
                            }
                        }
                        try {
                            HWPushUtil.pushZNX(deviceTokens, systemZNX.getTitle(), systemZNX.getContent());
                        } catch (Exception e) {
                            PushLogHelper.hwError(e);
                        }
                    }
                }
            });
 
        } else if (msg != null) {
            // 华为单推
            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(uid);
            if (hwDeviceList != null && hwDeviceList.size() > 0) {
                List<String> deviceList = new ArrayList<>();
                for (DeviceTokenHW dt : hwDeviceList)
                    deviceList.add(dt.getDeviceToken());
                try {
                    HWPushUtil.pushZNX(deviceList, msg.getTitle(), msg.getContent());
                } catch (Exception e) {
                    PushLogHelper.hwError(e);
                }
            }
        }
    }
 
    @Override
    public void pushWEEX(Long uid, String title, String content, String weexUrl) throws PushException {
        // TODO 推送weex页面
 
    }
 
    @Override
    public void pushBaiChuanUrl(Long uid, String title, String content, String url) throws PushException {
        // TODO 推送淘客百川链接
 
    }
 
}