yujian
2020-06-19 dcb37a9df7c41be08d3aed13667e9162382d134a
fanli/src/main/java/com/yeshi/fanli/service/impl/user/cloud/UserCloudServiceImpl.java
@@ -8,6 +8,7 @@
import javax.annotation.Resource;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.BigDecimalUtil;
@@ -55,8 +56,11 @@
import com.yeshi.fanli.service.inter.goods.CommonGoodsService;
import com.yeshi.fanli.service.inter.goods.ShareGoodsService;
import com.yeshi.fanli.service.inter.goods.ShareGoodsTextTemplateService;
import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
import com.yeshi.fanli.service.inter.msg.UserSystemMsgService;
import com.yeshi.fanli.service.inter.order.OrderHongBaoMoneyComputeService;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.push.PushService;
import com.yeshi.fanli.service.inter.user.QrCodeService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -159,6 +163,19 @@
   @Resource(name = "producer")
   private Producer producer;
   
   @Resource
   private UserOtherMsgNotificationService userOtherMsgNotificationService;
   @Resource
   private PushService pushService;
   @Resource
   private UserSystemMsgService userSystemMsgService;
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   
   @Override
   public UserCloud getValidByUid(Long uid) {
@@ -197,6 +214,15 @@
      if (userCloud == null)
         throw new UserCloudException(1, "云发单已过期");
      // 检测是否需要更新 - 1分钟间隔
      String key = "cloudLogin_" + uid;
      String result = redisManager.getCommonString(key);
      if (!StringUtil.isNullOrEmpty(result)) {
         return;
      }
      redisManager.cacheCommonString(key , "true" , 60);
      // 更新信息
      UserCloud update = new UserCloud();
      update.setId(userCloud.getId());
@@ -204,7 +230,24 @@
      update.setWxName(wxName);
      update.setWxPortrait(wxPortrait);
      userCloudMapper.updateByPrimaryKeySelective(update);
      try {
         userOtherMsgNotificationService.cloudMsg(uid, "云发单微信账号", "微信账号登录成功", "可以开启你的群发单了");
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      // 更新提醒状态
      UserCloudManage cloudManage = userCloudManageService.selectForUpdate(uid);
      if (cloudManage == null) {
         userCloudManageService.save(uid, false, false);
      } else {
         UserCloudManage updateManage = new UserCloudManage();
         updateManage.setId(uid);
         updateManage.setOfflineNotice(false);
         userCloudManageService.updateByPrimaryKeySelective(updateManage);
      }
      // 微信号变化-清空群信息
      if (!wxId.equals(userCloud.getWxId())) {
         userCloudGroupService.deleteGroupByUid(uid);
@@ -289,6 +332,19 @@
      
      // 开启发圈功能
      userCloudManageService.save(uid, null, null);
      try {
         String item = null;
         if (renew) {
            item = "成功续费" + menuEnum.getDescShow();
         } else {
            item = "成功开通" + menuEnum.getDescShow();
         }
         String desc = "完成充值支付"+ BigDecimal.valueOf(menuEnum.getMoney()).setScale(2, BigDecimal.ROUND_DOWN) +"元";
         userOtherMsgNotificationService.cloudMsg(uid, "云发单充值", item, desc);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
   }
   @Override
@@ -388,10 +444,6 @@
      if (StringUtil.isNullOrEmpty(wxId)) 
         throw new UserCloudException(1003, "微信号不存在,请先微信登录");
      
      if (!AitaokerApiUtil.onlineCheck(robotId)) {
         throw new UserCloudException(1003, "微信已掉线,请先重新登录微信");
      }
      // 验证开启状态
      List<UserCloudGroup> listGroup = userCloudGroupService.listByUid(uid);
      if (listGroup == null || listGroup.size() == 0)
@@ -422,7 +474,10 @@
      // 检测微信是否登录状态
      if (!AitaokerApiUtil.onlineCheck(robotId)) {
         throw new UserCloudException(1003, "请重新登录微信");
         // 通知登录微信
         offlineNotification(uid);
         throw new UserCloudException(1003, "微信已掉线,需要重新扫描二维码登录");
      }
      
      
@@ -466,7 +521,6 @@
      
      String qrCode = null;
      String cloudPic = null;
      ImgInfo imgVideo = null;
      List<String> listImg = new ArrayList<>();
      
@@ -477,26 +531,7 @@
               imgVideo = imgInfo;
               continue;
            }
            if (imgInfo.getType() == ImgEnum.goods) {
               cloudPic = imgInfo.getUrl();
            }
            listImg.add(imgInfo.getUrl());
            // 取消二维码图
//            GoodsDetailVO goodsVO = imgInfo.getGoodsVO();
//            if (goodsVO == null) {
//               listImg.add(imgInfo.getUrl());
//            } else {
//               String jumpLink = getJumpLink(goodsVO, user, relationId, inviteCode, imgInfo.getUrl());
//               if (!StringUtil.isNullOrEmpty(jumpLink)) {
//                  qrCode = jumpLink;
//               }
//            }
         }
         if (cloudPic == null) {
            cloudPic = imgs.get(0).getUrl();
         }
      }
      
@@ -573,16 +608,23 @@
            }
            
            // 发送图片
            if (!StringUtil.isNullOrEmpty(cloudPic)) {
               try {
                  String imgBase64 = ImageToBase64.NetImageToBase64(cloudPic);
                  boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
                  if (macsend)
                     sendContent.setPicUrl(cloudPic);
               } catch (Exception e) {
                  LogHelper.errorDetailInfo(e);
               }
            int num = 1;
            String picNum = configService.get(ConfigKeyEnum.robotCloudGroupPictureNum.getKey());
            if (!StringUtil.isNullOrEmpty(picNum)) {
               num = Integer.parseInt(picNum);
            }
            if (listImg.size() > 0)
               for (int i = 0; i < num && i < listImg.size(); i++) {
                  try {
                     String imgBase64 = ImageToBase64.NetImageToBase64(listImg.get(i));
                     boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
                     if (macsend)
                        sendContent.setPicUrl(listImg.get(i));
                  } catch (Exception e) {
                     LogHelper.errorDetailInfo(e);
                  }
               }
            
            // 评论文本
            List<String> list = new ArrayList<>();
@@ -697,11 +739,6 @@
      if (StringUtil.isNullOrEmpty(wxId)) 
         throw new UserCloudException(1002, "请先登录微信");
      
      if (!AitaokerApiUtil.onlineCheck(robotId)) {
         throw new UserCloudException(1003, "微信已掉线,请先重新登录微信");
      }
      // 验证开启状态
      List<UserCloudGroup> listGroup = userCloudGroupService.listByUid(uid);
      if (listGroup == null || listGroup.size() == 0)
@@ -720,9 +757,11 @@
      
      // 检测微信是否登录状态
      if (!AitaokerApiUtil.onlineCheck(robotId)) {
         throw new UserCloudException(1003, "请重新登录微信");
         // 通知登录微信
         offlineNotification(uid);
         throw new UserCloudException(1003, "微信已掉线,需要重新扫描二维码登录");
      }
      
      // 保存发送记录
      UserCloudSendRecord sendRecord = new UserCloudSendRecord();
@@ -1082,18 +1121,25 @@
            }
            
            // 发送图片
            String picurl = listImg.get(0);
            if (!StringUtil.isNullOrEmpty(picurl)) {
               try {
                  String imgBase64 = ImageToBase64.NetImageToBase64(picurl);
                  boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
                  if (macsend)
                     sendContent.setPicUrl(picurl);
               } catch (Exception e) {
                  LogHelper.errorDetailInfo(e);
            int num = 1;
            String picNum = configService.get(ConfigKeyEnum.robotCloudGroupPictureNum.getKey());
            if (!StringUtil.isNullOrEmpty(picNum)) {
               num = Integer.parseInt(picNum);
            }
            if (listImg.size() > 0)
               for (int i = 0; i < num && i < listImg.size(); i++) {
                  try {
                     String imgBase64 = ImageToBase64.NetImageToBase64(listImg.get(i));
                     boolean macsend = AitaokerApiUtil.macsendImgBase64(robotId, cloudGroup.getGroupId(), imgBase64);
                     if (macsend)
                        sendContent.setPicUrl(listImg.get(i));
                  } catch (Exception e) {
                     LogHelper.errorDetailInfo(e);
                  }
               }
            
            }
            // 评论文本
            List<String> list = new ArrayList<>();
            boolean macsend = AitaokerApiUtil.macsendText(robotId, cloudGroup.getGroupId(), comment);
@@ -1167,6 +1213,65 @@
            }
         }
      }
   }
   private void offlineNotification(Long uid) {
      UserCloudManage cloudManage = userCloudManageService.selectForUpdate(uid);
      if (cloudManage == null) {
          return;
      }
      Boolean offlineNotice = cloudManage.getOfflineNotice();
      if (offlineNotice != null && offlineNotice) {
         return;
      }
      try {
         userOtherMsgNotificationService.cloudMsg(uid, "云发单微信账号", "微信账号掉线", "需要你重新扫描二维码登录");
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
      // 站内信通知
      offlineNotificationZNX(uid);
      // 延迟10分钟再次提醒
      if(!Constant.IS_TEST) {
         UserCloudMQMsg msg = new UserCloudMQMsg(uid, UserCloudMQMsg.TYPE_PUSH);
         Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userCloud, msg);
         message.setStartDeliverTime(java.lang.System.currentTimeMillis() + 1000 * 60 * 10);
         producer.send(message);
      }
      // 更新已提醒
      UserCloudManage updateManage = new UserCloudManage();
      updateManage.setId(uid);
      updateManage.setOfflineNotice(true);
      userCloudManageService.updateByPrimaryKeySelective(updateManage);
   }
   // 站内信通知
   @Override
   public void offlineNotificationZNX(Long uid) {
      // 验证是否开通
      UserCloud userCloud = userCloudMapper.getValidByUid(uid);
      if (userCloud == null)
         return;
      Integer robotId = userCloud.getRobotId();
      if (robotId == null)
         return;
      // 登录状态
      if (AitaokerApiUtil.onlineCheck(robotId)) {
         return;
      }
      try {
         pushService.pushZNX(uid, "【重要通知】你的云发单微信已掉线。", "需要你重新扫描二维码登录", null, null);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
   }
}