zhangmei
2025-02-08 2573f2327a2046ec8545e80e4e56d1ee6c55bfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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.getPayableFeesMoney().add(bill.getPayableFeesPenalty())
                .subtract(bill.getActualMoney())
                .multiply(AmountConstant.b100)
                .setScale(0, RoundingMode.HALF_DOWN)
                .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 = caculateRentFee(bill);
            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.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) -> caculateRentFee(bill)).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.setUserName(user.getResidentName());
        save(order);
        tOrderBillService.saveBatch(orderBills);
        resp.setAmount(dto.getAmount());
        resp.setOrderNo(orderNo);
        resp.setAppletUrl(createAppletUrl(orderNo));
        return resp;
    }
}