package com.ruoyi.user.service.impl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.admin.api.entity.WithdrawalSetting;
|
import com.ruoyi.admin.api.feignClient.AdminClient;
|
import com.ruoyi.common.core.constant.Constants;
|
import com.ruoyi.common.core.exception.GlobalException;
|
import com.ruoyi.user.entity.Order;
|
import com.ruoyi.user.entity.RecoveryServe;
|
import com.ruoyi.user.entity.User;
|
import com.ruoyi.user.entity.Withdraw;
|
import com.ruoyi.user.mapper.WithdrawMapper;
|
import com.ruoyi.user.service.OrderService;
|
import com.ruoyi.user.service.RecoveryServeService;
|
import com.ruoyi.user.service.UserService;
|
import com.ruoyi.user.service.WithdrawService;
|
import com.ruoyi.user.vo.WithdrawListVO;
|
import com.ruoyi.user.vo.WithdrawMoneyVO;
|
import com.ruoyi.user.vo.WithdrawOrderVO;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 用户提现申请记录表 服务实现类
|
* </p>
|
*
|
* @author hjl
|
* @since 2024-06-07
|
*/
|
@Service
|
public class WithdrawServiceImpl extends ServiceImpl<WithdrawMapper, Withdraw> implements WithdrawService {
|
|
@Resource
|
private OrderService orderService;
|
@Resource
|
private RecoveryServeService recoveryServeService;
|
@Resource
|
private UserService userService;
|
@Resource
|
private AdminClient adminClient;
|
|
@Override
|
public WithdrawListVO withdrawList(Long userid) {
|
WithdrawListVO withdrawList = new WithdrawListVO();
|
List<Order> orderList = orderService.lambdaQuery().eq(Order::getUserId, userid)
|
.eq(Order::getState, 3).eq(Order::getIsDelete, 0).list();
|
// 总金额
|
BigDecimal totalMoney = orderList.stream().map(Order::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
|
// 未提现金额
|
BigDecimal undelivered = orderList.stream().filter(data -> Constants.ZERO.equals(data.getIsWithdrawal()))
|
.map(Order::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
|
// 已提现金额
|
BigDecimal withdrawn = orderList.stream().filter(data -> Constants.ONE.equals(data.getIsWithdrawal()))
|
.map(Order::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
|
withdrawList.setMoneyCount(new WithdrawMoneyVO(totalMoney, undelivered, withdrawn));
|
// 回收服务列表
|
List<RecoveryServe> serveList = recoveryServeService.lambdaQuery().eq(RecoveryServe::getIsDelete, 0)
|
.in(RecoveryServe::getId, orderList.stream().map(Order::getServeId).collect(Collectors.toList())).list();
|
Map<Integer, RecoveryServe> serveMap = serveList.stream().collect(Collectors.toMap(RecoveryServe::getId, serve -> serve));
|
// 订单列表
|
List<WithdrawOrderVO> list = new ArrayList<>();
|
for (Order order : orderList) {
|
RecoveryServe recoveryServe = serveMap.get(order.getServeId());
|
WithdrawOrderVO withdrawOrder = new WithdrawOrderVO();
|
withdrawOrder.setOrderNumber(order.getOrderNumber());
|
withdrawOrder.setServeId(order.getServeId());
|
withdrawOrder.setServeName(recoveryServe.getServeName());
|
withdrawOrder.setServeDescribe(recoveryServe.getServeDescribe());
|
withdrawOrder.setCover(recoveryServe.getCover());
|
withdrawOrder.setCompleteTime(order.getCompleteTime());
|
withdrawOrder.setIsWithdrawal(order.getIsWithdrawal());
|
list.add(withdrawOrder);
|
}
|
withdrawList.setWithdrawOrder(list);
|
return withdrawList;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public Boolean confirmWithdraw(Integer orderId, Long userid) {
|
Order order = orderService.lambdaQuery().eq(Order::getId, orderId).eq(Order::getIsDelete, 0).one();
|
if (null == order) {
|
throw new GlobalException("订单信息异常!");
|
}
|
// 校验提现
|
List<Withdraw> list = this.lambdaQuery().eq(Withdraw::getUserId, userid)
|
.eq(Withdraw::getOrderId, order).list();
|
List<Integer> stateList = list.stream().map(Withdraw::getState).collect(Collectors.toList());
|
if (stateList.contains(Constants.ZERO)) {
|
throw new GlobalException("当前订单已提交提现申请,请等待审核!");
|
} else if (stateList.contains(Constants.ONE)) {
|
throw new GlobalException("当前订单已提现通过!");
|
}
|
User user = userService.lambdaQuery().eq(User::getId, userid).eq(User::getIsDelete, 0).one();
|
// 系统审核设置
|
WithdrawalSetting setting = adminClient.withdrawProcess().getData();
|
Withdraw withdraw = new Withdraw();
|
if (Constants.ZERO.equals(setting.getEnableProcess())) {
|
withdraw.setState(Constants.ZERO);
|
// todo 商家微信打款至微信零钱
|
}
|
withdraw.setUserId(user.getId());
|
withdraw.setUserPhone(user.getPhone());
|
withdraw.setApplyForTime(new Date());
|
withdraw.setApplyForMoney(order.getOrderMoney());
|
withdraw.setOrderId(orderId);
|
return this.save(withdraw);
|
}
|
}
|