lyhzzz 2 éve
szülő
commit
e128053c4b

+ 14 - 1
src/main/java/com/fdkankan/sale/controller/RepairInfoController.java

@@ -5,6 +5,7 @@ import com.fdkankan.sale.common.ResultData;
 import com.fdkankan.sale.exception.BusinessException;
 import com.fdkankan.sale.service.IRepairService;
 import com.fdkankan.sale.service.impl.RepairInfoService;
+import com.fdkankan.sale.util.MyExcelUtil;
 import com.fdkankan.sale.vo.request.RepairInfoListParam;
 import lombok.Data;
 import org.apache.commons.lang3.StringUtils;
@@ -13,10 +14,12 @@ import org.springframework.web.bind.annotation.*;
 
 @RestController
 @RequestMapping("/sale/repairInfo")
-public class RepairInfoController {
+public class RepairInfoController extends BaseController{
 
     @Autowired
     RepairInfoService repairInfoService;
+    @Autowired
+    MyExcelUtil excelUtil;
 
     @GetMapping("/details")
     public ResultData details(@RequestParam(required = false) String repairId){
@@ -40,4 +43,14 @@ public class RepairInfoController {
     public ResultData list(@RequestBody RepairInfoListParam param){
         return ResultData.ok(repairInfoService.pageList(param));
     }
+
+    /**
+     * 0 四维时代服务清单(模板)
+     * 1 维修服务工作单(模板)
+     */
+    @GetMapping("/exportExcel")
+    public void exportExcel(@RequestParam(required = false) Integer type){
+
+        excelUtil.listFill(response,null);
+    }
 }

+ 19 - 2
src/main/java/com/fdkankan/sale/controller/TestController.java

@@ -2,17 +2,23 @@ package com.fdkankan.sale.controller;
 
 import com.alibaba.fastjson.JSONObject;
 import com.fdkankan.sale.common.ResultData;
+import com.fdkankan.sale.entity.PriceList;
 import com.fdkankan.sale.entity.SysUser;
 import com.fdkankan.sale.httpClient.client.WxClient;
 import com.fdkankan.sale.pay.wx.WXUtil;
+import com.fdkankan.sale.service.IPriceListService;
 import com.fdkankan.sale.service.ISysUserService;
+import com.fdkankan.sale.util.MyExcelUtil;
+import com.fdkankan.sale.util.OrderListVo;
 import org.springframework.beans.factory.annotation.Autowired;
 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 javax.sound.midi.Soundbank;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Scanner;
 
 @RestController
@@ -21,10 +27,21 @@ public class TestController {
 
     @Autowired
     ISysUserService sysUserService;
-
+    @Autowired
+    MyExcelUtil excelUtil;
+    @Autowired
+    IPriceListService priceListService;
 
     @GetMapping("/test1")
-    public ResultData test1() {
+    public ResultData test1(HttpServletResponse response) {
+        //模板中需要批量添加的,list{.**},obj{**}中的数据,
+        List<PriceList> list = priceListService.list();
+        OrderListVo vo = new OrderListVo();
+        vo.setCompanyName("测试公司");
+        vo.setCustomerName("测试客户名称");
+        vo.setCustomerPhone("12138");
+        vo.setPriceLists(list);
+        excelUtil.listFill(response,vo);
         return ResultData.ok();
     }
 

+ 8 - 1
src/main/java/com/fdkankan/sale/service/impl/RepairSaleService.java

@@ -78,6 +78,13 @@ public class RepairSaleService {
         if(param.getRepairId() == null){
             throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
         }
+        Repair repair = repairService.getById(param.getRepairId());
+        if(repair == null){
+            throw new BusinessException(ResultCode.REPAIR_NOT_EXITS);
+        }
+        if(repair.getStatus() !=1 ){
+            throw new BusinessException(ResultCode.REPAIR_STATUS_NOT_EXITS);
+        }
         param.setSysUserId(sysUserId);
         orderReceivingService.save(param);
 
@@ -140,7 +147,7 @@ public class RepairSaleService {
         if(repair == null){
             throw new BusinessException(ResultCode.REPAIR_NOT_EXITS);
         }
-        if(repair.getStatus() !=2){
+        if(repair.getStatus() !=2 && repair.getStatus() !=3){
             throw new BusinessException(ResultCode.REPAIR_STATUS_NOT_EXITS);
         }
 

+ 89 - 1
src/main/java/com/fdkankan/sale/util/ExcelFillCellMergeStrategy.java

@@ -1,2 +1,90 @@
-package com.fdkankan.sale.util;public class ExcelFillCellMergeStrategy {
+package com.fdkankan.sale.util;
+
+import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.metadata.data.WriteCellData;
+import com.alibaba.excel.write.handler.CellWriteHandler;
+import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
+import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+import java.util.List;
+
+public class ExcelFillCellMergeStrategy implements CellWriteHandler {
+    /** 需要进行单元格合并的列数组 **/
+    private int[] mergeColumnIndex;
+    /** 单元格合并从第几行开始 **/
+    private int[] mergeRowIndex;
+    public ExcelFillCellMergeStrategy() {}
+    public ExcelFillCellMergeStrategy(int[] mergeRowIndex, int[] mergeColumnIndex) {
+        this.mergeRowIndex = mergeRowIndex;
+        this.mergeColumnIndex = mergeColumnIndex;
+    }
+    @Override
+    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
+                                 List<WriteCellData<?>> list, Cell cell, Head head, Integer integer, Boolean isHead) {
+        int curRowIndex = cell.getRowIndex();
+        int curColIndex = cell.getColumnIndex();
+        int length = mergeRowIndex.length;
+        //判断是否合并行,
+        for (int k = 0; k < length; k++) {
+            //2个数为一组,一组做一次合并。
+            if(k!=0 && k%2!=0){
+                continue;
+            }
+            //每组数开始合并
+            if (curRowIndex >= mergeRowIndex[k] && curRowIndex <mergeRowIndex[k+1]) {
+                for (int i = 0; i < mergeColumnIndex.length; i++) {
+                    if (curColIndex == mergeColumnIndex[i]) {
+                        mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
+                        break;
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 当前单元格向上合并
+     *
+     * @param writeSheetHolder
+     * @param cell
+     *            当前单元格
+     * @param curRowIndex
+     *            当前行
+     * @param curColIndex
+     *            当前列
+     */
+    private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
+        Object curData =
+                cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
+        Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
+        Object preData =
+                preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();
+        // 将当前单元格数据与上一个单元格数据比较
+        Boolean dataBool = preData.equals(curData);
+        if (true) {
+            Sheet sheet = writeSheetHolder.getSheet();
+            List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
+            boolean isMerged = false;
+            for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
+                CellRangeAddress cellRangeAddr = mergeRegions.get(i);
+                // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
+                if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
+                    sheet.removeMergedRegion(i);
+                    cellRangeAddr.setLastRow(curRowIndex);
+                    sheet.addMergedRegion(cellRangeAddr);
+                    isMerged = true;
+                }
+            }
+            // 若上一个单元格未被合并,则新增合并单元
+            if (!isMerged) {
+                CellRangeAddress cellRangeAddress =
+                        new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
+                sheet.addMergedRegion(cellRangeAddress);
+            }
+        }
+    }
 }

+ 70 - 1
src/main/java/com/fdkankan/sale/util/MyExcelUtil.java

@@ -1,2 +1,71 @@
-package com.fdkankan.sale.util;public class MyExcelUtil {
+package com.fdkankan.sale.util;
+
+import com.fdkankan.sale.entity.Customer;
+import com.fdkankan.sale.entity.PriceList;
+import com.fdkankan.sale.service.IPriceListService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.*;
+
+
+@Component
+public class MyExcelUtil {
+
+    //根据模板导出excel
+    public void listFill(HttpServletResponse response,Object obj) {
+        response.setContentType("application/vnd.ms-excel");
+        response.setCharacterEncoding("utf-8");
+        String fileName = "四维时代服务清单"  + ".xlsx";
+        try {
+            fileName = URLEncoder.encode(fileName, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
+        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
+        // 填充list 的时候还要注意 模板中{.} 多了个点 表示list
+        //获取文件路径
+        ClassPathResource resource= new ClassPathResource("file/excel/orderList.xlsx");
+        String templateFileName = null;
+        try {
+            templateFileName = resource.getFile().getPath();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+
+        ExcelWriter excelWriter = null;
+        try {
+            //合并前6列
+            int[] mergeColumeIndex = {0,1,2,3,4,5};
+            //合并行,两组数字为一次合并,比如3、4为一组,合并3到4行,5、6同理
+            int[] mergeRowIndex = {3,4,5,6};
+            excelWriter = EasyExcel.write(response.getOutputStream())
+                    .withTemplate(templateFileName)
+                    //调用合并策略
+                    //.registerWriteHandler(new ExcelFillCellMergeStrategy(mergeRowIndex, mergeColumeIndex))
+                    .build();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        WriteSheet writeSheet = EasyExcel.writerSheet().build();
+        // 这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
+        // forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
+        // 简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
+        // 如果数据量大 list不是最后一行 参照下一个
+        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
+        excelWriter.fill(obj, fillConfig, writeSheet);
+        excelWriter.finish();
+    }
+
+
 }

+ 18 - 1
src/main/java/com/fdkankan/sale/util/OrderListVo.java

@@ -1,2 +1,19 @@
-package com.fdkankan.sale.util;public class OrderListVo {
+package com.fdkankan.sale.util;
+
+import com.fdkankan.sale.entity.PriceList;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class OrderListVo {
+
+    private String companyName;                     //客户名称
+    private String customerName;                    //联系人
+    private String customerPhone;                   //联系方式
+    private String customerAddress;                 //客户地址
+    private String cameraName;                      //产品名称
+    private String cameraSnCode;                    //相机snCode
+    private String sendDate;                        //送检日期
+    private List<PriceList> priceLists;             //备件列表
 }

BIN
src/main/resources/file/excel/orderList.xlsx