admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/service/impl/money/UserMoneyDebtServiceImpl.java
@@ -1,212 +1,297 @@
package com.yeshi.fanli.service.impl.money;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDebtMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDebtReturnHistoryMapper;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.money.UserMoneyDebt;
import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum;
import com.yeshi.fanli.entity.money.UserMoneyDebtReturnHistory;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack;
import com.yeshi.fanli.exception.money.UserMoneyDebtException;
import com.yeshi.fanli.exception.money.UserMoneyDetailException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.HongBaoV2Service;
import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
import com.yeshi.fanli.service.inter.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoWeiQuanDrawBackService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.service.inter.user.UserMoneyService;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
@Service
public class UserMoneyDebtServiceImpl implements UserMoneyDebtService {
   @Resource
   private UserMoneyDebtMapper userMoneyDebtMapper;
   @Resource
   private UserMoneyDebtReturnHistoryMapper userMoneyDebtReturnHistoryMapper;
   @Resource
   private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService;
   @Resource
   private TaoBaoOrderService taoBaoOrderService;
   @Resource
   private HongBaoV2Service hongBaoV2Service;
   @Resource
   private UserMoneyService userMoneyService;
   @Resource
   private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
   @Resource
   private UserInfoService userInfoService;
   @Override
   public void addUserMoneyDebt(UserMoneyDebt debt) throws UserMoneyDebtException {
      if (debt == null)
         return;
      if (debt.getType() == UserMoneyDebtTypeEnum.order) {
         if (debt.getSourceId() == null)
            throw new UserMoneyDebtException(1, "sourceId为空");
         if (debt.getUid() == null || debt.getOriginMoney() == null)
            throw new UserMoneyDebtException(2, "信息不完整");
         if (debt.getLeftMoney() == null)
            debt.setLeftMoney(debt.getOriginMoney());
         if (debt.getCreateTime() == null)
            debt.setCreateTime(new Date());
         UserMoneyDebt old = userMoneyDebtMapper.selectByUidAndTypeAndSourceId(debt.getUid(), debt.getType(),
               debt.getSourceId());
         if (old != null) {
            throw new UserMoneyDebtException(3, "对应维权已经存在");
         }
         userMoneyDebtMapper.insertSelective(debt);
      }
   }
   @Override
   public UserMoneyDebt selectByTypeAndSourceId(UserMoneyDebtTypeEnum type, Long sourceId) {
      return userMoneyDebtMapper.selectByTypeAndSourceId(type, sourceId);
   }
   @Transactional
   @Override
   public void repayDebt(UserMoneyDebt debt, BigDecimal money) throws UserMoneyDebtException {
      if (debt == null || debt.getId() == null)
         throw new UserMoneyDebtException(1, "参数不完整");
      if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
         throw new UserMoneyDebtException(2, "偿还资金需大于0");
      UserMoneyDebt old = userMoneyDebtMapper.selectByPrimaryKey(debt.getId());
      if (old == null)
         throw new UserMoneyDebtException(3, "借贷关系不存在");
      if (old.getUid().longValue() != debt.getUid())
         throw new UserMoneyDebtException(4, "只能本人还钱");
      if (money.compareTo(old.getLeftMoney()) < 0)
         throw new UserMoneyDebtException(5, "不够还");
      UserMoneyDebt update = new UserMoneyDebt();
      update.setId(old.getId());
      update.setUpdateTime(new Date());
      update.setLeftMoney(old.getLeftMoney().subtract(money));
      userMoneyDebtMapper.updateByPrimaryKeySelective(update);
      UserMoneyDebtReturnHistory history = new UserMoneyDebtReturnHistory();
      history.setBeiZhu(null);
      history.setCreateTime(new Date());
      history.setDebt(old);
      history.setMoney(money);
      history.setUid(old.getUid());
      userMoneyDebtReturnHistoryMapper.insertSelective(history);
      if (debt.getType() == UserMoneyDebtTypeEnum.order) {
         HongBaoV2 hb = hongBaoV2Service.selectByPrimaryKey(debt.getSourceId());
         if (hb == null) {
            throw new UserMoneyDebtException(6, "偿还的订单ID不存在");
         }
         TaoBaoWeiQuanDrawBack weiQuanDrawBack = taoBaoWeiQuanDrawBackService.selectByHongBaoId(hb.getId());
         if (weiQuanDrawBack == null)
            throw new UserMoneyDebtException(7, "尚未找到退款信息");
         // 资金变化,添加用户资金记录,添加相关通知
         Long uid = debt.getUid();
         switch (hb.getType()) {
         case HongBaoV2.TYPE_ZIGOU:
            // 新版资金记录
            try {
               UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createFanLiWeiQuan(uid, weiQuanDrawBack,
                     money);
               userMoneyService.subUserMoney(uid, money, userMoneyDetail);
            } catch (UserMoneyDetailException e) {
               throw new UserMoneyDebtException(12, "插入资金详情出错");
            }
            // 新版通知
            userMoneyMsgNotificationService.fanliOrderWeiQuan(uid, weiQuanDrawBack.getOrderId(), money,
                  userInfoService.getBalance(uid));
            break;
         case HongBaoV2.TYPE_SHARE_GOODS:
            // 新版资金记录
            try {
               UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShareWeiQuan(debt.getUid(),
                     weiQuanDrawBack, money);
               userMoneyService.subUserMoney(uid, money, userMoneyDetail);
               userMoneyMsgNotificationService.shareOrderWeiQuan(debt.getUid(), weiQuanDrawBack.getOrderId(),
                     money, userInfoService.getBalance(uid));
            } catch (UserMoneyDetailException e) {
               throw new UserMoneyDebtException(12, "插入资金详情出错");
            }
            break;
         case HongBaoV2.TYPE_SHARE_ERJI:
         case HongBaoV2.TYPE_SHARE_YIJI:
         case HongBaoV2.TYPE_ERJI:
         case HongBaoV2.TYPE_YIJI:
            // 新版资金记录
            try {
               UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInviteWeiQuan(uid, weiQuanDrawBack,
                     money);
               userMoneyService.subUserMoney(uid, money, userMoneyDetail);
            } catch (UserMoneyDetailException e) {
               throw new UserMoneyDebtException(12, "插入资金详情出错");
            }
            // 新版通知
            userMoneyMsgNotificationService.inviteOrderWeiQuan(uid, weiQuanDrawBack.getOrderId(), money,
                  userInfoService.getBalance(uid));
            break;
         }
      }
   }
   @Transactional
   @Override
   public void repayDebt(Long uid) {
      // 查询是否有欠债
      List<UserMoneyDebt> list = userMoneyDebtMapper.listByUidWithHasLeftMoney(uid, 0, 50);
      if (list != null && list.size() > 0) {// 有欠债
         for (UserMoneyDebt debt : list) {
            BigDecimal leftMoney = userInfoService.getBalance(uid);
            if (leftMoney.compareTo(debt.getLeftMoney()) >= 0) {// 有足够的资金偿还债务
               try {
                  repayDebt(debt, debt.getLeftMoney());
               } catch (UserMoneyDebtException e) {
                  try {
                     LogHelper.errorDetailInfo(e, "uid:" + uid + " debtId:" + debt.getId(), null);
                  } catch (Exception e1) {
                     e1.printStackTrace();
                  }
               }
            }
         }
      }
   }
}
package com.yeshi.fanli.service.impl.money;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yeshi.fanli.dao.mybatis.AlipayAccountValidNormalHistoryMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDebtMapper;
import com.yeshi.fanli.dao.mybatis.money.UserMoneyDebtReturnHistoryMapper;
import com.yeshi.fanli.entity.bus.user.AlipayAccountValidNormalHistory;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.entity.money.UserMoneyDebt;
import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum;
import com.yeshi.fanli.entity.money.UserMoneyDebtReturnHistory;
import com.yeshi.fanli.entity.money.UserMoneyDetail;
import com.yeshi.fanli.entity.order.CommonOrder;
import com.yeshi.fanli.entity.order.HongBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanDrawBack;
import com.yeshi.fanli.exception.money.UserMoneyDebtException;
import com.yeshi.fanli.exception.money.UserMoneyDetailException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
import com.yeshi.fanli.service.inter.money.UserMoneyService;
import com.yeshi.fanli.service.inter.money.msg.UserMoneyMsgNotificationService;
import com.yeshi.fanli.service.inter.money.tb.TaoBaoWeiQuanDrawBackService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoOrderService;
import com.yeshi.fanli.service.inter.order.HongBaoV2Service;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
@Service
public class UserMoneyDebtServiceImpl implements UserMoneyDebtService {
    @Resource
    private UserMoneyDebtMapper userMoneyDebtMapper;
    @Resource
    private UserMoneyDebtReturnHistoryMapper userMoneyDebtReturnHistoryMapper;
    @Resource
    private TaoBaoWeiQuanDrawBackService taoBaoWeiQuanDrawBackService;
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
    @Resource
    private HongBaoV2Service hongBaoV2Service;
    @Resource
    private HongBaoOrderService hongBaoOrderService;
    @Resource
    private UserMoneyService userMoneyService;
    @Resource
    private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private AlipayAccountValidNormalHistoryMapper alipayAccountValidNormalHistoryMapper;
    @Resource
    private CommonOrderService commonOrderService;
    @Override
    public void addUserMoneyDebt(UserMoneyDebt debt) throws UserMoneyDebtException {
        if (debt == null)
            return;
        if (debt.getType() == UserMoneyDebtTypeEnum.order) {
            if (debt.getSourceId() == null)
                throw new UserMoneyDebtException(1, "sourceId为空");
            if (debt.getUid() == null || debt.getOriginMoney() == null)
                throw new UserMoneyDebtException(2, "信息不完整");
            if (debt.getLeftMoney() == null)
                debt.setLeftMoney(debt.getOriginMoney());
            if (debt.getCreateTime() == null)
                debt.setCreateTime(new Date());
            UserMoneyDebt old = userMoneyDebtMapper.selectByUidAndTypeAndSourceId(debt.getUid(), debt.getType(),
                    debt.getSourceId());
            if (old != null) {
                throw new UserMoneyDebtException(3, "对应售后已经存在");
            }
            userMoneyDebtMapper.insertSelective(debt);
        }
    }
    @Override
    public UserMoneyDebt selectByTypeAndSourceId(UserMoneyDebtTypeEnum type, Long sourceId) {
        return userMoneyDebtMapper.selectByTypeAndSourceId(type, sourceId);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void repayDebt(UserMoneyDebt debt, BigDecimal money) throws UserMoneyDebtException {
        if (debt == null || debt.getId() == null)
            throw new UserMoneyDebtException(1, "参数不完整");
        if (money == null || money.compareTo(new BigDecimal(0)) <= 0)
            throw new UserMoneyDebtException(2, "偿还资金需大于0");
        UserMoneyDebt old = userMoneyDebtMapper.selectByPrimaryKey(debt.getId());
        if (old == null)
            throw new UserMoneyDebtException(3, "借贷关系不存在");
        if (old.getUid().longValue() != debt.getUid())
            throw new UserMoneyDebtException(4, "只能本人还钱");
        if (money.compareTo(old.getLeftMoney()) < 0)
            throw new UserMoneyDebtException(5, "不够还");
        UserMoneyDebt update = new UserMoneyDebt();
        update.setId(old.getId());
        update.setUpdateTime(new Date());
        update.setLeftMoney(old.getLeftMoney().subtract(money));
        userMoneyDebtMapper.updateByPrimaryKeySelective(update);
        UserMoneyDebtReturnHistory history = new UserMoneyDebtReturnHistory();
        history.setBeiZhu(null);
        history.setCreateTime(new Date());
        history.setDebt(old);
        history.setMoney(money);
        history.setUid(old.getUid());
        userMoneyDebtReturnHistoryMapper.insertSelective(history);
        if (debt.getType() == UserMoneyDebtTypeEnum.order) {
            HongBaoV2 hb = hongBaoV2Service.selectByPrimaryKey(debt.getSourceId());
            if (hb == null) {
                throw new UserMoneyDebtException(6, "偿还的订单ID不存在");
            }
            TaoBaoWeiQuanDrawBack weiQuanDrawBack = taoBaoWeiQuanDrawBackService.selectByHongBaoId(hb.getId());
            if (weiQuanDrawBack == null)
                throw new UserMoneyDebtException(7, "尚未找到退款信息");
            // 资金变化,添加用户资金记录,添加相关通知
            Long uid = debt.getUid();
            switch (hb.getType()) {
                case HongBaoV2.TYPE_ZIGOU:
                    // 新版资金记录
                    try {
                        UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createFanLiWeiQuan(uid, weiQuanDrawBack,
                                money);
                        userMoneyService.subUserMoney(uid, money, userMoneyDetail);
                    } catch (UserMoneyDetailException e) {
                        throw new UserMoneyDebtException(12, "插入资金详情出错");
                    }
                    // 新版通知
                    List<CommonOrder> orderList = commonOrderService.listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO,
                            weiQuanDrawBack.getOrderId());
                    int goodsCount = 0;
                    for (CommonOrder co : orderList) {
                        if (co.getState() != CommonOrder.STATE_SX)
                            goodsCount += co.getCount();
                    }
                    BigDecimal fanliMoney = new BigDecimal(0);
                    List<HongBaoOrder> hongBaoOrderList = hongBaoOrderService
                            .listDetailByOrderIdAndSourceType(weiQuanDrawBack.getOrderId(), Constant.SOURCE_TYPE_TAOBAO);
                    for (HongBaoOrder hongBaoOrder : hongBaoOrderList) {
                        fanliMoney = fanliMoney.add(hongBaoOrder.getHongBaoV2().getMoney());
                    }
                    userMoneyMsgNotificationService.fanliOrderWeiQuan(uid, weiQuanDrawBack.getOrderId(),
                            Constant.SOURCE_TYPE_TAOBAO, money, fanliMoney, goodsCount,
                            orderList.get(0).getThirdCreateTime());
                    break;
                case HongBaoV2.TYPE_SHARE_GOODS:
                    // 新版资金记录
                    try {
                        UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createShareWeiQuan(debt.getUid(),
                                weiQuanDrawBack, money);
                        userMoneyService.subUserMoney(uid, money, userMoneyDetail);
                        // 新版通知
                        List<CommonOrder> orderList1 = commonOrderService
                                .listBySourceTypeAndOrderId(Constant.SOURCE_TYPE_TAOBAO, weiQuanDrawBack.getOrderId());
                        int goodsCount1 = 0;
                        for (CommonOrder co : orderList1) {
                            if (co.getState() != CommonOrder.STATE_SX)
                                goodsCount1 += co.getCount();
                        }
                        BigDecimal fanliMoney1 = new BigDecimal(0);
                        List<HongBaoOrder> hongBaoOrderList1 = hongBaoOrderService.listDetailByOrderIdAndSourceType(
                                weiQuanDrawBack.getOrderId(), Constant.SOURCE_TYPE_TAOBAO);
                        for (HongBaoOrder hongBaoOrder : hongBaoOrderList1) {
                            fanliMoney1 = fanliMoney1.add(hongBaoOrder.getHongBaoV2().getMoney());
                        }
                        userMoneyMsgNotificationService.shareOrderWeiQuan(uid, weiQuanDrawBack.getOrderId(),
                                Constant.SOURCE_TYPE_TAOBAO, money, fanliMoney1, goodsCount1,
                                orderList1.get(0).getThirdCreateTime());
                    } catch (UserMoneyDetailException e) {
                        throw new UserMoneyDebtException(12, "插入资金详情出错");
                    }
                    break;
                case HongBaoV2.TYPE_SHARE_ERJI:
                case HongBaoV2.TYPE_SHARE_YIJI:
                case HongBaoV2.TYPE_ERJI:
                case HongBaoV2.TYPE_YIJI:
                    // 新版资金记录
                    try {
                        UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createInviteWeiQuan(uid, weiQuanDrawBack,
                                money);
                        userMoneyService.subUserMoney(uid, money, userMoneyDetail);
                    } catch (UserMoneyDetailException e) {
                        throw new UserMoneyDebtException(12, "插入资金详情出错");
                    }
                    break;
            }
        } else if (debt.getType() == UserMoneyDebtTypeEnum.extractVerify) {// 提现验证
            // 新版资金记录
            AlipayAccountValidNormalHistory aliPayAccountHistory = alipayAccountValidNormalHistoryMapper
                    .selectByPrimaryKey(debt.getSourceId());
            AlipayAccountValidNormalHistory first = alipayAccountValidNormalHistoryMapper
                    .selectLatestByUid(aliPayAccountHistory.getUid());
            if (aliPayAccountHistory == null)
                return;
            try {
                UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(aliPayAccountHistory,
                        money);
                userMoneyService.subUserMoney(aliPayAccountHistory.getUid(), money, userMoneyDetail);
            } catch (UserMoneyDetailException e) {
                throw new UserMoneyDebtException(12, "插入资金详情出错");
            }
            userMoneyMsgNotificationService.alipayAccountValid(aliPayAccountHistory, "1个月",
                    first.getId().longValue() != aliPayAccountHistory.getId());
        }
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void repayDebt(Long uid) {
        // 查询是否有欠债
        List<UserMoneyDebt> list = userMoneyDebtMapper.listByUidWithHasLeftMoney(uid, 0, 50);
        if (list != null && list.size() > 0) {// 有欠债
            for (UserMoneyDebt debt : list) {
                BigDecimal leftMoney = userInfoService.getBalance(uid);
                if (leftMoney.compareTo(debt.getLeftMoney()) >= 0) {// 有足够的资金偿还债务
                    try {
                        repayDebt(debt, debt.getLeftMoney());
                    } catch (UserMoneyDebtException e) {
                        try {
                            LogHelper.errorDetailInfo(e, "uid:" + uid + " debtId:" + debt.getId(), null);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    @Override
    public boolean isHaveDebtToRepay(Long uid) {
        List<UserMoneyDebt> debtList = userMoneyDebtMapper.listByUidWithHasLeftMoney(uid, 0, 1);
        if (debtList != null && debtList.size() > 0)
            return true;
        return false;
    }
    @Override
    public List<Long> listNeedRepayDebtUser(int page, int pageSize) {
        return userMoneyDebtMapper.listUidWithHasDebtToRepay((page - 1) * pageSize, pageSize);
    }
    @Override
    public long countNeedRepayDebtUser() {
        return userMoneyDebtMapper.countUidWithHasDebtToRepay();
    }
}