huliguo
2025-04-17 19df67e19f23cd2a04d1c7f355e1e656f4140af4
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/OrderServiceImpl.java
@@ -57,12 +57,12 @@
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.*;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -145,14 +145,22 @@
        OrderGood orderGood = orderGoodMapper.selectOne(new LambdaQueryWrapper<OrderGood>().eq(OrderGood::getOrderId, orderId));
        String goodJson = orderGood.getGoodJson();
        Goods goods = JSONObject.parseObject(goodJson, Goods.class);
        GoodsSeckill goodsSeckill = JSON.parseObject(orderGood.getSeckillJson(), GoodsSeckill.class);
        OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
        orderGoodsVO.setGoodsId(goods.getId());
        orderGoodsVO.setGoodsName(goods.getName());
        orderGoodsVO.setGoodsPic(goods.getHomePagePicture());
        orderGoodsVO.setNum(order.getNum());
        orderGoodsVO.setSellingPrice(goods.getSellingPrice());
        orderGoodsVO.setIntegral(goods.getIntegral());
        if (null!=goodsSeckill){
            orderGoodsVO.setSellingPrice(goodsSeckill.getSellingPrice());
            orderGoodsVO.setIntegral(goodsSeckill.getIntegral());
        }else {
            orderGoodsVO.setSellingPrice(goods.getSellingPrice());
            orderGoodsVO.setIntegral(goods.getIntegral());
        }
        orderGoodsVO.setOriginalPrice(goods.getOriginalPrice());
        OrderDetailVO orderDetailVO = new OrderDetailVO();
@@ -180,8 +188,12 @@
            e.printStackTrace();
            throw new ServiceException("生成核销码失败");
        }
        if(3!=order.getOrderStatus()){
            //不属于未使用的,应该有个核销\取消时间
            orderDetailVO.setEndTime(order.getEndTime());
        }
        //该商品是否被用户评论
        Long evaluateId = goodsEvaluateClient.getEvaluateIdByUserIdAndGoodId(goods.getId(), order.getAppUserId()).getData();
        Long evaluateId = goodsEvaluateClient.getEvaluateIdByOrderId(order.getId()).getData();
        orderDetailVO.setEvaluateId(evaluateId);
        return orderDetailVO;
@@ -207,6 +219,9 @@
        }
        if (!order.getShopId().equals(shopId)) {
            throw new ServiceException("该订单与当前扫码门店不一致");
        }
        if (order.getOrderStatus()!=3){
            throw new ServiceException("订单已被核销过");
        }
@@ -396,9 +411,20 @@
     */
    public R refundPayMoney(Order order) {
        //开始退款
        //先回退积分
        BigDecimal paymentAmount = order.getPaymentAmount();
        if (BigDecimal.ZERO.compareTo(order.getPaymentAmount()) < 0) {//支付的金额是否大于0
            //微信退款
            RefundResult refund = PaymentUtil.refund(order.getOrderNumber(), "R" + order.getOrderNumber(), paymentAmount.doubleValue(), "/order/order/refundPayMoneyCallback");
            if (!"100".equals(refund.getRa_Status())) {
                return R.fail(refund.getRc_CodeMsg());//退款失败
            }
        }
        //退款成功再回退积分
        AppUser appUser = appUserClient.getAppUserById(order.getAppUserId());
        if (order.getPoint()>0) {
            if(null==appUser.getCancelPoint()){
                appUser.setCancelPoint(0);
            }
            //返回订单抵扣积分
            Integer historicalPoint = appUser.getAvailablePoint();
            Integer availablePoint = appUser.getAvailablePoint() + order.getPoint();//可用积分
@@ -406,6 +432,7 @@
            appUser.setAvailablePoint(availablePoint);
            appUser.setCancelPoint(cancelPoint);
            appUser.setTotalPoint(appUser.getTotalPoint() + order.getPoint());
            appUserClient.editAppUserById(appUser);
            //构建积分流水
            UserPoint userPoint = new UserPoint();
@@ -419,15 +446,6 @@
            userPointClient.saveUserPoint(userPoint);
        }
        BigDecimal paymentAmount = order.getPaymentAmount();
        if (BigDecimal.ZERO.compareTo(order.getPaymentAmount()) < 0) {//支付的金额是否大于0
            //微信退款
            RefundResult refund = PaymentUtil.refund(order.getOrderNumber(), "R" + order.getOrderNumber(), paymentAmount.doubleValue(), "/order/order/refundPayMoneyCallback");
            if (!"100".equals(refund.getRa_Status())) {
                return R.fail(refund.getRc_CodeMsg());//退款失败
            }
        }
        order.setRefundStatus(2);
        order.setRefundTime(LocalDateTime.now());
@@ -436,13 +454,20 @@
        goodsClient.editGoodsNum(orderGood.getGoodsId(), -1);
        //获取商品json
        Goods good = JSON.parseObject(orderGood.getGoodJson(), Goods.class);
        GoodsSeckill goodsSeckill = JSON.parseObject(orderGood.getSeckillJson(), GoodsSeckill.class);
        //门店减少冻结资金 即减少余额, 冻结资金=余额-可用资金
        Shop shop = shopClient.getShopById(order.getShopId()).getData();
        BigDecimal historicalBalance=shop.getBalance();//历史余额
        BigDecimal variableAmount=good.getSellingPrice();//变动金额
        BigDecimal balance=shop.getBalance().subtract(good.getSellingPrice());//变动后余额
        BigDecimal variableAmount=BigDecimal.ZERO;//变动金额
        if (null != goodsSeckill) {
            variableAmount=goodsSeckill.getSellingPrice();
        }else {
            variableAmount=good.getSellingPrice();
        }
        BigDecimal balance=shop.getBalance().subtract(variableAmount);//变动后余额
        shop.setBalance(balance);
        shopClient.updateShop(shop);
@@ -658,21 +683,27 @@
            confirmOrderVo.setCash(goodsSeckill.getSellingPrice());//秒杀活动价格
            confirmOrderVo.setPoint(goodsSeckill.getIntegral());//秒杀活动积分价格
            confirmOrderVo.setPurchaseLimitNum(activityInfo.getMaxNum());//限购数量
            confirmOrderVo.setEndTimeStamp(activityInfo.getEndTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());//结束时间戳
        }
        //判断是否是积分支付
        if (type == 1) {//现金
            confirmOrderVo.setOrderMoney(confirmOrderVo.getCash());
            Integer availablePoint = appUser.getAvailablePoint();//用户可用积分
            BigDecimal maxPointDeductionAmount = getCashByPoint(availablePoint);//最大可抵扣金额
            //计算积分抵扣的金额  将积分转为金额,去掉小数
            BigDecimal deduction=getCashByPoint(confirmOrderVo.getResidualPoint());
            //实际抵扣金额
            BigDecimal deduction= maxPointDeductionAmount.min(confirmOrderVo.getCash());
            confirmOrderVo.setDeduction(deduction);
        }else {//积分
            confirmOrderVo.setOrderPoint(good.getIntegral());
            confirmOrderVo.setOrderPoint(confirmOrderVo.getPoint());
        }
        //限购检查
        //判断当前数量是否已经超出限购数量(需要计算已经购买的数量)
        if(null == good.getPurchaseLimit() || -1 == good.getPurchaseLimit()){
        if(null == confirmOrderVo.getPurchaseLimitNum() || -1 == confirmOrderVo.getPurchaseLimitNum()){
            confirmOrderVo.setIsPurchaseLimit(false);
            confirmOrderVo.setPurchaseLimitNum(-1);
        }else{
@@ -687,7 +718,7 @@
                sum = orderGoodList.stream().mapToInt(OrderGood::getNum).sum();
            }
            confirmOrderVo.setIsPurchaseLimit((1 + sum) > good.getPurchaseLimit());
            confirmOrderVo.setPurchaseLimitNum(good.getPurchaseLimit() - sum);
            confirmOrderVo.setPurchaseLimitNum(confirmOrderVo.getPurchaseLimitNum() - sum);
        }
        return confirmOrderVo;
@@ -703,6 +734,7 @@
        int type=1;//商品类型  1=普通商品,2=秒杀商品
        //商品信息
        Goods goods = goodsClient.getGoodsById(orderPayment.getGoodId()).getData();
        String goodsJson= JSON.toJSONString(goods);
        if (null == goods || 1==goods.getDelFlag()){
            return  R.fail( "商品不存在");
        }
@@ -796,7 +828,8 @@
        order.setDelFlag(0);
        order.setCreateTime(LocalDateTime.now());
        orderMapper.insert(order);
        this.save(order);
        //构建订单明细数据
        OrderGood orderGood = new OrderGood();
@@ -807,7 +840,7 @@
        if (2==type){
            orderGood.setSeckillJson(JSON.toJSONString(goodsSeckill));
        }
        orderGood.setGoodJson(JSON.toJSONString(goods));
        orderGood.setGoodJson(goodsJson);
        orderGood.setDelFlag(0);
        orderGood.setCreateTime(LocalDateTime.now());
        orderGood.setCashPayment(orderPayment.getPaymentType()==1 ? 1 : 0);
@@ -822,6 +855,13 @@
        }
        //判断积分是否为零,积分支付
        if (0 != order.getPoint()){
            if (null==appUser.getAvailablePoint()){
                appUser.setAvailablePoint(0);
            }
            if (null==appUser.getExchangePoint()){
                appUser.setExchangePoint(0);
            }
            //积分支付
            Integer historicalPoint = appUser.getAvailablePoint();//历史积分
            Integer availablePoint = historicalPoint - orderPoint;//可用积分
@@ -829,6 +869,7 @@
            //扣减订单支付积分
            appUser.setAvailablePoint(availablePoint);
            appUser.setExchangePoint(exchangePoint );
            appUser.setTotalPoint(appUser.getTotalPoint() + order.getPoint());//总积分
            //构建积分流水记录
            UserPoint userPoint = new UserPoint();
            userPoint.setType(4);//兑换商品
@@ -873,6 +914,10 @@
        //门店增加冻结资金 即增加余额, 冻结资金=余额-可用资金
        Shop shop = shopClient.getShopById(order.getShopId()).getData();
        if (null==shop.getBalance()){
            shop.setBalance(BigDecimal.ZERO);
        }
        BigDecimal historicalBalance=shop.getBalance();//历史余额
        BigDecimal variableAmount=goods.getSellingPrice();//变动金额
        BigDecimal balance=shop.getBalance().add(goods.getSellingPrice());//变动后余额
@@ -927,11 +972,16 @@
        //商品销量增加
        goodsClient.editGoodsNum(orderGood.getGoodsId(), 1);
        Goods goods = JSON.parseObject(orderGood.getGoodJson(), Goods.class);
        GoodsSeckill goodsSeckill = JSON.parseObject(orderGood.getSeckillJson(), GoodsSeckill.class);
        //门店增加冻结资金 即增加金额, 冻结资金=余额-可用资金
        Shop shop = shopClient.getShopById(order.getShopId()).getData();
        BigDecimal historicalBalance=shop.getBalance();//历史余额
        BigDecimal variableAmount=goods.getSellingPrice();//变动余额
        BigDecimal variableAmount=BigDecimal.ZERO;//变动金额
        if (null != goodsSeckill) {
            variableAmount=goodsSeckill.getSellingPrice();
        }else {
            variableAmount=goods.getSellingPrice();
        }
        BigDecimal balance=shop.getBalance().add(variableAmount);//变动后余额
        shop.setBalance(balance);
@@ -989,6 +1039,7 @@
                    Integer cancelPoint = appUser.getCancelPoint() + order.getPoint();//取消订单积分
                    appUser.setAvailablePoint(availablePoint);
                    appUser.setCancelPoint(cancelPoint);
                    appUser.setTotalPoint(appUser.getTotalPoint() + order.getPoint());
                    //构建积分流水记录
                    UserPoint userPoint = new UserPoint();
@@ -1019,8 +1070,11 @@
            queryWrapper.eq("shop_id", shopId);
        }
        // 添加订单状态条件
        if (status != null) {
            queryWrapper.eq("order_status", status);
        if (status != null&&status==4) { //4-已完成 8-已评价
            queryWrapper.in("order_status",Arrays.asList(4,8));
        }
        if (status != null&&status!=4) {//3-待核销 5-已取消
            queryWrapper.eq("order_status",status);
        }
        // 模糊查询条件
        if (StringUtils.isNotBlank(content)) {
@@ -1050,6 +1104,7 @@
            return R.fail("订单取消失败");
        }
        order.setOrderStatus(5);
        order.setEndTime(LocalDateTime.now());
        R r = refundPayMoney(order);
        if (200 == r.getCode()) {
            this.updateById(order);
@@ -1057,16 +1112,50 @@
        return r;
    }
    /**
     * 后台-工作台-折线图
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<OrderStatisticsDetail> getOrderListGroupByDate(LocalDate startTime, LocalDate endTime) {
        // 查询数据库获取原始数据
        List<OrderStatisticsDetail> rawData = orderMapper.getOrderListGroupByDate(
                startTime.atTime(0,0, 0),
                endTime.atTime(23,59,59)
        );
        // 创建完整日期范围的映射
        Map<LocalDate, OrderStatisticsDetail> dateMap = rawData.stream()
                .collect(Collectors.toMap(
                        OrderStatisticsDetail::getDateTime,
                        Function.identity()
                ));
        // 生成完整日期序列并填充缺失数据
        List<OrderStatisticsDetail> completeList = new ArrayList<>();
        for (LocalDate date = startTime; !date.isAfter(endTime); date = date.plusDays(1)) {
            if (dateMap.containsKey(date)) {
                completeList.add(dateMap.get(date));
            } else {
                completeList.add(new OrderStatisticsDetail(date, 0, BigDecimal.ZERO));
            }
        }
        return completeList;
    }
    private OrderPageListVo convertToOrderListVo(Order order) {
        OrderPageListVo vo = new OrderPageListVo();
        // 复制属性
        BeanUtils.copyProperties(order, vo);
        // 查询用户信息
        AppUser user = appUserClient.getAppUserById(order.getAppUserId());
        if (user != null) {
            vo.setPhone(user.getPhone());
        }
        vo.setIdStr(order.getId().toString());
        return vo;
    }