Your Name 3 år sedan
förälder
incheckning
63742f1c0d

+ 8
- 0
pom.xml Visa fil

@@ -84,6 +84,14 @@
84 84
 		</dependency>
85 85
 		<!--excel end-->
86 86
 
87
+		<!--zip start-->
88
+		<dependency>
89
+			<groupId>net.lingala.zip4j</groupId>
90
+			<artifactId>zip4j</artifactId>
91
+			<version>2.10.0</version>
92
+		</dependency>
93
+		<!--zip end-->
94
+
87 95
 		<!--weixin-miniapp start-->
88 96
 		<dependency>
89 97
 			<groupId>com.github.binarywang</groupId>

+ 14
- 0
src/main/java/com/njyunzhi/pet_identity/common/HttpUtils.java Visa fil

@@ -4,12 +4,26 @@ import org.springframework.http.*;
4 4
 import org.springframework.stereotype.Component;
5 5
 import org.springframework.web.client.RestTemplate;
6 6
 
7
+import java.io.InputStream;
7 8
 import java.io.UnsupportedEncodingException;
8 9
 import java.net.URLDecoder;
9 10
 import java.util.*;
10 11
 
11 12
 @Component
12 13
 public class HttpUtils {
14
+
15
+    public InputStream download(String url) {
16
+        RestTemplate restTemplate = new RestTemplate();
17
+        HttpHeaders headers = new HttpHeaders();
18
+        HttpEntity<String> entity = new HttpEntity<String>(headers);
19
+        Map<String, Object> urlAndParams = parseURL(url);
20
+        String formatURL = (String) urlAndParams.get("url");
21
+        Map<String, String> params = (Map<String, String>) urlAndParams.get("params");
22
+
23
+        ResponseEntity<InputStream> resp = restTemplate.exchange(formatURL, HttpMethod.GET, entity, InputStream.class, params);
24
+        return resp.getBody();
25
+    }
26
+
13 27
     public String get(String url) throws Exception {
14 28
         RestTemplate restTemplate = new RestTemplate();
15 29
         HttpHeaders headers = new HttpHeaders();

+ 100
- 0
src/main/java/com/njyunzhi/pet_identity/common/ZipUtil.java Visa fil

@@ -0,0 +1,100 @@
1
+package com.njyunzhi.pet_identity.common;
2
+
3
+import com.alibaba.excel.EasyExcel;
4
+import com.alibaba.excel.write.builder.ExcelWriterBuilder;
5
+import com.alibaba.fastjson.JSON;
6
+import lombok.Data;
7
+import net.lingala.zip4j.ZipFile;
8
+import net.lingala.zip4j.exception.ZipException;
9
+import net.lingala.zip4j.model.ZipParameters;
10
+import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.stereotype.Component;
12
+
13
+import javax.servlet.http.HttpServletResponse;
14
+import java.io.FileInputStream;
15
+import java.io.IOException;
16
+import java.io.InputStream;
17
+import java.io.OutputStream;
18
+import java.nio.ByteBuffer;
19
+import java.nio.channels.Channels;
20
+import java.nio.channels.ReadableByteChannel;
21
+import java.nio.channels.WritableByteChannel;
22
+import java.time.LocalDateTime;
23
+import java.time.ZoneId;
24
+import java.time.format.DateTimeFormatter;
25
+import java.util.List;
26
+
27
+@Component
28
+public class ZipUtil {
29
+
30
+    @Autowired
31
+    HttpUtils httpUtils;
32
+
33
+    private String today() {
34
+        String formatterStr = "yyyy-MM-dd";
35
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatterStr);
36
+        return LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(formatter);
37
+    }
38
+
39
+    public ZipFile zipLink(List<RFile> files) throws IOException {
40
+        ZipFile zipFile = new ZipFile(today() + ".zip");
41
+
42
+        if (files == null || files.size() < 1) {
43
+            return zipFile;
44
+        }
45
+
46
+        for (RFile f : files) {
47
+            ZipParameters zipParameters = new ZipParameters();
48
+            zipParameters.setFileNameInZip(f.getName());
49
+            InputStream inputStream = httpUtils.download(f.getUrl());
50
+            zipFile.addStream(inputStream, zipParameters);
51
+            inputStream.close();
52
+        }
53
+
54
+        return zipFile;
55
+    }
56
+
57
+    private long copyStream(InputStream input, OutputStream output) throws IOException {
58
+        ReadableByteChannel inputChannel = Channels.newChannel(input);
59
+        WritableByteChannel outputChannel = Channels.newChannel(output);
60
+        ByteBuffer buffer = ByteBuffer.allocateDirect(10240);
61
+        long size = 0;
62
+
63
+        while (inputChannel.read(buffer) != -1) {
64
+            buffer.flip();
65
+            size += outputChannel.write(buffer);
66
+            buffer.clear();
67
+        }
68
+
69
+        return size;
70
+    }
71
+
72
+    public void flush(HttpServletResponse response, ZipFile zipFile) throws IOException {
73
+        try {
74
+            String fileName = zipFile.getFile().getName();
75
+            InputStream inputStream = new FileInputStream(zipFile.getFile());
76
+
77
+            response.setContentType("application/zip");
78
+            response.setCharacterEncoding("utf-8");
79
+            response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName));
80
+
81
+            response.setContentLength(zipFile.getBufferSize());
82
+            copyStream(inputStream, response.getOutputStream());
83
+        } catch (Exception e) {
84
+            e.printStackTrace();
85
+
86
+            // 重置response
87
+            response.reset();
88
+            response.setContentType("application/json");
89
+            response.setCharacterEncoding("utf-8");
90
+            ResponseBean res = ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
91
+            response.getWriter().println(JSON.toJSONString(res));
92
+        }
93
+    }
94
+
95
+    @Data
96
+    public static class RFile {
97
+        String url;
98
+        String name;
99
+    }
100
+}

+ 55
- 1
src/main/java/com/njyunzhi/pet_identity/controller/TaCardNoController.java Visa fil

@@ -6,12 +6,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6 6
 import com.njyunzhi.pet_identity.common.BaseController;
7 7
 import com.njyunzhi.pet_identity.common.ResponseBean;
8 8
 import com.njyunzhi.pet_identity.common.StringUtils;
9
+import com.njyunzhi.pet_identity.common.ZipUtil;
9 10
 import com.njyunzhi.pet_identity.entity.TaSequence;
10 11
 import com.njyunzhi.pet_identity.service.ITaSequenceService;
11 12
 import com.njyunzhi.pet_identity.vo.BatchCardParams;
12 13
 import io.swagger.annotations.Api;
13 14
 import io.swagger.annotations.ApiOperation;
14 15
 import io.swagger.annotations.ApiParam;
16
+import net.lingala.zip4j.ZipFile;
15 17
 import org.slf4j.Logger;
16 18
 import org.slf4j.LoggerFactory;
17 19
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +26,10 @@ import com.njyunzhi.pet_identity.service.ITaCardNoService;
24 26
 import com.njyunzhi.pet_identity.entity.TaCardNo;
25 27
 import org.springframework.web.bind.annotation.RestController;
26 28
 
29
+import javax.servlet.http.HttpServletResponse;
30
+import java.util.ArrayList;
31
+import java.util.List;
32
+
27 33
 /**
28 34
  * <p>
29 35
     * 卡号库 前端控制器
@@ -46,6 +52,9 @@ public class TaCardNoController extends BaseController {
46 52
     @Autowired
47 53
     public ITaSequenceService iTaSequenceService;
48 54
 
55
+    @Autowired
56
+    public ZipUtil zipUtil;
57
+
49 58
     /**
50 59
      * 分页查询列表
51 60
      * @param pageNum
@@ -55,10 +64,19 @@ public class TaCardNoController extends BaseController {
55 64
     @RequestMapping(value="/cardNo",method= RequestMethod.GET)
56 65
     @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
57 66
     public ResponseBean taCardNoList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
58
-									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
67
+									 @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
68
+                                     @ApiParam("前缀") @RequestParam(value ="prefix", required = false) String prefix,
69
+                                     @ApiParam("起始号码") @RequestParam(value ="start", required = false) Integer start,
70
+                                     @ApiParam("终止号码") @RequestParam(value ="end", required = false) Integer end) throws Exception{
71
+
72
+        if (StringUtils.isEmpty(prefix) && (null != start || null != end)) {
73
+            return ResponseBean.error("卡号前缀不能为空");
74
+        }
59 75
 
60 76
         IPage<TaCardNo> pg = new Page<>(pageNum, pageSize);
61 77
         QueryWrapper<TaCardNo> queryWrapper = new QueryWrapper<>();
78
+        queryWrapper.ge(null != start, "card_no", iTaCardNoService.getFormatNo(prefix, start));
79
+        queryWrapper.le(null != end, "card_no", iTaCardNoService.getFormatNo(prefix, end));
62 80
         queryWrapper.orderByDesc("create_date");
63 81
 
64 82
         IPage<TaCardNo> result = iTaCardNoService.page(pg, queryWrapper);
@@ -66,6 +84,42 @@ public class TaCardNoController extends BaseController {
66 84
     }
67 85
 
68 86
 
87
+    /**
88
+     * 分页查询列表
89
+     * @return
90
+     */
91
+    @RequestMapping(value="/cardNo/export",method= RequestMethod.GET)
92
+    @ApiOperation(value="导出", notes = "导出", httpMethod = "GET", response = ResponseBean.class)
93
+    public ResponseBean export(@ApiParam("前缀") @RequestParam(value ="prefix") String prefix,
94
+                               @ApiParam("起始号码") @RequestParam(value ="start", required = false) Integer start,
95
+                               @ApiParam("终止号码") @RequestParam(value ="end", required = false) Integer end,
96
+                               HttpServletResponse response) throws Exception{
97
+
98
+        if (StringUtils.isEmpty(prefix)) {
99
+            return ResponseBean.error("卡号前缀不能为空");
100
+        }
101
+
102
+        QueryWrapper<TaCardNo> queryWrapper = new QueryWrapper<>();
103
+        queryWrapper.ge(null != start, "card_no", iTaCardNoService.getFormatNo(prefix, start));
104
+        queryWrapper.le(null != end, "card_no", iTaCardNoService.getFormatNo(prefix, end));
105
+        queryWrapper.orderByDesc("create_date");
106
+
107
+        List<TaCardNo> result = iTaCardNoService.list(queryWrapper);
108
+
109
+        List<ZipUtil.RFile> files = new ArrayList<>();
110
+        if (null != result && result.size() > 0) {
111
+            for (TaCardNo item : result) {
112
+                ZipUtil.RFile f = new ZipUtil.RFile();
113
+                f.setName(item.getCardNo() + ".png");
114
+                f.setUrl(item.getQrImage());
115
+            }
116
+        }
117
+
118
+        ZipFile data = zipUtil.zipLink(files);
119
+        zipUtil.flush(response, data);
120
+        return null;
121
+    }
122
+
69 123
     /**
70 124
      * 分页查询列表
71 125
      * @param id

+ 2
- 0
src/main/java/com/njyunzhi/pet_identity/service/ITaCardNoService.java Visa fil

@@ -17,4 +17,6 @@ public interface ITaCardNoService extends IService<TaCardNo> {
17 17
     TaCardNo getUnuseCard(String cardNo);
18 18
 
19 19
     void createBatchCardNo(BatchCardParams batchCardParams) throws Exception;
20
+
21
+    String getFormatNo(String prefix, Integer num);
20 22
 }

+ 7
- 1
src/main/java/com/njyunzhi/pet_identity/service/impl/TaCardNoServiceImpl.java Visa fil

@@ -62,7 +62,7 @@ public class TaCardNoServiceImpl extends ServiceImpl<TaCardNoMapper, TaCardNo> i
62 62
 
63 63
         for (int i = 0; i < batchCardParams.getNum(); i += 1) {
64 64
             Integer nextVal = taSequenceMapper.getNextVal("card-" + batchCardParams.getPrefix());
65
-            String cardNo = batchCardParams.getPrefix() + StringUtils.lpad(nextVal.toString(), "0", 5);
65
+            String cardNo = getFormatNo(batchCardParams.getPrefix(), nextVal);
66 66
             MiniAppScene scene = new MiniAppScene().setPage(cardpath).setQueryStr("cardno=" + StringUtils.urlEncode(cardNo));
67 67
 
68 68
             // 生成小程序码
@@ -82,4 +82,10 @@ public class TaCardNoServiceImpl extends ServiceImpl<TaCardNoMapper, TaCardNo> i
82 82
             }
83 83
         }
84 84
     }
85
+
86
+    @Override
87
+    public String getFormatNo(String prefix, Integer num) {
88
+        int maxLen = 5;
89
+        return String.format("%s%s", prefix, StringUtils.lpad(num.toString(), "0", maxLen));
90
+    }
85 91
 }