StorageConfigController.java
5.7 KB
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
package com.viontech.storage.controller;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.viontech.storage.entity.Context;
import com.viontech.storage.entity.Generator;
import com.viontech.storage.entity.Message;
import com.viontech.storage.model.StorageConfig;
import com.viontech.storage.service.StorageConfigService;
import com.viontech.storage.vo.StorageConfigVo;
import lombok.extern.slf4j.Slf4j;
import org.h2.util.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* .
*
* @author 谢明辉
* @date 2021/11/9
*/
@RestController
@RequestMapping("/storageConfigs")
@Slf4j
public class StorageConfigController {
@Resource
private StorageConfigService storageConfigService;
@GetMapping("/{id}")
public Message<StorageConfig> getById(@PathVariable("id") Long id) {
StorageConfig byId = storageConfigService.getById(id);
StorageConfigVo copy = StorageConfigVo.copy(byId);
try {
byte[] bytes = new Generator(id).build().getBytes(Charset.forName("GBK"));
copy.setConfig(bytes);
} catch (Exception e) {
log.info("", e);
}
return Message.success(copy);
}
@GetMapping
public Message<List<StorageConfig>> listAll(StorageConfigVo vo, @RequestParam(required = false, defaultValue = "true") Boolean withoutConfig) {
LambdaQueryChainWrapper<StorageConfig> query = storageConfigService.lambdaQuery();
if (!StringUtils.isNullOrEmpty(vo.getNameLike())) {
query.like(StorageConfig::getName, vo.getNameLike());
}
if (vo.getType() != null) {
query.eq(StorageConfig::getType, vo.getType());
}
List<StorageConfig> list = query.list();
Stream<StorageConfigVo> stream = list.stream().map(StorageConfigVo::copy);
if (withoutConfig) {
stream = stream.peek(x -> x.setConfig(null));
}
List<StorageConfig> collect = stream.collect(Collectors.toList());
return Message.success(collect);
}
@PostMapping
public Message<StorageConfig> add(@RequestBody StorageConfigVo storageConfigVo) {
Assert.hasText(storageConfigVo.getName(), "name不能为空");
Long count = storageConfigService.lambdaQuery().eq(StorageConfig::getName, storageConfigVo.getName())
.count();
Assert.isTrue(count == 0, "已存在同名存储配置");
List<Context> contexts = storageConfigVo.getContexts();
if (CollectionUtil.isNotEmpty(contexts)) {
for (Context item : contexts) {
Assert.notNull(item.getOrder(), "order 不能为空");
Assert.notNull(item.getPicConfigId(), "没有关联图片合成");
Assert.notNull(item.getDataTypes(), "没有关联数据类型");
}
storageConfigVo.setContext(JSONUtil.toJsonStr(contexts));
}
boolean save = storageConfigService.save(storageConfigVo);
return Message.success(storageConfigVo);
}
@PutMapping("/{id}")
public Message<StorageConfig> updateById(@PathVariable Long id, @RequestBody StorageConfigVo storageConfigVo) {
List<Context> contexts = storageConfigVo.getContexts();
if (contexts != null) {
storageConfigVo.setContext(JSONUtil.toJsonStr(contexts));
}
storageConfigVo.setId(id);
boolean b = storageConfigService.updateById(storageConfigVo);
return Message.success(storageConfigVo);
}
@DeleteMapping("/{id}")
public Message<StorageConfig> deleteById(@PathVariable Long id) {
boolean b = storageConfigService.removeById(id);
return Message.success();
}
@GetMapping("/build/{id}")
public Message<String> buildConfig(@PathVariable Long id) {
String build = new Generator(id).build();
return Message.success(build);
}
@PostMapping("/upload")
public Message<Object> upload(Long id, MultipartFile file) throws IOException {
FastByteArrayOutputStream read = IoUtil.read(file.getInputStream());
byte[] bytes = read.toByteArray();
Document document = XmlUtil.readXML(new ByteArrayInputStream(bytes));
StorageConfig byId = storageConfigService.getById(id).setContext(null).setType(1).setConfig(bytes);
storageConfigService.updateById(byId);
return Message.success();
}
@GetMapping("/download")
public void download(Long id, HttpServletResponse response) throws IOException {
StorageConfig item = storageConfigService.getById(id);
byte[] bytes;
if (item.getType() == 1) {
bytes = item.getConfig();
} else {
bytes = new Generator(id).build().getBytes(Charset.forName("GBK"));
}
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(item.getName() + ".xml", "utf-8"));
response.setCharacterEncoding("GBK");
IoUtil.write(response.getOutputStream(), false, bytes);
}
}