yujian
2019-11-12 48077e8c704216f38939cfbf1702f45d7190801d
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;
@@ -20,6 +19,7 @@
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.RedPackBalanceException;
import com.yeshi.fanli.exception.redpack.RedPackGiveRecordException;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
@@ -30,6 +30,7 @@
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;
@Service
@@ -64,38 +65,43 @@
   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), "请到我的-红包查看");
   }
   
   
@@ -110,7 +116,13 @@
      if (amount.compareTo(new BigDecimal(giveMin)) < 0 || amount.compareTo(new BigDecimal(giveMax)) > 0)
         throw new RedPackGiveRecordException(1, "赠送金额至少" + giveMin + "元至多" + giveMax + "元");
      
      BigDecimal balance = redPackBalanceService.getBalance(uid);
      BigDecimal balance = null;
      try {
         balance = redPackBalanceService.getBalance(uid);
      } catch (RedPackBalanceException e1) {
         throw new RedPackGiveRecordException(1, e1.getMsg());
      }
      if (balance == null || amount.compareTo(balance) > 0)
         throw new RedPackGiveRecordException(1, "余额不足");