Commit a1325de663cddab0d626040dd6eaa221ec73a329

Authored by 房远帅
1 parent 966a4040

楚江ERP:客户资信-导出完善

@@ -27,7 +27,6 @@ import com.lframework.xingyun.sc.vo.customer.credit.CreateCustomerCreditVo; @@ -27,7 +27,6 @@ import com.lframework.xingyun.sc.vo.customer.credit.CreateCustomerCreditVo;
27 import com.lframework.xingyun.sc.vo.customer.credit.QueryCorePersonnelVo; 27 import com.lframework.xingyun.sc.vo.customer.credit.QueryCorePersonnelVo;
28 import com.lframework.xingyun.sc.vo.customer.credit.QueryCustomerCreditVo; 28 import com.lframework.xingyun.sc.vo.customer.credit.QueryCustomerCreditVo;
29 import com.lframework.xingyun.sc.vo.customer.credit.UpdateCustomerCreditVo; 29 import com.lframework.xingyun.sc.vo.customer.credit.UpdateCustomerCreditVo;
30 -import freemarker.template.TemplateException;  
31 import io.swagger.annotations.ApiImplicitParam; 30 import io.swagger.annotations.ApiImplicitParam;
32 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; 31 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
33 import com.lframework.starter.common.exceptions.impl.DefaultClientException; 32 import com.lframework.starter.common.exceptions.impl.DefaultClientException;
@@ -35,18 +34,15 @@ import io.swagger.annotations.ApiOperation; @@ -35,18 +34,15 @@ import io.swagger.annotations.ApiOperation;
35 import com.lframework.starter.common.utils.CollectionUtil; 34 import com.lframework.starter.common.utils.CollectionUtil;
36 import io.swagger.annotations.Api; 35 import io.swagger.annotations.Api;
37 import org.springframework.beans.factory.annotation.Autowired; 36 import org.springframework.beans.factory.annotation.Autowired;
38 -import org.springframework.core.io.FileSystemResource;  
39 -import org.springframework.http.ResponseEntity; 37 +import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.validation.annotation.Validated; 38 import org.springframework.validation.annotation.Validated;
41 import org.springframework.web.bind.annotation.*; 39 import org.springframework.web.bind.annotation.*;
42 import org.springframework.web.multipart.MultipartFile; 40 import org.springframework.web.multipart.MultipartFile;
43 41
44 import javax.validation.Valid; 42 import javax.validation.Valid;
45 import javax.validation.constraints.NotNull; 43 import javax.validation.constraints.NotNull;
46 -import java.io.IOException;  
47 -import java.nio.file.Files;  
48 -import java.nio.file.Path;  
49 -import java.nio.file.Paths; 44 +import java.io.*;
  45 +import java.net.URLEncoder;
50 import java.util.List; 46 import java.util.List;
51 import java.util.stream.Collectors; 47 import java.util.stream.Collectors;
52 48
@@ -70,6 +66,8 @@ public class CustomerCreditController extends DefaultBaseController { @@ -70,6 +66,8 @@ public class CustomerCreditController extends DefaultBaseController {
70 private SysUserService sysUserService; 66 private SysUserService sysUserService;
71 @Autowired 67 @Autowired
72 private RedisHandler redisHandler; 68 private RedisHandler redisHandler;
  69 + @Value("${customer.credit.export:/web/service/erp/xingyun/export/templates/}")
  70 + private String exportTemplate;
73 71
74 /** 72 /**
75 * 查询列表 73 * 查询列表
@@ -267,33 +265,40 @@ public class CustomerCreditController extends DefaultBaseController { @@ -267,33 +265,40 @@ public class CustomerCreditController extends DefaultBaseController {
267 265
268 /** 266 /**
269 * 导出 267 * 导出
  268 + *
270 * @return 269 * @return
271 */ 270 */
272 @ApiOperation("导出") 271 @ApiOperation("导出")
273 @HasPermission({"customer-dev-manage:customer-dev-plan:export"}) 272 @HasPermission({"customer-dev-manage:customer-dev-plan:export"})
274 - @PostMapping("/export")  
275 - public ResponseEntity<FileSystemResource> export(@NotBlank(message = "ID不能为空") String id, HttpServletResponse response) { 273 + @GetMapping("/export")
  274 + public void export(@NotBlank(message = "ID不能为空") String id, HttpServletResponse response) {
276 try { 275 try {
277 String fileName = customerCreditService.export(id); 276 String fileName = customerCreditService.export(id);
278 - Path filePath = Paths.get("/web/service/erp/xingyun/export/templates", fileName);  
279 -  
280 - if (Files.exists(filePath)) {  
281 - // 创建FileSystemResource实例,用于读取文件  
282 - FileSystemResource file = new FileSystemResource(filePath.toString());  
283 -  
284 - // 返回ResponseEntity对象,设置响应头以便浏览器知道这是一个文件下载请求  
285 - return ResponseEntity.ok()  
286 - .header("Content-Disposition", "attachment; filename=" + file.getFilename())  
287 - .body(file);  
288 - } else {  
289 - response.sendError(404, "文件未找到"); 277 + String tmpLocation = exportTemplate + fileName;
  278 + // 创建文件对象
  279 + File file = new File(tmpLocation);
  280 + // 检查文件是否存在
  281 + if (!file.exists()) {
  282 + throw new DefaultClientException("文件不存在!");
  283 + }
  284 + // 设置响应头
  285 + response.setContentType("application/octet-stream");
  286 + response.setHeader("Content-Disposition", "attachment; filename=" +
  287 + URLEncoder.encode(file.getName(), "UTF-8"));
  288 + response.setContentLengthLong(file.length());
  289 + // 读取文件并写入响应输出流
  290 + try (FileInputStream fis = new FileInputStream(file);
  291 + OutputStream os = response.getOutputStream()) {
  292 + byte[] buffer = new byte[1024];
  293 + int len;
  294 + while ((len = fis.read(buffer)) > 0) {
  295 + os.write(buffer, 0, len);
  296 + }
  297 + os.flush();
290 } 298 }
291 - } catch (IOException e) {  
292 - e.printStackTrace();  
293 - } catch (TemplateException e) {  
294 - e.printStackTrace(); 299 + } catch (Exception e) {
  300 + throw new RuntimeException(e);
295 } 301 }
296 - return null;  
297 } 302 }
298 303
299 304
@@ -47,20 +47,17 @@ import freemarker.template.TemplateExceptionHandler; @@ -47,20 +47,17 @@ import freemarker.template.TemplateExceptionHandler;
47 import org.apache.commons.collections.CollectionUtils; 47 import org.apache.commons.collections.CollectionUtils;
48 import org.apache.commons.lang3.StringUtils; 48 import org.apache.commons.lang3.StringUtils;
49 import org.springframework.beans.factory.annotation.Autowired; 49 import org.springframework.beans.factory.annotation.Autowired;
  50 +import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.cache.annotation.CacheEvict; 51 import org.springframework.cache.annotation.CacheEvict;
51 import org.springframework.cache.annotation.Cacheable; 52 import org.springframework.cache.annotation.Cacheable;
52 import org.springframework.stereotype.Service; 53 import org.springframework.stereotype.Service;
53 import org.springframework.transaction.annotation.Transactional; 54 import org.springframework.transaction.annotation.Transactional;
54 import net.sourceforge.pinyin4j.PinyinHelper; 55 import net.sourceforge.pinyin4j.PinyinHelper;
55 import javax.annotation.Resource; 56 import javax.annotation.Resource;
56 -import java.lang.reflect.InvocationTargetException;  
57 -import java.lang.reflect.Method;  
58 -import java.lang.reflect.Modifier;  
59 import java.nio.charset.StandardCharsets; 57 import java.nio.charset.StandardCharsets;
60 import java.nio.file.Files; 58 import java.nio.file.Files;
61 import java.nio.file.Path; 59 import java.nio.file.Path;
62 import java.nio.file.Paths; 60 import java.nio.file.Paths;
63 -import java.time.format.DateTimeFormatter;  
64 import java.util.*; 61 import java.util.*;
65 import java.util.stream.Collectors; 62 import java.util.stream.Collectors;
66 import java.util.stream.IntStream; 63 import java.util.stream.IntStream;
@@ -94,6 +91,8 @@ public class CustomerCreditServiceImpl extends BaseMpServiceImpl<CustomerCreditM @@ -94,6 +91,8 @@ public class CustomerCreditServiceImpl extends BaseMpServiceImpl<CustomerCreditM
94 private FlowTaskWrapperMapper flowTaskWrapperMapper; 91 private FlowTaskWrapperMapper flowTaskWrapperMapper;
95 @Resource 92 @Resource
96 private SysUserDeptService sysUserDeptService; 93 private SysUserDeptService sysUserDeptService;
  94 + @Value("${customer.credit.export:/web/service/erp/xingyun/export/templates/}")
  95 + private String exportTemplate;
97 96
98 @Override 97 @Override
99 public PageResult<CustomerCredit> query(Integer pageIndex, Integer pageSize, QueryCustomerCreditVo vo) { 98 public PageResult<CustomerCredit> query(Integer pageIndex, Integer pageSize, QueryCustomerCreditVo vo) {
@@ -1333,9 +1332,7 @@ public class CustomerCreditServiceImpl extends BaseMpServiceImpl<CustomerCreditM @@ -1333,9 +1332,7 @@ public class CustomerCreditServiceImpl extends BaseMpServiceImpl<CustomerCreditM
1333 StringWriter stringWriter = new StringWriter(); 1332 StringWriter stringWriter = new StringWriter();
1334 template.process(data, stringWriter); 1333 template.process(data, stringWriter);
1335 String processedXml = stringWriter.toString(); 1334 String processedXml = stringWriter.toString();
1336 - // 1. 定义服务器导出目录  
1337 - String exportDir = "/web/service/erp/xingyun/export/templates";  
1338 - Path dirPath = Paths.get(exportDir); 1335 + Path dirPath = Paths.get(exportTemplate);
1339 // 2. 创建目录(如果不存在) 1336 // 2. 创建目录(如果不存在)
1340 if (!Files.exists(dirPath)) { 1337 if (!Files.exists(dirPath)) {
1341 Files.createDirectories(dirPath); 1338 Files.createDirectories(dirPath);