yujian
2020-05-09 7e7db2fa55a9a3af46d4fd8ede0dee147f101d64
fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackExchangeServiceImpl.java
@@ -7,7 +7,6 @@
import javax.annotation.Resource;
import org.omg.CORBA.LongHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -30,7 +29,9 @@
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.service.inter.redpack.RedPackDetailService;
import com.yeshi.fanli.service.inter.redpack.RedPackExchangeService;
import com.yeshi.fanli.service.inter.redpack.RedPackForbidService;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.TimeUtil;
import com.yeshi.fanli.util.factory.RedPackDetailFactory;
@@ -39,72 +40,79 @@
   @Resource
   private RedPackExchangeMapper redPackExchangeMapper;
   @Resource
   private RedPackConfigService redPackConfigService;
   @Resource
   private RedPackBalanceService redPackBalanceService;
   @Resource
   private UserMoneyService userMoneyService;
   @Resource
   private RedPackDetailService redPackDetailService;
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   @Resource
   private RedPackForbidService redPackForbidService;
   @Override
   public List<RedPackExchange> query(Integer start, Integer count, String key, Integer state){
   public List<RedPackExchange> query(Integer start, Integer count, String key, Integer state) {
      return redPackExchangeMapper.query(start, count, key, state);
   }
   @Override
   public Long count(String key, Integer state){
   public Long count(String key, Integer state) {
      return redPackExchangeMapper.count(key, state);
   }
   @Override
   public Long countByUidAndState(long uid, Integer state) {
      return redPackExchangeMapper.countByUidAndState(uid, state);
   }
   @Override
   public BigDecimal countMoneyByUidAndState(Long uid, Integer state){
   public BigDecimal countMoneyByUidAndState(Long uid, Integer state) {
      return redPackExchangeMapper.countMoneyByUidAndState(uid, state);
   }
   @Override
   public RedPackExchange selectByPrimaryKey(long id){
   public RedPackExchange selectByPrimaryKey(long id) {
      return redPackExchangeMapper.selectByPrimaryKey(id);
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void exchangeCash(Long uid, BigDecimal amount) throws RedPackExchangeException {
      if (uid == null || amount == null)
         throw new RedPackExchangeException(1, "参数不正确");
      if (redPackForbidService.verifyForbid(uid))
         throw new RedPackExchangeException(1, "红包功能已被封禁");
      String extractBanlenMin = redPackConfigService.getValueByKey("extract_banlen_min");
      BigDecimal balance = redPackBalanceService.getBalance(uid);
      if (balance == null || balance.compareTo(new BigDecimal(extractBanlenMin)) < 0)
         throw new RedPackExchangeException(1, "余额不足" + extractBanlenMin + "元");
      if (balance.compareTo(amount) < 0)
         throw new RedPackExchangeException(1, "提现余额不足");
      String moneyMin = redPackConfigService.getValueByKey("extract_money_min");
      String moneyMax = redPackConfigService.getValueByKey("extract_money_max");
      if (amount.compareTo(new BigDecimal(moneyMin)) < 0 || amount.compareTo(new BigDecimal(moneyMax)) > 0)
         throw new RedPackExchangeException(1, "提现金额至少" + moneyMin + "元至多" + moneyMax + "元");
      long countToday = redPackExchangeMapper.countTodayByUid(uid);
      String extractDayMax = redPackConfigService.getValueByKey("extract_day_max");
      if (!StringUtil.isNullOrEmpty(extractDayMax) && countToday >= Long.parseLong(extractDayMax))
         throw new RedPackExchangeException(1, "今日红包提现次数超限");
      Date nowDate = new Date();
      // 提现申请
      RedPackExchange exchange = new RedPackExchange();
      exchange.setUid(uid);
@@ -112,7 +120,7 @@
      exchange.setState(RedPackExchange.STATE_INIT);
      exchange.setCreateTime(nowDate);
      redPackExchangeMapper.insertSelective(exchange);
      // 减少红包
      try {
         redPackBalanceService.subRedPack(uid, amount, RedPackDetailFactory.createExchange(exchange));
@@ -120,24 +128,26 @@
         throw new RedPackExchangeException(1, "红包信息异常");
      }
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void passExchange(final long id, AdminUser admin) throws RedPackExchangeException {
      RedPackExchange record = redPackExchangeMapper.selectByPrimaryKey(id);
      if (record == null)
         throw new RedPackExchangeException(1,"申请记录已不存在");
      if (record == null)
         throw new RedPackExchangeException(1, "申请记录已不存在");
      if (RedPackExchange.STATE_INIT != record.getState())
         throw new RedPackExchangeException(1,"该申请已被处理,请刷新");
         throw new RedPackExchangeException(1, "该申请已被处理,请刷新");
      if (redPackForbidService.verifyForbid(record.getUid()))
         throw new RedPackExchangeException(1, "红包功能已被封禁");
      Date nowDate = new Date();
      record.setAuditId(admin.getId());
      record.setAuditTime(nowDate);
      record.setState(RedPackExchange.STATE_SUCCESS);
      redPackExchangeMapper.updateByPrimaryKeySelective(record);
      // 资金明细-添加资金
      UserMoneyDetail detail = new UserMoneyDetail();
      detail.setCreateTime(new Date());
@@ -145,13 +155,13 @@
      detail.setMoney(record.getMoney());
      detail.setType(UserMoneyDetailTypeEnum.redPackExchange);
      detail.setTitle(UserMoneyDetailTypeEnum.redPackExchange.getDesc());
      detail.setDescInfo("于"+ TimeUtil.formatDate(record.getCreateTime()) +"提现");
      detail.setDescInfo("于" + TimeUtil.formatDate(record.getCreateTime()) + "提现");
      detail.setUpdateTime(new Date());
      detail.setUserInfo(new UserInfo(record.getUid()));
      // 添加资金
      userMoneyService.addUserMoney(record.getUid(), record.getMoney(), detail);
      // 更新红包提现明细
      try {
         String identifyCode = StringUtil.Md5(RedPackDetailTypeEnum.redExchange.name() + ":" + record.getId());
@@ -161,54 +171,57 @@
         redPackDetailService.updateByPrimaryKeySelective(updateDetail);
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new RedPackExchangeException(1,"更新提现明细出错");
         throw new RedPackExchangeException(1, "更新提现明细出错");
      }
      //消息
      // 消息
      SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
      String beizu = "红包提现成功后请到“我的-账户余额”中查看";
      MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
      dto.setTitle("红包提现");
      dto.setTime(sd.format(record.getCreateTime()));
      dto.setHandle("人工审核" + sd.format(new Date()));
      dto.setMoney("¥" + record.getMoney().setScale(2));
      dto.setHandle("人工审核" + sd.format(new Date()));
      dto.setMoney("¥" + record.getMoney().setScale(2));
      dto.setBalance("¥" + redPackBalanceService.getBalance(record.getUid()));
      userMoneyMsgNotificationService.redPackMsg(record.getUid(), MsgTypeMoneyTypeEnum.redPackExchangePass, new Gson().toJson(dto), beizu);
      userMoneyMsgNotificationService.redPackMsg(record.getUid(), MsgTypeMoneyTypeEnum.redPackExchangePass,
            new Gson().toJson(dto), beizu);
   }
   @Override
   @Transactional(rollbackFor = Exception.class)
   public void rejectExchange(long id, String reason, AdminUser admin) throws RedPackExchangeException {
      RedPackExchange record = redPackExchangeMapper.selectByPrimaryKey(id);
      if (record == null)
         throw new RedPackExchangeException(1,"申请记录已不存在");
      if (record == null)
         throw new RedPackExchangeException(1, "申请记录已不存在");
      if (RedPackExchange.STATE_INIT != record.getState())
         throw new RedPackExchangeException(1,"该申请已被处理,请刷新");
         throw new RedPackExchangeException(1, "该申请已被处理,请刷新");
      record.setReason(reason);
      record.setAuditTime(new Date());
      record.setAuditId(admin.getId());
      record.setState(RedPackExchange.STATE_REJECT);
      redPackExchangeMapper.updateByPrimaryKeySelective(record);
      // 退回红包
      try {
         redPackBalanceService.addRedPack(record.getUid(), record.getMoney(), RedPackDetailFactory.createExchange(record));
         redPackBalanceService.addRedPack(record.getUid(), record.getMoney(),
               RedPackDetailFactory.createExchangeReject(record));
      } catch (Exception e) {
         LogHelper.errorDetailInfo(e);
         throw new RedPackExchangeException(1, "红包退回时出错");
      }
      //消息
      // 消息
      SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm");
      MsgRedPackExchangeContentDTO dto = new MsgRedPackExchangeContentDTO();
      dto.setTitle("红包提现");
      dto.setReason(reason);
      dto.setHandle(""); // TODO 处理方式待定
      dto.setTime(sd.format(record.getCreateTime()));
      userMoneyMsgNotificationService.redPackMsg(record.getUid(), MsgTypeMoneyTypeEnum.redPackExchangeReject, new Gson().toJson(dto), null);
      userMoneyMsgNotificationService.redPackMsg(record.getUid(), MsgTypeMoneyTypeEnum.redPackExchangeReject,
            new Gson().toJson(dto), null);
   }
}