zhou zhou
13 小时以前 25f0001a7e76d0565fa9de0651f1177b9f61472f
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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.OrderPrintTemplate;
import com.vincent.rsf.server.manager.mapper.OrderPrintTemplateMapper;
import com.vincent.rsf.server.manager.service.OrderPrintTemplateService;
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("orderPrintTemplateService")
public class OrderPrintTemplateServiceImpl
        extends ServiceImpl<OrderPrintTemplateMapper, OrderPrintTemplate>
        implements OrderPrintTemplateService {
 
    private static final Set<String> SUPPORTED_ELEMENT_TYPES = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("text", "barcode", "qrcode", "image", "line", "rect", "table"))
    );
 
    private static final Set<String> SUPPORTED_TEMPLATE_TYPES = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("in", "out"))
    );
 
    private static final Set<String> SUPPORTED_DOCUMENT_PAPER_SIZES = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("A4", "A5", "A6", "LETTER"))
    );
 
    private static final Set<String> SUPPORTED_DOCUMENT_ORIENTATIONS = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("portrait", "landscape"))
    );
 
    private static final Set<String> SUPPORTED_DOCUMENT_ALIGNMENTS = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("left", "center", "right"))
    );
 
    private static final Set<String> SUPPORTED_DOCUMENT_LOGO_POSITIONS = Collections.unmodifiableSet(
            new LinkedHashSet<>(Arrays.asList("left", "right"))
    );
 
    @Override
    public List<OrderPrintTemplate> listCurrentTenantTemplates(String type) {
        String normalizedType = normalizeTemplateType(type);
        List<OrderPrintTemplate> templates = this.list(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, normalizedType)
                .orderByDesc(OrderPrintTemplate::getIsDefault)
                .orderByDesc(OrderPrintTemplate::getUpdateTime)
                .orderByDesc(OrderPrintTemplate::getCreateTime)
        );
        return templates == null ? new ArrayList<>() : templates;
    }
 
    @Override
    public OrderPrintTemplate getCurrentTenantTemplate(Long id) {
        if (id == null) {
            throw new CoolException("模板ID不能为空");
        }
        OrderPrintTemplate template = this.getById(id);
        if (template == null) {
            throw new CoolException("模板不存在或已被删除");
        }
        normalizeTemplateType(template.getType());
        return template;
    }
 
    @Override
    public OrderPrintTemplate getCurrentTenantDefaultTemplate(String type) {
        String normalizedType = normalizeTemplateType(type);
        OrderPrintTemplate template = this.getOne(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, normalizedType)
                .eq(OrderPrintTemplate::getStatus, 1)
                .eq(OrderPrintTemplate::getIsDefault, 1)
                .orderByDesc(OrderPrintTemplate::getUpdateTime)
                .last("limit 1")
        );
        if (template != null) {
            return template;
        }
        return this.getOne(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, normalizedType)
                .eq(OrderPrintTemplate::getStatus, 1)
                .orderByDesc(OrderPrintTemplate::getUpdateTime)
                .orderByDesc(OrderPrintTemplate::getCreateTime)
                .last("limit 1")
        );
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderPrintTemplate saveTemplate(OrderPrintTemplate template) {
        OrderPrintTemplate normalized = prepareTemplateForSave(template, false);
        long currentCount = this.count(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, normalized.getType())
        );
        boolean shouldDefault = Objects.equals(normalized.getIsDefault(), 1) || currentCount == 0;
        normalized.setIsDefault(shouldDefault ? 1 : 0);
        if (shouldDefault) {
            clearCurrentTypeDefaults(normalized.getType());
        }
        if (!this.save(normalized)) {
            throw new CoolException("模板保存失败");
        }
        return this.getCurrentTenantTemplate(normalized.getId());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderPrintTemplate updateTemplate(OrderPrintTemplate template) {
        if (template == null || template.getId() == null) {
            throw new CoolException("模板ID不能为空");
        }
        OrderPrintTemplate existing = getCurrentTenantTemplate(template.getId());
        OrderPrintTemplate normalized = prepareTemplateForSave(template, true);
        normalized.setType(existing.getType());
        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) {
            clearCurrentTypeDefaults(existing.getType());
        } else if (Objects.equals(existing.getIsDefault(), 1)) {
            normalized.setIsDefault(1);
        }
        if (!this.updateById(normalized)) {
            throw new CoolException("模板更新失败");
        }
        ensureOneDefaultTemplate(existing.getType());
        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<OrderPrintTemplate> templates = this.listByIds(ids);
        if (templates == null || templates.isEmpty()) {
            return true;
        }
        Set<String> affectedTypes = new LinkedHashSet<>();
        boolean removedDefault = false;
        for (OrderPrintTemplate template : templates) {
            if (template == null) {
                continue;
            }
            affectedTypes.add(normalizeTemplateType(template.getType()));
            if (Objects.equals(template.getIsDefault(), 1)) {
                removedDefault = true;
            }
        }
        if (!this.removeByIds(ids)) {
            throw new CoolException("模板删除失败");
        }
        if (removedDefault) {
            for (String type : affectedTypes) {
                ensureOneDefaultTemplate(type);
            }
        }
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setDefaultTemplate(Long id) {
        OrderPrintTemplate template = getCurrentTenantTemplate(id);
        String type = normalizeTemplateType(template.getType());
        clearCurrentTypeDefaults(type);
        boolean updated = this.update(new LambdaUpdateWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getId, template.getId())
                .set(OrderPrintTemplate::getIsDefault, 1)
                .set(OrderPrintTemplate::getUpdateBy, resolveCurrentUserId())
                .set(OrderPrintTemplate::getUpdateTime, new Date())
        );
        if (!updated) {
            throw new CoolException("默认模板设置失败");
        }
        return true;
    }
 
    private OrderPrintTemplate prepareTemplateForSave(OrderPrintTemplate template, boolean updating) {
        if (template == null) {
            throw new CoolException("模板参数不能为空");
        }
        Long currentTenantId = resolveCurrentTenantId();
        Long currentUserId = resolveCurrentUserId();
        if (currentTenantId == null) {
            throw new CoolException("当前租户信息缺失");
        }
        String type = normalizeTemplateType(template.getType());
        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(type, code, updating ? template.getId() : null);
 
        Date now = new Date();
        template.setTenantId(currentTenantId)
                .setType(type)
                .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 type, String code, Long excludeId) {
        long duplicateCount = this.count(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, type)
                .eq(OrderPrintTemplate::getCode, code)
                .ne(excludeId != null, OrderPrintTemplate::getId, excludeId)
        );
        if (duplicateCount > 0) {
            throw new CoolException("模板编码已存在,请更换后重试");
        }
    }
 
    private void clearCurrentTypeDefaults(String type) {
        this.update(new LambdaUpdateWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, type)
                .eq(OrderPrintTemplate::getIsDefault, 1)
                .set(OrderPrintTemplate::getIsDefault, 0)
                .set(OrderPrintTemplate::getUpdateBy, resolveCurrentUserId())
                .set(OrderPrintTemplate::getUpdateTime, new Date())
        );
    }
 
    private void ensureOneDefaultTemplate(String type) {
        long defaultCount = this.count(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, type)
                .eq(OrderPrintTemplate::getIsDefault, 1)
        );
        if (defaultCount > 0) {
            return;
        }
        OrderPrintTemplate newest = this.getOne(new LambdaQueryWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getType, type)
                .orderByDesc(OrderPrintTemplate::getUpdateTime)
                .orderByDesc(OrderPrintTemplate::getCreateTime)
                .last("limit 1")
        );
        if (newest == null) {
            return;
        }
        this.update(new LambdaUpdateWrapper<OrderPrintTemplate>()
                .eq(OrderPrintTemplate::getId, newest.getId())
                .set(OrderPrintTemplate::getIsDefault, 1)
                .set(OrderPrintTemplate::getUpdateBy, resolveCurrentUserId())
                .set(OrderPrintTemplate::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("模板版本不能为空");
        }
        if (isDocumentSchema(root)) {
            validateDocumentSchema(root);
            return;
        }
        validateLegacyCanvasSchema(root);
    }
 
    private boolean isDocumentSchema(JSONObject root) {
        return "document".equals(normalizeText(root.getString("mode")))
                || root.containsKey("headerFields")
                || root.containsKey("tableColumns")
                || root.containsKey("footerFields");
    }
 
    private void validateDocumentSchema(JSONObject root) {
        String title = normalizeText(root.getString("title"));
        if (title.isEmpty()) {
            throw new CoolException("单据标题不能为空");
        }
 
        JSONObject page = root.getJSONObject("page");
        if (page == null) {
            throw new CoolException("纸张配置不能为空");
        }
 
        String size = normalizeText(page.getString("size"));
        if (!SUPPORTED_DOCUMENT_PAPER_SIZES.contains(size)) {
            throw new CoolException("纸张仅支持 A4、A5、A6 或 LETTER");
        }
 
        String orientation = normalizeText(page.getString("orientation"));
        if (!SUPPORTED_DOCUMENT_ORIENTATIONS.contains(orientation)) {
            throw new CoolException("纸张方向仅支持 portrait 或 landscape");
        }
 
        ensureNumber(page, "marginTop", "上边距");
        ensureNumber(page, "marginRight", "右边距");
        ensureNumber(page, "marginBottom", "下边距");
        ensureNumber(page, "marginLeft", "左边距");
 
        if (root.getBooleanValue("showLogo")) {
            String logoSrc = normalizeText(root.getString("logoSrc"));
            if (logoSrc.isEmpty()) {
                throw new CoolException("启用Logo时必须上传Logo图片");
            }
            String logoPosition = normalizeText(root.getString("logoPosition"));
            if (!logoPosition.isEmpty() && !SUPPORTED_DOCUMENT_LOGO_POSITIONS.contains(logoPosition)) {
                throw new CoolException("Logo位置仅支持 left 或 right");
            }
            getPositiveNumber(root, "logoWidth", "Logo宽度");
        }
 
        validateDocumentFields(root.getJSONArray("headerFields"), "页头字段", false);
        validateDocumentFields(root.getJSONArray("tableColumns"), "明细列", true);
        validateDocumentFields(root.getJSONArray("footerFields"), "页尾字段", false);
 
        if (root.getBooleanValue("showBarcode")
                && normalizeText(root.getString("barcodeField")).isEmpty()) {
            throw new CoolException("启用条码时必须设置条码字段");
        }
    }
 
    private void validateDocumentFields(JSONArray fields, String label, boolean tableSection) {
        if (fields == null) {
            throw new CoolException(label + "不能为空");
        }
        for (int index = 0; index < fields.size(); index++) {
            JSONObject field = fields.getJSONObject(index);
            if (field == null) {
                throw new CoolException(label + "格式不正确");
            }
            if (normalizeText(field.getString("key")).isEmpty()) {
                throw new CoolException(label + "第" + (index + 1) + "项缺少字段标识");
            }
            if (normalizeText(field.getString("label")).isEmpty()) {
                throw new CoolException(label + "第" + (index + 1) + "项缺少字段名称");
            }
            if (tableSection) {
                getPositiveNumber(field, "width", label + "宽度");
                String align = normalizeText(field.getString("align"));
                if (!align.isEmpty() && !SUPPORTED_DOCUMENT_ALIGNMENTS.contains(align)) {
                    throw new CoolException(label + "对齐方式仅支持 left、center 或 right");
                }
            } else if (field.containsKey("span")) {
                getPositiveNumber(field, "span", label + "栅格宽度");
            }
        }
    }
 
    private void validateLegacyCanvasSchema(JSONObject root) {
        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 "image":
                if (normalizeText(element.getString("src")).isEmpty()) {
                    throw new CoolException("图片元素地址不能为空");
                }
                String objectFit = normalizeText(element.getString("objectFit"));
                if (!objectFit.isEmpty() && !Arrays.asList("contain", "cover", "fill").contains(objectFit)) {
                    throw new CoolException("图片元素填充方式仅支持 contain、cover 或 fill");
                }
                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 normalizeTemplateType(String value) {
        String type = normalizeText(value);
        if (!SUPPORTED_TEMPLATE_TYPES.contains(type)) {
            throw new CoolException("模板类型仅支持 in 或 out");
        }
        return type;
    }
 
    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;
    }
}