From e3732fa01883aa7a93f7398e39b01b0009155b06 Mon Sep 17 00:00:00 2001
From: yujian <yujian>
Date: 星期四, 14 三月 2019 09:46:36 +0800
Subject: [PATCH] 等级代码调整

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java |  253 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 217 insertions(+), 36 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java
index 1b8730b..07c2392 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java
@@ -1,12 +1,11 @@
 package com.yeshi.fanli.service.impl.push;
 
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 
 import javax.annotation.Resource;
-
-import net.sf.json.JSONObject;
 
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -14,6 +13,7 @@
 import com.yeshi.fanli.controller.admin.PushController;
 import com.yeshi.fanli.dao.mybatis.push.DeviceTokenIOSMapper;
 import com.yeshi.fanli.dao.mybatis.push.PushQueueRecordMapper;
+import com.yeshi.fanli.entity.AppVersionInfo;
 import com.yeshi.fanli.entity.bus.user.AccountMessage;
 import com.yeshi.fanli.entity.push.DeviceTokenIOS;
 import com.yeshi.fanli.entity.push.PushQueueRecord;
@@ -21,14 +21,16 @@
 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.LogHelper;
 import com.yeshi.fanli.log.PushLogHelper;
+import com.yeshi.fanli.service.inter.config.AppVersionService;
 import com.yeshi.fanli.service.inter.push.IOSPushService;
+import com.yeshi.fanli.service.inter.user.UserCustomSettingsService;
 import com.yeshi.fanli.util.CMQManager;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.StringUtil;
-import com.yeshi.fanli.util.ThreadUtil;
 import com.yeshi.fanli.util.push.IOSPushUtil;
+
+import net.sf.json.JSONObject;
 
 @Service
 public class IOSPushServiceImpl implements IOSPushService {
@@ -39,8 +41,15 @@
 	@Resource
 	private PushQueueRecordMapper pushQueueRecordMapper;
 
+	@Resource
+	private UserCustomSettingsService userCustomSettingsService;
+	
+	@Resource
+	private AppVersionService appVersionService;
+	
+
 	@Override
-	public void pushGoods(Long uid, Long auctionId, String title, String content) throws PushException {
+	public void pushGoods(Long uid, Long auctionId, String title, String content, List<String> listVersion) throws PushException {
 
 		String url = "https://item.taobao.com/item.htm?id=" + auctionId;
 		JSONObject json = new JSONObject();
@@ -66,7 +75,7 @@
 
 		if (uid == null || uid == 0) {
 
-			addPushIOSQueue(info, url, PushController.GOODS);
+			addPushIOSQueue(info, url, PushController.GOODS, listVersion);
 
 			/*
 			 * // 鏌ヨIOS鎵�鏈夎澶囧彿 // TODO 闇�瑕佸叏鎺� List<String> deviceTokenList =
@@ -81,8 +90,12 @@
 			 * } } });
 			 */
 		} else {
+			if (!validateNotDisturbSingle(uid)) {
+				return; // 閫氱煡鍏嶆墦鎵�(20:00-24:00 / 00:00 -08:00)
+			}
+
 			// IOS 鍗曟帹
-			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
+			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
 			List<String> deviceTokenList = new ArrayList<>();
 			if (deviceTokenIosList != null)
 				for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -99,7 +112,7 @@
 	}
 
 	@Override
-	public void pushUrl(Long uid, String url, String title, String content) throws PushException {
+	public void pushUrl(Long uid, String url, String title, String content, List<String> listVersion) throws PushException {
 		JSONObject json = new JSONObject();
 		json.put("url", url);
 		json.put("type", "url");
@@ -121,7 +134,7 @@
 		if (uid == null || uid == 0) {
 			// TODO 闇�瑕佸叏鎺�
 
-			addPushIOSQueue(info, url, PushController.URL);
+			addPushIOSQueue(info, url, PushController.URL, listVersion);
 
 			/*
 			 * List<String> deviceTokenList = getDeviceTokenList(1, 1000);
@@ -135,7 +148,11 @@
 			 * } catch (Exception e) { PushLogHelper.iosError(e); } } });
 			 */
 		} else {
-			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
+			if (!validateNotDisturbSingle(uid)) {
+				return; // 閫氱煡鍏嶆墦鎵�(20:00-24:00 / 00:00 -08:00)
+			}
+
+			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
 			List<String> deviceTokenList = new ArrayList<>();
 			if (deviceTokenIosList != null)
 				for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -150,12 +167,7 @@
 	}
 
 	@Override
-	public void pushWEEXUrl(Long uid, String weexUrl, String title, String content) throws PushException {
-
-	}
-
-	@Override
-	public void pushZNX(Long uId, AccountMessage msg, SystemZnx systemZNX) throws PushException {
+	public void pushZNX(Long uId, AccountMessage msg, SystemZnx systemZNX, List<String> listVersion) throws PushException {
 		MessageInfo info = new MessageInfo();
 		info.setPackageName(Constant.systemCommonConfig.getAndroidPackageName());
 
@@ -185,7 +197,7 @@
 
 			// TODO 闇�瑕佸叏鎺�
 
-			addPushIOSQueue(info, id + "", PushController.ZNX);
+			addPushIOSQueue(info, id + "", PushController.ZNX, listVersion);
 
 			/*
 			 * List<String> deviceTokenList = getDeviceTokenList(1, 1000);
@@ -200,6 +212,13 @@
 			 * } });
 			 */
 		} else if (msg != null) {
+
+			if (uId != null) {
+				if (!validateNotDisturbSingle(uId)) {
+					return; // 閫氱煡鍏嶆墦鎵�(20:00-24:00 / 00:00 -08:00)
+				}
+			}
+
 			// 鎻掑叆鎺ㄩ�佽褰�
 			PushRecord pushRecord = new PushRecord();
 			info.setTitle(msg.getTitle());
@@ -221,7 +240,7 @@
 			json.put("miPushUrl", String.format("%s.ui.mine.AppMailDetailActivity",
 					Constant.systemCommonConfig.getAndroidBaseactivityName()));
 
-			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(msg.getUserInfo().getId());
+			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(msg.getUserInfo().getId(), listVersion);
 			List<String> deviceTokenList = new ArrayList<>();
 			if (deviceTokenIosList != null)
 				for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -241,7 +260,7 @@
 	@Override
 	public void addDeviceToken(Long uid, int version, String deviceToken, String device) {
 		// 濡傛灉device涓虹┖灏辫繑鍥�
-		if (StringUtil.isNullOrEmpty(device))
+		if (StringUtil.isNullOrEmpty(device) || StringUtil.isNullOrEmpty(deviceToken))
 			return;
 
 		// 鍒ゆ柇device鏄惁瀛樺湪
@@ -252,21 +271,33 @@
 			deviceTokenIos.setUpdateTime(new Date());
 			deviceTokenIos.setDevice(device);
 			deviceTokenIos.setDeviceToken(deviceToken);
+			deviceTokenIos.setDeviceTokenMd5(StringUtil.Md5(deviceToken));
 			deviceTokenIos.setUid(uid);
 			deviceTokenIos.setVersion(version);
 			deviceTokenIOSMapper.insertSelective(deviceTokenIos);
+		} else {// 鏇存柊devicetoken
+			DeviceTokenIOS updateDeviceTokenIOS = new DeviceTokenIOS();
+			updateDeviceTokenIOS.setId(deviceTokenIos.getId());
+			updateDeviceTokenIOS.setDeviceToken(deviceToken);
+			updateDeviceTokenIOS.setDeviceTokenMd5(StringUtil.Md5(deviceToken));
+			updateDeviceTokenIOS.setUid(uid);
+			updateDeviceTokenIOS.setUpdateTime(new Date());
+			deviceTokenIOSMapper.updateByPrimaryKeySelective(updateDeviceTokenIOS);
 		}
 
 	}
 
 	@Override
-	public List<DeviceTokenIOS> getDeviceTokenListByUid(Long uid) {
-		return deviceTokenIOSMapper.selectByUid(uid);
+	public List<DeviceTokenIOS> getDeviceTokenListByUid(Long uid, List<String> listVersion) {
+		// 鐗堟湰鍙�
+		List<Integer> listCode = appVersionService.listVersionCodeByVersions(AppVersionInfo.PLATFORM_IOS,listVersion);
+
+		return deviceTokenIOSMapper.selectByUid(uid, listCode);
 	}
 
 	@Override
 	public List<String> getDeviceTokenList(int page, int size) {
-		List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList((long) ((page - 1) * size), size);
+		List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList((long) ((page - 1) * size), size, null);
 		List<String> tokenList = new ArrayList<>();
 		for (DeviceTokenIOS ios : list)
 			if (!StringUtil.isNullOrEmpty(ios.getDeviceToken()))
@@ -288,8 +319,8 @@
 	}
 
 	@Override
-	public DeviceTokenIOS getDeviceTokenByDevice(String device) {
-		return deviceTokenIOSMapper.selectByDevice(device);
+	public DeviceTokenIOS getDeviceTokenByDeviceToken(String deviceToken) {
+		return deviceTokenIOSMapper.selectByDeviceToken(deviceToken);
 	}
 
 	/**
@@ -298,7 +329,7 @@
 	 * @param json
 	 */
 	@Transactional
-	public void addPushIOSQueue(MessageInfo info, String url, int type) {
+	public void addPushIOSQueue(MessageInfo info, String url, int type, List<String> listVersion) {
 
 		try {
 
@@ -313,6 +344,20 @@
 			// 姣忔鎵ц鏉$洰
 			int pushNum = 50;
 
+			String versionCodes = "";
+			List<Integer> listCode = appVersionService.listVersionCodeByVersions(AppVersionInfo.PLATFORM_IOS,listVersion);
+			
+			if (listCode != null && listCode.size() > 0) {
+				for (Integer code: listCode) {
+					versionCodes = versionCodes + code + ",";
+				}
+				
+				// 鍘婚櫎鏈熬閫楀彿
+				if (versionCodes.endsWith(",")) {
+					versionCodes = versionCodes.substring(0, versionCodes.length() - 1);
+				}
+			}
+			
 			for (long i = 0; i < tokenCount + totalCount;) {
 
 				PushQueueRecord pushQueueRecord = new PushQueueRecord();
@@ -326,6 +371,9 @@
 				pushQueueRecord.setPushNum(pushNum);
 				// 鎺ㄩ�佺 IOS
 				pushQueueRecord.setType(1);
+				// 鐗堟湰鍙�
+				pushQueueRecord.setVersions(versionCodes);
+				
 
 				if (tokenCount - i < totalCount) {
 					// 璧峰娓告爣
@@ -351,7 +399,7 @@
 					// 缁撴潫娓告爣
 					pushQueueRecord.setEndCursor(endCursor);
 
-					// TODO 瀛樺叆鏁版嵁搴�
+					// 瀛樺叆鏁版嵁搴�
 					pushQueueRecordMapper.insert(pushQueueRecord);
 					// 鍔犲叆闃熷垪
 					CMQManager.getInstance().addIOSPushMsg(pushQueueRecord);
@@ -361,7 +409,6 @@
 			}
 
 		} catch (Exception e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 
@@ -377,7 +424,13 @@
 		Long startCursor = pushQueueRecord.getStartCursor();
 		// 缁撴潫浣嶇疆
 		Long endCursor = pushQueueRecord.getEndCursor();
-
+		// 鐗堟湰鍙�
+		String[] versionArray = null;
+		String versions = pushQueueRecord.getVersions();
+		if (versions != null && versions.trim().length() == 0) {
+			versionArray = versions.split(",");
+		}
+		
 		if (startCursor == null && endCursor == null) {
 			return;
 		}
@@ -396,11 +449,12 @@
 		long recordCursor = startCursor;
 
 		for (int i = 0; i < count; i++) {
-
+			
+			
 			// 涓嶈冻50涓暟鎹�
 			if (moveNum > total) {
 				moveNum = (int) (endCursor - startCursor);
-				List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(startCursor, moveNum);
+				List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(startCursor, moveNum, versionArray);
 				recordCursor = endCursor;
 
 				if (list != null) {
@@ -414,7 +468,7 @@
 					// System.out.println("recordCursorend:" +recordCursor);
 					moveNum = (int) (endCursor - recordCursor);
 					// System.out.println(moveNum);
-					List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum);
+					List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum, versionArray);
 					if (list != null) {
 						executePushIOS(list, pushQueueRecord, endCursor);
 						PushLogHelper.iosInfo("鎺ㄩ�佹父鏍�:" + recordCursor + "锛�" + moveNum);
@@ -422,7 +476,7 @@
 					recordCursor = endCursor;
 
 				} else {
-					List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum);
+					List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum, versionArray);
 					recordCursor = recordCursor + 50;
 
 					if (list != null) {
@@ -443,20 +497,36 @@
 		JSONObject json = JSONObject.fromObject(jsonContent);
 
 		List<String> tokenList = new ArrayList<>();
+
+		boolean ispush = false;
+		List<Long> listNotPushId = null;
+
+		Calendar now = Calendar.getInstance();
+		int hour = now.get(Calendar.HOUR_OF_DAY);
+		if (hour >= 8 && hour < 20) {
+			ispush = true; // 鍙帹閫�
+		} else {
+			listNotPushId = validateNotDisturb();
+		}
+
 		for (DeviceTokenIOS ios : list) {
+
 			if (!StringUtil.isNullOrEmpty(ios.getDeviceToken())) {
+				if (!ispush && listNotPushId != null && listNotPushId.size() > 0) {
+					Long uid = ios.getUid();
+					if (uid != null && listNotPushId.contains(uid)) {
+						continue; // 閫氱煡鍏嶆墦鎵�(20:00-24:00 / 00:00 -08:00)
+					}
+				}
 				tokenList.add(ios.getDeviceToken());
-				// System.out.println(ios.getDeviceToken());
 			}
 		}
 
 		if (tokenList.size() > 0) {
-			// System.out.println(tokenList.size());
 			// 鎵ц鎺ㄩ��
 			try {
 				IOSPushUtil.executePushIOS(tokenList, json);
 			} catch (Exception e) {
-				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
 		}
@@ -464,8 +534,119 @@
 		pushQueueRecord.setEndtime(new Date());
 		pushQueueRecord.setRecordCursor(recordCursor);
 
-		// TODO 鏇存柊鏁版嵁搴�
+		// 鏇存柊鏁版嵁搴�
 		pushQueueRecordMapper.updateByPrimaryKeySelective(pushQueueRecord);
 	}
 
+	@Override
+	public void unBindUidAndDevice(String device) {
+		if (StringUtil.isNullOrEmpty(device))
+			return;
+		DeviceTokenIOS deviceTokenIOS = deviceTokenIOSMapper.selectByDevice(device);
+		if (device != null) {
+			deviceTokenIOS.setUpdateTime(new Date());
+			deviceTokenIOS.setUid(null);
+			deviceTokenIOSMapper.updateByPrimaryKey(deviceTokenIOS);
+		}
+	}
+
+	@Override
+	public void pushWEEX(Long uid, String title, String content, String weexUrl, List<String> listVersion) throws PushException {
+		MessageInfo info = new MessageInfo();
+		info.setTitle(title);
+		info.setContent(content);
+		info.setDescription(content);
+		// IOS 鍏ㄦ帹
+		if (uid == null || uid == 0) {
+			addPushIOSQueue(info, weexUrl, PushController.WEEX, listVersion);
+		} else {
+			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
+			List<String> deviceTokenList = new ArrayList<>();
+			if (deviceTokenIosList != null)
+				for (DeviceTokenIOS ios : deviceTokenIosList) {
+					deviceTokenList.add(ios.getDeviceToken());
+				}
+			try {
+				IOSPushUtil.allPushIOS(deviceTokenList, info, weexUrl, PushController.WEEX);
+			} catch (Exception e) {
+				PushLogHelper.iosError(e);
+			}
+		}
+	}
+
+	@Override
+	public void pushBaiChuanUrl(Long uid, String title, String content, String url, List<String> listVersion) throws PushException {
+		MessageInfo info = new MessageInfo();
+		info.setTitle(title);
+		info.setContent(content);
+		info.setDescription(content);
+		// IOS 鍏ㄦ帹
+		if (uid == null || uid == 0) {
+			addPushIOSQueue(info, url, PushController.BAICHUAN, listVersion);
+		} else {
+			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
+			List<String> deviceTokenList = new ArrayList<>();
+			if (deviceTokenIosList != null)
+				for (DeviceTokenIOS ios : deviceTokenIosList) {
+					deviceTokenList.add(ios.getDeviceToken());
+				}
+			try {
+				IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushController.BAICHUAN);
+			} catch (Exception e) {
+				PushLogHelper.iosError(e);
+			}
+		}
+	}
+
+	/**
+	 * 閫氱煡鍏嶆墦鎵�(20:00-8:00)
+	 */
+	public List<Long> validateNotDisturb() {
+		return userCustomSettingsService.getCancelNoticeUsers();
+	}
+
+	/**
+	 * 閫氱煡鍏嶆墦鎵�(20:00-8:00)
+	 */
+	public boolean validateNotDisturbSingle(Long uid) {
+		Calendar now = Calendar.getInstance();
+		int hour = now.get(Calendar.HOUR_OF_DAY);
+		boolean ispush;
+		if (hour >= 8 && hour < 20) {
+			ispush = true; // 鍙帹閫�
+		} else {
+			// 鑷畾涔夎缃� 閫氱煡鍏嶆墦鎵�(20:00-24:00 / 00:00 -08:00)
+			ispush = userCustomSettingsService.validateCancelNoticeByUid(uid);
+		}
+
+		return ispush;
+	}
+
+	@Override
+	public void pushWelfareCenter(Long uid, String title, String content, List<String> listVersion) throws PushException {
+		MessageInfo info = new MessageInfo();
+		info.setTitle(title);
+		info.setContent(content);
+		info.setDescription(content);
+		// IOS 鍏ㄦ帹
+		if (uid == null || uid == 0) {
+			addPushIOSQueue(info, "", PushController.WELFARE_CENTER, listVersion);
+		} else {
+			if (!validateNotDisturbSingle(uid)) {
+				return;
+			}
+			List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
+			List<String> deviceTokenList = new ArrayList<>();
+			if (deviceTokenIosList != null)
+				for (DeviceTokenIOS ios : deviceTokenIosList) {
+					deviceTokenList.add(ios.getDeviceToken());
+				}
+			try {
+				IOSPushUtil.allPushIOS(deviceTokenList, info, "", PushController.WELFARE_CENTER);
+			} catch (Exception e) {
+				PushLogHelper.iosError(e);
+			}
+		}
+	}
+
 }

--
Gitblit v1.8.0