yujian
2019-12-12 8c04d27e5cebb7e654c208533e9567c4df2c8acc
fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackGiveRecordServiceImpl.java
@@ -3,7 +3,6 @@
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
@@ -11,8 +10,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.yeshi.utils.DateUtil;
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.redpack.RedPackGiveRecordMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserRedPackGiftMQMsg;
import com.yeshi.fanli.dto.msg.MsgRedPackGiveContentDTO;
import com.yeshi.fanli.entity.bus.msg.MsgMoneyDetail.MsgTypeMoneyTypeEnum;
import com.yeshi.fanli.entity.bus.user.TokenRecord;
@@ -25,12 +28,16 @@
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.RedPackForbidService;
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.annotation.RequestSerializableByKeyService;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
@Service
public class RedPackGiveRecordServiceImpl implements RedPackGiveRecordService {
@@ -40,76 +47,88 @@
   @Resource
   private RedPackConfigService redPackConfigService;
   @Resource
   private RedPackBalanceService redPackBalanceService;
   @Resource
   private TokenRecordService tokenRecordService;
   @Resource
   private RedPackDetailService redPackDetailService;
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   @Resource
   private RedPackForbidService redPackForbidService;
   
   @Resource(name = "producer")
   private Producer producer;
   @Override
   public RedPackGiveRecord selectByPrimaryKey(Long id) {
      return redPackGiveRecordMapper.selectByPrimaryKey(id);
   }
   @Override
   public void updateByPrimaryKeySelective(RedPackGiveRecord record) {
      redPackGiveRecordMapper.updateByPrimaryKeySelective(record);
   }
   @Override
   public List<RedPackGiveRecord> overdueList(int count) {
      return redPackGiveRecordMapper.overdueList(count);
   }
   @Override
   @RequestSerializableByKeyService(key = "#id")
   @Transactional(rollbackFor = Exception.class)
   public void overdue(List<RedPackGiveRecord> list) throws Exception {
      if (list == null || list.size() == 0)
   public void overdueByPrimaryKey(Long id) throws Exception {
      if (id == null)
         return;
      RedPackGiveRecord giveRecord = redPackGiveRecordMapper.selectByPrimaryKey(id);
      if (giveRecord == null)
         return;
      if (giveRecord.getState() != null && giveRecord.getState() != RedPackGiveRecord.STATE_INIT)
         return;
      // 赠送记录失效
      RedPackGiveRecord updateRecord = new RedPackGiveRecord();
      updateRecord.setId(giveRecord.getId());
      updateRecord.setState(RedPackGiveRecord.STATE_OVERDUE);
      redPackGiveRecordMapper.updateByPrimaryKeySelective(updateRecord);
      // 口令失效
      tokenRecordService.invalidByRedPack(id);
      // 退回红包
      redPackBalanceService.addRedPack(giveRecord.getGiveUid(), giveRecord.getAmount(),
            RedPackDetailFactory.createGiveOthersFail(giveRecord));
      // 退回消息
      SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
      RedPackGiveRecord updateRecord = null;
      for (RedPackGiveRecord giveRecord: list) {
         updateRecord = new RedPackGiveRecord();
         updateRecord.setId(giveRecord.getId());
         updateRecord.setState(1);
         redPackGiveRecordMapper.updateByPrimaryKeySelective(updateRecord);
         // 退回红包
         redPackBalanceService.addRedPack(giveRecord.getGiveUid(), giveRecord.getAmount(), RedPackDetailFactory.createGiveOthersFail(giveRecord));
         // 退回消息
         MsgRedPackGiveContentDTO givedto = new MsgRedPackGiveContentDTO();
         givedto.setTitle("你赠送的红包未被成功领取");
         givedto.setMoney("¥" + giveRecord.getAmount().setScale(2));
         givedto.setTime(sd.format(new Date()));
         givedto.setGiveTime(sd.format(giveRecord.getGiveTime()));
         userMoneyMsgNotificationService.redPackMsg(giveRecord.getGiveUid(), MsgTypeMoneyTypeEnum.redPackGiveBack, new Gson().toJson(givedto), "请到我的-红包查看");
      }
      MsgRedPackGiveContentDTO givedto = new MsgRedPackGiveContentDTO();
      givedto.setTitle("你赠送的红包未被成功领取");
      givedto.setMoney("¥" + giveRecord.getAmount().setScale(2));
      givedto.setTime(sd.format(new Date()));
      givedto.setGiveTime(sd.format(giveRecord.getGiveTime()));
      userMoneyMsgNotificationService.redPackMsg(giveRecord.getGiveUid(), MsgTypeMoneyTypeEnum.redPackGiveBack,
            new Gson().toJson(givedto), "请到我的-红包查看");
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public String giving(Long uid, BigDecimal amount) throws RedPackGiveRecordException {
      if (uid == null || amount == null)
         throw new RedPackGiveRecordException(1, "参数不正确");
      if(redPackForbidService.verifyForbid(uid))
         throw new RedPackGiveRecordException(1, "红包功能已被封禁");
      String giveMin = redPackConfigService.getValueByKey("give_money_min");
      String giveMax = redPackConfigService.getValueByKey("give_money_max");
      if (amount.compareTo(new BigDecimal(giveMin)) < 0 || amount.compareTo(new BigDecimal(giveMax)) > 0)
         throw new RedPackGiveRecordException(1, "赠送金额至少" + giveMin + "元至多" + giveMax + "元");
      BigDecimal balance = redPackBalanceService.getBalance(uid);
      if (balance == null || amount.compareTo(balance) > 0)
         throw new RedPackGiveRecordException(1, "余额不足");
@@ -144,55 +163,68 @@
      while (tips.contains("{APP名称}")) {
         tips = tips.replace("{APP名称}", projectChineseName);
      }
      tips = tips.replace("{口令}", token).replace("{下载链接}", redPackConfigService.getValueByKey("app_down_link")).replace("{面额}",
            amount.toString());
      tips = tips.replace("{口令}", token).replace("{下载链接}", redPackConfigService.getValueByKey("app_down_link"))
            .replace("{面额}", amount.setScale(2).toString());
      // 减少红包
      try {
         redPackBalanceService.subRedPack(uid, amount, RedPackDetailFactory.createGiveOthers(giveRecord));
      } catch (Exception e) {
         throw new RedPackGiveRecordException(1, "红包创建失败");
      }
      UserRedPackGiftMQMsg msg = new UserRedPackGiftMQMsg();
      msg.setId(giveRecord.getId());
      msg.setUid(uid);
      Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.redPackGiftDrawback,
            msg);
      try {
         producer.send(message);
      } catch (Exception e) {
         throw new RedPackGiveRecordException(1, "红包创建失败");
      }
      return tips;
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public RedPackGiveRecord receiveFriendsGive(Long uid, Long id) throws RedPackGiveRecordException{
   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.setReceiveUid(uid);
      giveRecord.setReceiveTime(now);
      giveRecord.setState(RedPackGiveRecord.STATE_RECEIVE);
      // 领取人增加红包
      try {
         redPackBalanceService.addRedPack(uid, giveRecord.getAmount(), RedPackDetailFactory.createGiveOthersReceive(giveRecord));
         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);
         RedPackDetail updateDetail = RedPackDetailFactory.createGiveOthersSucceed(redPackDetail.getId(),
               giveRecord);
         redPackDetailService.updateByPrimaryKeySelective(updateDetail);
      } catch (Exception e) {
         throw new RedPackGiveRecordException(1,"更新提现明细出错");
         throw new RedPackGiveRecordException(1, "更新提现明细出错");
      }
      return giveRecord;
   }
}