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.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.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 UserMoneyService userMoneyService;
|
|
@Resource
|
private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
|
|
@Resource
|
private UserInfoService userInfoService;
|
|
@Resource
|
private AlipayAccountValidNormalHistoryMapper alipayAccountValidNormalHistoryMapper;
|
|
@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(),
|
Constant.SOURCE_TYPE_TAOBAO, 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(),
|
Constant.SOURCE_TYPE_TAOBAO, 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(),
|
Constant.SOURCE_TYPE_TAOBAO, money, userInfoService.getBalance(uid));
|
break;
|
}
|
} else if (debt.getType() == UserMoneyDebtTypeEnum.extractVerify) {// 提现验证
|
// 新版资金记录
|
AlipayAccountValidNormalHistory aliPayAccountHistory = alipayAccountValidNormalHistoryMapper
|
.selectByPrimaryKey(debt.getSourceId());
|
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);
|
}
|
}
|
|
@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();
|
}
|
}
|
}
|
}
|
}
|
}
|
|
@Override
|
public boolean isHaveDebtToRepay(Long uid) {
|
List<UserMoneyDebt> debtList = userMoneyDebtMapper.listByUidWithHasLeftMoney(uid, 0, 1);
|
if (debtList != null && debtList.size() > 0)
|
return true;
|
return false;
|
}
|
|
}
|