admin
2019-03-15 a8e45a802600ca1cde28fe8522a26635328bbd99
fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java
@@ -1,6 +1,8 @@
package com.yeshi.fanli.service.impl.push;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@@ -12,6 +14,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;
@@ -20,7 +23,9 @@
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.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;
@@ -37,8 +42,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();
@@ -64,7 +76,7 @@
      if (uid == null || uid == 0) {
         addPushIOSQueue(info, url, PushController.GOODS);
         addPushIOSQueue(info, url, PushController.GOODS, listVersion);
         /*
          * // 查询IOS所有设备号 // TODO 需要全推 List<String> deviceTokenList =
@@ -79,8 +91,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) {
@@ -97,7 +113,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");
@@ -119,7 +135,7 @@
      if (uid == null || uid == 0) {
         // TODO 需要全推
         addPushIOSQueue(info, url, PushController.URL);
         addPushIOSQueue(info, url, PushController.URL, listVersion);
         /*
          * List<String> deviceTokenList = getDeviceTokenList(1, 1000);
@@ -133,7 +149,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) {
@@ -148,7 +168,7 @@
   }
   @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());
@@ -178,7 +198,7 @@
         // TODO 需要全推
         addPushIOSQueue(info, id + "", PushController.ZNX);
         addPushIOSQueue(info, id + "", PushController.ZNX, listVersion);
         /*
          * List<String> deviceTokenList = getDeviceTokenList(1, 1000);
@@ -193,6 +213,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());
@@ -214,7 +241,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) {
@@ -262,13 +289,22 @@
   }
   @Override
   public List<DeviceTokenIOS> getDeviceTokenListByUid(Long uid) {
      return deviceTokenIOSMapper.selectByUid(uid);
   public List<DeviceTokenIOS> getDeviceTokenListByUid(Long uid, List<String> listVersion) throws PushException {
      List<Integer> listCode = null;
      if (listVersion != null && listVersion.size() > 0) {
         // 版本号推送限制
         listCode = appVersionService.listVersionCodeByVersions(AppVersionInfo.PLATFORM_IOS,listVersion);
         if (listCode == null || listCode.size() == 0) {
            throw new PushException(1, "IOS推送失败,推送版本不存在!");
         }
      }
      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()))
@@ -300,7 +336,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) throws PushException {
      try {
@@ -315,6 +351,30 @@
         // 每次执行条目
         int pushNum = 50;
         String versionCodes = "";
         if (listVersion != null && listVersion.size() > 0) {
            // 加入版本推送限制
            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);
               }
            }
            if (versionCodes == null || versionCodes.trim().length() == 0) {
               throw new PushException(1, "IOS推送失败,推送版本不存在!");
            }
         }
         for (long i = 0; i < tokenCount + totalCount;) {
            PushQueueRecord pushQueueRecord = new PushQueueRecord();
@@ -328,6 +388,9 @@
            pushQueueRecord.setPushNum(pushNum);
            // 推送端 IOS
            pushQueueRecord.setType(1);
            // 版本号
            pushQueueRecord.setVersions(versionCodes);
            if (tokenCount - i < totalCount) {
               // 起始游标
@@ -353,7 +416,7 @@
               // 结束游标
               pushQueueRecord.setEndCursor(endCursor);
               // TODO 存入数据库
               // 存入数据库
               pushQueueRecordMapper.insert(pushQueueRecord);
               // 加入队列
               CMQManager.getInstance().addIOSPushMsg(pushQueueRecord);
@@ -363,7 +426,6 @@
         }
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
@@ -379,11 +441,33 @@
      Long startCursor = pushQueueRecord.getStartCursor();
      // 结束位置
      Long endCursor = pushQueueRecord.getEndCursor();
      if (startCursor == null && endCursor == null) {
         return;
      }
      // 推送的版本号
      String versions = pushQueueRecord.getVersions();
      List<Integer> listVersionCode = null;
      if (versions != null && versions.trim().length() > 0) {
         List<String> listVersion  = Arrays.asList(versions.split(","));
         if (listVersion != null && listVersion.size() > 0) {
            listVersionCode = new ArrayList<Integer>();
            for (String version: listVersion) {
               if (version != null && version.trim().length() > 0) {
                  listVersionCode.add(Integer.parseInt(version));
               }
            }
         }
         if (listVersionCode == null || listVersionCode.size() == 0) {
            return; //版本号存在异常
         }
      }
      int total = (int) (endCursor - startCursor);
      // 每次推送总条数
@@ -398,11 +482,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, listVersionCode);
            recordCursor = endCursor;
            if (list != null) {
@@ -416,7 +501,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, listVersionCode);
               if (list != null) {
                  executePushIOS(list, pushQueueRecord, endCursor);
                  PushLogHelper.iosInfo("推送游标:" + recordCursor + "," + moveNum);
@@ -424,7 +509,7 @@
               recordCursor = endCursor;
            } else {
               List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum);
               List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum, listVersionCode);
               recordCursor = recordCursor + 50;
               if (list != null) {
@@ -432,11 +517,8 @@
                  PushLogHelper.iosInfo("推送游标:" + recordCursor + "," + moveNum);
               }
            }
            // System.out.println("recordCursor:" +recordCursor);
         }
      }
   }
   public void executePushIOS(List<DeviceTokenIOS> list, PushQueueRecord pushQueueRecord, long recordCursor) {
@@ -445,20 +527,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();
         }
      }
@@ -466,7 +564,7 @@
      pushQueueRecord.setEndtime(new Date());
      pushQueueRecord.setRecordCursor(recordCursor);
      // TODO 更新数据库
      // 更新数据库
      pushQueueRecordMapper.updateByPrimaryKeySelective(pushQueueRecord);
   }
@@ -483,16 +581,16 @@
   }
   @Override
   public void pushWEEX(Long uid, String title, String content, String weexUrl) throws PushException {
   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);
         addPushIOSQueue(info, weexUrl, PushController.WEEX, listVersion);
      } else {
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -507,16 +605,16 @@
   }
   @Override
   public void pushBaiChuanUrl(Long uid, String title, String content, String url) throws PushException {
   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);
         addPushIOSQueue(info, url, PushController.BAICHUAN, listVersion);
      } else {
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, listVersion);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -530,4 +628,55 @@
      }
   }
   /**
    * 通知免打扰(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);
         }
      }
   }
}