jinlin
2024-03-21 c3a5d85d389276fc2cc846b640dcdbf448f15508
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.zt.life.modules.mainPart.sysPictureBase.controller;
 
 
import com.zt.common.annotation.LogOperation;
import com.zt.common.constant.Constant;
import com.zt.common.annotation.QueryParam;
import com.zt.common.db.query.QueryFilter;
import com.zt.common.servlet.Result;
import com.zt.common.validator.AssertUtils;
import com.zt.life.modules.mainPart.sysPictureBase.model.SysPictureBase;
import com.zt.life.modules.mainPart.sysPictureBase.service.SysPictureBaseService;
import com.zt.life.sys.service.SysOssConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
 
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
 
 
/**
 * sys_picture_base
 *
 * @author zt generator
 * @since 1.0.0 2024-02-27
 */
@RestController
@RequestMapping("/sysPictureBase/")
@Api(tags = "sys_picture_base")
public class SysPictureBaseController {
    @Autowired
    private SysPictureBaseService sysPictureBaseService;
 
    @Autowired
    private SysOssConfigService sysOssConfigService;
    @Value("${zt.oss.local-path}")
    private String localPath;
 
    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.Q.ORDER_FIELD, value = Constant.QV.ORDER_FIELD, dataType = Constant.QT.STRING),
            @ApiImplicitParam(name = Constant.Q.ORDER, value = Constant.QV.ORDER, dataType = Constant.QT.STRING),
            @ApiImplicitParam(name = "productType", value = "检索关键字", dataType = Constant.QT.STRING, format = "CONTENT_TYPE^LK"), @ApiImplicitParam(name = "systemMark", value = "系统标识", dataType = Constant.QT.STRING, format = "SYSTEM_MARK^LK")})
    public Result<List<SysPictureBase>> page(@ApiIgnore @QueryParam QueryFilter queryFilter) {
 
        return Result.ok(sysPictureBaseService.page(queryFilter));
    }
 
    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<SysPictureBase> get(@PathVariable("id") Long id) {
        SysPictureBase data = sysPictureBaseService.get(id);
        return Result.ok(data);
    }
 
    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody MultipartFile file, Long id, Integer isDefault,
                       String name, String contentType, Integer productType,  String remark) {
        SysPictureBase sysPictureBase;
        if (id != 0) {
            sysPictureBase = sysPictureBaseService.get(id);
            sysPictureBase.setIsDefault(isDefault);
            sysPictureBase.setName(name);
            sysPictureBase.setContentType(contentType);
            sysPictureBase.setProductType(productType);
            sysPictureBase.setRemark(remark);
            sysPictureBaseService.update(sysPictureBase);
        } else {
            sysPictureBase = new SysPictureBase();
            sysPictureBase.setIsDefault(isDefault);
            sysPictureBase.setName(name);
            sysPictureBase.setContentType(contentType);
            sysPictureBase.setProductType(productType);
            sysPictureBase.setRemark(remark);
            sysPictureBaseService.insert(sysPictureBase);
        }
        if (sysPictureBase.getIsDefault()==1){
            sysPictureBaseService.updateByDefault(sysPictureBase.getId(),sysPictureBase.getProductType());
            sysPictureBaseService.updateProdeuctImg(sysPictureBase.getId(),sysPictureBase.getProductType());
        }
        if (file != null) {
            BufferedImage bufferedImage = null;
            try {
                String fileName = file.getOriginalFilename();
                String[] arr = fileName.split("\\.");
                String suffixName = arr[arr.length - 1].toLowerCase();
 
                bufferedImage = ImageIO.read(file.getInputStream());
                // 宽度
                int width = bufferedImage.getWidth();
                sysPictureBase.setWidth(width);
                // 高度
                int height = bufferedImage.getHeight();
                sysPictureBase.setHeight(height);
                sysPictureBaseService.update(sysPictureBase);
                String tempUploadDir = localPath + "/product_img/";
                File dir = new File(tempUploadDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                ImageIO.write(bufferedImage, suffixName, new File(tempUploadDir + sysPictureBase.getId().toString()));
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        }
        return Result.ok();
    }
 
    @RequestMapping("/getProductImg")
    public void getProductImg(HttpServletResponse response, Long id) {
        try {
            String tempUploadDir = localPath + "/product_img/" + id;
            File file = new File(tempUploadDir);
            //读取指定路径下面的文件
            InputStream in = new FileInputStream(file);
 
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            //创建存放文件内容的数组
            byte[] buff = new byte[1024];
            //所读取的内容使用n来接收
            int n;
            //当没有读
            //取完时,继续读取,循环
            while ((n = in.read(buff)) != -1) {
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff, 0, n);
            }
            //强制将缓存区的数据进行输出
            outputStream.flush();
            //关流
            outputStream.close();
            in.close();
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        sysPictureBaseService.delete(ids);
 
        return Result.ok();
    }
 
}