Bladeren bron

导出优化

wanxl 1 jaar geleden
bovenliggende
commit
b55bd84c76

+ 8 - 0
pom.xml

@@ -416,6 +416,14 @@
             <version>2.1.6</version>
         </dependency>
 
+        <!-- excel操作 -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>3.1.1</version>
+            <scope>compile</scope>
+        </dependency>
+
         <!--<dependency>-->
         <!--<groupId>com.baidu.aip</groupId>-->
         <!--            <artifactId>java-sdk</artifactId>-->

+ 136 - 0
src/main/java/com/sqx/modules/exportExecl/controller/exportExeclController.java

@@ -0,0 +1,136 @@
+package com.sqx.modules.exportExecl.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.sqx.common.utils.DateUtils;
+import com.sqx.common.utils.PageUtils;
+import com.sqx.common.utils.Result;
+import com.sqx.modules.exportExecl.mapper.ExportJobMapper;
+import com.sqx.modules.exportExecl.model.ExportJob;
+import com.sqx.modules.exportExecl.service.ExportJobService;
+import com.sqx.modules.order.dao.AppOrderDao;
+import com.sqx.modules.order.entity.TbOrder;
+import com.sqx.modules.order.service.AppOrderService;
+import com.sqx.modules.utils.excel.ExcelData;
+import com.sqx.modules.utils.excel.ExportExcelUtils;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.util.IOUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.List;
+
+@Slf4j
+@Api(tags={"管理端-导出"})
+@RestController
+@RequestMapping("/admin/export")
+public class exportExeclController {
+
+    @Autowired
+    private AppOrderDao appOrderDao;
+
+    @Autowired
+    private ExportJobMapper exportJobMapper;
+
+    @Autowired
+    private ExportJobService exportJobService;
+
+    @GetMapping("excelOrder")
+    @ApiOperation("导出卡密列表")
+    public Result excelOrder(Integer status, String phone, Long shopId, String userName,
+                             String orderNumber, Integer orderType, String shopName, String riderPhone,
+                             String startTime, String endTime, String userId,HttpServletResponse response) throws Exception{
+        Integer size = appOrderDao.excelAllOrderAdminCount( status, phone, shopId, userName, orderNumber, orderType,
+                shopName, riderPhone,startTime,endTime);
+        int i=10000;
+        if(size>i){
+            return Result.error("导出总数不能超过"+i+",请添加筛选条件");
+        }
+        StringBuffer stringBuffer=new StringBuffer();
+        if(status!=null){stringBuffer.append("状态:"+status+";");}
+        if(phone!=null&&phone!=""){stringBuffer.append("手机号:"+phone+";");}
+//        if(shopId!=null){stringBuffer.append(":"+shopId+";");}
+        if(userName!=null&&userName!=""){stringBuffer.append("昵称:"+userName+";");}
+        if(orderNumber!=null&&orderNumber!=""){stringBuffer.append("订单号:"+orderNumber+";");}
+        if(orderType!=null){stringBuffer.append("订单类型:"+orderType+";");}
+        if(shopName!=null&&shopName!=""){stringBuffer.append("商铺名称:"+shopName+";");}
+        if(riderPhone!=null&&riderPhone!=""){stringBuffer.append("骑手手机号:"+riderPhone+";");}
+        if(startTime!=null&&startTime!=""){stringBuffer.append("开始时间:"+startTime+";");}
+        if(endTime!=null&&endTime!=""){stringBuffer.append("结束时间:"+endTime+";");}
+
+        ExportJob exportJob =new ExportJob();
+        exportJob.setFlag(0);
+        exportJob.setFileType("order");
+        exportJob.setConditionDetail(stringBuffer.toString());
+        exportJob.setConditions(","+(status==null?"":status)+","+phone+","+(shopId==null?"":shopId)+","+userName+","+orderNumber+","
+                +(orderType==null?"":orderType)+","+shopName+","
+                +riderPhone+","+startTime+","+endTime);
+        LocalDateTime localDateTime = LocalDateTime.now();
+        String time=localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
+        exportJob.setFileName(time+"订单列表.xlsx");
+        exportJob.setUserId(userId);
+        int a=exportJobMapper.insert(exportJob);
+        if (a==1){
+            return Result.success("新增导出计划成功");
+        }else{
+            return Result.error("新增导出计划失败");
+        }
+    }
+    //    任意文件读取/下载
+    @ApiOperation(value = "查询下载列表")
+    @GetMapping("listByUser")
+    public Result List(String userId,String state,String startTime,String endTime,Integer curretPage,Integer pageSize) throws IOException {
+
+        QueryWrapper queryWrapper=new QueryWrapper<ExportJob>();
+
+        queryWrapper.eq("user_id",userId);
+        if(StringUtils.hasText(state)){
+            queryWrapper.eq("flag",state);
+        }
+        if(StringUtils.hasText(startTime)){
+            queryWrapper.ge("create_time",startTime);
+        }
+        if(StringUtils.hasText(endTime)){
+            queryWrapper.le("create_time",endTime);
+        }
+        IPage<ExportJob> iPage=exportJobService.page(new Page<>(curretPage,pageSize),queryWrapper);
+        PageUtils pageUtils = new PageUtils(iPage);
+        return Result.success().put("data", pageUtils);
+    }
+
+    //    任意文件读取/下载
+    @ApiOperation(value = "任意文件读取/下载")
+    @GetMapping("readBuffer")
+    public Result readbuffer(String filename,HttpServletResponse response) throws IOException {
+        if(filename.contains("..") || filename.contains("/")){
+            return Result.error("文件名异常");
+        }
+        try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(Paths.get(filename)))){
+            response.setHeader("Content-Disposition", "attachment; filename=" + filename);
+            response.setContentLength((int) Files.size(Paths.get(filename)));
+            response.setContentType("application/octet-stream");
+
+            // 使用 Apache Commons IO 库的工具方法将输入流中的数据拷贝到输出流中
+            IOUtils.copy(inputStream, response.getOutputStream());
+            return Result.success("文件下载成功");
+        }catch (IOException e){
+            log.error(e.getMessage());
+            return Result.error("文件下载失败");
+        }
+    }
+}

+ 18 - 0
src/main/java/com/sqx/modules/exportExecl/mapper/ExportJobMapper.java

@@ -0,0 +1,18 @@
+package com.sqx.modules.exportExecl.mapper;
+
+import com.sqx.modules.exportExecl.model.ExportJob;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * <p>
+ *  Mapper 接口
+ * </p>
+ *
+ * @author wan
+ * @since 2024-09-25
+ */
+@Mapper
+public interface ExportJobMapper extends BaseMapper<ExportJob> {
+
+}

+ 64 - 0
src/main/java/com/sqx/modules/exportExecl/model/ExportJob.java

@@ -0,0 +1,64 @@
+package com.sqx.modules.exportExecl.model;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+
+import java.io.Serializable;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author wan
+ * @since 2024-09-25
+ */
+@Data
+@EqualsAndHashCode(callSuper = false)
+@Accessors(chain = true)
+@ApiModel(value="ExportJob对象", description="")
+public class ExportJob implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @TableId(value = "export_id", type = IdType.AUTO)
+    private Integer exportId;
+
+    @ApiModelProperty(value = "文件名称")
+    private String fileName;
+
+    @ApiModelProperty(value = "文件类型")
+    private String fileType;
+
+    @ApiModelProperty(value = "用户id")
+    private String userId;
+
+    @ApiModelProperty(value = "创建时间")
+//    @TableField(fill = FieldFill.INSERT)
+    private Date createTime;
+
+    @ApiModelProperty(value = "更新时间")
+//    @TableField(fill = FieldFill.INSERT_UPDATE)
+    private Date updateTime;
+
+    @ApiModelProperty(value = "导出条件描述")
+    private String conditionDetail;
+
+    @ApiModelProperty(value = "导出条件")
+    private String conditions;
+
+    @ApiModelProperty(value = "文件大小")
+    private String fileSize;
+
+    @ApiModelProperty(value = "状态 0待导出 1导出已完成 2导出失败")
+    private Integer flag;
+
+
+}

+ 118 - 0
src/main/java/com/sqx/modules/exportExecl/model/ExportOrderVo.java

@@ -0,0 +1,118 @@
+package com.sqx.modules.exportExecl.model;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.alibaba.excel.annotation.write.style.ColumnWidth;
+import lombok.Data;
+
+/**
+ * <p>
+ * 
+ * </p>
+ *
+ * @author wan
+ * @since 2024-07-22
+ */
+@Data
+public class ExportOrderVo {
+
+
+    @ColumnWidth(0)
+    @ExcelProperty(value = "编号" , index = 0)
+    private String orderId;
+    @ColumnWidth(1)
+    @ExcelProperty(value = "下单用户" , index = 0)
+    private String userName;
+
+    @ColumnWidth(2)
+    @ExcelProperty(value = "用户头像" , index = 0)
+    private String avatar;
+
+    @ColumnWidth(3)
+    @ExcelProperty(value = "手机号" , index = 0)
+    private String phone;
+
+    @ColumnWidth(4)
+    @ExcelProperty(value = "商铺信息" , index = 0)
+    private String shopDetail;
+
+    @ColumnWidth(5)
+    @ExcelProperty(value = "订单类型" , index = 0)
+    private String orderType;
+
+    @ColumnWidth(6)
+    @ExcelProperty(value = "配送信息" , index = 0)
+    private String identail;
+
+    @ColumnWidth(7)
+    @ExcelProperty(value = "骑手昵称" , index = 0)
+    private String riderNickName;
+
+    @ColumnWidth(8)
+    @ExcelProperty(value = "骑手电话" , index = 0)
+    private String riderPhone;
+
+    @ColumnWidth(9)
+    @ExcelProperty(value = "订单号" , index = 0)
+    private String orderNumber;
+
+    @ColumnWidth(10)
+    @ExcelProperty(value = "内容" , index = 0)
+    private String goodsDetail;
+
+    @ColumnWidth(11)
+    @ExcelProperty(value = "商品总价(元)" , index = 0)
+    private String sumPrice;
+
+    @ColumnWidth(12)
+    @ExcelProperty(value = "优惠金额(元)" , index = 0)
+    private String couponMoney;
+
+    @ColumnWidth(13)
+    @ExcelProperty(value = "参与活动名称" , index = 0)
+    private String activityTitle;
+
+    @ColumnWidth(14)
+    @ExcelProperty(value = "活动优惠金额(元)" , index = 0)
+    private String activityDiscountAmount;
+
+    @ColumnWidth(15)
+    @ExcelProperty(value = "打包费(元/个 具体按照数量计算)" , index = 0)
+    private String packMoney;
+
+    @ColumnWidth(16)
+    @ExcelProperty(value = "跑腿费(元)" , index = 0)
+    private String errandMoney;
+
+    @ColumnWidth(17)
+    @ExcelProperty(value = "支付金额(元)" , index = 0)
+    private String payMoney;
+
+    @ColumnWidth(18)
+    @ExcelProperty(value = "跑腿费说明" , index = 0)
+    private String errandMoneyIsShop;
+
+    @ColumnWidth(19)
+    @ExcelProperty(value = "订单备注" , index = 0)
+    private String remark;
+
+    @ColumnWidth(20)
+    @ExcelProperty(value = "支付时间" , index = 0)
+    private String payTime;
+
+    @ColumnWidth(21)
+    @ExcelProperty(value = "支付方式" , index = 0)
+    private String payType;
+
+    @ColumnWidth(22)
+    @ExcelProperty(value = "取餐号" , index = 0)
+    private String qucanhao;
+
+    @ColumnWidth(23)
+    @ExcelProperty(value = "状态" , index = 0)
+    private String orderCode;
+
+    @ColumnWidth(24)
+    @ExcelProperty(value = "是否转单" , index = 0)
+    private String isRider;
+
+}

+ 16 - 0
src/main/java/com/sqx/modules/exportExecl/service/ExportJobService.java

@@ -0,0 +1,16 @@
+package com.sqx.modules.exportExecl.service;
+
+import com.sqx.modules.exportExecl.model.ExportJob;
+import com.baomidou.mybatisplus.extension.service.IService;
+
+/**
+ * <p>
+ *  服务类
+ * </p>
+ *
+ * @author wan
+ * @since 2024-09-25
+ */
+public interface ExportJobService extends IService<ExportJob> {
+
+}

+ 20 - 0
src/main/java/com/sqx/modules/exportExecl/service/impl/ExportJobServiceImpl.java

@@ -0,0 +1,20 @@
+package com.sqx.modules.exportExecl.service.impl;
+
+import com.sqx.modules.exportExecl.model.ExportJob;
+import com.sqx.modules.exportExecl.mapper.ExportJobMapper;
+import com.sqx.modules.exportExecl.service.ExportJobService;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.stereotype.Service;
+
+/**
+ * <p>
+ *  服务实现类
+ * </p>
+ *
+ * @author wan
+ * @since 2024-09-25
+ */
+@Service
+public class ExportJobServiceImpl extends ServiceImpl<ExportJobMapper, ExportJob> implements ExportJobService {
+
+}

+ 6 - 0
src/main/java/com/sqx/modules/order/dao/AppOrderDao.java

@@ -35,6 +35,12 @@ public interface AppOrderDao extends BaseMapper<TbOrder> {
                                      @Param("riderPhone") String riderPhone,@Param("startTime") String startTime,
                                      @Param("endTime") String endTime);
 
+    Integer excelAllOrderAdminCount(@Param("status") Integer status,@Param("phone") String phone, @Param("shopId") Long shopId,
+                                     @Param("userName") String userName, @Param("orderNumber") String orderNumber,
+                                     @Param("orderType") Integer orderType,@Param("shopName")String shopName,
+                                     @Param("riderPhone") String riderPhone,@Param("startTime") String startTime,
+                                     @Param("endTime") String endTime);
+
 
     IPage<TbOrder> selectOrderByAdmin(Page<TbOrder> pages,@Param("status") Integer status,@Param("phone") String phone,
                                       @Param("shopId") Long shopId, @Param("userName") String userName,

+ 8 - 0
src/main/java/com/sqx/modules/order/entity/TbOrder.java

@@ -255,5 +255,13 @@ public class TbOrder implements Serializable {
     @TableField(exist = false)
     private Integer isRider;
 
+    @TableField(exist = false)
+    @ApiModelProperty("导出商品详情")
+    private String detail;
+
+    @TableField(exist = false)
+    @ApiModelProperty("导出商品总价")
+    private Double sumPrice;
+
     public TbOrder() {}
 }

+ 18 - 18
src/main/java/com/sqx/modules/order/service/impl/AppAppOrderServiceImpl.java

@@ -1288,11 +1288,11 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
                                         String riderPhone, String startTime, String endTime) {
         List<TbOrder> tbOrderIPage = appOrderDao.excelAllOrderAdmin( status, phone, shopId, userName, orderNumber, orderType,
                 shopName, riderPhone,startTime,endTime);
-        for (int i = 0; i < tbOrderIPage.size(); i++) {
-            List<OrderGoods> orderGoodsList = orderGoodsDao.selectList(new QueryWrapper<OrderGoods>()
-                    .eq("order_id", tbOrderIPage.get(i).getOrderId()));
-            tbOrderIPage.get(i).setOrderGoodsList(orderGoodsList);
-        }
+//        for (int i = 0; i < tbOrderIPage.size(); i++) {
+//            List<OrderGoods> orderGoodsList = orderGoodsDao.selectList(new QueryWrapper<OrderGoods>()
+//                    .eq("order_id", tbOrderIPage.get(i).getOrderId()));
+//            tbOrderIPage.get(i).setOrderGoodsList(orderGoodsList);
+//        }
         ExcelData data = new ExcelData();
         data.setName("订单列表");
         List<String> titles = new ArrayList();
@@ -1338,19 +1338,19 @@ public class AppAppOrderServiceImpl extends ServiceImpl<AppOrderDao, TbOrder> im
             row.add(order.getRiderNickName());
             row.add(order.getRiderPhone());
             row.add(order.getOrderNumber());
-            stringBuffer=new StringBuffer();
-            BigDecimal sumGoodsPrice=BigDecimal.ZERO;
-            List<OrderGoods> orderGoodsList = order.getOrderGoodsList();
-            for(int i=0;i<orderGoodsList.size();i++){
-                OrderGoods orderGoods = orderGoodsList.get(i);
-                stringBuffer.append(i + 1).append(". 商品名:").append(orderGoods.getGoodsName()).append(",数量:").append(orderGoods.getGoodsNum()).append(",规格:").append(orderGoods.getSkuMessage());
-                if(orderGoods.getGoodsPrice()!=null && orderGoods.getGoodsNum()!=null){
-                    BigDecimal goodsPrice = orderGoods.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoods.getGoodsNum()));
-                    sumGoodsPrice=sumGoodsPrice.add(goodsPrice);
-                }
-            }
-            row.add(stringBuffer.toString());
-            row.add(sumGoodsPrice);
+//            stringBuffer=new StringBuffer();
+//            BigDecimal sumGoodsPrice=BigDecimal.ZERO;
+//            List<OrderGoods> orderGoodsList = order.getOrderGoodsList();
+//            for(int i=0;i<orderGoodsList.size();i++){
+//                OrderGoods orderGoods = orderGoodsList.get(i);
+//                stringBuffer.append(i + 1).append(". 商品名:").append(orderGoods.getGoodsName()).append(",数量:").append(orderGoods.getGoodsNum()).append(",规格:").append(orderGoods.getSkuMessage());
+//                if(orderGoods.getGoodsPrice()!=null && orderGoods.getGoodsNum()!=null){
+//                    BigDecimal goodsPrice = orderGoods.getGoodsPrice().multiply(BigDecimal.valueOf(orderGoods.getGoodsNum()));
+//                    sumGoodsPrice=sumGoodsPrice.add(goodsPrice);
+//                }
+//            }
+            row.add(order.getDetail());
+            row.add(order.getSumPrice());
             row.add(order.getCouponMoney());
             row.add(order.getActivityTitle());
             row.add(order.getActivityDiscountAmount());

+ 58 - 4
src/main/java/com/sqx/modules/utils/excel/ExportExcelUtils.java

@@ -1,13 +1,19 @@
 package com.sqx.modules.utils.excel;
 
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.metadata.WriteSheet;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.*;
 import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
 
 import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.OutputStream;
 import java.net.URLEncoder;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -202,8 +208,56 @@ public class ExportExcelUtils {
         return new int[] { r, g, b };
     }
 
-
-
-
-
+    public static void writeExcel2(File file,String fileName, ExcelData excelData) throws Exception {
+        // 文件输出位置
+//        File file=new File(fileName);
+        OutputStream out = new FileOutputStream(file);
+//        ExcelWriter writer = EasyExcelFactory.getWriter(out);
+//
+//        // 动态添加表头,适用一些表头动态变化的场景
+//        Sheet sheet=new S
+//
+//        sheet1.setSheetName("第一个sheet");
+//
+//        // 创建一个表格,用于 Sheet 中使用
+//        Table table1 = new Table(1);
+//
+//        // 无注解的模式,动态添加表头
+//        table1.setHead(DataUtil.createTestListStringHead());
+//        // 写数据
+//        writer.write1(createDynamicModelList(), sheet1, table1);
+//
+//        // 将上下文中的最终 outputStream 写入到指定文件中
+//        writer.finish();
+//
+//        // 关闭流
+//        out.close();
+        List<List<String>> headList=new ArrayList<>();
+        List<String> list=excelData.getTitles();
+        for (int i = 0; i < list.size(); i++) {
+            List<String> temList=new ArrayList<>();
+            temList.add(list.get(i));
+            headList.add(temList);
+        }
+        ExcelWriter excelWriter =null;
+        try {
+            //设置前端下载文件名
+//            fileName = new String(("床位导入模板").getBytes("utf-8"),"iso-8859-1");
+//            response.setCharacterEncoding("utf-8");
+            // 指定写用哪个class去写
+            excelWriter = EasyExcel.write(out).head(headList).build();
+            // 同一个sheet只要创建一次
+            WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
+            // 调用写入
+            excelWriter.write(excelData.getRows(), writeSheet);
+        } catch (Exception e) {
+            System.out.println("下载模板失败"+e.getMessage());
+        } finally {
+            // 关闭流
+            if (excelWriter != null) {
+                excelWriter.finish();
+            }
+            out.close();
+        }
+    }
 }

+ 5 - 0
src/main/java/com/sqx/scheduler/config/SchedulerLock.java

@@ -32,4 +32,9 @@ public interface SchedulerLock {
      * 跑腿订单自动推送锁
      */
     String INDENT_AUTO_PUSH_LOCK = "wm:lock:indent:push";
+
+    /**
+     * 导出锁
+     */
+    String EXPORT_LOCK = "export:lock";
 }

+ 124 - 0
src/main/java/com/sqx/scheduler/export/ExportScheduler.java

@@ -0,0 +1,124 @@
+package com.sqx.scheduler.export;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.sqx.modules.coupon.service.TbCouponUserService;
+import com.sqx.modules.exportExecl.mapper.ExportJobMapper;
+import com.sqx.modules.exportExecl.model.ExportJob;
+import com.sqx.modules.order.service.AppOrderService;
+import com.sqx.modules.utils.excel.ExcelData;
+import com.sqx.modules.utils.excel.ExportExcelUtils;
+import com.sqx.scheduler.config.SchedulerLock;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import java.io.File;
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 优惠券定时任务
+ *
+ * @author : codingliang
+ * @date : 2024-09-09 12:22
+ */
+@Slf4j
+@Component
+@RequiredArgsConstructor
+@RequestMapping("/admin/aaaaaa")
+public class ExportScheduler {
+
+    private final TbCouponUserService tbCouponUserService;
+    private final RedissonClient redissonClient;
+    @Autowired
+    private ExportJobMapper exportJobMapper;
+    @Autowired
+    private AppOrderService appOrderService;
+
+    /**
+     * 将所有超过失效时间的优惠券改为失效状态
+     * 每分钟运行一次
+     */
+    @Async
+    @Scheduled(cron = "0 */1 * * * ?", zone = "Asia/Shanghai")
+    @GetMapping("excelOrder")
+    public void orderExport() throws Exception {
+        List<ExportJob> exportJobList=exportJobMapper.selectList(new QueryWrapper<ExportJob>()
+                .eq("flag",0).last("limit 2"));
+        log.info("exportJobList==========="+exportJobList);
+        if (null == exportJobList ||exportJobList.isEmpty()){
+            return;
+        }
+        for(ExportJob exportJob:exportJobList){
+            long bgTime = System.currentTimeMillis();
+            String jobExecuteKey = SchedulerLock.EXPORT_LOCK + ":" + exportJob.getExportId();
+            RLock lock = redissonClient.getLock(jobExecuteKey);
+            if (lock.tryLock(0, 30, TimeUnit.SECONDS)) {
+                String fileName=exportJob.getFileName();
+                String fileType=exportJob.getFileType();
+                try {
+                    ExcelData excelData=new ExcelData();
+                    if("order".equals(fileType)){
+                        excelData=getOrderExcelData(exportJob);
+                    }
+                    File file=new File(fileName);
+                    ExportExcelUtils.writeExcel2(file,fileName,excelData);
+                    double fileSize = file.length();
+                    double fileSizeKB = new BigDecimal(fileSize/1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //转换单位为KB
+                    exportJob.setFileSize(String.valueOf(fileSizeKB));
+                    exportJob.setFlag(1);
+                    exportJobMapper.updateById(exportJob);
+                } catch (Exception e) {
+                    exportJob.setFlag(2);
+                    exportJobMapper.updateById(exportJob);
+                    log.error(e.getMessage());
+                } finally {
+                    try {
+                        if (lock.isLocked()) {
+                            lock.unlock();
+                        }
+                    } catch (RuntimeException re) {
+                        log.error("报表解锁失败");
+                    }
+                }
+            }
+            long edTime = System.currentTimeMillis();
+            log.info("执行:通用数据导出定时任务......执行任务完成,执行时长:{} s", (edTime - bgTime) / 1000);
+        }
+    }
+    //订单导出
+    private ExcelData getOrderExcelData(ExportJob exportJob){
+        String[] conditions=exportJob.getConditions().split(",",-1);
+        Integer status=null;
+        if(!"".equals(conditions[1])){
+            status= Integer.valueOf(conditions[1]);
+        }
+        String phone=conditions[2];
+        Long shopId=null;
+        if(!"".equals(conditions[3])){
+            shopId= Long.valueOf(conditions[3]);
+        }
+        String userName=conditions[4];
+        String orderNumber=conditions[5];
+
+        Integer orderType=null;
+        if(!"".equals(conditions[6])){
+            orderType= Integer.valueOf(conditions[6]);
+        }
+        String shopName=conditions[7];
+        String riderPhone=conditions[8];
+        String startTime=conditions[9];
+        String endTime=conditions[10];
+        ExcelData excelData = appOrderService.excelAllOrderAdmin(status, phone, shopId, userName, orderNumber, orderType, shopName, riderPhone, startTime, endTime);
+
+        return excelData;
+    }
+}

+ 46 - 1
src/main/resources/mapper/order/OrderMapper.xml

@@ -149,7 +149,7 @@
         shopPhone,
         tiu.nick_name as riderNickName,tiu.phone as riderPhone,ti.indent_id as indentId,ti.is_rider as isRider,
         ti.rider_user_id as riderUserId, tcu.money as couponMoney,
-        apr.discount_amount as activityDiscountAmount, ai.title activityTitle
+        apr.discount_amount as activityDiscountAmount, ai.title activityTitle,ogg.detail,ogg.sumPrice
         from tb_order tor
         left join tb_user tu on tor.user_id = tu.user_id
         left join goods_shop gs on tor.shop_id = gs.shop_id
@@ -158,6 +158,51 @@
         left join tb_coupon_user tcu on tor.coupon_id = tcu.id
         left join activity_part_record apr on apr.order_id = tor.order_id
         left join activity ai on ai.id = apr.activity_id
+        left join (  select @a:=0,order_id ,group_concat(@a:=@a+1,".商品名:",goods_name,",数量:",goods_num,",规格:",sku_message) detail,sum(goods_num*goods_price) sumPrice  from order_goods og group by order_id
+        ) ogg on ogg.order_id =tor.order_id
+        where 1 = 1
+        <if test="riderPhone!=null and riderPhone!=''">
+            and tiu.phone =#{riderPhone}
+        </if>
+        <if test="shopName!=null and shopName!=''">
+            and gs.shop_name like concat('%',#{shopName},'%')
+        </if>
+        <if test="userName!=null and userName!=''">
+            and tor.user_name like concat('%',#{userName},'%')
+        </if>
+        <if test="phone!=null and phone!=''">
+            and tor.phone like concat('%',#{phone},'%')
+        </if>
+        <if test="orderNumber!=null and orderNumber!=''">
+            and tor.order_number = #{orderNumber}
+        </if>
+        <if test="status!=null and status!=-1 and status!=1">
+            and tor.status = #{status}
+        </if>
+        <if test="status!=null and status==1">
+            and tor.status in (1,2)
+        </if>
+        <if test="shopId!=null">
+            and tor.shop_id = #{shopId}
+        </if>
+        <if test="orderType!=null">
+            and tor.order_type = #{orderType}
+        </if>
+        <if test="startTime!=null and startTime!=''">
+            and date_format(tor.create_time,'%Y-%m-%d') >= date_format(#{startTime},'%Y-%m-%d')
+        </if>
+        <if test="endTime!=null and endTime!='' ">
+            and date_format(tor.create_time,'%Y-%m-%d') &lt;= date_format(#{endTime},'%Y-%m-%d')
+        </if>
+        order by tor.pay_time desc, tor.create_time desc
+    </select>
+
+    <select id="excelAllOrderAdminCount" resultType="java.lang.Integer">
+        select count(1) from tb_order tor
+        left join tb_user tu on tor.user_id = tu.user_id
+        left join goods_shop gs on tor.shop_id = gs.shop_id
+        left join tb_indent ti on tor.order_id = ti.order_id
+        left join tb_user tiu on tiu.user_id = ti.rider_user_id
         where 1 = 1
         <if test="riderPhone!=null and riderPhone!=''">
             and tiu.phone =#{riderPhone}