xin
2025-07-04 1eee71ca750c806ee0e6d80a4fd44500ceff2ffa
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
package com.oying.modules.sh.service.impl;
 
import com.oying.exception.BadRequestException;
import com.oying.modules.pc.product.domain.Product;
import com.oying.modules.pc.product.domain.enums.ProductStatusEnum;
import com.oying.modules.pc.product.service.ProductService;
import com.oying.modules.sh.domain.Order;
import com.oying.modules.sh.domain.request.GeneratorOrder;
import com.oying.modules.sh.domain.request.ProductOrder;
import com.oying.modules.sh.domain.request.SubmitOrder;
import com.oying.modules.sh.domain.vo.OrderInfo;
import com.oying.modules.sh.domain.vo.ProductInfo;
import com.oying.modules.sh.service.UserAddressService;
import com.oying.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oying.modules.sh.service.OrderService;
import com.oying.modules.sh.domain.dto.OrderQueryCriteria;
import com.oying.modules.sh.mapper.OrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.oying.utils.PageUtil;
 
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
 
import com.oying.utils.PageResult;
 
/**
 * @author lixin
 * @description 服务实现
 * @date 2025-06-11
 **/
@Service
@RequiredArgsConstructor
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
 
    private final OrderMapper orderMapper;
    private final UserAddressService userAddressService;
    private final ProductService productService;
 
    @Override
    public PageResult<Order> queryAll(OrderQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(orderMapper.findAll(criteria, page));
    }
 
    @Override
    public List<Order> queryAll(OrderQueryCriteria criteria) {
        return orderMapper.findAll(criteria);
    }
 
    @Override
    public Order submitOrder(SubmitOrder submit) {
        return null;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderInfo generatorOrder(GeneratorOrder criteria) {
        List<ProductInfo> products = new ArrayList<>();
        BigDecimal amount = BigDecimal.ZERO;
        for (ProductOrder productOrder : criteria.getProducts()) {
            Product product = productService.getById(productOrder.getProductId());
            if (product == null) {
                throw new BadRequestException("商品不存在");
            }
            if (product.getStoreId().equals(criteria.getStoreId())) {
                throw new BadRequestException("不支持跨门店下单");
            }
            if (!product.getStatus().equals(ProductStatusEnum.AVAILABLE.getValue())) {
                ProductStatusEnum statusEnum = ProductStatusEnum.get(product.getStatus());
                throw new BadRequestException(product.getName() + ":" + (statusEnum != null ? statusEnum.getReasonPhrase() : "状态"));
            }
            if (productOrder.getProductCount() > product.getStockQuantity()) {
                throw new BadRequestException("商品库存不足");
            }
            if (productOrder.getProductCount() < product.getMinPurchaseQuantity()) {
                throw new BadRequestException("起售数量不足");
            }
            ProductInfo info = new ProductInfo();
            info.setProduct(product);
            info.setProductLabels(product.getLabels());
            info.setCount(productOrder.getProductCount());
            products.add(info);
            amount = amount.add(product.getPrice().multiply(BigDecimal.valueOf(productOrder.getProductCount())));
        }
        OrderInfo info = new OrderInfo();
        info.setProducts(products);
        info.setAmount(amount);
        info.setPayAmount(amount);
        info.setPromotionAmount(BigDecimal.ZERO);
        info.setUserAddresses(userAddressService.queryUserAddress());
        return info;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Order resources) {
        orderMapper.insert(resources);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Order resources) {
        Order order = getById(resources.getOrderId());
        order.copy(resources);
        orderMapper.updateById(order);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        orderMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void download(List<Order> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Order order : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("订单号", order.getOrderNum());
            map.put("用户id", order.getUserId());
            map.put("门店ID", order.getStoreId());
            map.put("门店", order.getStoreName());
            map.put("描述", order.getOrderDescribe());
            map.put("原金额", order.getOriginalPrice());
            map.put("折扣价", order.getPaidPrice());
            map.put("实付金额", order.getActuallyPayPrice());
            map.put("支付状态", order.getPayState());
            map.put("消息", order.getPayMessage());
            map.put("支付类型", order.getPayType());
            map.put("支付时间", order.getPayTime());
            map.put("订单失效时间RFC3339", order.getExpireTime());
            map.put("openid", order.getOpenid());
            map.put("APPID", order.getAppId());
            map.put("创建人", order.getCreateBy());
            map.put("创建时间", order.getCreateTime());
            map.put("修改者", order.getUpdateBy());
            map.put("修改时间", order.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}