yujian
2020-01-18 f4a0f2acc63d7785eab108419a4e16f5f688cb95
fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java
@@ -82,23 +82,22 @@
   @Resource
   private UserOtherMsgNotificationService userOtherMsgNotificationService;
   @Lazy
   @Resource
   private IntegralGetService  integralGetService;
   private IntegralGetService integralGetService;
   @Resource
   private SystemCouponService systemCouponService;
   @Resource
   private UserInviteMsgNotificationService userInviteMsgNotificationService;
   @Resource
   private RedPackGiveRecordService redPackGiveRecordService;
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   @Override
   public void insertSelective(TokenRecord record) {
@@ -107,24 +106,28 @@
      tokenRecordMapper.insertSelective(record);
   }
   @Override
   public void updateByPrimaryKeySelective(TokenRecord record) {
      tokenRecordMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   public TokenRecord getNearByTypeAndIdentify(String type, String identify) {
      return tokenRecordMapper.getNearByTypeAndIdentify(type, identify);
   }
   @Override
   public TokenVO discernToken(String token, Long uid, AcceptData acceptData) throws TokenRecordException {
   public TokenRecord getByTypeAndIdentify(String type, String identify) {
      return tokenRecordMapper.getByTypeAndIdentify(type, identify);
   }
   @Override
   public TokenVO discernToken(String token, Long uid, String platform, String version) throws TokenRecordException {
      // 解析口令
      String parseToken = TokenUtil.parseToken(token);
      if (StringUtil.isNullOrEmpty(parseToken))
      if (StringUtil.isNullOrEmpty(parseToken))
         throw new TokenRecordException(1, "未发现口令");
      TokenRecord rokenRecord = tokenRecordMapper.getByToken(parseToken);
      if (rokenRecord == null)
         throw new TokenRecordException(1, "口令不存在");
@@ -132,22 +135,21 @@
      Long uidToken = rokenRecord.getUid();
      if (uid != null && uidToken != null && uid.longValue() == uidToken.longValue())
         throw new TokenRecordException(1, "不能自己赠送给自己");
      Date now = new Date();
      Date endTimeToken = rokenRecord.getEndTime();
      Integer stateTtoken = rokenRecord.getState();
      if (endTimeToken != null && endTimeToken.getTime() < now.getTime()
            || (stateTtoken != null && stateTtoken == 1) || StringUtil.isNullOrEmpty(rokenRecord.getIdentify()))
      if (endTimeToken != null && endTimeToken.getTime() < now.getTime() || (stateTtoken != null && stateTtoken == 1)
            || StringUtil.isNullOrEmpty(rokenRecord.getIdentify()))
         throw new TokenRecordException(1, "口令已失效");
      Integer num = 0;
      Integer type = 0;
      String money = null;
      boolean state = false;
      List<String> tips = new ArrayList<String>();
      String identify = rokenRecord.getIdentify();
      TokenTypeEnum tokenType = rokenRecord.getType();
      if (tokenType == TokenTypeEnum.freeCoupon) {
         type = 10;
@@ -155,25 +157,26 @@
               .selectByPrimaryKey(Long.parseLong(identify));
         if (giveRecord == null)
            throw new TokenRecordException(1, "赠送记录不存在");
         Date endTime = giveRecord.getEndTime();
         // 用户券信息
         UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(giveRecord.getCouponId());
         if (userCoupon == null || userCoupon.getGive() == null || !userCoupon.getGive())
            throw new TokenRecordException(1, "券不存在或非赠送状态");
         if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE || (endTime != null && endTime.getTime() < now.getTime())) {
         if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE
               || (endTime != null && endTime.getTime() < now.getTime())) {
            tips.add("哎呀,这张免单券已失效了!");
         } else if (userCoupon.getState() == UserSystemCoupon.STATE_END_USE) {
            tips.add("哎呀,这张免单券已被领取了!");
         } else if (userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
            if (uid == null || uid <= 0)
               throw new TokenRecordException(1001, "温馨提示,[免单券]需要登录后领取");
            state = true;
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
               tips.add("确认领取后,你将成为赠送人的一级队员;");
               tips.add("确认领取后,你将成为赠送人的直接粉丝;");
               tips.add("获赠的免单券,需要激活后才能使用,详情参见免单券激活规则;");
               tips.add("成功领取后,请到“我的-福利中心”查看。");
            } else {
@@ -191,14 +194,14 @@
               .selectByPrimaryKey(Long.parseLong(identify));
         if (giveRecord == null)
            throw new TokenRecordException(1, "赠送记录不存在");
         Date endTime = giveRecord.getEndTime();
         UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(giveRecord.getCouponId());
         if (userCoupon == null || userCoupon.getGive() == null || !userCoupon.getGive())
            throw new TokenRecordException(1, "券不存在或非赠送状态");
         if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE|| (endTime != null && endTime.getTime() < now.getTime())) {
         if (userCoupon.getState() == UserSystemCoupon.STATE_OVERDUE
               || (endTime != null && endTime.getTime() < now.getTime())) {
            tips.add("哎呀,这张返利奖励券已失效了!");
         } else if (userCoupon.getState() == UserSystemCoupon.STATE_END_USE) {
            tips.add("哎呀,这张返利奖励券已被领取了!");
@@ -209,7 +212,7 @@
            state = true;
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
               tips.add("确认领取后,你将成为赠送人的一级队员;");
               tips.add("确认领取后,你将成为赠送人的直接粉丝;");
               tips.add("板栗快省奖励券,可用于“已到账”的返利订单,在返利的基础上再获得一定比例的返利;");
               tips.add("成功领取后,请到“我的-福利中心”中查看。");
            } else {
@@ -220,20 +223,19 @@
            throw new TokenRecordException(1, "口令已失效");
         }
      } else if (tokenType == TokenTypeEnum.redPack) {
         if(!VersionUtil.greaterThan_2_1(acceptData.getPlatform(), acceptData.getVersion()))
         if (!VersionUtil.greaterThan_2_0_5(platform, version))
            throw new TokenRecordException(1, "请升级到最新版");
         type = 13;
         RedPackGiveRecord giveRecord = redPackGiveRecordService.selectByPrimaryKey(Long.parseLong(identify));
         if (giveRecord == null)
            throw new TokenRecordException(1, "口令已失效");
         Date endTime = giveRecord.getEndTime();
         // 赠送面额
         money = giveRecord.getAmount().setScale(2).toString();
         if(giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_OVERDUE
         if (giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_OVERDUE
               || (endTime != null && endTime.getTime() < now.getTime())) {
            tips.add("哎呀,这个红包已失效了!");
         } else if (giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_RECEIVE) {
@@ -244,7 +246,7 @@
            state = true;
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
               tips.add("确认领取后,你将成为赠送人的一级队员;");
               tips.add("确认领取后,你将成为赠送人的直接粉丝;");
               tips.add("红包可提现,可购买折扣商品;");
               tips.add("成功领取后,请到“我的-红包”中查看。");
            } else {
@@ -278,7 +280,7 @@
      if (nickName.length() > 6) {
         nickName = nickName.substring(0, 6) + "...";
      }
      TokenVO tokenVO = new TokenVO();
      tokenVO.setAmount(num);
      tokenVO.setMoney(money);
@@ -290,8 +292,6 @@
      tokenVO.setType(type);
      return tokenVO;
   }
   @Override
   public String receiveToken(String token, Long uid, AcceptData acceptData) throws TokenRecordException {
@@ -304,11 +304,11 @@
      TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
      if (rokenRecord == null)
         throw new TokenRecordException(1, "口令已失效");
      Long uidToken = rokenRecord.getUid();
      if (uidToken != null && uid.longValue() == uidToken.longValue())
         throw new TokenRecordException(1, "不能自己赠送给自己");
      Date now = new Date();
      Date endTimeToken = rokenRecord.getEndTime();
      if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
@@ -344,11 +344,11 @@
         if (userCoupon == null || userCoupon.getGive() == null || !userCoupon.getGive()
               || userCoupon.getState() != UserSystemCoupon.STATE_IN_USE)
            throw new TokenRecordException(1, "该券不存在或非赠送");
         UserSystemCoupon userSystemCoupon= null;
         UserSystemCoupon userSystemCoupon = null;
         try {
            userSystemCoupon = userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.freeCoupon.name(),
                  UserSystemCoupon.SOURCE_GIVE, null,true);
                  UserSystemCoupon.SOURCE_GIVE, null, true);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new TokenRecordException(1, "领取失败");
@@ -384,7 +384,7 @@
         userSystemCouponGiveRecordService.updateByPrimaryKeySelective(updateGiveRecord);
         integralGetService.addGiveFreeCoupon(giveUid);
         msg = "领取成功[免单券]成功,请到[我的-福利中心]中查看";
         // 消息 + 队员
         executor.execute(new Runnable() {
@@ -394,7 +394,7 @@
               boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
               String beiZhu = "无";
               if (addTeam)
                  beiZhu = "领取人已经成为你的一级队员";
                  beiZhu = "领取人已经成为你的直接粉丝";
               String userName = "无";
               UserInfo user = userInfoService.selectByPKey(uid);
@@ -409,10 +409,10 @@
               msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
               msgOther.setReceiveTime(sd.format(new Date()));
               userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
               // 激活邀请信息
               if (addTeam)
                   addInviteMsg(uid, giveUid, "免单券");
                  addInviteMsg(uid, giveUid, "免单券");
            }
         });
      } else if (tokenType == TokenTypeEnum.rebatePercentCoupon) {
@@ -434,15 +434,16 @@
         SystemCoupon systemCoupon = userCoupon.getSystemCoupon();
         if (systemCoupon == null)
            throw new TokenRecordException(1, "该券不存在");
         systemCoupon = systemCouponService.selectByPrimaryKey(systemCoupon.getId());
         if (systemCoupon == null)
            throw new TokenRecordException(1, "该券不存在");
         UserSystemCoupon userSystemCoupon= null;
         UserSystemCoupon userSystemCoupon = null;
         try {
            userSystemCoupon = userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(),
                  UserSystemCoupon.SOURCE_GIVE, systemCoupon.getPercent(),true);
            userSystemCoupon = userSystemCouponService.insertUserCoupon(uid,
                  CouponTypeEnum.rebatePercentCoupon.name(), UserSystemCoupon.SOURCE_GIVE,
                  systemCoupon.getPercent(), true);
         } catch (Exception e) {
            LogHelper.errorDetailInfo(e);
            throw new TokenRecordException(1, "领取失败");
@@ -478,17 +479,18 @@
         userSystemCouponGiveRecordService.updateByPrimaryKeySelective(updateGiveRecord);
         integralGetService.addGiveRebateCoupon(giveUid);
         msg = "领取成功[返利奖励券]成功,请到[我的-福利中心]中查看";
         // 消息 + 队员
         executor.execute(new Runnable() {
            @Override
            public void run() {
               SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
               boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
               String beiZhu = "无";
               if (addTeam)
                  beiZhu = "领取人已经成为你的一级队员";
                  beiZhu = "领取人已经成为你的直接粉丝";
               String userName = "无";
               UserInfo user = userInfoService.selectByPKey(uid);
@@ -503,36 +505,37 @@
               msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
               msgOther.setReceiveTime(sd.format(new Date()));
               userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
               // 激活邀请信息
               if (addTeam)
                   addInviteMsg(uid, giveUid, "返利奖励券");
                  addInviteMsg(uid, giveUid, "返利奖励券");
            }
         });
      } else if (tokenType == TokenTypeEnum.redPack) {
         if(!VersionUtil.greaterThan_2_1(acceptData.getPlatform(), acceptData.getVersion()))
         if (!VersionUtil.greaterThan_2_0_5(acceptData.getPlatform(), acceptData.getVersion()))
            throw new TokenRecordException(1, "请升级到最新版");
         try {
            msg = "领取红包成功,请到[我的-红包]中查看";
            // 领取红包、更新记录
            RedPackGiveRecord giveRecord = redPackGiveRecordService.receiveFriendsGive(uid, Long.parseLong(identify));
            RedPackGiveRecord giveRecord = redPackGiveRecordService.receiveFriendsGive(uid,
                  Long.parseLong(identify));
            // 消息 + 队员
            executor.execute(new Runnable() {
               @Override
               public void run() {
                  SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
                  boolean addTeam = addInviteTeam(uid, giveUid, userInfoExtra);
                  String beiZhu = "无";
                  String giveBeiZhu = "无";
                  if (addTeam) {
                     beiZhu = "你已成为赠送人的一级队员";
                     giveBeiZhu = "领取人已成为你的一级队员";
                     beiZhu = "你已成为赠送人的直接粉丝";
                     giveBeiZhu = "领取人已成为你的直接粉丝";
                     // 激活邀请信息
                     addInviteMsg(uid, giveUid, giveRecord.getAmount().setScale(2) + "元红包");
                  }
                  // 领取人消息
                  String giveUserName = "无";
                  UserInfo giveuser = userInfoService.selectByPKey(giveUid);
@@ -543,8 +546,9 @@
                  dto.setUserInfo("昵称:" + giveUserName + " ID:" + giveUid);
                  dto.setTime(sd.format(giveRecord.getGiveTime()));
                  dto.setMoney("¥" + giveRecord.getAmount().setScale(2));
                  userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackReceiveOff, new Gson().toJson(dto), beiZhu);
                  userMoneyMsgNotificationService.redPackMsg(uid, MsgTypeMoneyTypeEnum.redPackReceiveOff,
                        new Gson().toJson(dto), beiZhu);
                  // 赠送人消息
                  String userName = "无";
                  UserInfo user = userInfoService.selectByPKey(uid);
@@ -555,7 +559,8 @@
                  givedto.setUserInfo("昵称:" + userName + " ID:" + uid);
                  givedto.setTime(sd.format(new Date()));
                  givedto.setMoney("¥" + giveRecord.getAmount().setScale(2));
                  userMoneyMsgNotificationService.redPackMsg(giveUid, MsgTypeMoneyTypeEnum.redPackGiveOff, new Gson().toJson(givedto), giveBeiZhu);
                  userMoneyMsgNotificationService.redPackMsg(giveUid, MsgTypeMoneyTypeEnum.redPackGiveOff,
                        new Gson().toJson(givedto), giveBeiZhu);
               }
            });
         } catch (RedPackGiveRecordException e) {
@@ -596,70 +601,63 @@
      }
      return invite;
   }
   private void addInviteMsg(Long uid, Long giveUid, String giftName) {
      try {
         UserInfo user = userInfoService.selectByPKey(giveUid);
         UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
         String inviteCode = null;
         if (!StringUtil.isNullOrEmpty(userInfoExtra.getInviteCodeVip())) {
            inviteCode = userInfoExtra.getInviteCodeVip();
         } else {
            inviteCode = userInfoExtra.getInviteCode();
         }
         MsgInviteContentDTO msgInvite = new MsgInviteContentDTO();
         msgInvite.setTitle("邀请消息");
         msgInvite.setState("成功激活邀请");
         msgInvite.setCode("你的邀请码:" + inviteCode);
         msgInvite.setInviter("昵称:"+user.getNickName());
         msgInvite.setInviter("昵称:" + user.getNickName());
         msgInvite.setMode("成功领取邀请人的" + giftName);
         userInviteMsgNotificationService.receiveGift(uid, "邀请关系一旦确立无法更改 ", msgInvite);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
      }
   }
   @Override
   public List<TokenRecord> overdueList(int count) {
      return tokenRecordMapper.overdueList(count);
   }
   @Override
   public void overdue(List<TokenRecord> list) {
      if (list == null || list.size() == 0)
         return;
      Date date = new Date();
      for (TokenRecord tokenRecord: list) {
         Integer state = tokenRecord.getState();
         if (state != null && state == 0)
            continue;
      for (TokenRecord tokenRecord : list) {
         tokenRecord.setState(1);
         tokenRecord.setUpdateTime(date);
         tokenRecordMapper.updateByPrimaryKeySelective(tokenRecord);
      }
   }
   @Override
   public void invalidByRedPack(Long id) {
      if (id == null)
         return;
      List<String> list = new ArrayList<String>();
      list.add(TokenTypeEnum.redPack.name());
      tokenRecordMapper.invalidByTypeAndIdentify(list, id.toString());
   }
   @Override
   public void invalidByCoupon(Long id) {
      if (id == null)
         return;
      List<String> list = new ArrayList<String>();
      list.add(TokenTypeEnum.freeCoupon.name());
      list.add(TokenTypeEnum.rebatePercentCoupon.name());