admin
2019-11-03 788deca1b4a24f8a24e49c24f7d89975a1d74bbe
fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackGiveRecordServiceImpl.java
@@ -12,14 +12,17 @@
import com.yeshi.fanli.dao.mybatis.redpack.RedPackGiveRecordMapper;
import com.yeshi.fanli.entity.bus.user.TokenRecord;
import com.yeshi.fanli.entity.bus.user.TokenRecord.TokenTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackDetail;
import com.yeshi.fanli.entity.redpack.RedPackDetail.RedPackDetailTypeEnum;
import com.yeshi.fanli.entity.redpack.RedPackGiveRecord;
import com.yeshi.fanli.exception.redpack.RedPackGiveRecordException;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
import com.yeshi.fanli.service.inter.redpack.RedPackGiveRecordService;
import com.yeshi.fanli.service.inter.user.TokenRecordService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TokenUtil;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
@@ -38,6 +41,22 @@
   
   @Resource
   private TokenRecordService tokenRecordService;
   @Resource
   private RedPackDetailService redPackDetailService;
   @Override
   public RedPackGiveRecord selectByPrimaryKey(Long id) {
      return redPackGiveRecordMapper.selectByPrimaryKey(id);
   }
   @Override
   public void updateByPrimaryKeySelective(RedPackGiveRecord record) {
      redPackGiveRecordMapper.updateByPrimaryKeySelective(record);
   }
   
   @Override
   @Transactional(rollbackFor = Exception.class)
@@ -85,14 +104,54 @@
         tips = tips.replace("{APP名称}", projectChineseName);
      }
      tips = tips.replace("{口令}", token).replace("{下载链接}", redPackConfigService.getValueByKey("app_down_link")).replace("{面额}",
            amount.setScale(0).toString());
            amount.toString());
      
      // 减少红包
      try {
         redPackBalanceService.subRedPack(uid, amount, RedPackDetailFactory.createGiveOthers(giveRecord, null, RedPackDetailTypeEnum.giveOthers));
         redPackBalanceService.subRedPack(uid, amount, RedPackDetailFactory.createGiveOthers(giveRecord));
      } catch (Exception e) {
         throw new RedPackGiveRecordException(1, "红包创建失败");
      }
      return tips;
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public RedPackGiveRecord receiveFriendsGive(Long uid, Long id) throws RedPackGiveRecordException{
      RedPackGiveRecord giveRecord = redPackGiveRecordMapper.selectByPrimaryKey(id);
      if (giveRecord == null || giveRecord.getState() != RedPackGiveRecord.STATE_INIT)
         throw new RedPackGiveRecordException(1, "赠送记录失效或已被领取");
      Date now = new Date();
      Date endTime = giveRecord.getEndTime();
      if (endTime != null && endTime.getTime() < now.getTime())
         throw new RedPackGiveRecordException(1, "红包已失效了");
      giveRecord.setGiveUid(uid);
      giveRecord.setReceiveTime(now);
      giveRecord.setState(RedPackGiveRecord.STATE_RECEIVE);
      // 领取人增加红包
      try {
         redPackBalanceService.addRedPack(uid, giveRecord.getAmount(), RedPackDetailFactory.createGiveOthersReceive(giveRecord));
      } catch (Exception e) {
         throw new RedPackGiveRecordException(1, "红包领取失败");
      }
      // 更新赠送记录
      redPackGiveRecordMapper.updateByPrimaryKey(giveRecord);
      try {
         String identifyCode = StringUtil.Md5(RedPackDetailTypeEnum.giveOthers.name() + ":" + giveRecord.getId());
         RedPackDetail redPackDetail = redPackDetailService.getByIdentifyCode(identifyCode);
         RedPackDetail updateDetail = RedPackDetailFactory.createGiveOthersSucceed(redPackDetail.getId(), giveRecord);
         redPackDetailService.updateByPrimaryKeySelective(updateDetail);
      } catch (Exception e) {
         throw new RedPackGiveRecordException(1,"更新提现明细出错");
      }
      return giveRecord;
   }
}