fenghaifu 3 месяцев назад
Родитель
Сommit
8ca158b399

+ 97 - 0
srm-module-code/src/main/java/org/jeecg/modules/saleCode/controller/SaleQuotationController.java

@@ -1,18 +1,33 @@
 package org.jeecg.modules.saleCode.controller;
 
 import java.io.IOException;
+import java.net.URLEncoder;
 import java.util.*;
 import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import cn.hutool.core.bean.BeanUtil;
+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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import org.apache.commons.lang.StringUtils;
 import org.jeecg.common.aspect.annotation.PermissionData;
+import org.jeecg.modules.baseCode.entity.BaseShipArchive;
+import org.jeecg.modules.baseCode.entity.BaseTemplates;
+import org.jeecg.modules.baseCode.service.IBaseShipArchiveService;
+import org.jeecg.modules.baseCode.service.IBaseTemplatesService;
 import org.jeecg.modules.baseCode.service.ISerialPatternService;
+import org.jeecg.modules.purCode.entity.PurInquiryForm;
+import org.jeecg.modules.purCode.entity.PurInquiryFormProduct;
 import org.jeecg.modules.saleCode.entity.*;
 import org.jeecg.modules.saleCode.service.*;
 import org.jeecg.modules.saleCode.vo.SaleQuotationAlert;
+import org.jeecg.modules.system.entity.SysUser;
+import org.jeecg.modules.system.service.ISysUserService;
 import org.jeecgframework.poi.excel.ExcelImportUtil;
 import org.jeecgframework.poi.excel.def.NormalExcelConstants;
 import org.jeecgframework.poi.excel.entity.ExportParams;
@@ -27,6 +42,7 @@ import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.saleCode.vo.SaleQuotationPage;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.multipart.MultipartFile;
@@ -66,6 +82,15 @@ public class SaleQuotationController {
     private ISaleContractService saleContractService;
     @Autowired
     private ISaleOrderService saleOrderService;
+    @Autowired
+    private IBaseTemplatesService baseTemplatesService;
+    @Autowired
+    private ISysUserService sysUserService;
+    @Autowired
+    private IBaseShipArchiveService baseShipArchiveService;
+
+    @Value(value = "${jeecg.path.upload}")
+    private String uploadpath;
     /**
      * 分页列表查询
      *
@@ -539,4 +564,76 @@ public class SaleQuotationController {
         return Result.OK("文件导入失败!");
     }
 
+    /**
+     * 导出销售报价单
+     * @param request
+     * @return
+     */
+    @GetMapping(value = "/exportQuotationXls")
+    public void exportQuotationXls(String id, HttpServletRequest request, HttpServletResponse response) {
+
+        BaseTemplates templates = baseTemplatesService.getByTemplateType("销售报价单");
+        SaleQuotation saleQuotation = saleQuotationService.getById(id);
+        LambdaQueryWrapper<SaleQuotationProduct> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(SaleQuotationProduct::getHeadId, id);
+        List<SaleQuotationProduct> productList = saleQuotationProductService.list(queryWrapper);
+
+        LambdaQueryWrapper<SaleQuotationShip> shipQueryWrapper = new LambdaQueryWrapper<>();
+        shipQueryWrapper.eq(SaleQuotationShip::getHeadId, id);
+        List<SaleQuotationShip> shipList = saleQuotationShipService.list(shipQueryWrapper);
+
+        List<BaseShipArchive> baseShipArchiveList = new ArrayList<>();
+        if (oConvertUtils.listIsNotEmpty(shipList)){
+            LambdaQueryWrapper<BaseShipArchive> shipArchiveLambdaQueryWrapper = new LambdaQueryWrapper<>();
+            shipArchiveLambdaQueryWrapper.in(BaseShipArchive::getId,shipList.stream().map(SaleQuotationShip::getShipId).collect(Collectors.toList()));
+            baseShipArchiveList = baseShipArchiveService.list(shipArchiveLambdaQueryWrapper);
+        }
+
+        if (templates != null){
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            try {
+                String fileName = URLEncoder.encode("销售报价单" + saleQuotation.getBillCode(), "UTF-8").replaceAll("\\+", "%20");
+                response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
+
+                String filePath = uploadpath + templates.getTemplateFile();
+
+                try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).withTemplate(filePath).build()) {
+                    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(productList, fillConfig, writeSheet);
+                    Map<String, Object> map = BeanUtil.beanToMap(saleQuotation);
+                    // 制单人信息
+                    SysUser sysUser = sysUserService.getUserByName(saleQuotation.getCreateBy());
+                    map.put("salesman", sysUser.getRealname());
+                    map.put("salesPhone", sysUser.getPhone());
+                    map.put("salesEmail", sysUser.getEmail());
+                    String shipname = "";
+                    String imo = "";
+                    if (oConvertUtils.listIsNotEmpty(shipList)){
+                        shipname = shipList.stream().map(SaleQuotationShip::getShipName).distinct().collect(Collectors.joining(","));
+
+                    }
+                    if (oConvertUtils.listIsNotEmpty(baseShipArchiveList)){
+                        imo = baseShipArchiveList.stream().map(BaseShipArchive::getImo).distinct().collect(Collectors.joining(","));
+                    }
+                    map.put("shipName", shipname);
+                    map.put("imo", imo);
+
+                    excelWriter.fill(map, writeSheet);
+                }
+
+            }catch (Exception ex){
+                ex.printStackTrace();
+            }
+
+        }
+
+
+    }
+
 }