admin
2024-04-26 5e7b0ed4a154ad067cbcf4aa1a1c7cce32f9864c
fanli/src/main/java/com/yeshi/fanli/service/impl/push/HWPushServiceImpl.java
@@ -1,236 +1,252 @@
package com.yeshi.fanli.service.impl.push;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dto.push.PushTypeEnum;
import com.yeshi.fanli.entity.AppVersionInfo;
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.exception.push.PushException;
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.StringUtil;
import com.yeshi.fanli.util.push.HWPushUtil;
@Service
public class HWPushServiceImpl implements HWPushService {
   @Resource
   private DeviceTokenHWService deviceTokenHWService;
   @Resource
   private AppVersionService appVersionService;
   @Resource
   private UserCustomSettingsService userCustomSettingsService;
   private List<Integer> getVersionCodeList(List<Integer> versionCodeList) {
      List<Integer> versionList = new ArrayList<>();
      if (versionCodeList == null) {// 全推
         List<AppVersionInfo> appInfoList = appVersionService.listByPlatformAndMinVersionCode("android", 46);
         if (appInfoList != null)
            for (AppVersionInfo version : appInfoList) {
               versionList.add(version.getVersionCode());
            }
      } else {// 按版本推送
         for (Integer versionCode : versionCodeList) {
            if (versionCode.intValue() >= 46) {
               versionList.add(versionCode);
            }
         }
      }
      return versionList;
   }
   @Async("pushExecutor")
   @Override
   public void pushGoods(Long uid, Long auctionId, String title, String content, List<Integer> versionCodeList)
         throws PushException {
      push(PushTypeEnum.goodsdetail, uid, title, content, null, null, auctionId, versionCodeList);
   }
   @Async("pushExecutor")
   @Override
   public void pushUrl(Long uid, String url, String title, String content, List<Integer> versionCodeList)
         throws PushException {
      push(PushTypeEnum.url, uid, title, content, null, url, null, versionCodeList);
   }
   @Async("pushExecutor")
   @Override
   public void pushZNX(Long uid, String title, String content, List<Integer> versionCodeList) throws PushException {
      push(PushTypeEnum.ZNX, uid, title, content, null, null, null, versionCodeList);
   }
   @Async("pushExecutor")
   @Override
   public void pushWEEX(Long uid, String title, String content, String weexUrl, List<Integer> versionCodeList)
         throws PushException {
      push(PushTypeEnum.weex, uid, title, content, weexUrl, null, null, versionCodeList);
   }
   @Async("pushExecutor")
   @Override
   public void pushBaiChuanUrl(Long uid, String title, String content, String url, List<Integer> versionCodeList)
         throws PushException {
      push(PushTypeEnum.baichuan, uid, title, content, null, url, null, versionCodeList);
   }
   @Async("pushExecutor")
   @Override
   public void pushWelfareCenter(Long uid, String title, String content, List<Integer> versionCodeList)
         throws PushException {
      push(PushTypeEnum.welfare, uid, title, content, null, null, null, versionCodeList);
   }
   @Override
   public void pushUserSignInNotification(Long uid, String title, String content, List<Integer> versionCodeList)
         throws PushException {
      push(PushTypeEnum.signin, uid, title, content, null, null, null, versionCodeList);
   }
   private List<DeviceTokenHW> filterDeviceToken(List<DeviceTokenHW> hwDeviceList, int hour) {
      // 处理消息免打扰
      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;
   }
   /**
    * 推送
    *
    * @param type
    * @param uid
    * @param title
    * @param content
    * @param url
    * @param webUrl
    * @param versionCodeList
    */
   private void push(PushTypeEnum type, Long uid, String title, String content, String url, String webUrl,
         Long goodsId, List<Integer> versionCodeList) throws PushException {
      int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
      List<Integer> newVersionList = getVersionCodeList(versionCodeList);
      // 1.6.5后开始推送
      for (int i = 0; i < newVersionList.size(); i++) {
         if (newVersionList.get(i) < 47)
            newVersionList.remove(i--);
      }
      if (uid == null || uid == 0) {
         long deviceCount = deviceTokenHWService.countDeviceToken(newVersionList);
         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);
            hwDeviceList = filterDeviceToken(hwDeviceList, hour);
            List<String> tokenList = new ArrayList<>();
            for (DeviceTokenHW token : hwDeviceList)
               tokenList.add(token.getDeviceToken());
            try {
               if (type == PushTypeEnum.goodsdetail) {// 商品详情
                  if (goodsId == null)
                     throw new PushException(2, "参数不完整");
                  HWPushUtil.pushGoods(tokenList, title, content, 1, goodsId);
               } else if (type == PushTypeEnum.url) {// 链接
                  if (StringUtil.isNullOrEmpty(webUrl))
                     throw new PushException(2, "参数不完整");
                  HWPushUtil.pushUrl(tokenList, title, content, webUrl);
               } else if (type == PushTypeEnum.ZNX) {// 站内信
                  HWPushUtil.pushZNX(tokenList, title, content);
               } else if (type == PushTypeEnum.weex) {// weex
                  if (StringUtil.isNullOrEmpty(url))
                     throw new PushException(2, "参数不完整");
                  HWPushUtil.pushWEEX(tokenList, title, content, url);
               } else if (type == PushTypeEnum.baichuan) {// 百川链接
                  if (StringUtil.isNullOrEmpty(webUrl))
                     throw new PushException(2, "参数不完整");
                  HWPushUtil.pushBaiChuanUrl(tokenList, title, content, webUrl);
               } else if (type == PushTypeEnum.welfare) {// 福利中心
                  HWPushUtil.pushWelfareCenter(tokenList, title, content);
               } else if (type == PushTypeEnum.signin) {// 签到
                  HWPushUtil.pushUserSignInNotification(tokenList, title, content);
               }
            } catch (Exception e) {
               PushLogHelper.hwError(e);
            }
         }
      } else {
         // 华为单推
         List<DeviceTokenHW> hwDeviceList = deviceTokenHWService.getDeviceTokenByUid(uid, newVersionList);
         List<String> tokenList = new ArrayList<>();
         if (hwDeviceList != null) {
            hwDeviceList = filterDeviceToken(hwDeviceList, hour);
            for (DeviceTokenHW token : hwDeviceList)
               tokenList.add(token.getDeviceToken());
         }
         if (tokenList.size() == 0)
            return;
         try {
            if (type == PushTypeEnum.goodsdetail) {// 商品详情
               if (goodsId == null)
                  throw new PushException(2, "参数不完整");
               HWPushUtil.pushGoods(tokenList, title, content, 1, goodsId);
            } else if (type == PushTypeEnum.url) {// 链接
               if (StringUtil.isNullOrEmpty(webUrl))
                  throw new PushException(2, "参数不完整");
               HWPushUtil.pushUrl(tokenList, title, content, webUrl);
            } else if (type == PushTypeEnum.ZNX) {// 站内信
               HWPushUtil.pushZNX(tokenList, title, content);
            } else if (type == PushTypeEnum.weex) {// weex
               if (StringUtil.isNullOrEmpty(url))
                  throw new PushException(2, "参数不完整");
               HWPushUtil.pushWEEX(tokenList, title, content, url);
            } else if (type == PushTypeEnum.baichuan) {// 百川链接
               if (StringUtil.isNullOrEmpty(webUrl))
                  throw new PushException(2, "参数不完整");
               HWPushUtil.pushBaiChuanUrl(tokenList, title, content, webUrl);
            } else if (type == PushTypeEnum.welfare) {// 福利中心
               HWPushUtil.pushWelfareCenter(tokenList, title, content);
            } else if (type == PushTypeEnum.signin) {// 签到
               HWPushUtil.pushUserSignInNotification(tokenList, title, content);
            }
         } catch (Exception e) {
            PushLogHelper.hwError(e);
         }
      }
   }
}
package com.yeshi.fanli.service.impl.push;
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.config.SystemConfigKeyEnum;
import com.yeshi.fanli.entity.push.DeviceTokenHW;
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.config.SystemConfigService;
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.StringUtil;
import com.yeshi.fanli.util.mq.cmq.PushCMQManager;
import com.yeshi.fanli.util.push.HWPushUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
@Service
public class HWPushServiceImpl implements HWPushService {
    @Resource
    private DeviceTokenHWService deviceTokenHWService;
    @Resource
    private AppVersionService appVersionService;
    @Resource
    private UserCustomSettingsService userCustomSettingsService;
    @Resource
    private SystemConfigService systemConfigService;
    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());
                }
        } else {// 按版本推送
            for (Integer versionCode : versionCodeList) {
                if (versionCode.intValue() >= 46) {
                    versionList.add(versionCode);
                }
            }
        }
        return versionList;
    }
    @Override
    public void pushGoods(Long uid, String auctionId, PushBaseContent baseContent)
            throws PushException {
        addPushQueue(PushTypeEnum.goodsdetail, uid, null, null, auctionId, baseContent);
    }
    @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 pushWEEX(Long uid, String weexUrl, PushBaseContent baseContent)
            throws PushException {
        addPushQueue(PushTypeEnum.weex, uid, weexUrl, null, null, baseContent);
    }
    @Override
    public void pushBaiChuanUrl(Long uid, String url, PushBaseContent baseContent)
            throws PushException {
        addPushQueue(PushTypeEnum.baichuan, uid, null, url, null, baseContent);
    }
    @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,
                              String 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(),getAndroidBaseActivity( dto.getContent().getSystem()));
                    } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                    } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                    } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                    } else if (dto.getType() == PushTypeEnum.signin) {// 签到
                        HWPushUtil.pushUserSignInNotification(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(),getAndroidBaseActivity(dto.getContent().getSystem()));
                    }
                } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                } 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(),getAndroidBaseActivity(dto.getContent().getSystem()));
                } else if (dto.getType() == PushTypeEnum.signin) {// 签到
                    HWPushUtil.pushUserSignInNotification(tokenList, dto.getContent().getTitle(), dto.getContent().getContent(),getAndroidBaseActivity(dto.getContent().getSystem()));
                }
            } catch (Exception e) {
                PushLogHelper.hwError(e);
            }
        }
    }
    private String getAndroidBaseActivity(SystemEnum system) {
        return systemConfigService.getValueCache(SystemConfigKeyEnum.androidBaseActivityName, system);
    }
}