admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/service/impl/push/IOSPushServiceImpl.java
@@ -1,25 +1,27 @@
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;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.dto.push.PushTypeEnum;
import com.yeshi.fanli.entity.bus.user.AccountMessage;
import com.yeshi.fanli.entity.push.DeviceTokenIOS;
import com.yeshi.fanli.entity.push.PushQueueRecord;
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.PushLogHelper;
import com.yeshi.fanli.service.inter.push.IOSPushService;
import com.yeshi.fanli.service.inter.user.UserCustomSettingsService;
@@ -41,9 +43,11 @@
   @Resource
   private UserCustomSettingsService userCustomSettingsService;
   @Async("pushExecutor")
   @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, String versionCodes)
         throws PushException {
      String url = "https://item.taobao.com/item.htm?id=" + auctionId;
      JSONObject json = new JSONObject();
@@ -62,14 +66,14 @@
      PushRecord pushRecord = new PushRecord();
      pushRecord.setState(1);
      pushRecord.setType(PushController.GOODS); // 都成功
      pushRecord.setType(PushTypeEnum.goodsdetail.getCode()); // 都成功
      pushRecord.setUrl(url);
      pushRecord.setTitle(title);
      pushRecord.setAppName(Constant.systemCommonConfig.getProjectChineseName());
      if (uid == null || uid == 0) {
         addPushIOSQueue(info, url, PushController.GOODS);
         addPushIOSQueue(info, url, PushTypeEnum.goodsdetail.getCode(), versionCodes);
         /*
          * // 查询IOS所有设备号 // TODO 需要全推 List<String> deviceTokenList =
@@ -85,11 +89,12 @@
          */
      } else {
         if (!validateNotDisturbSingle(uid)) {
            return; //通知免打扰(20:00-24:00 / 00:00 -08:00)
            return; // 通知免打扰(20:00-24:00 / 00:00 -08:00)
         }
         // IOS 单推
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, versionCodes);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -97,7 +102,7 @@
            }
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushController.GOODS);
            IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushTypeEnum.goodsdetail.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
@@ -105,8 +110,9 @@
      }
   }
   @Async("pushExecutor")
   @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, String versionCodes) throws PushException {
      JSONObject json = new JSONObject();
      json.put("url", url);
      json.put("type", "url");
@@ -128,7 +134,7 @@
      if (uid == null || uid == 0) {
         // TODO 需要全推
         addPushIOSQueue(info, url, PushController.URL);
         addPushIOSQueue(info, url, PushTypeEnum.url.getCode(), versionCodes);
         /*
          * List<String> deviceTokenList = getDeviceTokenList(1, 1000);
@@ -143,25 +149,26 @@
          */
      } else {
         if (!validateNotDisturbSingle(uid)) {
            return; //通知免打扰(20:00-24:00 / 00:00 -08:00)
            return; // 通知免打扰(20:00-24:00 / 00:00 -08:00)
         }
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, versionCodes);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
               deviceTokenList.add(ios.getDeviceToken());
            }
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushController.URL);
            IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushTypeEnum.url.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
      }
   }
   @Async("pushExecutor")
   @Override
   public void pushZNX(Long uId, AccountMessage msg, SystemZnx systemZNX) throws PushException {
   public void pushZNX(Long uId, AccountMessage msg, SystemZnx systemZNX, String versionCodes) throws PushException {
      MessageInfo info = new MessageInfo();
      info.setPackageName(Constant.systemCommonConfig.getAndroidPackageName());
@@ -191,7 +198,7 @@
         // TODO 需要全推
         addPushIOSQueue(info, id + "", PushController.ZNX);
         addPushIOSQueue(info, id + "", PushTypeEnum.ZNX.getCode(), versionCodes);
         /*
          * List<String> deviceTokenList = getDeviceTokenList(1, 1000);
@@ -206,13 +213,13 @@
          * } });
          */
      } else if (msg != null) {
         if (uId !=null) {
         if (uId != null) {
            if (!validateNotDisturbSingle(uId)) {
               return; //通知免打扰(20:00-24:00 / 00:00 -08:00)
               return; // 通知免打扰(20:00-24:00 / 00:00 -08:00)
            }
         }
         // 插入推送记录
         PushRecord pushRecord = new PushRecord();
         info.setTitle(msg.getTitle());
@@ -234,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(), versionCodes);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
@@ -243,7 +250,7 @@
         // IOS 单推
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, "0", PushController.ZNX);
            IOSPushUtil.allPushIOS(deviceTokenList, info, "0", PushTypeEnum.ZNX.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
@@ -258,7 +265,7 @@
         return;
      // 判断device是否存在
      DeviceTokenIOS deviceTokenIos = deviceTokenIOSMapper.selectByDevice(device);
      DeviceTokenIOS deviceTokenIos = deviceTokenIOSMapper.selectByDeviceForUpdate(device);
      if (deviceTokenIos == null) {
         deviceTokenIos = new DeviceTokenIOS();
         deviceTokenIos.setCreateTime(new Date());
@@ -282,13 +289,13 @@
   }
   @Override
   public List<DeviceTokenIOS> getDeviceTokenListByUid(Long uid) {
      return deviceTokenIOSMapper.selectByUid(uid);
   public List<DeviceTokenIOS> getDeviceTokenListByUid(Long uid, String versionCodes) throws PushException {
      return deviceTokenIOSMapper.selectByUid(uid, convertVersionType(versionCodes));
   }
   @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()))
@@ -319,8 +326,11 @@
    * 
    * @param json
    */
   @Transactional
   public void addPushIOSQueue(MessageInfo info, String url, int type) {
   @Transactional(rollbackFor=Exception.class)
   public void addPushIOSQueue(MessageInfo info, String url, int type, String versionCodes) throws PushException {
      if (Constant.IS_TEST)
         return;
      try {
@@ -348,6 +358,8 @@
            pushQueueRecord.setPushNum(pushNum);
            // 推送端 IOS
            pushQueueRecord.setType(1);
            // 版本号
            pushQueueRecord.setVersions(versionCodes);
            if (tokenCount - i < totalCount) {
               // 起始游标
@@ -403,6 +415,10 @@
         return;
      }
      // 推送的版本号
      String versions = pushQueueRecord.getVersions();
      List<Integer> listCode = convertVersionType(versions);
      int total = (int) (endCursor - startCursor);
      // 每次推送总条数
@@ -421,7 +437,7 @@
         // 不足50个数据
         if (moveNum > total) {
            moveNum = (int) (endCursor - startCursor);
            List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(startCursor, moveNum);
            List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(startCursor, moveNum, listCode);
            recordCursor = endCursor;
            if (list != null) {
@@ -435,7 +451,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, listCode);
               if (list != null) {
                  executePushIOS(list, pushQueueRecord, endCursor);
                  PushLogHelper.iosInfo("推送游标:" + recordCursor + "," + moveNum);
@@ -443,7 +459,7 @@
               recordCursor = endCursor;
            } else {
               List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum);
               List<DeviceTokenIOS> list = deviceTokenIOSMapper.selectList(recordCursor, moveNum, listCode);
               recordCursor = recordCursor + 50;
               if (list != null) {
@@ -451,38 +467,37 @@
                  PushLogHelper.iosInfo("推送游标:" + recordCursor + "," + moveNum);
               }
            }
            // System.out.println("recordCursor:" +recordCursor);
         }
      }
   }
   public void executePushIOS(List<DeviceTokenIOS> list, PushQueueRecord pushQueueRecord, long recordCursor) {
      String jsonContent = pushQueueRecord.getJsonContent();
      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();
      }
//      if (hour >= 8 && hour < 20) {
//         ispush = true; // 可推送
//      } else {
//         listNotPushId = validateNotDisturb();
//      }
      
      ispush=true;//TODO 处理消息免打扰
      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)
                  continue; // 通知免打扰(20:00-24:00 / 00:00 -08:00)
               }
            }
            tokenList.add(ios.getDeviceToken());
@@ -517,63 +532,67 @@
      }
   }
   @Async("pushExecutor")
   @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, String versionCodes)
         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, PushTypeEnum.weex.getCode(), versionCodes);
      } else {
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, versionCodes);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
               deviceTokenList.add(ios.getDeviceToken());
            }
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, weexUrl, PushController.WEEX);
            IOSPushUtil.allPushIOS(deviceTokenList, info, weexUrl, PushTypeEnum.weex.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
      }
   }
   @Async("pushExecutor")
   @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, String versionCodes)
         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, PushTypeEnum.baichuan.getCode(), versionCodes);
      } else {
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid);
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, versionCodes);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
               deviceTokenList.add(ios.getDeviceToken());
            }
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushController.BAICHUAN);
            IOSPushUtil.allPushIOS(deviceTokenList, info, url, PushTypeEnum.baichuan.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
      }
   }
   /**
    *  通知免打扰(20:00-8:00)
    * 通知免打扰(20:00-8:00)
    */
   public List<Long> validateNotDisturb() {
      return userCustomSettingsService.getCancelNoticeUsers();
   }
   /**
    *  通知免打扰(20:00-8:00)
    * 通知免打扰(20:00-8:00)
    */
   public boolean validateNotDisturbSingle(Long uid) {
      Calendar now = Calendar.getInstance();
@@ -582,12 +601,83 @@
      if (hour >= 8 && hour < 20) {
         ispush = true; // 可推送
      } else {
         // 自定义设置  通知免打扰(20:00-24:00 / 00:00 -08:00)
         // 自定义设置 通知免打扰(20:00-24:00 / 00:00 -08:00)
         ispush = userCustomSettingsService.validateCancelNoticeByUid(uid);
      }
      return ispush;
   }
   @Async("pushExecutor")
   @Override
   public void pushWelfareCenter(Long uid, String title, String content, String versionCodes) throws PushException {
      MessageInfo info = new MessageInfo();
      info.setTitle(title);
      info.setContent(content);
      info.setDescription(content);
      // IOS 全推
      if (uid == null || uid == 0) {
         addPushIOSQueue(info, "", PushTypeEnum.welfare.getCode(), versionCodes);
      } else {
         if (!validateNotDisturbSingle(uid)) {
            return;
         }
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, versionCodes);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
               deviceTokenList.add(ios.getDeviceToken());
            }
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, "", PushTypeEnum.welfare.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
      }
   }
   private List<Integer> convertVersionType(String versionCodes) {
      List<Integer> listVersionCode = null;
      if (versionCodes != null && versionCodes.trim().length() > 0) {
         List<String> listVersion = Arrays.asList(versionCodes.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));
               }
            }
         }
      }
      return listVersionCode;
   }
   @Override
   public void pushUserSignInNotification(Long uid, String title, String content, String versions)
         throws PushException {
      MessageInfo info = new MessageInfo();
      info.setTitle(title);
      info.setContent(content);
      info.setDescription(content);
      // IOS 全推
      if (uid == null || uid == 0) {
         addPushIOSQueue(info, "", PushTypeEnum.signin.getCode(), versions);
      } else {
         List<DeviceTokenIOS> deviceTokenIosList = getDeviceTokenListByUid(uid, versions);
         List<String> deviceTokenList = new ArrayList<>();
         if (deviceTokenIosList != null)
            for (DeviceTokenIOS ios : deviceTokenIosList) {
               deviceTokenList.add(ios.getDeviceToken());
            }
         try {
            IOSPushUtil.allPushIOS(deviceTokenList, info, "", PushTypeEnum.signin.getCode());
         } catch (Exception e) {
            PushLogHelper.iosError(e);
         }
      }
   }
}