zhou zhou
17 小时以前 28c6a76ead9b65a0b5861d70f0838ef2a46f5c45
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.vincent.rsf.server.manager.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vincent.rsf.framework.exception.CoolException;
import com.vincent.rsf.server.manager.entity.MatnrPrintTemplate;
import com.vincent.rsf.server.manager.mapper.MatnrPrintTemplateMapper;
import com.vincent.rsf.server.manager.service.MatnrPrintTemplateService;
import com.vincent.rsf.server.system.entity.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
 
@Service("matnrPrintTemplateService")
public class MatnrPrintTemplateServiceImpl
        extends ServiceImpl<MatnrPrintTemplateMapper, MatnrPrintTemplate>
        implements MatnrPrintTemplateService {
 
    private static final Set<String> SUPPORTED_ELEMENT_TYPES = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("text", "barcode", "qrcode", "line", "rect", "table"))
    );
 
    @Override
    public List<MatnrPrintTemplate> listCurrentTenantTemplates() {
        List<MatnrPrintTemplate> templates = this.list(new LambdaQueryWrapper<MatnrPrintTemplate>()
                .orderByDesc(MatnrPrintTemplate::getIsDefault)
                .orderByDesc(MatnrPrintTemplate::getUpdateTime)
                .orderByDesc(MatnrPrintTemplate::getCreateTime)
        );
        return templates == null ? new ArrayList<>() : templates;
    }
 
    @Override
    public MatnrPrintTemplate getCurrentTenantTemplate(Long id) {
        if (id == null) {
            throw new CoolException("模板ID不能为空");
        }
        MatnrPrintTemplate template = this.getById(id);
        if (template == null) {
            throw new CoolException("模板不存在或已被删除");
        }
        return template;
    }
 
    @Override
    public MatnrPrintTemplate getCurrentTenantDefaultTemplate() {
        MatnrPrintTemplate template = this.getOne(new LambdaQueryWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getStatus, 1)
                .eq(MatnrPrintTemplate::getIsDefault, 1)
                .orderByDesc(MatnrPrintTemplate::getUpdateTime)
                .last("limit 1")
        );
        if (template != null) {
            return template;
        }
        return this.getOne(new LambdaQueryWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getStatus, 1)
                .orderByDesc(MatnrPrintTemplate::getUpdateTime)
                .orderByDesc(MatnrPrintTemplate::getCreateTime)
                .last("limit 1")
        );
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MatnrPrintTemplate saveTemplate(MatnrPrintTemplate template) {
        MatnrPrintTemplate normalized = prepareTemplateForSave(template, false);
        long currentCount = this.count();
        boolean shouldDefault = Objects.equals(normalized.getIsDefault(), 1) || currentCount == 0;
        normalized.setIsDefault(shouldDefault ? 1 : 0);
        if (shouldDefault) {
            clearCurrentTenantDefaults();
        }
        if (!this.save(normalized)) {
            throw new CoolException("模板保存失败");
        }
        return this.getCurrentTenantTemplate(normalized.getId());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MatnrPrintTemplate updateTemplate(MatnrPrintTemplate template) {
        if (template == null || template.getId() == null) {
            throw new CoolException("模板ID不能为空");
        }
        MatnrPrintTemplate existing = getCurrentTenantTemplate(template.getId());
        MatnrPrintTemplate normalized = prepareTemplateForSave(template, true);
        normalized.setTenantId(existing.getTenantId());
        normalized.setCreateBy(existing.getCreateBy());
        normalized.setCreateTime(existing.getCreateTime());
        normalized.setDeleted(existing.getDeleted());
        boolean shouldDefault = Objects.equals(normalized.getIsDefault(), 1);
        if (shouldDefault) {
            clearCurrentTenantDefaults();
        } else if (Objects.equals(existing.getIsDefault(), 1)) {
            normalized.setIsDefault(1);
        }
        if (!this.updateById(normalized)) {
            throw new CoolException("模板更新失败");
        }
        ensureOneDefaultTemplate();
        return this.getCurrentTenantTemplate(normalized.getId());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeTemplates(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new CoolException("请选择要删除的模板");
        }
        List<MatnrPrintTemplate> templates = this.listByIds(ids);
        if (templates == null || templates.isEmpty()) {
            return true;
        }
        boolean removedDefault = templates.stream().anyMatch(item -> Objects.equals(item.getIsDefault(), 1));
        if (!this.removeByIds(ids)) {
            throw new CoolException("模板删除失败");
        }
        if (removedDefault) {
            ensureOneDefaultTemplate();
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setDefaultTemplate(Long id) {
        MatnrPrintTemplate template = getCurrentTenantTemplate(id);
        clearCurrentTenantDefaults();
        boolean updated = this.update(new LambdaUpdateWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getId, template.getId())
                .set(MatnrPrintTemplate::getIsDefault, 1)
                .set(MatnrPrintTemplate::getUpdateBy, resolveCurrentUserId())
                .set(MatnrPrintTemplate::getUpdateTime, new Date())
        );
        if (!updated) {
            throw new CoolException("默认模板设置失败");
        }
        return true;
    }
 
    private MatnrPrintTemplate prepareTemplateForSave(MatnrPrintTemplate template, boolean updating) {
        if (template == null) {
            throw new CoolException("模板参数不能为空");
        }
        Long currentTenantId = resolveCurrentTenantId();
        Long currentUserId = resolveCurrentUserId();
        if (currentTenantId == null) {
            throw new CoolException("当前租户信息缺失");
        }
        String name = normalizeText(template.getName());
        String code = normalizeText(template.getCode());
        if (name.isEmpty()) {
            throw new CoolException("模板名称不能为空");
        }
        if (code.isEmpty()) {
            throw new CoolException("模板编码不能为空");
        }
        Map<String, Object> canvasJson = template.getCanvasJson();
        if (canvasJson == null || canvasJson.isEmpty()) {
            throw new CoolException("模板画布不能为空");
        }
        validateCanvasJson(canvasJson);
        ensureTemplateCodeUnique(code, updating ? template.getId() : null);
 
        Date now = new Date();
        template.setTenantId(currentTenantId)
                .setName(name)
                .setCode(code)
                .setStatus(template.getStatus() == null ? 1 : template.getStatus())
                .setIsDefault(Objects.equals(template.getIsDefault(), 1) ? 1 : 0)
                .setMemo(normalizeText(template.getMemo()))
                .setUpdateBy(currentUserId)
                .setUpdateTime(now);
        if (!updating) {
            template.setCreateBy(currentUserId);
            template.setCreateTime(now);
        }
        return template;
    }
 
    private void ensureTemplateCodeUnique(String code, Long excludeId) {
        long duplicateCount = this.count(new LambdaQueryWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getCode, code)
                .ne(excludeId != null, MatnrPrintTemplate::getId, excludeId)
        );
        if (duplicateCount > 0) {
            throw new CoolException("模板编码已存在,请更换后重试");
        }
    }
 
    private void clearCurrentTenantDefaults() {
        this.update(new LambdaUpdateWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getIsDefault, 1)
                .set(MatnrPrintTemplate::getIsDefault, 0)
                .set(MatnrPrintTemplate::getUpdateBy, resolveCurrentUserId())
                .set(MatnrPrintTemplate::getUpdateTime, new Date())
        );
    }
 
    private void ensureOneDefaultTemplate() {
        long defaultCount = this.count(new LambdaQueryWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getIsDefault, 1)
        );
        if (defaultCount > 0) {
            return;
        }
        MatnrPrintTemplate newest = this.getOne(new LambdaQueryWrapper<MatnrPrintTemplate>()
                .orderByDesc(MatnrPrintTemplate::getUpdateTime)
                .orderByDesc(MatnrPrintTemplate::getCreateTime)
                .last("limit 1")
        );
        if (newest == null) {
            return;
        }
        this.update(new LambdaUpdateWrapper<MatnrPrintTemplate>()
                .eq(MatnrPrintTemplate::getId, newest.getId())
                .set(MatnrPrintTemplate::getIsDefault, 1)
                .set(MatnrPrintTemplate::getUpdateBy, resolveCurrentUserId())
                .set(MatnrPrintTemplate::getUpdateTime, new Date())
        );
    }
 
    private void validateCanvasJson(Map<String, Object> canvasJson) {
        JSONObject root = JSONObject.parseObject(JSON.toJSONString(canvasJson));
        if (root == null) {
            throw new CoolException("模板画布格式不正确");
        }
        if (root.getInteger("version") == null) {
            throw new CoolException("模板版本不能为空");
        }
        JSONObject canvas = root.getJSONObject("canvas");
        if (canvas == null) {
            throw new CoolException("模板画布配置不能为空");
        }
        double width = getPositiveNumber(canvas, "width", "画布宽度");
        double height = getPositiveNumber(canvas, "height", "画布高度");
        if (width <= 0 || height <= 0) {
            throw new CoolException("画布尺寸必须大于0");
        }
        String unit = normalizeText(canvas.getString("unit"));
        if (!"mm".equals(unit)) {
            throw new CoolException("画布单位仅支持 mm");
        }
        JSONArray elements = root.getJSONArray("elements");
        if (elements == null) {
            throw new CoolException("模板元素不能为空");
        }
        for (int index = 0; index < elements.size(); index++) {
            JSONObject element = elements.getJSONObject(index);
            if (element == null) {
                throw new CoolException("模板元素格式不正确");
            }
            validateElement(element, index);
        }
    }
 
    private void validateElement(JSONObject element, int index) {
        String type = normalizeText(element.getString("type"));
        if (!SUPPORTED_ELEMENT_TYPES.contains(type)) {
            throw new CoolException("第" + (index + 1) + "个元素类型不支持");
        }
        if (normalizeText(element.getString("id")).isEmpty()) {
            throw new CoolException("第" + (index + 1) + "个元素缺少 ID");
        }
        ensureNumber(element, "x", "元素 X 坐标");
        ensureNumber(element, "y", "元素 Y 坐标");
        if (!"line".equals(type)) {
            getPositiveNumber(element, "w", "元素宽度");
            getPositiveNumber(element, "h", "元素高度");
        } else {
            String direction = normalizeText(element.getString("direction"));
            if (!Arrays.asList("horizontal", "vertical").contains(direction)) {
                throw new CoolException("线条元素方向仅支持 horizontal 或 vertical");
            }
            getPositiveNumber(element, "w", "线条长度");
            getPositiveNumber(element, "h", "线条粗细");
        }
 
        switch (type) {
            case "text":
                String contentMode = normalizeText(element.getString("contentMode"));
                if (!Arrays.asList("static", "template").contains(contentMode)) {
                    throw new CoolException("文本元素内容模式不支持");
                }
                if (normalizeText(element.getString("contentTemplate")).isEmpty()) {
                    throw new CoolException("文本元素内容不能为空");
                }
                break;
            case "barcode":
                if (normalizeText(element.getString("valueTemplate")).isEmpty()) {
                    throw new CoolException("条码元素值模板不能为空");
                }
                String symbology = normalizeText(element.getString("symbology"));
                if (!symbology.isEmpty() && !"CODE128".equals(symbology)) {
                    throw new CoolException("一维码仅支持 CODE128");
                }
                break;
            case "qrcode":
                if (normalizeText(element.getString("valueTemplate")).isEmpty()) {
                    throw new CoolException("二维码元素值模板不能为空");
                }
                break;
            case "table":
                if (element.getJSONArray("columns") == null) {
                    throw new CoolException("表格元素 columns 不能为空");
                }
                if (element.getJSONArray("rows") == null) {
                    throw new CoolException("表格元素 rows 不能为空");
                }
                if (element.getJSONArray("cells") == null) {
                    throw new CoolException("表格元素 cells 不能为空");
                }
                break;
            default:
                break;
        }
    }
 
    private void ensureNumber(JSONObject object, String key, String label) {
        if (object.getBigDecimal(key) == null) {
            throw new CoolException(label + "不能为空");
        }
    }
 
    private double getPositiveNumber(JSONObject object, String key, String label) {
        if (object.getBigDecimal(key) == null) {
            throw new CoolException(label + "不能为空");
        }
        double value = object.getBigDecimal(key).doubleValue();
        if (value <= 0) {
            throw new CoolException(label + "必须大于0");
        }
        return value;
    }
 
    private String normalizeText(String value) {
        return value == null ? "" : value.trim();
    }
 
    private Long resolveCurrentTenantId() {
        User loginUser = getCurrentUser();
        return loginUser == null ? null : loginUser.getTenantId();
    }
 
    private Long resolveCurrentUserId() {
        User loginUser = getCurrentUser();
        return loginUser == null ? null : loginUser.getId();
    }
 
    private User getCurrentUser() {
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.getPrincipal() instanceof User) {
                return (User) authentication.getPrincipal();
            }
        } catch (Exception ignored) {
        }
        return null;
    }
}