xuhy
2025-04-21 2e1039b373bf2ad12f44e5fda0574cfd053b08ae
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.ruoyi.system.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
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 com.taxi591.bankapi.BankProperties;
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.net.URLEncoder;
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){
        JSONObject param = new JSONObject();
        param.put("input1",orderNo);
        StringBuffer sb = new StringBuffer();
        sb.append(BankProperties.H5_BANK_URL)
                .append("?code=").append("JF-EPAY2025030510983")
                .append("&userInput=")
                .append(URLEncoder.encode(param.toJSONString()))
                .append("&showBill=0");
        return sb.toString();
    }
 
    @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;
    }
}