From dc5be7d38446f70e6ff86df311119c32b41fe7f8 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 28 十一月 2020 16:37:05 +0800
Subject: [PATCH] 大淘客搜索接口升级

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java |  400 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 215 insertions(+), 185 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java
index 0390017..e8c4849 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java
@@ -1,211 +1,241 @@
 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.dto.push.PushBaseContent;
+import com.yeshi.fanli.dto.push.PushContentDetailDTO;
+import com.yeshi.fanli.dto.push.PushTypeEnum;
+import com.yeshi.fanli.entity.AppVersionInfo;
+import com.yeshi.fanli.entity.SystemEnum;
+import com.yeshi.fanli.entity.bus.user.UserCustomSettings;
+import com.yeshi.fanli.entity.bus.user.UserCustomSettings.UserSettingTypeEnum;
 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.exception.push.PushException;
+import com.yeshi.fanli.log.LogHelper;
+import com.yeshi.fanli.log.LogManager;
 import com.yeshi.fanli.log.PushLogHelper;
+import com.yeshi.fanli.service.inter.config.AppVersionService;
 import com.yeshi.fanli.service.inter.push.DeviceTokenHWService;
 import com.yeshi.fanli.service.inter.push.HWPushService;
+import com.yeshi.fanli.service.inter.user.UserCustomSettingsService;
 import com.yeshi.fanli.util.Constant;
-import com.yeshi.fanli.util.ThreadUtil;
+import com.yeshi.fanli.util.StringUtil;
+import com.yeshi.fanli.util.mq.cmq.PushCMQManager;
 import com.yeshi.fanli.util.push.HWPushUtil;
+import org.springframework.stereotype.Service;
 
-import net.sf.json.JSONObject;
+import javax.annotation.Resource;
+import java.util.*;
 
 @Service
 public class HWPushServiceImpl implements HWPushService {
 
-	@Resource
-	private DeviceTokenHWService deviceTokenHWService;
+    @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);
-				}
-			}
-		}
-	}
+    @Resource
+    private AppVersionService appVersionService;
 
-	@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()));
+    @Resource
+    private UserCustomSettingsService userCustomSettingsService;
 
-		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()));
+    private List<Integer> getVersionCodeList(List<Integer> versionCodeList, SystemEnum system) {
+        List<Integer> versionList = new ArrayList<>();
+        if (versionCodeList == null) {// 鍏ㄦ帹
+            List<AppVersionInfo> appInfoList = appVersionService.listByPlatformAndMinVersionCode("android", 46, system);
+            if (appInfoList != null)
+                for (AppVersionInfo version : appInfoList) {
+                    versionList.add(version.getVersionCode());
+                }
 
-		PushRecord pushRecord = new PushRecord();
-		pushRecord.setState(1);
-		pushRecord.setType(PushController.URL);
-		pushRecord.setUrl(url);
-		pushRecord.setTitle(title);
-		pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
+        } else {// 鎸夌増鏈帹閫�
+            for (Integer versionCode : versionCodeList) {
+                if (versionCode.intValue() >= 46) {
+                    versionList.add(versionCode);
+                }
+            }
+        }
+        return versionList;
+    }
 
-		// 鍗庝负鍏ㄦ帹閫� --鏆傛椂璁剧疆鎺ㄩ��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 pushGoods(Long uid, Long auctionId, PushBaseContent baseContent)
+            throws PushException {
+        addPushQueue(PushTypeEnum.goodsdetail, uid, null, null, auctionId, baseContent);
+    }
 
-	@Override
-	public void pushWEEXUrl(Long uid, String weexUrl, String title, String content) throws PushException {
+    @Override
+    public void pushUrl(Long uid, String url, PushBaseContent baseContent)
+            throws PushException {
+        addPushQueue(PushTypeEnum.url, uid, null, url, null, baseContent);
+    }
 
-	}
+    @Override
+    public void pushZNX(Long uid, PushBaseContent baseContent) throws PushException {
+        addPushQueue(PushTypeEnum.ZNX, uid, null, null, null, baseContent);
+    }
 
-	@Override
-	public void pushZNX(Long uid, AccountMessage msg, SystemZnx systemZNX) throws PushException {
+    @Override
+    public void pushWEEX(Long uid, String weexUrl, PushBaseContent baseContent)
+            throws PushException {
+        addPushQueue(PushTypeEnum.weex, uid, weexUrl, null, null, baseContent);
+    }
 
-		JSONObject contentJson = new JSONObject();
-		contentJson.put("isOpen", 0);
-		contentJson.put("createTime", java.lang.System.currentTimeMillis());
+    @Override
+    public void pushBaiChuanUrl(Long uid, String url, PushBaseContent baseContent)
+            throws PushException {
 
-		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);
-						}
-					}
-				}
-			});
+        addPushQueue(PushTypeEnum.baichuan, uid, null, url, null, baseContent);
+    }
 
-		} 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 pushWelfareCenter(Long uid, PushBaseContent baseContent)
+            throws PushException {
+        addPushQueue(PushTypeEnum.welfare, uid, null, null, null, baseContent);
+    }
 
+    @Override
+    public void pushUserSignInNotification(Long uid, PushBaseContent baseContent)
+            throws PushException {
+        addPushQueue(PushTypeEnum.signin, uid, null, null, null, baseContent);
+    }
+
+    private List<DeviceTokenHW> filterDeviceToken(List<DeviceTokenHW> hwDeviceList, int hour) {
+        // TODO 鏆傛椂涓嶅鐞嗘秷鎭厤鎵撴壈
+        if (1 > 0)
+            return hwDeviceList;
+        // 澶勭悊娑堟伅鍏嶆墦鎵�
+        if (hour >= 20 || hour < 8) {
+            Set<Long> uidSets = new HashSet<>();
+            for (DeviceTokenHW dt : hwDeviceList)
+                if (dt.getUser() != null)
+                    uidSets.add(dt.getUser().getId());
+            List<Long> uidList = new ArrayList<>();
+            uidList.addAll(uidSets);
+            List<UserCustomSettings> settingList = userCustomSettingsService.listByUidListAndTypeAndState(uidList,
+                    UserSettingTypeEnum.cancelNotice.name(), UserCustomSettings.STATE_VALID);
+            uidSets.clear();
+            if (settingList != null) {
+                for (UserCustomSettings seeting : settingList)
+                    uidSets.add(seeting.getUserInfo().getId());
+            }
+            if (uidSets.size() > 0)
+                for (int i = 0; i < hwDeviceList.size(); i++) {
+                    if (hwDeviceList.get(i).getUser() != null
+                            && uidSets.contains(hwDeviceList.get(i).getUser().getId())) {
+                        hwDeviceList.remove(i--);
+                    }
+                }
+        }
+        return hwDeviceList;
+    }
+
+    //娣诲姞鍒版帹閫侀槦鍒�
+    private void addPushQueue(PushTypeEnum type, Long uid, String url, String webUrl,
+                              Long goodsId, PushBaseContent content) {
+        //娣诲姞鍒版帹閫侀槦鍒�
+        PushContentDetailDTO dto = new PushContentDetailDTO(type, uid, url, webUrl, goodsId, content);
+        PushCMQManager.getInstance().addHWPush(dto);
+    }
+
+    /**
+     * @param dto
+     * @throws PushException
+     */
+    public void push(PushContentDetailDTO dto) throws PushException {
+        LogHelper.pushHW("寮�濮嬫帹閫�");
+        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
+        List<Integer> newVersionList = getVersionCodeList(dto.getContent().getVersionCodeList(), dto.getContent().getSystem());
+        // 1.6.5鍚庡紑濮嬫帹閫�
+        for (int i = 0; i < newVersionList.size(); i++) {
+            if (newVersionList.get(i) < 47)
+                newVersionList.remove(i--);
+        }
+
+        if (dto.getUid() == null || dto.getUid() == 0) {
+            LogHelper.pushHW("鍏ㄦ帹");
+            //鍙帹閫佽繎30澶╃殑娲昏穬
+            Date minUpdateTime = new Date(System.currentTimeMillis() - Constant.PUSH_MIN_TIME);
+            long deviceCount = deviceTokenHWService.countDeviceToken(newVersionList, minUpdateTime);
+            int page = (int) (deviceCount % 100 == 0 ? deviceCount / 100 : deviceCount / 100 + 1);
+            for (int p = 1; p <= page; p++) {
+                List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenList(p, 100, newVersionList, minUpdateTime);
+                hwDeviceList = filterDeviceToken(hwDeviceList, hour);
+                LogHelper.pushHW(String.format("鍏ㄦ帹:璁惧鏁�%s",hwDeviceList.size()));
+                List<String> tokenList = new ArrayList<>();
+                for (DeviceTokenHW token : hwDeviceList)
+                    tokenList.add(token.getDeviceToken());
+                try {
+                    if (dto.getType() == PushTypeEnum.goodsdetail) {// 鍟嗗搧璇︽儏
+                        if (dto.getGoodsId() == null)
+                            throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                        HWPushUtil.pushGoods(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), 1, dto.getGoodsId());
+                    } else if (dto.getType() == PushTypeEnum.url) {// 閾炬帴
+                        if (StringUtil.isNullOrEmpty(dto.getWebUrl()))
+                            throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                        HWPushUtil.pushUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl());
+                    } else if (dto.getType() == PushTypeEnum.ZNX) {// 绔欏唴淇�
+                        HWPushUtil.pushZNX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent());
+                    } else if (dto.getType() == PushTypeEnum.weex) {// weex
+                        if (StringUtil.isNullOrEmpty(dto.getUrl()))
+                            throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                        HWPushUtil.pushWEEX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getUrl());
+                    } else if (dto.getType() == PushTypeEnum.baichuan) {// 鐧惧窛閾炬帴
+                        if (StringUtil.isNullOrEmpty(dto.getWebUrl()))
+                            throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                        HWPushUtil.pushBaiChuanUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl());
+                    } else if (dto.getType() == PushTypeEnum.welfare) {// 绂忓埄涓績
+                        HWPushUtil.pushWelfareCenter(tokenList, dto.getContent().getTitle(), dto.getContent().getContent());
+                    } else if (dto.getType() == PushTypeEnum.signin) {// 绛惧埌
+                        HWPushUtil.pushUserSignInNotification(tokenList, dto.getContent().getTitle(), dto.getContent().getContent());
+                    }
+                } catch (Exception e) {
+                    PushLogHelper.hwError(e);
+                }
+
+            }
+        } else {
+            LogHelper.pushHW("鍗曟帹");
+            // 鍗庝负鍗曟帹
+            List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(dto.getUid(), newVersionList);
+            List<String> tokenList = new ArrayList<>();
+            if (hwDeviceList != null) {
+                hwDeviceList = filterDeviceToken(hwDeviceList, hour);
+                for (DeviceTokenHW token : hwDeviceList)
+                    tokenList.add(token.getDeviceToken());
+            }
+
+            LogHelper.pushHW(String.format("鍗曟帹:璁惧鏁�%s 鐢ㄦ埛:%s",hwDeviceList.size(),dto.getUid()+""));
+
+            if (tokenList.size() == 0)
+                return;
+
+            try {
+                if (dto.getType() == PushTypeEnum.goodsdetail) {// 鍟嗗搧璇︽儏
+                    if (dto.getGoodsId() == null)
+                        throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                    HWPushUtil.pushGoods(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), 1, dto.getGoodsId());
+                } else if (dto.getType() == PushTypeEnum.url) {// 閾炬帴
+                    if (StringUtil.isNullOrEmpty(dto.getWebUrl()))
+                        throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                    HWPushUtil.pushUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl());
+                } else if (dto.getType() == PushTypeEnum.ZNX) {// 绔欏唴淇�
+                    HWPushUtil.pushZNX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent());
+                } else if (dto.getType() == PushTypeEnum.weex) {// weex
+                    if (StringUtil.isNullOrEmpty(dto.getUrl()))
+                        throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                    HWPushUtil.pushWEEX(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getUrl());
+                } else if (dto.getType() == PushTypeEnum.baichuan) {// 鐧惧窛閾炬帴
+                    if (StringUtil.isNullOrEmpty(dto.getWebUrl()))
+                        throw new PushException(2, "鍙傛暟涓嶅畬鏁�");
+                    HWPushUtil.pushBaiChuanUrl(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(), dto.getWebUrl());
+                } else if (dto.getType() == PushTypeEnum.welfare) {// 绂忓埄涓績
+                    HWPushUtil.pushWelfareCenter(tokenList, dto.getContent().getTitle(), dto.getContent().getContent());
+                } else if (dto.getType() == PushTypeEnum.signin) {// 绛惧埌
+                    HWPushUtil.pushUserSignInNotification(tokenList, dto.getContent().getTitle(), dto.getContent().getContent());
+                }
+            } catch (Exception e) {
+                PushLogHelper.hwError(e);
+            }
+
+        }
+    }
 }

--
Gitblit v1.8.0