admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.yeshi.fanli.service.impl.push;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import com.yeshi.fanli.entity.SystemEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dao.mybatis.push.PushInfoMapper;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsg;
import com.yeshi.fanli.entity.bus.msg.UserSystemMsgTypeEnum;
import com.yeshi.fanli.entity.push.PushInfo;
import com.yeshi.fanli.entity.push.PushInfo.PushTypeEnum;
import com.yeshi.fanli.exception.push.PushException;
import com.yeshi.fanli.exception.push.PushInfoException;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.push.PushInfoService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
@Service
public class PushInfoServiceImpl implements PushInfoService {
 
    @Resource
    private PushService pushService;
 
    @Resource
    private UserSystemMsgService userSystemMsgService;
 
    @Resource
    private PushInfoMapper pushInfoMapper;
 
    private Logger logger = LoggerFactory.getLogger("debugLog");
 
    @Override
    public void save(PushInfo record) throws PushInfoException, Exception {
        if (record == null) {
            throw new PushInfoException(1, "参数不正确");
        }
 
        PushTypeEnum type = record.getType();
        if (type == null) {
            throw new PushInfoException(1, "推送类型不能为空");
        }
 
        String title = record.getTitle();
        if (title == null || title.trim().length() == 0) {
            throw new PushInfoException(1, "标题不能为空");
        }
 
        String arrayAndroid = record.getArrayAndroid();
        String arrayIOS = record.getArrayIOS();
        if (StringUtil.isNullOrEmpty(arrayIOS) && StringUtil.isNullOrEmpty(arrayAndroid)) {
            throw new PushInfoException(1, "推送版本不能为空");
        }
 
        // 定时时间
        Boolean timeTask = record.isTimeTask();
        if (timeTask != null && timeTask) {
            String controlTime_str = record.getControlTime_str();
            if (controlTime_str == null || controlTime_str.trim().length() == 0) {
                throw new PushInfoException(1, "预设时间不能为空");
            }
 
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            controlTime_str = controlTime_str.replaceAll("T", " ");
            record.setControlTime(format.parse(controlTime_str));
        } else {
            record.setControlTime(null);
        }
 
        String url = record.getUrl();
        String content = record.getContent();
        if (StringUtil.isNullOrEmpty(url) && StringUtil.isNullOrEmpty(content)) {
            throw new PushInfoException(1, "推送内容或URL不能为空");
        }
 
        if (url != null && (url.trim().length() == 0 || url.equalsIgnoreCase("null"))) {
            record.setUrl(null);
        }
 
        if (content != null && (content.trim().length() == 0 || content.equalsIgnoreCase("null"))) {
            record.setContent(null);
        }
 
        String uids = record.getUids();
        if (uids != null && (uids.trim().length() == 0 || uids.equalsIgnoreCase("null"))) {
            record.setUids(null);
        }
 
        // 数据转换json
        convertJson(record);
        record.setState(PushInfo.STATE_INIT);
 
        Long id = record.getId();
        if (id == null) {
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            pushInfoMapper.insert(record);
        } else {
            // 修改
            PushInfo current = pushInfoMapper.selectByPrimaryKey(id);
            if (current == null) {
                throw new PushInfoException(1, "该记录已不存在");
            }
 
            if (PushInfo.STATE_SUCCESS == current.getState()) {
                throw new PushInfoException(1, "已推送成功的信息不能修改");
            }
            record.setCreateTime(current.getCreateTime());
            record.setUpdateTime(new Date());
            pushInfoMapper.updateByPrimaryKey(record);
        }
    }
 
 
    /**
     * 转换json
     */
    public void convertJson(PushInfo record) {
        JSONObject json = new JSONObject();
        String url = record.getUrl();
        if (StringUtil.isNullOrEmpty(url)) {
            url = "";
        }
        json.put("url", url);
        json.put("ios", convertVersion(record.getArrayIOS()));
        json.put("android", convertVersion(record.getArrayAndroid()));
        record.setJsonData(json.toString());
    }
 
    /**
     * 转换list
     */
    public String convertVersion(String array) {
        String versions = "";
        if (array != null && array.trim().length() > 0) {
            Gson gson = new Gson();
            List<String> list = gson.fromJson(array, new TypeToken<ArrayList<String>>() {
            }.getType());
 
            if (list != null && list.size() > 0) {
 
                for (String version : list) {
                    versions += version + ",";
                }
                if (versions.endsWith(",")) {
                    versions = versions.substring(0, versions.length() - 1);
                }
            }
        }
        return versions;
    }
 
    @Override
    public void deleteBatchByPrimaryKey(List<Long> list) {
        pushInfoMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public List<PushInfo> listQuery(long start, int count, String key, Integer keyType, Integer state, String type, SystemEnum system) {
 
        List<PushInfo> list = pushInfoMapper.listQuery(start, count, key, keyType, state, type, system);
        if (list == null || list.size() == 0) {
            return list;
        }
 
        for (PushInfo pushInfo : list) {
            Date controlTime = pushInfo.getControlTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
            if (controlTime == null) {
                pushInfo.setTimeTask(false);
                pushInfo.setControlTime_str("");
            } else {
                pushInfo.setTimeTask(true);
                pushInfo.setControlTime_str(sdf.format(controlTime));
            }
 
            String jsonData = pushInfo.getJsonData();
            JSONObject json = JSONObject.fromObject(jsonData);
            String url = json.getString("url");
            pushInfo.setUrl(url);
 
            List<String> listIOS = new ArrayList<String>();
            String versionsIOS = json.getString("ios");
            if (versionsIOS != null && versionsIOS.trim().length() > 0) {
                listIOS = Arrays.asList(versionsIOS.split(","));
            }
            pushInfo.setListIOS(listIOS);
 
            List<String> listAndroid = new ArrayList<String>();
            String versionsAndroid = json.getString("android");
            if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
                listAndroid = Arrays.asList(versionsAndroid.split(","));
            }
            pushInfo.setListAndroid(listAndroid);
        }
 
        return list;
    }
 
    @Override
    public long countQuery(String key, Integer keyType, Integer state, String type, SystemEnum system) {
        return pushInfoMapper.countQuery(key, keyType, state, type, system);
    }
 
    @Override
    public List<PushInfo> listTask(SystemEnum system) {
        return pushInfoMapper.listTask(system);
    }
 
    @Override
    public void handPush(Long id) throws Exception, PushInfoException, PushException {
        PushInfo record = pushInfoMapper.selectByPrimaryKey(id);
        if (record == null) {
            throw new PushInfoException(1, "推送信息已不存在");
        }
        // 执行推送
        executePush(record);
        record.setState(PushInfo.STATE_SUCCESS);
        record.setPushTime(new Date());
        record.setUpdateTime(new Date());
        pushInfoMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public void taskPush(PushInfo record) {
        String msg = null;
        int state = PushInfo.STATE_FAIL;
        try {
            // 执行推送
            executePush(record);
            state = PushInfo.STATE_SUCCESS;
        } catch (PushInfoException e) {
            msg = e.getMsg();
        } catch (PushException e) {
            msg = e.getMsg();
        } catch (Exception e) {
            msg = "系统推送失败";
            logger.error("系统推送失败",e);
        }
        record.setState(state);
        record.setPushTime(new Date());
        record.setRemark(msg);
        record.setUpdateTime(new Date());
        pushInfoMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public PushInfo selectByPrimaryKey(Long id) {
        return pushInfoMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void updateSelectiveByPrimaryKey(PushInfo info) {
        pushInfoMapper.updateByPrimaryKeySelective(info);
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executePush(PushInfo record) throws Exception, PushInfoException, PushException {
 
        if (PushInfo.STATE_SUCCESS == record.getState()) {
            throw new PushInfoException(1, "该信息不能重复推送");
        }
 
        String title = record.getTitle();
        if (title == null || title.trim().length() == 0) {
            throw new PushInfoException(1, "标题不能为空");
        }
 
        String jsonData = record.getJsonData();
        if (StringUtil.isNullOrEmpty(jsonData) && StringUtil.isNullOrEmpty(jsonData)) {
            throw new PushInfoException(1, "推送版本不能为空");
        }
 
        JSONObject json = JSONObject.fromObject(jsonData);
        String url = json.getString("url");
        String content = record.getContent();
        if (StringUtil.isNullOrEmpty(url) && StringUtil.isNullOrEmpty(content)) {
            throw new PushInfoException(1, "推送内容或URL不能为空");
        }
 
        List<String> listIOS = new ArrayList<String>();
        String versionsIOS = json.getString("ios");
        if (versionsIOS != null && versionsIOS.trim().length() > 0) {
            if (versionsIOS.contains("全推")) {
                listIOS = null;
            } else {
                listIOS = Arrays.asList(versionsIOS.split(","));
            }
        }
 
        List<String> listAndroid = new ArrayList<String>();
        String versionsAndroid = json.getString("android");
        if (versionsAndroid != null && versionsAndroid.trim().length() > 0) {
            if (versionsAndroid.contains("全推")) {
                listAndroid = null;
            } else {
                listAndroid = Arrays.asList(versionsAndroid.split(","));
            }
        }
 
        List<String> listuid = null;
        String uids = record.getUids();
        if (uids != null && uids.trim().length() > 0) {
            listuid = Arrays.asList(uids.split(","));
            if (listuid == null || listuid.size() == 0) {
                throw new PushInfoException(1, "用户id格式不正确");
            }
        }
 
        PushTypeEnum type = record.getType();
        if (type == null) {
            throw new PushInfoException(1, "推送类型不能为空");
        }
 
        int pushWay = 0;
        if (PushTypeEnum.ZNX == type) {
            pushWay = 1;
        } else if (PushTypeEnum.URL == type) {
            pushWay = 2;
        } else if (PushTypeEnum.BAICHUAN == type) {
            pushWay = 3;
        } else {
            throw new PushInfoException(1, "推送类型不匹配");
        }
 
        if (listuid == null) { // 全推
            switch (pushWay) {
                case 1: // 站内信
                    pushService.pushZNX(null, title, content, listIOS, listAndroid, record.getSystem());
                    break;
                case 2: // 网页推送
                    pushService.pushUrl(null, title, content, url, listIOS, listAndroid, record.getSystem());
                    break;
                case 3: // 百川
                    pushService.pushBaiChuanUrl(null, title, content, url, listIOS, listAndroid, record.getSystem());
                    break;
                default:
                    throw new PushInfoException(1, "推送类型不匹配");
            }
 
        } else {
            for (String str_uid : listuid) {
                if (str_uid != null && str_uid.trim().length() > 0) {
                    long uid = Long.parseLong(str_uid);
                    switch (pushWay) {
                        case 1: // 站内信
                            pushService.pushZNX(uid, title, content, listIOS, listAndroid, record.getSystem());
                            userSystemMsgService.addUserSystemMsg(uid, UserSystemMsgTypeEnum.question, title, content,
                                    UserSystemMsg.TIME_TAG_COMMON, null);
                            break;
                        case 2: // 网页推送
                            pushService.pushUrl(uid, title, content, url, listIOS, listAndroid, record.getSystem());
                            break;
                        case 3: // 百川
                            pushService.pushBaiChuanUrl(uid, title, content, url, listIOS, listAndroid, record.getSystem());
                            break;
                        default:
                            throw new PushInfoException(1, "推送类型不匹配");
                    }
                }
            }
        }
 
 
    }
}