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
package com.ruoyi.basic.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.SealConfigExport;
import com.ruoyi.basic.mapper.SealConfigMapper;
import com.ruoyi.basic.pojo.SealConfig;
import com.ruoyi.basic.service.SealConfigService;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.QueryWrappers;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 印章管理(报告用印) 服务实现
 */
@Service
public class SealConfigServiceImpl extends ServiceImpl<SealConfigMapper, SealConfig> implements SealConfigService {
 
    @Resource
    private SealConfigMapper sealConfigMapper;
 
    private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    @Override
    public IPage<SealConfig> pageSeal(Page page, SealConfig query) {
        QueryWrapper<SealConfig> wrapper = QueryWrappers.queryWrappers(query);
        if (wrapper == null) {
            wrapper = Wrappers.query();
        }
        wrapper.orderByDesc("create_time").orderByAsc("code");
        return this.page(page, wrapper);
    }
 
    @Override
    public int addSeal(SealConfig seal) {
        validate(seal);
        seal.setCode(generateCode());
        return sealConfigMapper.insert(seal);
    }
 
    @Override
    public int updateSeal(SealConfig seal) {
        if (seal.getId() == null) {
            throw new BaseException("印章id不能为空");
        }
        validate(seal);
        // 编号不允许前端修改,保持自动生成值不变
        SealConfig db = sealConfigMapper.selectById(seal.getId());
        if (db != null) {
            seal.setCode(db.getCode());
        }
        return sealConfigMapper.updateById(seal);
    }
 
    @Override
    public int delSeal(Integer id) {
        return sealConfigMapper.deleteById(id);
    }
 
    @Override
    public List<SealConfig> selectEnabled() {
        return this.list(Wrappers.<SealConfig>lambdaQuery()
                .eq(SealConfig::getStatus, 1)
                .orderByAsc(SealConfig::getCode));
    }
 
    @Override
    public List<SealConfigExport> listForExport(SealConfig query) {
        QueryWrapper<SealConfig> wrapper = QueryWrappers.queryWrappers(query);
        if (wrapper == null) {
            wrapper = Wrappers.query();
        }
        wrapper.orderByAsc("code");
        List<SealConfig> list = this.list(wrapper);
        List<SealConfigExport> exports = new ArrayList<>();
        for (SealConfig seal : list) {
            SealConfigExport export = new SealConfigExport();
            export.setCode(seal.getCode());
            export.setName(seal.getName());
            export.setUseScope(seal.getUseScope());
            export.setStatusName(seal.getStatus() != null && seal.getStatus() == 1 ? "启用" : "停用");
            export.setRemark(seal.getRemark());
            export.setCreateTime(seal.getCreateTime() == null ? "" : seal.getCreateTime().format(DATE_FORMAT));
            exports.add(export);
        }
        return exports;
    }
 
    private void validate(SealConfig seal) {
        if (StringUtils.isBlank(seal.getName())) {
            throw new BaseException("印章名称不能为空");
        }
        if (StringUtils.isBlank(seal.getAddress())) {
            throw new BaseException("印章图片不能为空");
        }
    }
 
    /**
     * 自动生成印章编号:YZ-001、YZ-002 ...
     */
    private String generateCode() {
        Integer max = sealConfigMapper.selectMaxCodeSuffix();
        int next = (max == null ? 0 : max) + 1;
        return String.format("YZ-%03d", next);
    }
}