admin
2019-08-19 27920d99bc5925de113aa4f0c22dbba3dcea672f
fanli/src/main/java/com/yeshi/fanli/service/impl/user/TokenRecordServiceImpl.java
@@ -1,25 +1,34 @@
package com.yeshi.fanli.service.impl.user;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import com.yeshi.fanli.dao.mybatis.user.TokenRecordMapper;
import com.yeshi.fanli.dto.msg.MsgOtherGiveContentDTO;
import com.yeshi.fanli.entity.bus.tlj.UserTaoLiJinGiveRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord.TokenTypeEnum;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
import com.yeshi.fanli.entity.bus.user.UserSystemCoupon;
import com.yeshi.fanli.entity.bus.user.UserSystemCouponGiveRecord;
import com.yeshi.fanli.entity.system.SystemCoupon.CouponTypeEnum;
import com.yeshi.fanli.exception.tlj.UserTaoLiJinOriginException;
import com.yeshi.fanli.exception.user.TokenRecordException;
import com.yeshi.fanli.exception.user.UserInfoExtraException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.msg.UserOtherMsgNotificationService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinGiveRecordService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.user.TokenRecordService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -32,6 +41,9 @@
@Service
public class TokenRecordServiceImpl implements TokenRecordService {
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Resource
   private TokenRecordMapper tokenRecordMapper;
@@ -53,6 +65,14 @@
   @Resource
   private UserSystemCouponGiveRecordService userSystemCouponGiveRecordService;
   
   @Resource
   private UserTaoLiJinOriginService userTaoLiJinOriginService;
   @Resource
   private UserOtherMsgNotificationService userOtherMsgNotificationService;
   @Override
   public void insertSelective(TokenRecord record) {
      record.setCreateTime(new Date());
@@ -73,6 +93,15 @@
      TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
      if (rokenRecord == null)
         throw new TokenRecordException(1, "口令不存在");
      Date now = new Date();
      Date endTimeToken = rokenRecord.getEndTime();
      if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
         throw new TokenRecordException(1, "口令已失效");
      Integer stateTtoken = rokenRecord.getState();
      if (stateTtoken != null && stateTtoken == 1)
         throw new TokenRecordException(1, "口令已失效");
      String nickName = null;
      String portrait = null;
@@ -95,12 +124,9 @@
         nickName = nickName.substring(0, 6) + "...";
      }
      Integer stateTtoken = rokenRecord.getState();
      if (stateTtoken == 1)
         throw new TokenRecordException(1, "口令已失效");
      TokenVO tokenVO = new TokenVO();
      tokenVO.setNickName(nickName);
      tokenVO.setToken(token);
      tokenVO.setNickName(nickName + "赠送");
      tokenVO.setPortrait(portrait);
      String identify = rokenRecord.getIdentify();
@@ -132,6 +158,7 @@
                  throw new TokenRecordException(1001, "温馨提示,[免单券]需要登录后领取");
               state = true;
               tokenVO.setAmount(1);
               UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
               if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
                  tips.add("确认领取后,你将成为赠送者的一级队员;");
@@ -166,6 +193,7 @@
                  throw new TokenRecordException(1001, "温馨提示,[返利奖励券]需要登录后领取");
               state = true;
               tokenVO.setAmount(1);
               UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
               if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
                  tips.add("确认领取后,你将成为赠送者的一级队员;");
@@ -191,7 +219,10 @@
            if (uid == null || uid <= 0)
               throw new TokenRecordException(1001, "温馨提示,[推广红包]需要登录后领取");
            BigDecimal amount = giveRecord.getAmount();
            state = true;
            tokenVO.setAmount(Integer.parseInt(amount.setScale(0).toString()));
            UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
            if (userInfoExtra == null || StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
               tips.add("确认领取后,你将成为赠送者的一级队员;");
@@ -213,7 +244,7 @@
   }
   @Override
   public void receiveToken(String token, Long uid) throws TokenRecordException {
   public String receiveToken(String token, Long uid) throws TokenRecordException {
      if (uid == null || uid <= 0)
         throw new TokenRecordException(1, "用户未登录");
@@ -221,23 +252,34 @@
         throw new TokenRecordException(1, "口令不能为空");
      TokenRecord rokenRecord = tokenRecordMapper.getByToken(token);
      if (rokenRecord == null || rokenRecord.getState() == 1)
      if (rokenRecord == null)
         throw new TokenRecordException(1, "口令已失效");
      Date now = new Date();
      Date endTimeToken = rokenRecord.getEndTime();
      if (endTimeToken != null && endTimeToken.getTime() < now.getTime())
         throw new TokenRecordException(1, "口令已失效");
      Integer stateTtoken = rokenRecord.getState();
      if (stateTtoken != null && stateTtoken == 1)
         throw new TokenRecordException(1, "口令已失效");
      String identify = rokenRecord.getIdentify();
      if (StringUtil.isNullOrEmpty(identify))
         throw new TokenRecordException(1, "口令标识不存在");
      
      Long giveUid = rokenRecord.getUid();
      
      Long giveUid = rokenRecord.getUid();
      UserInfoExtra userInfoExtra = userInfoExtraService.getUserInfoExtra(uid);
      if (userInfoExtra == null) 
         throw new TokenRecordException(1, "用户信息缺失");
         
      boolean receive = false;
      String msg = "领取成功";
      TokenTypeEnum tokenType = rokenRecord.getType();
      if (tokenType == TokenTypeEnum.freeCoupon) {
         UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
         long couponId = Long.parseLong(identify);
         UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(couponId);
         if (userCoupon == null)
            throw new TokenRecordException(1, "该券不存在");
@@ -255,6 +297,14 @@
            throw new TokenRecordException(1, "口令已失效");
         
         if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
            UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(giveUid, couponId);
            if (giveRecord == null )
               throw new TokenRecordException(1, "口令已失效");
            Date endTime = giveRecord.getEndTime();
            if (endTime != null && endTime.getTime() < now.getTime())
               throw new TokenRecordException(1, "哎呀,这张免单券已失效了!");
            try {
               userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.freeCoupon.name(), UserSystemCoupon.SOURCE_GIVE);
            } catch (Exception e) {
@@ -262,17 +312,47 @@
               throw new TokenRecordException(1, "领取失败");
            }
            
            // 更新记录
            userSystemCouponGiveRecordService.updateRecord(uid, giveUid, Long.parseLong(identify));
            // 领取成功
            receive = true;
            // 更新赠送券信息
            userSystemCouponService.updateGiveRecord(uid, couponId, CouponTypeEnum.freeCoupon.name());
            // 更新赠送记录
            giveRecord.setReceiveUid(uid);
            giveRecord.setReceiveTime(new Date());
            userSystemCouponGiveRecordService.updateByPrimaryKeySelective(giveRecord);
            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 = "领取人已经成为你的一级队员";
                  String userName = "无";
                  UserInfo user = userInfoService.selectByPKey(uid);
                  if (user != null && !StringUtil.isNullOrEmpty(user.getNickName()))
                     userName = user.getNickName();
                  MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
                  msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
                  msgOther.setTitle("赠送免单券");
                  msgOther.setGiveType("你赠送的免单券被成功领取");
                  msgOther.setReceiveInfo("昵称:  " + userName +" ID:" + uid);
                  msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
                  msgOther.setReceiveTime(sd.format(new Date()));
                  userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
               }
            });
         } else {
            throw new TokenRecordException(1, "口令已失效");
         }
      } else if (tokenType == TokenTypeEnum.rebatePercentCoupon) {
         UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(Long.parseLong(identify));
         long couponId = Long.parseLong(identify);
         UserSystemCoupon userCoupon = userSystemCouponService.selectByPrimaryKey(couponId);
         if (userCoupon == null)
            throw new TokenRecordException(1, "该券不存在");
@@ -290,14 +370,55 @@
            throw new TokenRecordException(1, "口令已失效");
         
          if (give && userCoupon.getState() == UserSystemCoupon.STATE_IN_USE) {
             UserSystemCouponGiveRecord giveRecord = userSystemCouponGiveRecordService.getRecordByUidAndCouponId(giveUid, couponId);
            if (giveRecord == null )
               throw new TokenRecordException(1, "口令已失效");
            Date endTime = giveRecord.getEndTime();
            if (endTime != null && endTime.getTime() < now.getTime())
               throw new TokenRecordException(1, "哎呀,这张返利奖励券已失效了!");
             try {
               userSystemCouponService.insertUserCoupon(uid, CouponTypeEnum.rebatePercentCoupon.name(), UserSystemCoupon.SOURCE_GIVE);
            } catch (Exception e) {
               LogHelper.errorDetailInfo(e);
               throw new TokenRecordException(1, "领取失败");
            }
            // 领取成功
            receive = true;
             // 更新赠送券信息
            userSystemCouponService.updateGiveRecord(uid, couponId, CouponTypeEnum.rebatePercentCoupon.name());
            // 更新赠送记录
            giveRecord.setReceiveUid(uid);
            giveRecord.setReceiveTime(new Date());
            userSystemCouponGiveRecordService.updateByPrimaryKeySelective(giveRecord);
            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 = "领取人已经成为你的一级队员";
                  String userName = "无";
                  UserInfo user = userInfoService.selectByPKey(uid);
                  if (user != null && !StringUtil.isNullOrEmpty(user.getNickName()))
                     userName = user.getNickName();
                  MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
                  msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
                  msgOther.setTitle("赠送奖励券");
                  msgOther.setGiveType("你赠送的奖励券被成功领取");
                  msgOther.setReceiveInfo("昵称:  " + userName +" ID:" + uid);
                  msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
                  msgOther.setReceiveTime(sd.format(new Date()));
                  userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
               }
            });
         } else {
            throw new TokenRecordException(1, "口令已失效");
         }
@@ -305,28 +426,91 @@
         UserTaoLiJinGiveRecord giveRecord = userTaoLiJinGiveRecordService
               .selectByPrimaryKey(Long.parseLong(identify));
         if (giveRecord == null || giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_OVERDUE) {
            throw new TokenRecordException(1, "哎呀,哎呀,推广红包已失效了!");
            throw new TokenRecordException(1, "哎呀,推广红包已失效了!");
         } else if (giveRecord.getState() == UserTaoLiJinGiveRecord.STATE_RECEIVE) {
            throw new TokenRecordException(1, "哎呀,推广红包已被领取了!");
         } else {
            Date endTime = giveRecord.getEndTime();
            if (endTime != null && endTime.getTime() < now.getTime())
               throw new TokenRecordException(1, "哎呀,推广红包已失效了!");
            
            BigDecimal amount = giveRecord.getAmount();
            try {
               // 领取
               userTaoLiJinOriginService.receiveFriendsGive(uid, amount);
               // 更新记录
               UserTaoLiJinGiveRecord updateRecord = new UserTaoLiJinGiveRecord();
               updateRecord.setId(Long.parseLong(identify));
               updateRecord.setReceiveTime(new Date());
               updateRecord.setReceiveUid(uid);
               updateRecord.setState(UserTaoLiJinGiveRecord.STATE_RECEIVE);
               userTaoLiJinGiveRecordService.updateByPrimaryKeySelective(updateRecord);
               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 = "领取人已经成为你的一级队员";
                     String userName = "无";
                     UserInfo user = userInfoService.selectByPKey(uid);
                     if (user != null && !StringUtil.isNullOrEmpty(user.getNickName()))
                        userName = user.getNickName();
                     MsgOtherGiveContentDTO msgOther = new MsgOtherGiveContentDTO();
                     msgOther.setType(MsgOtherGiveContentDTO.TYEP_COUPON);
                     msgOther.setTitle("赠送推广红包");
                     msgOther.setGiveType("你赠送的推广红包被成功领取");
                     msgOther.setReceiveInfo("昵称:  " + userName +" ID:" + uid);
                     msgOther.setGiveTime(sd.format(giveRecord.getGiveTime()));
                     msgOther.setReceiveTime(sd.format(new Date()));
                     userOtherMsgNotificationService.tokenGiveMsg(giveUid, beiZhu, msgOther);
                  }
               });
            } catch (UserTaoLiJinOriginException e) {
               e.printStackTrace();
            }
         }
      } else {
         throw new TokenRecordException(1, "无对应类型");
      }
      if (receive)
         throw new TokenRecordException(1, "领取失败啦");
      
      if (StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
         // 成为下级
      }
      return msg;
   }
   /**
    *     未激活邀请码: 上下级关系确立
    * @param uid
    * @param giveUid
    * @param userInfoExtra
    * @return
    */
   public boolean addInviteTeam(Long uid, Long giveUid, UserInfoExtra userInfoExtra) {
      boolean invite = false;
      if (StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode())) {
         UserInfoExtra userInfoExtraGive = userInfoExtraService.getUserInfoExtra(giveUid);
         if (userInfoExtraGive != null ) {
            String inviteCode = userInfoExtraGive.getInviteCode();
            if(!StringUtil.isNullOrEmpty(inviteCode)) {
               try {
                  userInfoExtraService.activateInviteCode(uid, inviteCode);
                  invite = true;
               } catch (UserInfoExtraException e) {
                  e.printStackTrace();
               }
            }
         }
      }
      return invite;
   }
}