peng
2 天以前 fa27fd4a21e04352a97988c153b7709ebecc4e89
framework/src/main/java/cn/lili/modules/order/order/serviceimpl/OrderServiceImpl.java
@@ -10,27 +10,32 @@
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.lili.base.Result;
import cn.lili.common.enums.ClientTypeEnum;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.event.TransactionCommitSendMQEvent;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.properties.RocketmqCustomProperties;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.OperationalJudgment;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsCompleteMessage;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.lmk.domain.entity.CouponVirtual;
import cn.lili.modules.lmk.domain.vo.OrderCountVO;
import cn.lili.modules.lmk.enums.general.AdminRoleEnum;
import cn.lili.modules.member.entity.dos.Member;
import cn.lili.modules.lmk.enums.general.VirtualGoodsTypeEnum;
import cn.lili.modules.lmk.mapper.LmkOrderSelectMapper;
import cn.lili.modules.lmk.service.CouponVirtualService;
import cn.lili.modules.member.entity.dto.MemberAddressDTO;
import cn.lili.modules.member.entity.dto.UpdateTracesDTO;
import cn.lili.modules.member.mapper.MemberMapper;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.order.cart.entity.enums.DeliveryMethodEnum;
@@ -44,11 +49,12 @@
import cn.lili.modules.order.trade.entity.dos.OrderLog;
import cn.lili.modules.order.trade.service.OrderLogService;
import cn.lili.modules.payment.entity.enums.PaymentMethodEnum;
import cn.lili.modules.permission.entity.dos.AdminUser;
import cn.lili.modules.permission.service.AdminUserService;
import cn.lili.modules.permission.service.RoleService;
import cn.lili.modules.promotion.entity.dos.Coupon;
import cn.lili.modules.promotion.entity.dos.Pintuan;
import cn.lili.modules.promotion.service.CouponService;
import cn.lili.modules.promotion.service.PintuanService;
import cn.lili.modules.statistics.entity.dto.StatisticsQueryParam;
import cn.lili.modules.store.entity.dto.StoreDeliverGoodsAddressDTO;
import cn.lili.modules.store.service.StoreDetailService;
import cn.lili.modules.system.aspect.annotation.SystemLogPoint;
@@ -79,6 +85,7 @@
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
@@ -87,6 +94,7 @@
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
@@ -100,11 +108,13 @@
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import cn.lili.modules.permission.entity.dos.Role;
/**
 * 子订单业务层实现
 *
@@ -199,7 +209,25 @@
    @Resource
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private CouponService couponService;
    @Autowired
    private CouponVirtualService couponVirtualService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private GoodsSkuService goodsSkuService;
    @Autowired
    private LmkOrderSelectMapper lmkOrderSelectMapper;
    private final static  String LOCK_ORDER_NO_MQ="lock_order_no_mq:";
    private final static  String LOCK_EDIT_ORDER_ADDRESS="lock_edit_order_address:";
    private final static  String LOCK_COUPON_CARD="lock_coupon_card:";
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void intoDB(TradeDTO tradeDTO) {
@@ -235,7 +263,24 @@
//                        item.getPriceDetailDTO().setPlatFormCommissionPoint(Double.valueOf(goods.getCommission().toString()));
//                        e.setPriceDetailDTO(item.getPriceDetailDTO());
//                        orderItems.add(e);
                        orderItems.add(new OrderItem(sku, item, tradeDTO));
                        //特殊处理线上礼品卡问题
                        String skuId = sku.getGoodsSku().getId();
                        GoodsSku skuInfo = goodsSkuService.getById(skuId);
                        OrderItem orderItem = new OrderItem(sku, item, tradeDTO);
                        if (skuInfo != null) {
                            String goodsId = skuInfo.getGoodsId();
                            Goods goodsInfo = goodsService.getById(goodsId);
                            if (goodsInfo != null&& VirtualGoodsTypeEnum.COUPON.name().equals(goodsInfo.getVirtualGoodsType())) {
                                String couponId = skuInfo.getCouponId();
                                String couponName = skuInfo.getCouponName();
                                orderItem.setCouponId(couponId);
                                orderItem.setCouponName(couponName);
                            }//订单中有定制商品将订单改为定制商品
                            else if (CustomizeFlagEnum.CUSTOMIZE.name().equals(skuInfo.getCustomizeFlag())) {
                                order.setCustomizeFlag(CustomizeFlagEnum.CUSTOMIZE.name());
                            }
                        }
                        orderItems.add(orderItem);
                        currentOrderItems.add(new OrderItem(sku, item, tradeDTO));
                    }
            );
@@ -294,6 +339,23 @@
        queryWrapper.groupBy("o.id");
        queryWrapper.orderByDesc("o.id");
        IPage<OrderSimpleVO> page =  this.baseMapper.queryByParams(PageUtil.initPage(orderSearchParams), queryWrapper);
        if (needHide){
            if (!adminUserService.havePermissionRole(AdminRoleEnum.ORDER_INFO_PERMISSION)){
                for (OrderSimpleVO vo : page.getRecords()){
                    vo.setConsigneeName(CommonUtil.maskName(vo.getConsigneeName()));
                    vo.setConsigneeMobile(CommonUtil.maskMobile(vo.getConsigneeMobile()));
                }
            }
        }
        return page;
    }
    @Override
    public IPage<OrderSimpleVO> queryShareMineOrder(OrderSearchParams orderSearchParams,Boolean needHide) {
        QueryWrapper queryWrapper = orderSearchParams.queryWrapper();
        queryWrapper.groupBy("o.id");
        queryWrapper.orderByDesc("o.id");
        IPage<OrderSimpleVO> page =  this.baseMapper.queryByShareParams(PageUtil.initPage(orderSearchParams), queryWrapper);
        if (needHide){
            if (!adminUserService.havePermissionRole(AdminRoleEnum.ORDER_INFO_PERMISSION)){
                for (OrderSimpleVO vo : page.getRecords()){
@@ -472,6 +534,35 @@
            }
        }
    }
    @Override
    public void queryShareExportOrder(HttpServletResponse response, OrderSearchParams orderSearchParams) {
        List<OrderExportDTO> orderExportDTOS = this.baseMapper.queryShareExportOrder(orderSearchParams.queryWrapper());
        if (!adminUserService.havePermissionRole(AdminRoleEnum.ORDER_INFO_PERMISSION)){
            for (OrderExportDTO vo : orderExportDTOS){
                vo.setConsigneeName(CommonUtil.maskName(vo.getConsigneeName()));
                vo.setConsigneeMobile(CommonUtil.maskMobile(vo.getConsigneeMobile()));
            }
        }
        XSSFWorkbook workbook = initOrderExportData(orderExportDTOS);
        try {
            // 设置响应头
            String fileName = URLEncoder.encode("订单列表", "UTF-8");
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public OrderDetailVO queryDetail(String orderSn) {
@@ -487,6 +578,18 @@
        Receipt receipt = receiptService.getByOrderSn(orderSn);
        //查询订单和自订单,然后写入vo返回
        return new OrderDetailVO(order, orderItems, orderLogs, receipt);
    }
    @Override
    public OrderDetailVO queryEditAddressDetail(String orderSn) {
        Order order = this.getBySn(orderSn);
        if (order == null) {
            throw new ServiceException(ResultCode.ORDER_NOT_EXIST);
        }
        //查询订单项信息
        List<OrderItem> orderItems = orderItemService.getByOrderSn(orderSn);
        //查询订单和自订单,然后写入vo返回
        return new OrderDetailVO(order, orderItems, null, null);
    }
    @Override
@@ -666,7 +769,40 @@
        return order;
    }
    @Override
    @SystemLogPoint(description = "修改订单", customerLog = "'订单[' + #orderSn + ']收货信息修改,修改为'+#memberAddressDTO.consigneeDetail")
    @Transactional(rollbackFor = Exception.class)
    public Order updateAddressConsignee(String orderSn, MemberAddressDTO memberAddressDTO) {
        Order order = this.getBySn(orderSn);
        if (order == null) {
            throw new ServiceException(ResultCode.ORDER_NOT_EXIST);
        }
        //限制30秒只能请求一次避免出现重新提交问题
        Boolean b = redisTemplate.opsForValue().setIfAbsent(LOCK_EDIT_ORDER_ADDRESS + orderSn, orderSn,30, TimeUnit.SECONDS);
        if ( Boolean.FALSE.equals(b)){
            throw new ServiceException("请在30秒后重试");
        }
        String modifyAddressFlag = order.getModifyAddressFlag();
        if (StringUtils.isNotBlank(modifyAddressFlag)) {
            throw new ServiceException("当前订单已经被领取");
        }
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("id", order.getId());
        // 使用 last 方法拼接 FOR UPDATE 语句
        wrapper.last("FOR UPDATE");
        baseMapper.selectOne(wrapper);
        //要记录之前的收货地址,所以需要以代码方式进行调用 不采用注解
        String message = "订单[" + orderSn + "]收货信息修改,由[" +order.getConsigneeAddressPath()+  order.getConsigneeDetail() + "]修改为[" + memberAddressDTO.getConsigneeAddressPath()+ memberAddressDTO.getConsigneeDetail() + "]";
        //记录订单操作日志
        BeanUtil.copyProperties(memberAddressDTO, order);
        order.setModifyAddressFlag(ModifyAddressEnums.USED.name());
        this.updateById(order);
        OrderLog orderLog = new OrderLog(orderSn, UserContext.getCurrentUser().getId(), UserContext.getCurrentUser().getRole().getRole(),
                UserContext.getCurrentUser().getUsername(), message);
        orderLogService.save(orderLog);
        return order;
    }
    @Override
    @OrderLogPoint(description = "'订单['+#orderSn+']发货,发货单号['+#logisticsNo+']'", orderSn = "#orderSn")
    @Transactional(rollbackFor = Exception.class)
@@ -724,6 +860,26 @@
    }
    @Override
    public void updateTraces(UpdateTracesDTO updateTracesDTO) {
        LambdaQueryWrapper<OrderPackage> eq = Wrappers.<OrderPackage>lambdaQuery()
                .eq(OrderPackage::getOrderSn, updateTracesDTO.getOrderSn())
                .eq(OrderPackage::getPackageNo, updateTracesDTO.getPackageNo());
        OrderPackage one = orderPackageService.getOne(eq);
        if (one == null) {
            throw new ServiceException("当前包裹不存在");
        }
        String logisticsId = updateTracesDTO.getLogisticsId();
        if (StringUtils.isNotBlank(logisticsId)) {
            Logistics logic = logisticsService.getById(logisticsId);
            one.setLogisticsNo(updateTracesDTO.getLogisticsNo());
            one.setLogisticsCode(logic.getCode());
            one.setLogisticsName(logic.getName());
            orderPackageService.updateById(one);
        }
    }
    @Override
    public Traces getMapTraces(String orderSn) {
        //获取订单信息
        Order order = this.getBySn(orderSn);
@@ -740,17 +896,80 @@
    @OrderLogPoint(description = "'订单['+#orderSn+']核销,核销码['+#verificationCode+']'", orderSn = "#orderSn")
    @Transactional(rollbackFor = Exception.class)
    public Order take(String orderSn, String verificationCode) {
        log.info("订单核销订单号为:{},核销码为{}",orderSn,verificationCode);
        Order order;
        RLock lock = redissonClient.getLock(LOCK_COUPON_CARD + orderSn);
        try {
             lock.lock();
            //获取订单信息
            order = this.getBySn(orderSn);
            log.info("获取到的订单信息为{}",JSON.toJSONString(order));
            //订单幂等问题
            if (OrderStatusEnum.COMPLETED.name().equals(order.getOrderStatus())) {
                throw new ServiceException("当前订单已完成无法再次核验");
            }
        //获取订单信息
        Order order = this.getBySn(orderSn);
        //检测虚拟订单信息
        checkVerificationOrder(order, verificationCode);
        order.setOrderStatus(OrderStatusEnum.COMPLETED.name());
        //订单完成
        this.complete(orderSn);
            //检测虚拟订单信息
            checkVerificationOrder(order, verificationCode);
            order.setOrderStatus(OrderStatusEnum.COMPLETED.name());
            //订单完成
            //获取所有的订单子项用于生成优惠卷订单信息
            List<OrderItem> orderItems = orderItemService.getByOrderSn(orderSn);
            List<CouponVirtual> couponVirtuals = new  ArrayList<>();
            for (OrderItem orderItem : orderItems) {
                log.info("获取到的子订单信息为{}",JSON.toJSONString(orderItem));
                String couponId = orderItem.getCouponId();
                if (StringUtils.isBlank(couponId)) {
                    continue;
                }
                String storeId = order.getStoreId();
                Coupon one = couponService.getOne(Wrappers.<Coupon>lambdaQuery().eq(Coupon::getStoreId, storeId).eq(Coupon::getId, couponId));
                if (one == null) {
                    log.error("当前订单订单号为:{}不存在中的优惠卷不存在----------------------->{}",order.getId(),orderItem.getOrderSn());
                }else {
                    Integer num = orderItem.getNum();
                    //当购买数量不为空的时候进行
                    if (num != null) {
                        for (int i = 1; i <= num; i++) {
                            CouponVirtual couponVirtual = getCouponVirtual(orderItem);
                            couponVirtual.setCouponNo(String.format("%08d", i));
                            couponVirtuals.add(couponVirtual);
                        }
                    }
                }
            }
            if (!couponVirtuals.isEmpty()) {
                log.info("生成的兑换卡为{}",JSON.toJSONString(couponVirtuals));
                order.setCouponFlag(CouPonFlagEnum.COUPON.name());
                couponVirtualService.saveBatch(couponVirtuals);
                //更新状态用于后续小程序判断弹出卷列表
                this.updateById(order);
            }
            this.complete(orderSn);
        } finally {
            assert lock != null;
            if (lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return order;
    }
    private static CouponVirtual getCouponVirtual(OrderItem orderItem) {
        CouponVirtual couponVirtual = new CouponVirtual();
        couponVirtual.setOrderId(orderItem.getOrderSn());
        couponVirtual.setCouponId(orderItem.getCouponId());
        couponVirtual.setCouponName(orderItem.getCouponName());
        couponVirtual.setGoodsId(orderItem.getGoodsId());
        couponVirtual.setSkuId(orderItem.getSkuId());
        couponVirtual.setItemOrderId(orderItem.getId());
        couponVirtual.setSkuName(orderItem.getGoodsName());
        couponVirtual.setName(orderItem.getCouponName());
        couponVirtual.setShareStatus(ShareStatusEnum.NOT_SHARE.name());
        couponVirtual.setClaimStatus(ClaimStatusEnum.NOT_CLAIM.name());
        return couponVirtual;
    }
    @Override
    public Order take(String verificationCode) {
        String storeId = OperationalJudgment.judgment(UserContext.getCurrentUser()).getStoreId();
@@ -956,7 +1175,31 @@
        checkBatchDeliver(orderBatchDeliverDTOList);
        //订单批量发货
        for (OrderBatchDeliverDTO orderBatchDeliverDTO : orderBatchDeliverDTOList) {
            this.delivery(orderBatchDeliverDTO.getOrderSn(), orderBatchDeliverDTO.getLogisticsNo(), orderBatchDeliverDTO.getLogisticsId());
            String logisticsNo = orderBatchDeliverDTO.getLogisticsNo();
            String[] split = logisticsNo.split(",");
            //如果物流单号包含多个,则进行拆单
            if (split.length > 1) {
                for (int i = 0; i < split.length; i++) {
                    PartDeliveryParamsDTO partDeliveryParamsDTO = new PartDeliveryParamsDTO();
                    partDeliveryParamsDTO.setOrderSn(orderBatchDeliverDTO.getOrderSn());
                    partDeliveryParamsDTO.setLogisticsNo(split[i]);
                    partDeliveryParamsDTO.setLogisticsId(orderBatchDeliverDTO.getLogisticsId());
                    ArrayList<PartDeliveryDTO> partDeliveryDTOList = new ArrayList<>();
                    partDeliveryParamsDTO.setPartDeliveryDTOList(partDeliveryDTOList);
                    if (i == split.length - 1){
                        List<OrderItem> orderItemList = orderItemService.getByOrderSn(orderBatchDeliverDTO.getOrderSn());
                        for (OrderItem orderItem : orderItemList) {
                            if (RefundStatusEnum.NO_REFUND.name().equals(orderItem.getIsRefund())) {
                                PartDeliveryDTO partDeliveryDTO = new PartDeliveryDTO();
                                partDeliveryDTO.setDeliveryNum(orderItem.getNum());
                                partDeliveryDTO.setOrderItemId(orderItem.getId());
                                partDeliveryDTOList.add(partDeliveryDTO);
                            }
                        }
                    }
                    this.partDelivery(partDeliveryParamsDTO);
                }
            }
        }
    }
@@ -1103,12 +1346,31 @@
        orderLogService.saveBatch(orderLogList);
        //判断订单货物是否全部发货完毕
        Boolean delivery = true;
        log.info("打印订单货物:{}",orderItemList);
        //需要再此处判断订单是否有申请售后状态
        for (OrderItem orderItem : orderItemList) {
            if (orderItem.getDeliverNumber() < orderItem.getNum()) {
                delivery = false;
                break;
            // 先判断是否是未申请售后的订单
            if (OrderItemAfterSaleStatusEnum.NOT_APPLIED.name().equals(orderItem.getAfterSaleStatus())){
                // 发货数量小于订单数量则全发
                log.info("打印发货数量:{},该商品订单数量货物对应的:{}",orderItem.getDeliverNumber(),orderItem.getNum());
                if (orderItem.getDeliverNumber() < orderItem.getNum()) {
                    delivery = false;
                    break;
                }
            }else{
                //全部退回情况
                if(Objects.equals(orderItem.getReturnGoodsNumber(), orderItem.getNum())){
                    break;
                }
                //存在情况 退回数量不是全部退回而是部分退回
                if (orderItem.getReturnGoodsNumber() + orderItem.getDeliverNumber() < orderItem.getNum()) {
                    delivery = false;
                    break;
                }
            }
        }
        log.info("delivery状态:{}",delivery);
        //是否全部发货
        if (delivery) {
            return delivery(orderSn, invoiceNumber, logisticsId);
@@ -1122,6 +1384,485 @@
        order.setSellerRemark(sellerRemark);
        this.updateById(order);
        return order;
    }
    @Override
    public Result getOrderCount(StatisticsQueryParam queryParam) {
        Date startTime = null;
        Date endTime = new Date(); // 结束时间默认是当前时间
        List<String> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(); // 用于日期计算的日历实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        //判断是否是按年月查询
        if (queryParam.getYear() != null && queryParam.getMonth() != null) {
            Date[] dates =CommonUtil.getMonthStartAndEnd(queryParam.getYear(),queryParam.getMonth());
            startTime = dates[0];
            endTime = dates[1];
            System.out.println("开始打印");
            System.out.println(startTime);
            System.out.println(endTime);
            Calendar tempCalendar = Calendar.getInstance();
            tempCalendar.setTime(startTime);
            days = tempCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int i = 0; i < days; i++) {
                dateList.add(sdf.format(tempCalendar.getTime()));
                tempCalendar.add(Calendar.DATE, 1); // 每天累加1天
            }
        }else{
            switch (queryParam.getSearchType()) {
                case "TODAY":
                    // 今天:从今天0点到现在
                    calendar.setTime(new Date()); // 重置为当前时间
                    calendar.set(Calendar.HOUR_OF_DAY, 0); // 小时设为0(24小时制)
                    calendar.set(Calendar.MINUTE, 0);      // 分钟设为0
                    calendar.set(Calendar.SECOND, 0);      // 秒设为0
                    calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0
                    startTime = calendar.getTime();        // 得到今天0点的Date对象
                    dateList.add(sdf.format(startTime));
                    break;
                case "YESTERDAY":
                    // 昨天:从昨天0点到昨天23:59:59.999
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -1); // 日期减1天(变为昨天)
                    // 设置昨天0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    // 设置昨天23:59:59.999
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    endTime = calendar.getTime();
                    dateList.add(sdf.format(startTime));
                    break;
                case "LAST_SEVEN":
                    // 过去七天:从7天前0点到现在(含今天共7天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -6); // 日期减6天(7天前的今天)
                    // 设置7天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    days = 7;
                    // 循环生成7天的日期字符串
                    Calendar tempCalendar = Calendar.getInstance();
                    tempCalendar.setTime(startTime);
                    for (int i = 0; i < days; i++) {
                        dateList.add(sdf.format(tempCalendar.getTime()));
                        tempCalendar.add(Calendar.DATE, 1); // 每天累加1天
                    }
                    break;
                case "LAST_THIRTY":
                    // 过去30天:从30天前0点到现在(含今天共30天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -29); // 日期减29天(30天前的今天)
                    // 设置30天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    days = 30;
                    // 循环生成30天的日期字符串
                    Calendar tempCalendar30 = Calendar.getInstance();
                    tempCalendar30.setTime(startTime);
                    for (int i = 0; i < days; i++) {
                        dateList.add(sdf.format(tempCalendar30.getTime()));
                        tempCalendar30.add(Calendar.DATE, 1); // 每天累加1天
                    }
                    break;
                default:
                    return Result.error("不支持的时间范围类型");
            }
        }
        List<Map<String, Object>> maps = lmkOrderSelectMapper.selectOrderCountByDay(startTime,endTime,queryParam.getStoreId());
        Map<String, Long> dayCountMap = maps.stream()
                .collect(Collectors.toMap(
                        map -> map.get("day").toString(),  // 日期字符串(如2023-09-19)
                        map -> Long.valueOf(map.get("count").toString())  // 订单数量
                ));
        List<Long> orderCounts = new ArrayList<>();
        for (String date : dateList) {
            // 核心:存在则取查询结果,不存在则补0
            orderCounts.add(dayCountMap.getOrDefault(date, 0L));
        }
        return Result.ok().data(orderCounts);
    }
    @Override
    public Result getOrderTimePeriod(StatisticsQueryParam queryParam) {
        Date startTime = null;
        Date endTime = new Date(); // 结束时间默认是当前时间
        List<String> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(); // 用于日期计算的日历实例
        for (int i =0;i<24;i++){
            dateList.add(i + "点");
        }
        if (queryParam.getYear() != null && queryParam.getMonth() != null) {
            Date[] dates =CommonUtil.getMonthStartAndEnd(queryParam.getYear(),queryParam.getMonth());
            startTime = dates[0];
            endTime = dates[1];
        }else{
            switch (queryParam.getSearchType()) {
                case "TODAY":
                    // 今天:从今天0点到现在
                    calendar.setTime(new Date()); // 重置为当前时间
                    calendar.set(Calendar.HOUR_OF_DAY, 0); // 小时设为0(24小时制)
                    calendar.set(Calendar.MINUTE, 0);      // 分钟设为0
                    calendar.set(Calendar.SECOND, 0);      // 秒设为0
                    calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0
                    startTime = calendar.getTime();        // 得到今天0点的Date对象
                    break;
                case "YESTERDAY":
                    // 昨天:从昨天0点到昨天23:59:59.999
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -1); // 日期减1天(变为昨天)
                    // 设置昨天0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    // 设置昨天23:59:59.999
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    endTime = calendar.getTime();
                    break;
                case "LAST_SEVEN":
                    // 过去七天:从7天前0点到现在(含今天共7天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -6); // 日期减6天(7天前的今天)
                    // 设置7天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    break;
                case "LAST_THIRTY":
                    // 过去30天:从30天前0点到现在(含今天共30天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -29); // 日期减29天(30天前的今天)
                    // 设置30天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    break;
                default:
                    return Result.error("不支持的时间范围类型");
            }
        }
        List<Map<String, Object>> maps = lmkOrderSelectMapper.selectOrderTimePeriod(startTime,endTime,queryParam.getStoreId());
        List<String> goodsIds = new ArrayList<>();
        //如果查询条件带了店铺 查询店铺的商品
        if (StringUtils.isNotBlank(queryParam.getStoreId())){
            List<Goods> list = new LambdaQueryChainWrapper<>(goodsService.getBaseMapper())
                    .eq(Goods::getDeleteFlag, Boolean.FALSE)
                    .eq(Goods::getStoreId, queryParam.getStoreId())
                    .list();
            goodsIds = list.stream().map(Goods::getId).collect(Collectors.toList());
        }
        List<Map<String,Object>> countMap = lmkOrderSelectMapper.selectViewDataCount(startTime,endTime,queryParam.getStoreId(),goodsIds);
        Map<String, Long> countByDayHour = maps.stream()
                .collect(Collectors.toMap(
                        map -> map.get("hour").toString(),
                        map -> Long.valueOf(map.get("count").toString()),
                        Long::sum  // 如果有重复,累加count
                ));
        Map<String, Long> viewDataCountByDayHour = countMap.stream()
                        .collect(Collectors.toMap(
                                map -> map.get("hour").toString(),
                                map -> Long.valueOf(map.get("count").toString()),
                                Long::sum
                        ));
        // 遍历查看结果
        List<Long> countList = new ArrayList<>();
        List<Long> viewData = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            String hourKey = String.valueOf(i);
            Long viewDataCount = viewDataCountByDayHour.getOrDefault(hourKey,0L);
            Long count = countByDayHour.getOrDefault(hourKey, 0L);
            countList.add(count);
            viewData.add(viewDataCount);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("dateData",dateList);
        map.put("countData",countList);
        map.put("viewData",viewData);
        return Result.ok().data(map);
    }
    @Override
    public Result getPvUv(StatisticsQueryParam queryParam) {
        Date startTime = null;
        Date endTime = new Date(); // 结束时间默认是当前时间
        List<String> dateList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance(); // 用于日期计算的日历实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        if (queryParam.getYear() != null && queryParam.getMonth() != null) {
            Date[] dates =CommonUtil.getMonthStartAndEnd(queryParam.getYear(),queryParam.getMonth());
            startTime = dates[0];
            endTime = dates[1];
            Calendar tempCalendar = Calendar.getInstance();
            tempCalendar.setTime(startTime);
            days = tempCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int i = 0; i < days; i++) {
                dateList.add(sdf.format(tempCalendar.getTime()));
                tempCalendar.add(Calendar.DATE, 1); // 每天累加1天
            }
        }else{
            switch (queryParam.getSearchType()) {
                case "TODAY":
                    // 今天:从今天0点到现在
                    calendar.setTime(new Date()); // 重置为当前时间
                    calendar.set(Calendar.HOUR_OF_DAY, 0); // 小时设为0(24小时制)
                    calendar.set(Calendar.MINUTE, 0);      // 分钟设为0
                    calendar.set(Calendar.SECOND, 0);      // 秒设为0
                    calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0
                    startTime = calendar.getTime();        // 得到今天0点的Date对象
                    dateList.add(sdf.format(startTime));
                    break;
                case "YESTERDAY":
                    // 昨天:从昨天0点到昨天23:59:59.999
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -1); // 日期减1天(变为昨天)
                    // 设置昨天0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    // 设置昨天23:59:59.999
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    endTime = calendar.getTime();
                    dateList.add(sdf.format(startTime));
                    break;
                case "LAST_SEVEN":
                    // 过去七天:从7天前0点到现在(含今天共7天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -6); // 日期减6天(7天前的今天)
                    // 设置7天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    days = 7;
                    // 循环生成7天的日期字符串
                    Calendar tempCalendar = Calendar.getInstance();
                    tempCalendar.setTime(startTime);
                    for (int i = 0; i < days; i++) {
                        dateList.add(sdf.format(tempCalendar.getTime()));
                        tempCalendar.add(Calendar.DATE, 1); // 每天累加1天
                    }
                    break;
                case "LAST_THIRTY":
                    // 过去30天:从30天前0点到现在(含今天共30天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -29); // 日期减29天(30天前的今天)
                    // 设置30天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    days = 30;
                    // 循环生成30天的日期字符串
                    Calendar tempCalendar30 = Calendar.getInstance();
                    tempCalendar30.setTime(startTime);
                    for (int i = 0; i < days; i++) {
                        dateList.add(sdf.format(tempCalendar30.getTime()));
                        tempCalendar30.add(Calendar.DATE, 1); // 每天累加1天
                    }
                    break;
                default:
                    return Result.error("不支持的时间范围类型");
            }
        }
        List<String> goodsIds = new ArrayList<>();
        //如果查询条件带了店铺 查询店铺的商品
        if (StringUtils.isNotBlank(queryParam.getStoreId())){
            List<Goods> list = new LambdaQueryChainWrapper<>(goodsService.getBaseMapper())
                    .eq(Goods::getDeleteFlag, Boolean.FALSE)
                    .eq(Goods::getStoreId, queryParam.getStoreId())
                    .list();
            goodsIds = list.stream().map(Goods::getId).collect(Collectors.toList());
        }
        List<Map<String,Object>> maps = lmkOrderSelectMapper.selectPvUv(startTime,endTime,queryParam.getStoreId(),goodsIds);
        Map<String, Long> pvDataMap = maps.stream()
                .collect(Collectors.toMap(
                        map -> map.get("day").toString(),
                        map -> Long.valueOf(map.get("pvData").toString()),
                        Long::sum  // 如果有重复,累加count
                ));
        Map<String, Long> uvDataMap = maps.stream()
                .collect(Collectors.toMap(
                        map -> map.get("day").toString(),
                        map -> Long.valueOf(map.get("uvData").toString()),
                        Long::sum  // 如果有重复,累加count
                ));
        List<Long> pvData = new ArrayList<>();
        List<Long> uvData = new ArrayList<>();
        for (String date: dateList){
            String dayKey = String.valueOf(date);
            Long pv = pvDataMap.getOrDefault(dayKey,0L);
            Long uv = uvDataMap.getOrDefault(dayKey, 0L);
            pvData.add(pv);
            uvData.add(uv);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("dateData",dateList);
        map.put("pvData",pvData);
        map.put("uvData",uvData);
        return Result.ok().data(map);
    }
    @Override
    public Result gerProductRepurchase(StatisticsQueryParam queryParam) {
        Date startTime = null;
        Date endTime = new Date(); // 结束时间默认是当前时间
        Calendar calendar = Calendar.getInstance(); // 用于日期计算的日历实例
        if (queryParam.getYear() != null && queryParam.getMonth() != null) {
            Date[] dates =CommonUtil.getMonthStartAndEnd(queryParam.getYear(),queryParam.getMonth());
            startTime = dates[0];
            endTime = dates[1];
        }else{
            switch (queryParam.getSearchType()) {
                case "TODAY":
                    // 今天:从今天0点到现在
                    calendar.setTime(new Date()); // 重置为当前时间
                    calendar.set(Calendar.HOUR_OF_DAY, 0); // 小时设为0(24小时制)
                    calendar.set(Calendar.MINUTE, 0);      // 分钟设为0
                    calendar.set(Calendar.SECOND, 0);      // 秒设为0
                    calendar.set(Calendar.MILLISECOND, 0); // 毫秒设为0
                    startTime = calendar.getTime();        // 得到今天0点的Date对象
                    break;
                case "YESTERDAY":
                    // 昨天:从昨天0点到昨天23:59:59.999
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -1); // 日期减1天(变为昨天)
                    // 设置昨天0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    // 设置昨天23:59:59.999
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    calendar.set(Calendar.MILLISECOND, 999);
                    endTime = calendar.getTime();
                    break;
                case "LAST_SEVEN":
                    // 过去七天:从7天前0点到现在(含今天共7天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -6); // 日期减6天(7天前的今天)
                    // 设置7天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    break;
                case "LAST_THIRTY":
                    // 过去30天:从30天前0点到现在(含今天共30天)
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE, -29); // 日期减29天(30天前的今天)
                    // 设置30天前0点
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    startTime = calendar.getTime();
                    break;
                default:
                    return Result.error("不支持的时间范围类型");
            }
        }
        List<Map<String, Object>> maps = lmkOrderSelectMapper.selectProductRepurchase(startTime,endTime,queryParam.getCurrentLimit(),queryParam.getStoreId());
        List<String> goodsData = new ArrayList<>();
        List<BigDecimal> rateData = new ArrayList<>();
        // 遍历结果集,提取数据到集合
        for (Map<String, Object> map : maps) {
            // 提取商品名(注意:键是resultMap中定义的property值"goodsName")
            if (Objects.nonNull(map.get("goodsName"))) {
                String goodsName = map.get("goodsName").toString();
                String buyerCount = map.get("buyerCount").toString();
                String repurchaseBuyerCount = map.get("repurchaseBuyerCount").toString();
                String format = String.format("%s:购买人数 %s 复购人数%s", goodsName, buyerCount, repurchaseBuyerCount);
                goodsData.add(format);
            }
            // 提取复购率(复购率通常是数字类型,这里用BigDecimal接收)
            if (Objects.nonNull(map.get("repurchaseRate"))) {
                // 转换为BigDecimal(根据实际类型调整,也可能是Double等)
                BigDecimal rate = new BigDecimal(map.get("repurchaseRate").toString()).setScale(2, RoundingMode.HALF_UP);
                rateData.add(rate);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("goodsData",goodsData);
        map.put("rateData",rateData);
        return Result.ok().data(map);
    }
    /**
@@ -1140,6 +1881,7 @@
            for (Order unpaidOrder : unpaidOrders) {
                this.systemCancel(unpaidOrder.getSn(), "拼团活动结束订单未付款,系统自动取消订单", false);
            }
        }
        List<Order> paidOrders = listMap.get(PayStatusEnum.PAID.name());
        //如待参团人数大于0,并已开启虚拟成团
@@ -1457,8 +2199,19 @@
            orderExportDetailDTO.setPaymentMethod(CharSequenceUtil.isNotBlank(orderExportDTO.getPaymentMethod()) ? PaymentMethodEnum.valueOf(orderExportDTO.getPaymentMethod()).paymentName() : "");
            orderExportDetailDTO.setClientType(ClientTypeEnum.valueOf(orderExportDTO.getClientType()).value());
            orderExportDetailDTO.setOrderType(orderExportDTO.getOrderType().equals(OrderTypeEnum.NORMAL.name()) ? "普通订单" : "虚拟订单");
            orderExportDetailDTO.setAfterSaleStatus(OrderItemAfterSaleStatusEnum.valueOf(orderExportDTO.getAfterSaleStatus()).description());
            String afterSaleStatus = orderExportDTO.getAfterSaleStatus();
            if (StringUtils.isNotBlank(afterSaleStatus)) {
                try {
                    orderExportDetailDTO.setAfterSaleStatus(OrderItemAfterSaleStatusEnum.valueOf(afterSaleStatus).description());
                } catch (IllegalArgumentException e) {
                    // 处理枚举值不存在的情况
                    orderExportDetailDTO.setAfterSaleStatus("未知状态");
                }
            } else {
                // 设置默认值或空字符串
                orderExportDetailDTO.setAfterSaleStatus("无售后状态");
            }
            //时间
            orderExportDetailDTO.setCreateTime(DateUtil.formatDateTime(orderExportDTO.getCreateTime()));
            orderExportDetailDTO.setPaymentTime(DateUtil.formatDateTime(orderExportDTO.getPaymentTime()));
@@ -1473,7 +2226,7 @@
        // 创建表头
        Row header = sheet.createRow(0);
        String[] headers = {"主订单编号", "子订单编号", "选购商品", "商品数量", "商品ID", "商品单价", "订单应付金额",
                "运费", "优惠总金额", "平台优惠", "商家优惠", "商家改价", "支付方式","买家名称", "收件人", "收件人手机号",
                "运费", "优惠总金额", "平台优惠", "商家优惠", "商家改价", "支付方式","会员ID","买家名称", "收件人", "收件人手机号",
                "省", "市", "区", "街道", "详细地址", "买家留言", "订单提交时间", "支付完成时间", "来源",
                "订单状态", "订单类型", "售后状态", "取消原因", "发货时间", "完成时间", "店铺"};
@@ -1488,7 +2241,7 @@
            row.createCell(0).setCellValue(dto.getOrderSn());
            row.createCell(1).setCellValue(dto.getOrderItemSn());
            row.createCell(2).setCellValue(dto.getGoodsName());
            row.createCell(3).setCellValue(dto.getNum());
            row.createCell(3).setCellValue(dto.getNum()!=null?dto.getNum():0);
            row.createCell(4).setCellValue(dto.getGoodsId());
            row.createCell(5).setCellValue(dto.getUnitPrice()!=null?dto.getUnitPrice():0);
            row.createCell(6).setCellValue(dto.getFlowPrice()!=null?dto.getFlowPrice():0);
@@ -1498,27 +2251,27 @@
            row.createCell(10).setCellValue(dto.getStoreMarketingCost()!=null?dto.getStoreMarketingCost():0);
            row.createCell(11).setCellValue(dto.getUpdatePrice()!=null?dto.getUpdatePrice():0);
            row.createCell(12).setCellValue(dto.getPaymentMethod());
            row.createCell(13).setCellValue(dto.getMemberId());
            row.createCell(14).setCellValue(dto.getNickName());
            row.createCell(13).setCellValue(dto.getNickName());
            row.createCell(14).setCellValue(dto.getConsigneeName());
            row.createCell(15).setCellValue(dto.getConsigneeMobile());
            row.createCell(16).setCellValue(dto.getProvince());
            row.createCell(17).setCellValue(dto.getCity());
            row.createCell(18).setCellValue(dto.getDistrict());
            row.createCell(19).setCellValue(dto.getStreet());
            row.createCell(20).setCellValue(dto.getConsigneeDetail());
            row.createCell(21).setCellValue(dto.getRemark());
            row.createCell(22).setCellValue(dto.getCreateTime());
            row.createCell(23).setCellValue(dto.getPaymentTime());
            row.createCell(24).setCellValue(dto.getClientType());
            row.createCell(25).setCellValue(dto.getOrderStatus());
            row.createCell(26).setCellValue(dto.getOrderType());
            row.createCell(27).setCellValue(dto.getAfterSaleStatus());
            row.createCell(28).setCellValue(dto.getCancelReason());
            row.createCell(29).setCellValue(dto.getLogisticsTime());
            row.createCell(30).setCellValue(dto.getCompleteTime());
            row.createCell(31).setCellValue(dto.getStoreName());
            row.createCell(15).setCellValue(dto.getConsigneeName());
            row.createCell(16).setCellValue(dto.getConsigneeMobile());
            row.createCell(17).setCellValue(dto.getProvince());
            row.createCell(18).setCellValue(dto.getCity());
            row.createCell(19).setCellValue(dto.getDistrict());
            row.createCell(20).setCellValue(dto.getStreet());
            row.createCell(21).setCellValue(dto.getConsigneeDetail());
            row.createCell(22).setCellValue(dto.getRemark());
            row.createCell(23).setCellValue(dto.getCreateTime());
            row.createCell(24).setCellValue(dto.getPaymentTime());
            row.createCell(25).setCellValue(dto.getClientType());
            row.createCell(26).setCellValue(dto.getOrderStatus());
            row.createCell(27).setCellValue(dto.getOrderType());
            row.createCell(28).setCellValue(dto.getAfterSaleStatus());
            row.createCell(29).setCellValue(dto.getCancelReason());
            row.createCell(30).setCellValue(dto.getLogisticsTime());
            row.createCell(31).setCellValue(dto.getCompleteTime());
            row.createCell(32).setCellValue(dto.getStoreName());
        }
        //修改列宽