无关风月
2025-08-05 2134c13b7b2815983281fdc819e3138ffde1bc48
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java
@@ -9,6 +9,7 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaidi100.sdk.request.samecity.OrderGoods;
import com.ruoyi.account.api.feignClient.*;
import com.ruoyi.account.api.model.*;
@@ -25,6 +26,7 @@
import com.ruoyi.order.mapper.OrderGoodMapper;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.order.model.ChargeOrder;
import com.ruoyi.order.model.Order;
import com.ruoyi.order.model.OrderGood;
import com.ruoyi.order.service.OrderGoodService;
@@ -48,18 +50,25 @@
import com.ruoyi.system.api.feignClient.SysUserClient;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
@@ -415,8 +424,6 @@
    }
    /**
     * 取消订单操作
     *
@@ -624,8 +631,6 @@
    }
    
    /**
     * 收货操作
     *
@@ -695,7 +700,6 @@
            orderInfo.setSeckillJson(orderGood.getSeckillJson());
            orderInfo.setActivityName("秒杀活动");
        }
        orderInfo.setPoint(order.getPoint());
@@ -899,7 +903,8 @@
        if(3 != orderPayment.getPaymentType()){
            //现金支付
            orderMoney = goods.getSellingPrice();
            paymentMoney=orderMoney.setScale(2, RoundingMode.HALF_EVEN);;
            paymentMoney = orderMoney.setScale(2, RoundingMode.HALF_EVEN);
            ;
            //是否使用积分抵扣
            if (orderPayment.getIsPointDeduction() == 1) {
@@ -1284,6 +1289,7 @@
    /**
     * 后台-工作台-折线图
     *
     * @param startTime
     * @param endTime
     * @return
@@ -1316,6 +1322,237 @@
        return completeList;
    }
    @Override
    public R importExpress(MultipartFile file) {
        List<Shop> shops = shopClient.getAllShop().getData();
        List<Goods> goods = goodsClient.getAllGoods().getData();
        List<Order> orderList = new ArrayList<>();
        List<String> orderNumberList = new ArrayList<>();
        List<OrderGood> orderGoodsList = new ArrayList<>();
        cn.hutool.json.JSONObject result = new cn.hutool.json.JSONObject();
        try (InputStream inputStream = file.getInputStream();
             Workbook workbook = new XSSFWorkbook(inputStream)) {
//            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 1; i <= lastRowNum; i++) {//第二行开始
                Row row = sheet.getRow(i);
                // 订单状态
                if (row.getCell(0) == null) {
                    throw new ServiceException("第" + i + "行订单状态为空", 500);
                }
                row.getCell(0).setCellType(CellType.STRING);
                String orderStatus = row.getCell(0).getStringCellValue();
                // 订单编号
                if (row.getCell(1) == null) {
                    throw new ServiceException("第" + i + "行订单编号为空", 500);
                }
                row.getCell(1).setCellType(CellType.STRING);
                String orderNum = row.getCell(1).getStringCellValue();
                // 订单类型
                if (row.getCell(2) == null) {
                    throw new ServiceException("第" + i + "行订单类型为空", 500);
                }
                row.getCell(2).setCellType(CellType.STRING);
                String orderType = row.getCell(2).getStringCellValue();
                // 下单时间
                Cell beginTimeCell = row.getCell(3);
                if (beginTimeCell == null) {
                    throw new ServiceException("第" + (i + 1) + "行下单时间为空", 500);
                }
                LocalDateTime beginTime;
                if (beginTimeCell.getCellType() == CellType.NUMERIC || DateUtil.isCellDateFormatted(beginTimeCell)) {
                    // 处理数值型日期(包括Excel内置日期格式)
                    beginTime = beginTimeCell.getDateCellValue().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime();
                } else {
                    // 处理文本型日期(如 "2025/4/3 23:25:38")
                    String dateStr = beginTimeCell.getStringCellValue().trim();
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d H:mm:ss");
                    beginTime = LocalDateTime.parse(dateStr, formatter);
                }
                // 下单用户
                if (row.getCell(4) == null) {
                    throw new ServiceException("第" + i + "行下单用户为空", 500);
                }
                row.getCell(4).setCellType(CellType.STRING);
                String userName = row.getCell(4).getStringCellValue();
                // 联系电话
                if (row.getCell(5) == null) {
                    throw new ServiceException("第" + i + "行联系方式为空", 500);
                }
                row.getCell(5).setCellType(CellType.STRING);
                String phone = row.getCell(5).getStringCellValue();
                // 商品名称
                if (row.getCell(6) == null) {
                    throw new ServiceException("第" + i + "行商品名称为空", 500);
                }
                row.getCell(6).setCellType(CellType.STRING);
                String goodsName = row.getCell(6).getStringCellValue();
                // 店铺名称
                if (row.getCell(7) == null) {
                    throw new ServiceException("第" + i + "行店铺名称为空", 500);
                }
                row.getCell(7).setCellType(CellType.STRING);
                String shopName = row.getCell(7).getStringCellValue();
                // 商品售价
                if (row.getCell(8) == null) {
                    throw new ServiceException("第" + i + "行商品售价为空", 500);
                }
                row.getCell(8).setCellType(CellType.STRING);
                String sellingPrice = row.getCell(8).getStringCellValue();
                // 支付方式
                if (row.getCell(9) == null) {
                    throw new ServiceException("第" + i + "行支付方式为空", 500);
                }
                row.getCell(9).setCellType(CellType.STRING);
                String payType = row.getCell(9).getStringCellValue();
                // 支付积分
                String pointPayment;
                if (row.getCell(10) == null) {
                    pointPayment="0";
                }else{
                    row.getCell(10).setCellType(CellType.STRING);
                    pointPayment = row.getCell(10).getStringCellValue();
                }
                // 支付金额
                String cashPayment;
                if (row.getCell(11) == null) {
                    cashPayment = "0";
                }else{
                    row.getCell(11).setCellType(CellType.STRING);
                    cashPayment = row.getCell(11).getStringCellValue();
                }
                //数据库中是否存在
                Order order = this.getOne(new LambdaQueryWrapper<Order>()
                        .eq(Order::getOrderNumber, orderNum)
                );
                if (order != null) {
//                    throw new ServiceException("数据库中订单已存在:" + orderNum, 500);
                    result.append("订单号:[", orderNum + "]数据库中订单已存在");
                    continue;
                }
                AppUser appuser = appUserClient.getAppUserByPhone1(phone).getData();
                if (null == appuser) {
                    //创建用户
                    appuser = new AppUser();
                    appuser.setPhone(phone);
                    appuser.setStatus(1);
                    appuser.setAvatar(DEFAULT_AVATAR_URL);
                    appuser.setCreateTime(LocalDateTime.now());
                    appuser.setDelFlag(false);
                    appuser.setName(appuser.getPhone().substring(0, 3) + "****" + appuser.getPhone().substring(7));
                    appuser.setRechargePoint(0);
                    appuser.setAvailablePoint(0);
                    appuser.setTotalPoint(0);
                    Long userId = appUserClient.saveOrUpdateAppUser(appuser);
                    appuser.setId(userId);
                }
                if (!orderNumberList.contains(orderNum)){
                    orderNumberList.add(orderNum);
                    Order order1 = new Order();
                    order1.setAppUserId(appuser.getId());
                    switch (orderStatus){
                        case "待使用":
                            order1.setOrderStatus(3);
                            order1.setOldOrderStatus(3);
                            break;
                        case "已完成":
                            order1.setOrderStatus(4);
                            order1.setOldOrderStatus(4);
                            break;
                        case "已取消":
                            order1.setOrderStatus(5);
                            order1.setOldOrderStatus(5);
                            break;
                        case "已评价":
                            order1.setOrderStatus(8);
                            order1.setOldOrderStatus(8);
                            break;
                    }
                    order1.setOrderNumber(orderNum);
                    order1.setPayStatus(2);
                    Shop shop = shops.stream().filter(e -> e.getName().equals(shopName)).findFirst().orElse(null);
                    if (shop!=null){
                        order1.setShopId(shop.getId());
                    }
                    order1.setDelFlag(0);
                    order1.setCreateTime(LocalDateTime.now());
                    orderList.add(order1);//添加到订单集合中
                }
                OrderGood orderGood = new OrderGood();
                Shop shop = shops.stream().filter(e -> e.getName().equals(shopName)).findFirst().orElse(null);
                if (shop!=null){
                    Goods goods1 = goods.stream().filter(e -> e.getName().equals(goodsName)
                    && e.getShopId().equals(shop.getId())).findFirst().orElse(null);
                    if(goods1!=null){
                        orderGood.setGoodsId(goods1.getId());
                        orderGood.setSellingPrice(goods1.getSellingPrice());
                        orderGood.setIntegral(goods1.getIntegral());
                    }
                }
                orderGood.setDelFlag(0);
                orderGood.setCreateTime(LocalDateTime.now());
                orderGood.setCashPayment(cashPayment.equals("0")?0:1);
                orderGood.setPointPayment(pointPayment.equals("0")?0:1);
                orderGood.setCashPaymentValue(new BigDecimal(cashPayment));
                orderGood.setPointPaymentValue(Integer.parseInt(pointPayment));
                orderGood.setOrderNumber(orderNum);
                orderGoodsList.add(orderGood);
            }
            // 新增数据
            for (Order order : orderList) {
                List<OrderGood> orderGoods = orderGoodsList.stream().filter(e -> e.getOrderNumber().equals(order.getOrderNumber())).collect(Collectors.toList());
                BigDecimal totalAmount = orderGoods.stream().map(OrderGood::getCashPaymentValue).reduce(BigDecimal::add).get();
                Integer totalIntegral = orderGoods.stream().map(OrderGood::getIntegral).reduce(Integer::sum).get();
                order.setNum(orderGoods.size());
                order.setTotalAmount(totalAmount);
                order.setPaymentAmount(totalAmount);
                order.setPoint(totalIntegral);
                if (totalAmount.compareTo(BigDecimal.ZERO)!=0&&totalIntegral!=0){
                    order.setPayMethod(4);
                }else if (totalAmount.compareTo(BigDecimal.ZERO)!=0){
                    order.setPayMethod(1);
                }else if (totalIntegral!=0){
                    order.setPayMethod(3);
                }
                this.save(order);
                for (OrderGood orderGood : orderGoods) {
                    orderGood.setOrderId(order.getId());
                }
                orderGoodService.saveBatch(orderGoods);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException(e.getMessage());
        }
        if (!result.isEmpty()) {
            return R.ok(result);
        }
        return R.ok();
    }
    private static final String DEFAULT_AVATAR_URL = "http://qijishenghuiyuan.obs.cn-southwest-2.myhuaweicloud.com/admin/58084a0ce8c34dc79f9d8b4c0bc3774a.png";
    private OrderPageListVo convertToOrderListVo(Order order) {
        OrderPageListVo vo = new OrderPageListVo();