自动化立体仓库 - WMS系统
#
luxiaotao1123
2020-07-03 2580ade8b1369051fd32089fb4d95d427ea485e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.zy.common.web;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.common.R;
import com.zy.asrs.entity.MatCode;
import com.zy.asrs.service.MatCodeService;
import com.zy.common.config.AdminInterceptor;
import com.zy.common.utils.BarcodeUtils;
import com.zy.common.utils.QrCode;
import com.zy.common.utils.excel.matcode.MatCodeExcel;
import com.zy.common.utils.excel.matcode.MatCodeExcelListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
 
/**
 * todo
 * Created by vincent on 2019-11-25
 */
@RestController
@RequestMapping("file/")
public class FileController {
 
    @Autowired
    private MatCodeService matCodeService;
    /**
     * 文件上传
     */
    @PostMapping("upload")
    @ResponseBody
    public R upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), MatCodeExcel.class, new MatCodeExcelListener()).sheet().doRead();
        return R.ok();
    }
 
    /**
     * 文件下载(失败了会返回一个有部分数据的Excel)
     */
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        List<MatCode> list = matCodeService.selectList(new EntityWrapper<>());
 
        List<MatCodeExcel> excels = new ArrayList<>();
        for (MatCode matCode : list) {
            MatCodeExcel excel = new MatCodeExcel();
            excel.setMatNo(matCode.getMatNo());
            excel.setBarcode(matCode.getBarcode());
            excel.setMatName(matCode.getMatName());
            excel.setStr1(matCode.getStr1());
            excels.add(excel);
        }
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), MatCodeExcel.class).sheet("模板").doWrite(excels);
    }
 
    @RequestMapping(value = "/barcode/qrcode/auth")
//    @ManagerAuth(memo = "物料编码条形码获取(type:1(条形码);2(二维码)")
    public R matCodeBarcode(@RequestParam(defaultValue = "1") Integer type
                            , @RequestParam String param
                            , @RequestParam(required = false) Integer width
                            , @RequestParam(required = false) Integer height
                            , HttpServletResponse response) throws Exception {
        AdminInterceptor.cors(response);
        if (Cools.isEmpty(param)){
            return R.parse(BaseRes.EMPTY);
        }
        BufferedImage img;
        if (type == 1) {
            img = BarcodeUtils.encode(param, width, height);
        } else {
            img = QrCode.createImg(param, width, height);
        }
        if (!ImageIO.write(img, "jpg", response.getOutputStream())) {
            throw new IOException("Could not write an image of format jpg");
        }
        response.getOutputStream().flush();
        response.getOutputStream().close();
        return R.ok();
    }
 
}