admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java
@@ -1,5 +1,7 @@
package com.yeshi.fanli.service.impl.user;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@@ -9,19 +11,22 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.FileUtil;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.entity.FileUploadResult;
import org.yeshi.utils.tencentcloud.COSManager;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper;
import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
@@ -34,6 +39,8 @@
import com.yeshi.fanli.entity.bus.user.UserRank;
import com.yeshi.fanli.entity.bus.user.UserRankRecord;
import com.yeshi.fanli.entity.bus.user.WeiXinUser;
import com.yeshi.fanli.entity.bus.user.notify.UserActivedRecord;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.user.ThreeSaleException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.log.LogHelper;
@@ -46,7 +53,10 @@
import com.yeshi.fanli.service.inter.user.UserInviteRecordService;
import com.yeshi.fanli.service.inter.user.UserRankService;
import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.user.msg.UserAccountMsgNotificationService;
import com.yeshi.fanli.service.inter.user.notify.UserActivedRecordService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.FilePathEnum;
import com.yeshi.fanli.util.InviteCodeFilterUtil;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.account.UserUtil;
@@ -54,6 +64,8 @@
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import com.yeshi.fanli.util.wx.WXLoginUtil;
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
import net.coobird.thumbnailator.Thumbnails;
@Service
public class UserInfoExtraServiceImpl implements UserInfoExtraService {
@@ -88,6 +100,13 @@
   @Resource
   private UserInviteRecordService userInviteRecordService;
   @Resource
   @Lazy
   private UserActivedRecordService userActivedRecordService;
   @Resource
   private UserAccountMsgNotificationService userAccountMsgNotificationService;
   @Resource(name = "producer")
   private Producer producer;
@@ -100,10 +119,10 @@
      }
      // 等级对照表
      String contrast = configService.get("user_rank_contrast");
      String contrast = configService.get(ConfigKeyEnum.userRankContrast.getKey());
      extraVO.setRankContrast(contrast);
      // 特别提醒
      String rankTip = configService.get("user_rank_tip");
      String rankTip = configService.get(ConfigKeyEnum.userRankTip.getKey());
      extraVO.setRankTip(rankTip);
      Integer rankOrderNum = extraVO.getRankOrderNum();
@@ -338,46 +357,14 @@
         throw new UserInfoExtraException(1, "已经激活, 无需再次激活");
      inviteCode = inviteCode.toUpperCase();
      // 邀请码对应用户信息
      UserInfo inviter = userInfoService.getUserInfoByInviteCode(inviteCode);
      if (inviter == null)
      UserInfo boss = userInfoService.getUserInfoByInviteCode(inviteCode);
      if (boss == null || boss.getState() != UserInfo.STATE_NORMAL)
         throw new UserInfoExtraException(1, "上级邀请码不存在");
      // 绑定关系
      try {
         threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter);
      } catch (ThreeSaleException e) {
         try {
            LogHelper.errorDetailInfo(e);
         } catch (Exception e1) {
            e1.printStackTrace();
         }
         throw new UserInfoExtraException(1, "激活失败");
      }
      // 生成邀请码
      String mycode = createInviteCode(uid);
      if (StringUtil.isNullOrEmpty(mycode))
         throw new UserInfoExtraException(1, "激活码生成失败");
      UserInfoExtra userInfoExtra = new UserInfoExtra();
      userInfoExtra.setUserInfo(invitee);
      userInfoExtra.setInviteCode(mycode);
      userInfoExtra.setUpdateTime(new Date());
      if (extra != null) {
         userInfoExtra.setId(extra.getId());
         userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
      } else {
         userInfoExtra.setCreateTime(new Date());
         userInfoExtraMapper.insertSelective(userInfoExtra);
      }
      // 发送激活成功消息
      Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.inviteCodeActive,
            new Gson().toJson(new InviteCodeActiveMQMsg(uid, new Date())));
      producer.send(message);
      return mycode;
      // 生成邀请关系
      return inviteCodeActive(uid, boss.getId());
   }
   @Override
@@ -439,8 +426,9 @@
            if (asInputStream == null) {
               LogHelper.test("微信头像下载失败: " + weiXinUser.getUnionid() + " " + headimgurl);
            } else {
               FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String.format(
                     "/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
               FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                     String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(),
                           System.currentTimeMillis() + ""));
               if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
                  headimgurl = result.getUrl();
               } else {
@@ -460,39 +448,14 @@
      Long inviterId = userInviteRecordService.getNewestInviterId(wxUnionId);
      if (inviterId == null)
         throw new UserInfoExtraException(1, "没有对应的邀请关系");
      // 绑定关系
      try {
         threeSaleSerivce.bindRelationshipByWX(invitee, inviterId);
      } catch (ThreeSaleException e) {
         try {
            LogHelper.errorDetailInfo(e);
         } catch (Exception e1) {
            e1.printStackTrace();
         }
         throw new UserInfoExtraException(1, "关系绑定时失败");
      UserInfo boss = userInfoService.selectByPKey(inviterId);
      if (boss == null || boss.getState() != UserInfo.STATE_NORMAL) {
         throw new UserInfoExtraException(1, "没有对应的邀请关系");
      }
      // 生成邀请码
      String inviteCode = createInviteCode(uid);
      if (StringUtil.isNullOrEmpty(inviteCode))
         throw new UserInfoExtraException(1, "激活码生成失败");
      UserInfoExtra userInfoExtra = new UserInfoExtra();
      userInfoExtra.setUserInfo(invitee);
      userInfoExtra.setInviteCode(inviteCode);
      userInfoExtra.setUpdateTime(new Date());
      if (extra != null) {
         userInfoExtra.setId(extra.getId());
         userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
      } else {
         userInfoExtra.setCreateTime(new Date());
         userInfoExtraMapper.insertSelective(userInfoExtra);
      }
      // 发送激活成功消息
      Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.inviteCodeActive,
            new Gson().toJson(new InviteCodeActiveMQMsg(uid, new Date())));
      producer.send(message);
      // 邀请码激活
      inviteCodeActive(uid, boss.getId());
   }
   @Override
@@ -506,15 +469,6 @@
      UserInfo invitee = userInfoService.selectByPKey(uid);
      if (invitee == null) {
         throw new UserInfoExtraException(1, "用户不存在");
      }
      // 用户额外信息
      UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
      if (extra != null) {
         String inviteCodeHas = extra.getInviteCode();
         if (inviteCodeHas != null && inviteCodeHas.trim().length() > 0) {
            throw new UserInfoExtraException(1, "已经激活, 无需再次激活");
         }
      }
      String wxUnionId = weiXinUser.getUnionid();
@@ -556,8 +510,9 @@
            if (asInputStream == null) {
               LogHelper.test("微信头像下载失败: " + weiXinUser.getUnionid() + " " + headimgurl);
            } else {
               FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String.format(
                     "/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
               FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream,
                     String.format(FilePathEnum.userWXPortrait.getPath() + "%s_%s.jpg", weiXinUser.getUnionid(),
                           System.currentTimeMillis() + ""));
               if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
                  headimgurl = result.getUrl();
               } else {
@@ -578,10 +533,33 @@
      if (inviterId == null) {
         throw new UserInfoExtraException(1, "没有对应的邀请关系");
      }
      UserInfo boss = userInfoService.selectByPKey(inviterId);
      if (boss == null || boss.getState() != UserInfo.STATE_NORMAL) {
         throw new UserInfoExtraException(1, "没有对应的邀请关系");
      }
      // 邀请码激活
      inviteCodeActive(uid, boss.getId());
   }
   /**
    * 邀请关系生成激活码
    * @param uid
    * @param bossId
    * @return
    * @throws UserInfoExtraException
    */
   private String inviteCodeActive(Long uid, Long bossId) throws UserInfoExtraException{
      UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
      if (extra != null && !StringUtil.isNullOrEmpty(extra.getInviteCode())) {
         throw new UserInfoExtraException(1, "已经激活, 无需再次激活");
      }
      // 绑定关系
      try {
         threeSaleSerivce.bindRelationshipByWX(invitee, inviterId);
         threeSaleSerivce.bindInviteRelationship(uid, bossId);
      } catch (ThreeSaleException e) {
         try {
            LogHelper.errorDetailInfo(e);
@@ -593,12 +571,11 @@
      // 生成邀请码
      String inviteCode = createInviteCode(uid);
      ;
      if (StringUtil.isNullOrEmpty(inviteCode))
         throw new UserInfoExtraException(1, "激活码生成失败");
      UserInfoExtra userInfoExtra = new UserInfoExtra();
      userInfoExtra.setUserInfo(invitee);
      userInfoExtra.setUserInfo(new UserInfo(uid));
      userInfoExtra.setInviteCode(inviteCode);
      userInfoExtra.setUpdateTime(new Date());
      if (extra != null) {
@@ -608,7 +585,19 @@
         userInfoExtra.setCreateTime(new Date());
         userInfoExtraMapper.insertSelective(userInfoExtra);
      }
      userActivedRecordService.add(new UserActivedRecord(uid));
      if (!Constant.IS_TEST) { // 发送激活成功消息
         Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.inviteCodeActive,
               new InviteCodeActiveMQMsg(uid, new Date()));
         producer.send(message);
      }
      return inviteCode;
   }
   @Override
   public void updateActiveTime(Long uid, Date date) {
@@ -750,6 +739,10 @@
      UserInfo inviter = userInfoService.getUserInfoByInviteCode(inviteCode);
      if (inviter == null)
         throw new UserInfoExtraException(1, "上级邀请码不存在");
      if (inviter.getState() != UserInfo.STATE_NORMAL) {
         throw new UserInfoExtraException(1, "上级邀请码不存在");
      }
      return inviter;
   }
@@ -796,15 +789,16 @@
      if (inviter == null) {
         throw new UserInfoExtraException(1, "对应的邀请关系不存在");
      }
      if (inviter.getState() != UserInfo.STATE_NORMAL) {
         throw new UserInfoExtraException(1, "对应的邀请关系不存在");
      }
      return inviter;
   }
   @Override
   public void updateAutoExtract(Integer days) {
      userInfoExtraMapper.updateAutoExtract(days);
   }
   @Transactional(rollbackFor=Exception.class)
   @Transactional(rollbackFor = Exception.class)
   @Override
   public void updateInviteCodeVip(String inviteCodeVip, Long uid) throws UserInfoExtraException {
      if (inviteCodeVip == null || inviteCodeVip.length() < 4 || inviteCodeVip.length() > 12)
@@ -839,9 +833,54 @@
      update.setInviteCodeVip(inviteCodeVip);
      update.setUpdateTime(new Date());
      userInfoExtraMapper.updateByPrimaryKeySelective(update);
       //发送邀请码更改成功的消息
      Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userInfoUpdate,
            new Gson().toJson(new UserInfoUpdateMQMsg(uid, UserInfoUpdateTypeEnum.inviteCode, inviteCodeVip, new Date())));
      producer.send(message);
      if (!Constant.IS_TEST) { // 发送邀请码更改成功的消息
         Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.userInfoUpdate,
               new UserInfoUpdateMQMsg(uid, UserInfoUpdateTypeEnum.inviteCode, inviteCodeVip,
                     new Date()));
         producer.send(message);
      }
      // 消息
      userAccountMsgNotificationService.createChangeInviteCode(uid, extra.getInviteCode(), inviteCodeVip);
   }
   @Override
   public void uploadERCode(MultipartFile file, Long uid) {
      UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
      if (extra == null)
         return;
      // 压缩
      String targetPath = FileUtil.getCacheDir() + "/erCode_" + uid + "_" + System.currentTimeMillis() + ".jpg";
      try {
         Thumbnails.of(file.getInputStream()).size(200, 200).toFile(targetPath);
      } catch (IOException e) {
         LogHelper.errorDetailInfo(e);
         return;
      }
      // 执行上传
      String filePath = FilePathEnum.userERcode.getPath() + UUID.randomUUID().toString().replace("-", "") + ".jpg";
      String fileLink = COSManager.getInstance().uploadFile(new File(targetPath), filePath).getUrl();
      // 删除本地图片
      if (new File(targetPath).exists()) {
         new File(targetPath).delete();
      }
      // 原二维码
      String erCode = extra.getErCode();
      UserInfoExtra update = new UserInfoExtra();
      update.setId(extra.getId());
      update.setErCode(fileLink);
      userInfoExtraMapper.updateByPrimaryKeySelective(update);
      // 原二维码
      if (!StringUtil.isNullOrEmpty(erCode)) {
         COSManager.getInstance().deleteFile(erCode);
      }
   }
}