mitao
2025-03-19 0ab9dfd8f122195e4e9f09bd50c59e0a47450bec
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/TPayOrderServiceImpl.java
New file
@@ -0,0 +1,134 @@
package com.ruoyi.system.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.AmountConstant;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.OrderNos;
import com.ruoyi.system.dto.MakeOrderDto;
import com.ruoyi.system.dto.MakeOrderResp;
import com.ruoyi.system.mapper.TPayOrderMapper;
import com.ruoyi.system.model.TBill;
import com.ruoyi.system.model.TOrderBill;
import com.ruoyi.system.model.TPayOrder;
import com.ruoyi.system.model.TTenant;
import com.ruoyi.system.service.TBillService;
import com.ruoyi.system.service.TOrderBillService;
import com.ruoyi.system.service.TPayOrderService;
import com.ruoyi.system.service.TTenantService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
 * <p>
 * 支付订单表 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-02-07
 */
@Service
public class TPayOrderServiceImpl extends ServiceImpl<TPayOrderMapper, TPayOrder> implements TPayOrderService {
    @Autowired
    TBillService billService;
    @Autowired
    TTenantService tTenantService;
    @Autowired
    TOrderBillService tOrderBillService;
    public long caculateRentFee(TBill bill){
        return bill.getOutstandingMoney()
                .longValue();
    }
    public String createAppletUrl(String orderNo){
        return ""+orderNo;
    }
    @Override
    public MakeOrderResp makeOrder(MakeOrderDto dto) {
        TTenant user = tTenantService.getById(dto.getUserId());
        if (user==null){
            throw new ServiceException("租户不存在");
        }
        MakeOrderResp resp = new MakeOrderResp();
        if (dto.getBillIds().size()==1){
            TBill bill = billService.getById(dto.getBillIds().get(0));
            if (bill == null) {
                throw new ServiceException("账单不存在");
            }
            //欠费金额转成单位分比较
            long rent = bill.getOutstandingMoney().multiply(AmountConstant.b100)
                    .setScale(2,RoundingMode.HALF_DOWN).longValue();
            if (rent==0){
                throw new ServiceException("该账单已缴费");
            }
            if (rent<dto.getAmount()){
                throw new ServiceException("支付金额超过了账单欠费金额");
            }
            TPayOrder order = new TPayOrder();
            order.setId(OrderNos.getDid(32));
            order.setAmount(dto.getAmount());
            order.setUserId(user.getId());
            order.setPhone(user.getPhone());
            order.setOpenId(dto.getOpenId());
            order.setUserName(user.getResidentName());
            save(order);
            TOrderBill tOrderBill = new TOrderBill();
            tOrderBill.setBillId(bill.getId());
            tOrderBill.setOrderNo(order.getId());
            tOrderBillService.save(tOrderBill);
            resp.setAmount(dto.getAmount());
            resp.setOrderNo(order.getId());
            resp.setAppletUrl(createAppletUrl(order.getId()));
            return resp;
        }
        List<TOrderBill> orderBills = new ArrayList<>();
        String orderNo = OrderNos.getDid(32);
        List<TBill> bills = dto.getBillIds().stream().map(id -> {
            TBill bill = billService.getById(id);
            if (bill == null) {
                throw new ServiceException("billId:" + id + "不存在");
            }
            orderBills.add(new TOrderBill(orderNo,bill.getId()));
            return bill;
        }).collect(Collectors.toList());
        //欠费金额转成单位分比较
        long sumRent = bills.stream().mapToLong((bill)
                -> bill.getOutstandingMoney().multiply(AmountConstant.b100)
                .setScale(2,RoundingMode.HALF_DOWN).longValue())
                .sum();
        if (sumRent==0){
            throw new ServiceException("账单已缴费");
        }
        if (dto.getAmount()>sumRent){
            throw new ServiceException("支付金额超过所选账单欠费金额");
        }
        TPayOrder order = new TPayOrder();
        order.setId(orderNo);
        order.setAmount(dto.getAmount());
        order.setUserId(user.getId());
        order.setPhone(user.getPhone());
        order.setOpenId(dto.getOpenId());
        order.setUserName(user.getResidentName());
        save(order);
        tOrderBillService.saveBatch(orderBills);
        resp.setAmount(dto.getAmount());
        resp.setOrderNo(orderNo);
        resp.setAppletUrl(createAppletUrl(orderNo));
        return resp;
    }
}