“mitao”
2025-12-16 f894e386d81e43831ea5fda78e30a029eca6fda5
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.ruoyi.system.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.config.ICBCConfig;
import com.ruoyi.system.dto.BigGiftPlaceOrderDTO;
import com.ruoyi.system.mapper.TBigGiftPackageMapper;
import com.ruoyi.system.mapper.TBigGiftPackageOrderMapper;
import com.ruoyi.system.mapper.TCompanyMapper;
import com.ruoyi.system.model.TBigGiftPackage;
import com.ruoyi.system.model.TBigGiftPackageOrder;
import com.ruoyi.system.model.TCompany;
import com.ruoyi.system.query.BigGiftPackageOrderQuery;
import com.ruoyi.system.service.TBigGiftPackageOrderService;
import com.ruoyi.system.utils.ICBCPayUtil;
import com.ruoyi.system.utils.OrderCodeGenerator;
import com.ruoyi.system.vo.PlaceOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 大礼包订单 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-12-08
 */
@Slf4j
@Service
public class TBigGiftPackageOrderServiceImpl extends ServiceImpl<TBigGiftPackageOrderMapper, TBigGiftPackageOrder> implements TBigGiftPackageOrderService {
 
    @Resource
    private TCompanyMapper companyMapper;
 
    @Resource
    private TBigGiftPackageMapper bigGiftPackageMapper;
 
    @Autowired
    private ICBCPayUtil icbcPayUtil;
 
    @Autowired
    private ICBCConfig icbcConfig;
 
    @Override
    public List<TBigGiftPackageOrder> pageList(Page<TBigGiftPackageOrder> tBigGiftPackageOrderPage, BigGiftPackageOrderQuery query) {
        String sTime = null;
        String eTime = null;
        if(query.getTime()!=null && !query.getTime().isEmpty()){
 
            String[] split = query.getTime().split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        List<TBigGiftPackageOrder> tBigGiftPackageOrders = this.baseMapper.pageList(tBigGiftPackageOrderPage, query, sTime, eTime);
        if(!tBigGiftPackageOrders.isEmpty()){
            List<Integer> companyId = tBigGiftPackageOrders.stream().map(TBigGiftPackageOrder::getCompanyId).collect(Collectors.toList());
            List<TCompany> tCompanies = companyMapper.selectBatchIds(companyId);
            tBigGiftPackageOrders.forEach(tBigGiftPackageOrder -> {
                tBigGiftPackageOrder.setCompanyName(tCompanies.stream().filter(tCompany -> tCompany.getId().equals(tBigGiftPackageOrder.getCompanyId())).findFirst().get().getName());
            });
            List<Integer> goodsIds = tBigGiftPackageOrders.stream().map(TBigGiftPackageOrder::getBigGiftPackageId).collect(Collectors.toList());
            List<TBigGiftPackage> bigGiftPackages = bigGiftPackageMapper.selectBatchIds(goodsIds);
            tBigGiftPackageOrders.forEach(tBigGiftPackageOrder -> {
                tBigGiftPackageOrder.setBigGiftPackageName(bigGiftPackages.stream().filter(tBigGiftPackage -> tBigGiftPackage.getId().equals(tBigGiftPackageOrder.getBigGiftPackageId())).findFirst().get().getName());
            });
 
        }
        return tBigGiftPackageOrders ;
    }
 
    @Override
    public List<TBigGiftPackageOrder> export(BigGiftPackageOrderQuery query) {
        String sTime = null;
        String eTime = null;
        if(query.getTime()!=null && !query.getTime().isEmpty()){
            String[] split = query.getTime().split(" - ");
            sTime = split[0] + " 00:00:00";
            eTime = split[1] + " 23:59:59";
        }
        List<TBigGiftPackageOrder> tBigGiftPackageOrders = this.baseMapper.export(query, sTime, eTime);
        if(!tBigGiftPackageOrders.isEmpty()){
            List<Integer> companyId = tBigGiftPackageOrders.stream().map(TBigGiftPackageOrder::getCompanyId).collect(Collectors.toList());
            List<TCompany> tCompanies = companyMapper.selectBatchIds(companyId);
            tBigGiftPackageOrders.forEach(tBigGiftPackageOrder -> {
                tBigGiftPackageOrder.setCompanyName(tCompanies.stream().filter(tCompany -> tCompany.getId().equals(tBigGiftPackageOrder.getCompanyId())).findFirst().get().getName());
            });
            List<Integer> goodsIds = tBigGiftPackageOrders.stream().map(TBigGiftPackageOrder::getBigGiftPackageId).collect(Collectors.toList());
            List<TBigGiftPackage> bigGiftPackages = bigGiftPackageMapper.selectBatchIds(goodsIds);
            tBigGiftPackageOrders.forEach(tBigGiftPackageOrder -> {
                tBigGiftPackageOrder.setBigGiftPackageName(bigGiftPackages.stream().filter(tBigGiftPackage -> tBigGiftPackage.getId().equals(tBigGiftPackageOrder.getBigGiftPackageId())).findFirst().get().getName());
            });
        }
        return tBigGiftPackageOrders ;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PlaceOrderVO placeOrder(BigGiftPlaceOrderDTO dto, Integer appUserId, String openid) {
        // 1. 校验商品是否存在
        TBigGiftPackage bigGiftPackage = bigGiftPackageMapper.selectById(dto.getBigGiftPackageId());
        if (bigGiftPackage == null) {
            throw new ServiceException("商品不存在");
        }
 
        // 2. 校验库存是否充足
        if (bigGiftPackage.getInventory() == null || bigGiftPackage.getInventory() < dto.getQuantity()) {
            throw new ServiceException("商品库存不足");
        }
 
        // 3. 校验支付方式
        if (dto.getPaymentType() != 1 && dto.getPaymentType() != 2) {
            throw new ServiceException("支付方式不正确");
        }
 
        // 4. 使用乐观锁扣减库存(防止超卖)
        int updatedRows = bigGiftPackageMapper.deductInventory(dto.getBigGiftPackageId(), dto.getQuantity());
        if (updatedRows == 0) {
            throw new ServiceException("商品库存不足,请稍后重试");
        }
 
        // 5. 计算支付金额
        BigDecimal paymentAmount;
        if (dto.getPaymentType() == 1) {
            // 订金+尾款模式,先支付订金
            paymentAmount = bigGiftPackage.getDeposit().multiply(new BigDecimal(dto.getQuantity()));
        } else {
            // 全款模式
            paymentAmount = bigGiftPackage.getTotalPrice().multiply(new BigDecimal(dto.getQuantity()));
        }
 
        // 6. 先生成支付订单号(所有订单共用同一个支付订单号,便于支付回调时查询)
        String payOrderCode = OrderCodeGenerator.generateBigGiftPayOrderCode();
 
        // 7. 批量创建订单
        List<TBigGiftPackageOrder> orderList = new ArrayList<>();
        List<String> orderCodes = new ArrayList<>();
        String goodsJson = JSON.toJSONString(bigGiftPackage);
        LocalDateTime now = LocalDateTime.now();
 
        for (int i = 0; i < dto.getQuantity(); i++) {
            TBigGiftPackageOrder order = new TBigGiftPackageOrder();
            String orderCode = OrderCodeGenerator.generateBigGiftOrderCode();
            order.setCode(orderCode);
            order.setPayOrderCode(payOrderCode);
            order.setAppUserId(appUserId);
            order.setCreateTime(now);
            order.setBigGiftPackageId(dto.getBigGiftPackageId());
            order.setBigGiftPackage(goodsJson);
            order.setOrderPrice(bigGiftPackage.getTotalPrice());
            order.setDeposit(bigGiftPackage.getDeposit());
            order.setFinalPayment(bigGiftPackage.getTotalPrice().subtract(bigGiftPackage.getDeposit()));
            order.setPaymentType(dto.getPaymentType());
            order.setEffectDayFlag(dto.getEffectDayFlag());
            // 订单状态:1=待付款
            order.setStatus(1);
            order.setDelFlag(0);
 
            orderList.add(order);
            orderCodes.add(orderCode);
        }
 
        // 批量插入订单
        this.saveBatch(orderList);
 
        // 获取订单ID列表
        List<Integer> orderIds = orderList.stream()
                .map(TBigGiftPackageOrder::getId)
                .collect(Collectors.toList());
 
        // 8. 调用工行支付
        log.info("大礼包下单,支付订单号:{},业务订单号:{},支付金额:{},用户ID:{}", payOrderCode, orderCodes, paymentAmount, appUserId);
        JSONObject paymentParams = icbcPayUtil.wechatPay(payOrderCode, paymentAmount, bigGiftPackage.getName(), openid,
                icbcConfig.getNotifyUrl() + "/big-gift/pay-notify");
 
        // 9. 构建返回结果
        PlaceOrderVO vo = new PlaceOrderVO();
        vo.setOrderIds(orderIds);
        vo.setOrderCodes(orderCodes);
        vo.setPaymentParams(paymentParams);
        return vo;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PlaceOrderVO payFinalPayment(Integer orderId, Integer appUserId, String openid) {
        // 1. 查询订单
        TBigGiftPackageOrder order = this.getById(orderId);
        if (order == null) {
            throw new ServiceException("订单不存在");
        }
 
        // 2. 校验订单归属
        if (!order.getAppUserId().equals(appUserId)) {
            throw new ServiceException("无权操作此订单");
        }
 
        // 3. 校验支付方式(必须是订金+尾款模式)
        if (order.getPaymentType() != 1) {
            throw new ServiceException("该订单不支持尾款支付");
        }
 
        // 4. 校验订单状态(必须是已付订金状态)
        if (order.getStatus() != 2) {
            throw new ServiceException("订单状态不正确,无法支付尾款");
        }
 
        // 5. 获取尾款金额
        BigDecimal finalPayment = order.getFinalPayment();
        if (finalPayment == null || finalPayment.compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("尾款金额异常");
        }
 
        // 6. 生成尾款支付订单号
        String finalPayOrderCode = OrderCodeGenerator.generateBigGiftFinalPayOrderCode();
 
        // 7. 调用工行支付
        log.info("大礼包尾款支付,订单ID:{},支付订单号:{},尾款金额:{},用户ID:{}", orderId, finalPayOrderCode, finalPayment, appUserId);
        JSONObject paymentParams = icbcPayUtil.wechatPay(finalPayOrderCode, finalPayment, "大礼包尾款", openid,
                icbcConfig.getNotifyUrl() + "/big-gift/final-pay-notify");
 
        // 8. 构建返回结果
        PlaceOrderVO vo = new PlaceOrderVO();
        vo.setOrderIds(Collections.singletonList(orderId));
        vo.setOrderCodes(Collections.singletonList(order.getCode()));
        vo.setPaymentParams(paymentParams);
        return vo;
    }
}