张延森 3 years ago
parent
commit
06e33ce411

+ 20
- 0
src/main/java/com/njyunzhi/pet_identity/common/ExcelUtils.java View File

@@ -14,7 +14,9 @@ import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
14 14
 import org.apache.poi.xssf.usermodel.XSSFColor;
15 15
 
16 16
 import javax.servlet.http.HttpServletResponse;
17
+import java.io.ByteArrayOutputStream;
17 18
 import java.io.IOException;
19
+import java.io.OutputStream;
18 20
 import java.util.List;
19 21
 
20 22
 public class ExcelUtils {
@@ -70,6 +72,24 @@ public class ExcelUtils {
70 72
         return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
71 73
     }
72 74
 
75
+    /**
76
+     * 内存生成 Excel
77
+     * @param dataClass
78
+     * @param data
79
+     * @param style
80
+     * @return
81
+     */
82
+    public static byte[] buildExcel(Class dataClass, List data, HorizontalCellStyleStrategy style) {
83
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(128);
84
+        ExcelWriterBuilder builder = EasyExcel.write(outputStream, dataClass);
85
+        if (null == style) {
86
+            style = defaultStyle();
87
+            builder.registerWriteHandler(style);
88
+        }
89
+        builder.autoCloseStream(Boolean.TRUE).sheet("sheet1").doWrite(data);
90
+        return outputStream.toByteArray();
91
+    }
92
+
73 93
     /**
74 94
      * 发送 excel 到客户端
75 95
      * 暂时只支持单 sheet 页

+ 57
- 10
src/main/java/com/njyunzhi/pet_identity/common/ZipUtil.java View File

@@ -1,11 +1,7 @@
1 1
 package com.njyunzhi.pet_identity.common;
2 2
 
3
-import com.alibaba.excel.EasyExcel;
4
-import com.alibaba.excel.write.builder.ExcelWriterBuilder;
5 3
 import com.alibaba.fastjson.JSON;
6 4
 import lombok.Data;
7
-import net.lingala.zip4j.ZipFile;
8
-import net.lingala.zip4j.exception.ZipException;
9 5
 import net.lingala.zip4j.io.outputstream.ZipOutputStream;
10 6
 import net.lingala.zip4j.model.ZipParameters;
11 7
 import org.springframework.beans.factory.annotation.Autowired;
@@ -13,10 +9,6 @@ import org.springframework.stereotype.Component;
13 9
 
14 10
 import javax.servlet.http.HttpServletResponse;
15 11
 import java.io.*;
16
-import java.nio.ByteBuffer;
17
-import java.nio.channels.Channels;
18
-import java.nio.channels.ReadableByteChannel;
19
-import java.nio.channels.WritableByteChannel;
20 12
 import java.time.LocalDateTime;
21 13
 import java.time.ZoneId;
22 14
 import java.time.format.DateTimeFormatter;
@@ -34,7 +26,7 @@ public class ZipUtil {
34 26
         return LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(formatter);
35 27
     }
36 28
 
37
-    private void zipStream(List<RFile> files, OutputStream outputStream) throws IOException {
29
+    private void zipImages(List<RFile> files, OutputStream outputStream) throws IOException {
38 30
         ZipOutputStream zos = new ZipOutputStream(outputStream);
39 31
         ZipParameters zipParameters = new ZipParameters();
40 32
 
@@ -48,6 +40,25 @@ public class ZipUtil {
48 40
         zos.close();
49 41
     }
50 42
 
43
+    private void zipFiles(List<ZFile> files, OutputStream outputStream) throws IOException {
44
+        ZipOutputStream zos = new ZipOutputStream(outputStream);
45
+        ZipParameters zipParameters = new ZipParameters();
46
+
47
+        for (ZFile f : files) {
48
+            zipParameters.setFileNameInZip(f.getName());
49
+            zos.putNextEntry(zipParameters);
50
+            zos.write(f.getData());
51
+            zos.closeEntry();
52
+        }
53
+        zos.close();
54
+    }
55
+
56
+    /**
57
+     * 打包远程文件,并导出
58
+     * @param response
59
+     * @param files
60
+     * @throws IOException
61
+     */
51 62
     public void flush(HttpServletResponse response, List<RFile> files) throws IOException {
52 63
         try {
53 64
             if (files == null || files.size() < 1) {
@@ -58,7 +69,37 @@ public class ZipUtil {
58 69
             response.setContentType("application/zip");
59 70
             response.setCharacterEncoding("utf-8");
60 71
             response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName));
61
-            zipStream(files, response.getOutputStream());
72
+            zipImages(files, response.getOutputStream());
73
+            response.flushBuffer();
74
+        } catch (Exception e) {
75
+            e.printStackTrace();
76
+
77
+            // 重置response
78
+            response.reset();
79
+            response.setContentType("application/json");
80
+            response.setCharacterEncoding("utf-8");
81
+            ResponseBean res = ResponseBean.error(e.getMessage(), ResponseBean.ERROR_UNAVAILABLE);
82
+            response.getWriter().println(JSON.toJSONString(res));
83
+        }
84
+    }
85
+
86
+    /**
87
+     * 打包文件,并导出
88
+     * @param response
89
+     * @param files
90
+     * @throws IOException
91
+     */
92
+    public void flushMixFile(HttpServletResponse response, List<ZFile> files) throws IOException {
93
+        try {
94
+            if (files == null || files.size() < 1) {
95
+                throw new Exception("导出内容为空");
96
+            }
97
+
98
+            String fileName = today() + ".zip";
99
+            response.setContentType("application/zip");
100
+            response.setCharacterEncoding("utf-8");
101
+            response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName));
102
+            zipFiles(files, response.getOutputStream());
62 103
             response.flushBuffer();
63 104
         } catch (Exception e) {
64 105
             e.printStackTrace();
@@ -77,4 +118,10 @@ public class ZipUtil {
77 118
         String url;
78 119
         String name;
79 120
     }
121
+
122
+    @Data
123
+    public static class ZFile {
124
+        byte[] data;
125
+        String name;
126
+    }
80 127
 }

+ 61
- 4
src/main/java/com/njyunzhi/pet_identity/controller/TaApplicationController.java View File

@@ -3,10 +3,7 @@ package com.njyunzhi.pet_identity.controller;
3 3
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
4 4
 import com.baomidou.mybatisplus.core.metadata.IPage;
5 5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6
-import com.njyunzhi.pet_identity.common.BaseController;
7
-import com.njyunzhi.pet_identity.common.Constants;
8
-import com.njyunzhi.pet_identity.common.ResponseBean;
9
-import com.njyunzhi.pet_identity.common.StringUtils;
6
+import com.njyunzhi.pet_identity.common.*;
10 7
 import com.njyunzhi.pet_identity.entity.*;
11 8
 import com.njyunzhi.pet_identity.service.ITaCardNoService;
12 9
 import com.njyunzhi.pet_identity.service.ITaOrderService;
@@ -27,8 +24,10 @@ import org.springframework.web.bind.annotation.RequestParam;
27 24
 import com.njyunzhi.pet_identity.service.ITaApplicationService;
28 25
 import org.springframework.web.bind.annotation.RestController;
29 26
 
27
+import javax.servlet.http.HttpServletResponse;
30 28
 import java.time.LocalDateTime;
31 29
 import java.util.ArrayList;
30
+import java.util.List;
32 31
 
33 32
 /**
34 33
  * <p>
@@ -58,6 +57,12 @@ public class TaApplicationController extends BaseController {
58 57
     @Autowired
59 58
     public ITaCardNoService iTaCardNoService;
60 59
 
60
+    @Autowired
61
+    HttpUtils httpUtils;
62
+
63
+    @Autowired
64
+    ZipUtil zipUtil;
65
+
61 66
     /**
62 67
      * 分页查询列表
63 68
      * @param pageNum
@@ -140,6 +145,58 @@ public class TaApplicationController extends BaseController {
140 145
         return ResponseBean.success(result);
141 146
     }
142 147
 
148
+    @RequestMapping(value="/admin/application/card/export",method= RequestMethod.POST)
149
+    @ApiOperation(value="发证导出", notes = "发证导出", httpMethod = "POST", response = ResponseBean.class)
150
+    public ResponseBean exportCard(@ApiParam("证件号列表") @RequestBody List<String> cardNoList,
151
+                                   HttpServletResponse response) throws Exception {
152
+        List<TaPetIdentity> taPetIdentityList = iTaPetIdentityService.getListByCard(cardNoList);
153
+
154
+        if (null == taPetIdentityList || taPetIdentityList.size() < 1) {
155
+            return ResponseBean.error("未找到导出数据");
156
+        }
157
+
158
+        List<ZipUtil.ZFile> zipFiles = new ArrayList<>();
159
+
160
+        // 批量生成图片
161
+        for (TaPetIdentity item : taPetIdentityList) {
162
+            // 如果是企业
163
+            if (!StringUtils.isEmpty(item.getOrgName())) {
164
+                item.setPersonName(item.getOrgName());
165
+            }
166
+
167
+            // 下载图片
168
+            if (!StringUtils.isEmpty(item.getPetImg1())) {
169
+                ZipUtil.ZFile f = buildImage(item.getPetImg1());
170
+                zipFiles.add(f);
171
+                item.setPetImg1(f.getName());
172
+            }
173
+            if (!StringUtils.isEmpty(item.getQrImage())) {
174
+                ZipUtil.ZFile f = buildImage(item.getQrImage());
175
+                zipFiles.add(f);
176
+                item.setQrImage(f.getName());
177
+            }
178
+        }
179
+
180
+        // 生成 excel 文档
181
+        byte[] excelData = ExcelUtils.buildExcel(TaPetIdentity.class, taPetIdentityList, null);
182
+        ZipUtil.ZFile f = new ZipUtil.ZFile();
183
+        String fname = StringUtils.random(16);
184
+        f.setName(fname + ".xlsx");
185
+        f.setData(excelData);
186
+        zipFiles.add(f);
187
+
188
+        zipUtil.flushMixFile(response, zipFiles);
189
+        return null;
190
+    }
191
+
192
+    private ZipUtil.ZFile buildImage(String imgURL) {
193
+        ZipUtil.ZFile f = new ZipUtil.ZFile();
194
+        String fname = StringUtils.random(16);
195
+        f.setName(fname + ".jpg");
196
+        f.setData(httpUtils.download(imgURL));
197
+        return f;
198
+    }
199
+
143 200
 
144 201
     /**
145 202
      * 分页查询列表

+ 39
- 0
src/main/java/com/njyunzhi/pet_identity/entity/TaPetIdentity.java View File

@@ -1,7 +1,11 @@
1 1
 package com.njyunzhi.pet_identity.entity;
2 2
 
3
+import com.alibaba.excel.annotation.ExcelIgnore;
4
+import com.alibaba.excel.annotation.ExcelProperty;
3 5
 import com.baomidou.mybatisplus.annotation.IdType;
4 6
 import java.time.LocalDateTime;
7
+
8
+import com.baomidou.mybatisplus.annotation.TableField;
5 9
 import com.baomidou.mybatisplus.annotation.TableId;
6 10
 import java.io.Serializable;
7 11
 import io.swagger.annotations.ApiModel;
@@ -24,70 +28,105 @@ import lombok.experimental.Accessors;
24 28
 @ApiModel(value="TaPetIdentity对象", description="宠物证件")
25 29
 public class TaPetIdentity implements Serializable {
26 30
 
31
+    @ExcelIgnore
27 32
     private static final long serialVersionUID = 1L;
28 33
 
34
+    @ExcelIgnore
29 35
     @ApiModelProperty(value = "证件ID")
30 36
     @TableId(value = "card_id", type = IdType.UUID)
31 37
     private String cardId;
32 38
 
39
+    @ExcelProperty(value = "证件号码", index = 6)
33 40
     @ApiModelProperty(value = "证件号码")
34 41
     private String cardNo;
35 42
 
43
+    @ExcelIgnore
36 44
     @ApiModelProperty(value = "属主ID")
37 45
     private String personId;
38 46
 
47
+    @ExcelProperty(value = "犬主", index = 1)
39 48
     @ApiModelProperty(value = "属主姓名")
40 49
     private String personName;
41 50
 
51
+    @ExcelIgnore
42 52
     @ApiModelProperty(value = "属主手机")
43 53
     private String phone;
44 54
 
55
+    @ExcelIgnore
45 56
     @ApiModelProperty(value = "宠物ID")
46 57
     private String petId;
47 58
 
59
+    @ExcelProperty(value = "犬名", index = 2)
48 60
     @ApiModelProperty(value = "宠物名称")
49 61
     private String petName;
50 62
 
63
+    @ExcelIgnore
51 64
     @ApiModelProperty(value = "宠物性别")
52 65
     private Integer petSex;
53 66
 
67
+    @ExcelProperty(value = "毛色", index = 4)
54 68
     @ApiModelProperty(value = "毛色")
55 69
     private String petColor;
56 70
 
71
+    @ExcelProperty(value = "犬种", index = 3)
57 72
     @ApiModelProperty(value = "宠物类别")
58 73
     private String petType;
59 74
 
75
+    @ExcelProperty(value = "狗照片", index = 7)
60 76
     @ApiModelProperty(value = "宠物照片")
61 77
     private String petImg1;
62 78
 
79
+    @ExcelIgnore
63 80
     @ApiModelProperty(value = "宠物照片")
64 81
     private String petImg2;
65 82
 
83
+    @ExcelProperty(value = "地址", index = 5)
66 84
     @ApiModelProperty(value = "详细地址")
67 85
     private String address;
68 86
 
87
+    @ExcelIgnore
69 88
     @ApiModelProperty(value = "起始期;格式YYYY-MM-DD")
70 89
     private String startDate;
71 90
 
91
+    @ExcelIgnore
72 92
     @ApiModelProperty(value = "有效期;格式YYYY-MM-DD")
73 93
     private String expireDate;
74 94
 
95
+    @ExcelIgnore
75 96
     @ApiModelProperty(value = "状态;1正常,2挂失,3过期")
76 97
     private Integer status;
77 98
 
99
+    @ExcelIgnore
78 100
     @ApiModelProperty(value = "创建日期")
79 101
     private LocalDateTime createDate;
80 102
 
103
+    @ExcelIgnore
81 104
     @ApiModelProperty(value = "是否企业",notes = "")
82 105
     private Boolean isOrg ;
106
+
107
+    @ExcelIgnore
83 108
     @ApiModelProperty(value = "企业ID",notes = "")
84 109
     private String orgId ;
110
+
111
+    @ExcelIgnore
85 112
     @ApiModelProperty(value = "企业名称",notes = "")
86 113
     private String orgName ;
114
+
115
+    @ExcelIgnore
87 116
     @ApiModelProperty(value = "营业执照",notes = "")
88 117
     private String orgLicense ;
118
+
119
+    @ExcelIgnore
89 120
     @ApiModelProperty(value = "伤人信息",notes = "")
90 121
     private String criminalInfo ;
122
+
123
+    @ExcelIgnore
91 124
     @ApiModelProperty(value = "处罚信息",notes = "")
92 125
     private String penaltyInfo ;
126
+
127
+    @ExcelProperty(value = "小程序码", index = 8)
128
+    @TableField(exist = false)
129
+    @ApiModelProperty(value = "二维码",notes = "")
130
+    private String qrImage ;
131
+
93 132
 }

+ 5
- 0
src/main/java/com/njyunzhi/pet_identity/mapper/TaPetIdentityMapper.java View File

@@ -3,8 +3,11 @@ package com.njyunzhi.pet_identity.mapper;
3 3
 import com.njyunzhi.pet_identity.entity.TaPetIdentity;
4 4
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5 5
 import org.apache.ibatis.annotations.Mapper;
6
+import org.apache.ibatis.annotations.Param;
6 7
 import org.apache.ibatis.annotations.Select;
7 8
 
9
+import java.util.List;
10
+
8 11
 /**
9 12
  * <p>
10 13
  * 宠物证件 Mapper 接口
@@ -18,4 +21,6 @@ public interface TaPetIdentityMapper extends BaseMapper<TaPetIdentity> {
18 21
 
19 22
     @Select("SELECT COUNT(*) FROM ta_pet_identity WHERE status > -1")
20 23
     int countNotDelete();
24
+
25
+    List<TaPetIdentity> getListByCard(@Param("cardNoList") List<String> cardNoList);
21 26
 }

+ 4
- 0
src/main/java/com/njyunzhi/pet_identity/service/ITaPetIdentityService.java View File

@@ -4,6 +4,8 @@ import com.njyunzhi.pet_identity.entity.TaApplication;
4 4
 import com.njyunzhi.pet_identity.entity.TaPetIdentity;
5 5
 import com.njyunzhi.pet_identity.vo.MakeCardParam;
6 6
 
7
+import java.util.List;
8
+
7 9
 /**
8 10
  * <p>
9 11
  * 宠物证件 服务类
@@ -23,4 +25,6 @@ public interface ITaPetIdentityService extends IBaseService<TaPetIdentity> {
23 25
     TaPetIdentity getByPet(String petId, String personId);
24 26
 
25 27
     int countNotDelete();
28
+
29
+    List<TaPetIdentity> getListByCard(List<String> cardNoList);
26 30
 }

+ 6
- 0
src/main/java/com/njyunzhi/pet_identity/service/impl/TaPetIdentityServiceImpl.java View File

@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
16 16
 
17 17
 import java.time.LocalDateTime;
18 18
 import java.time.ZoneId;
19
+import java.util.List;
19 20
 
20 21
 /**
21 22
  * <p>
@@ -117,4 +118,9 @@ public class TaPetIdentityServiceImpl extends BaseServiceImpl<TaPetIdentityMappe
117 118
     public int countNotDelete() {
118 119
         return baseMapper.countNotDelete();
119 120
     }
121
+
122
+    @Override
123
+    public List<TaPetIdentity> getListByCard(List<String> cardNoList) {
124
+        return baseMapper.getListByCard(cardNoList);
125
+    }
120 126
 }

+ 10
- 0
src/main/resources/mapper/TaPetIdentityMapper.xml View File

@@ -2,4 +2,14 @@
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3
 <mapper namespace="com.njyunzhi.pet_identity.mapper.TaPetIdentityMapper">
4 4
 
5
+    <select id="getListByCard" resultType="com.njyunzhi.pet_identity.entity.TaPetIdentity">
6
+        SELECT t.*, s.qr_image
7
+        FROM ta_pet_identity t
8
+            INNER JOIN ta_card_no s ON t.card_no = s.card_no
9
+        WHERE t.status &gt; -1
10
+            AND t.card_no IN
11
+        <foreach item="item" collection="cardNoList" separator="," open="(" close=")" index="">
12
+            #{item}
13
+        </foreach>
14
+    </select>
5 15
 </mapper>