liu
2026-09-22 0837c3ff473499d8a267924125c776b34cc8b517
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
package com.ruoyi.system.service.impl;
 
import java.util.List;
import java.util.Map;
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CjFormConfig;
import com.ruoyi.system.mapper.CjFillTaskMapper;
import com.ruoyi.system.mapper.CjFormConfigMapper;
import com.ruoyi.system.service.ICjFormConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
 
/**
 * 填表配置Service业务层处理
 *
 * @author ruoyi
 * @date 2026-09-09
 */
@Service
public class CjFormConfigServiceImpl implements ICjFormConfigService
{
    private static final Logger log = LoggerFactory.getLogger(CjFormConfigServiceImpl.class);
 
    /** 内置35字段定义资源(classpath) */
    private static final String BUILTIN_RESOURCE = "form/builtinDefault.json";
 
    @Autowired
    private CjFormConfigMapper cjFormConfigMapper;
    @Autowired
    private CjFillTaskMapper cjFillTaskMapper;
 
    private final ObjectMapper objectMapper = new ObjectMapper();
 
    /**
     * 查询填表配置
     *
     * @param configId 填表配置主键
     * @return 填表配置
     */
    @Override
    public CjFormConfig selectCjFormConfigByConfigId(Long configId)
    {
        return cjFormConfigMapper.selectCjFormConfigByConfigId(configId);
    }
 
    /**
     * 查询填表配置列表
     *
     * @param cjFormConfig 填表配置
     * @return 填表配置集合
     */
    @Override
    public List<CjFormConfig> selectCjFormConfigList(CjFormConfig cjFormConfig)
    {
        return cjFormConfigMapper.selectCjFormConfigList(cjFormConfig);
    }
 
    /**
     * 新增填表配置
     *
     * @param cjFormConfig 填表配置
     * @return 结果
     */
    @Override
    public int insertCjFormConfig(CjFormConfig cjFormConfig)
    {
        if (cjFormConfig.getConfigName() == null || cjFormConfig.getConfigName().trim().isEmpty())
        {
            throw new ServiceException("请填写配置名称");
        }
        // groups 为空则自动载入内置35字段;attachments 缺省为空数组
        String groups = cjFormConfig.getGroupsJson();
        if (groups == null || groups.trim().isEmpty())
        {
            try
            {
                JsonNode root = readBuiltin();
                groups = objectMapper.writeValueAsString(root.path("groups"));
                cjFormConfig.setGroupsJson(groups);
                if (cjFormConfig.getAttachmentsJson() == null || cjFormConfig.getAttachmentsJson().trim().isEmpty())
                {
                    cjFormConfig.setAttachmentsJson(objectMapper.writeValueAsString(root.path("attachments")));
                }
            }
            catch (Exception e)
            {
                throw new ServiceException("读取内置字段库失败: " + e.getMessage());
            }
        }
        cjFormConfig.setFieldCount(countFields(cjFormConfig.getGroupsJson()));
        if (cjFormConfig.getVersion() == null) { cjFormConfig.setVersion(1L); }
        if (cjFormConfig.getStatus() == null || cjFormConfig.getStatus().isEmpty()) { cjFormConfig.setStatus("0"); }
        cjFormConfig.setCreateBy(username());
        cjFormConfig.setCreateTime(DateUtils.getNowDate());
        return cjFormConfigMapper.insertCjFormConfig(cjFormConfig);
    }
 
    /**
     * 修改填表配置
     *
     * @param cjFormConfig 填表配置
     * @return 结果
     */
    @Override
    public int updateCjFormConfig(CjFormConfig cjFormConfig)
    {
        CjFormConfig old = cjFormConfigMapper.selectCjFormConfigByConfigId(cjFormConfig.getConfigId());
        if (old == null)
        {
            throw new ServiceException("填表配置不存在");
        }
        if (cjFormConfig.getGroupsJson() != null && !cjFormConfig.getGroupsJson().trim().isEmpty())
        {
            cjFormConfig.setFieldCount(countFields(cjFormConfig.getGroupsJson()));
        }
        cjFormConfig.setVersion(old.getVersion() == null ? 2L : old.getVersion() + 1);
        cjFormConfig.setUpdateBy(username());
        cjFormConfig.setUpdateTime(DateUtils.getNowDate());
        return cjFormConfigMapper.updateCjFormConfig(cjFormConfig);
    }
 
    /**
     * 批量删除填表配置(被填表任务引用时拒绝)
     *
     * @param configIds 需要删除的填表配置主键
     * @return 结果
     */
    @Override
    public int deleteCjFormConfigByConfigIds(Long[] configIds)
    {
        if (configIds == null || configIds.length == 0) { return 0; }
        if (cjFillTaskMapper.countByConfigIds(configIds) > 0)
        {
            throw new ServiceException("所选配置已被填表任务引用,禁止删除(任务会保留表单快照)");
        }
        return cjFormConfigMapper.deleteCjFormConfigByConfigIds(configIds);
    }
 
    /**
     * 获取内置默认表单定义({configName, groups, attachments})
     *
     * @return 内置表单 Map
     */
    @Override
    public Map<String, Object> getBuiltin()
    {
        try
        {
            return objectMapper.convertValue(readBuiltin(), new TypeReference<Map<String, Object>>() { });
        }
        catch (Exception e)
        {
            throw new ServiceException("读取内置字段库失败: " + e.getMessage());
        }
    }
 
    // ========== 私有工具 ==========
 
    /** 读取 classpath 内置字段库根节点 */
    private JsonNode readBuiltin() throws Exception
    {
        ClassPathResource resource = new ClassPathResource(BUILTIN_RESOURCE);
        return objectMapper.readTree(resource.getInputStream());
    }
 
    /** 统计 groups 数组内字段总数 */
    private Long countFields(String groupsJson)
    {
        if (groupsJson == null || groupsJson.trim().isEmpty()) { return 0L; }
        try
        {
            JsonNode groups = objectMapper.readTree(groupsJson);
            long n = 0;
            if (groups.isArray())
            {
                for (JsonNode group : groups)
                {
                    JsonNode fields = group.path("fields");
                    if (fields.isArray()) { n += fields.size(); }
                }
            }
            return n;
        }
        catch (Exception e)
        {
            log.warn("解析 groups_json 统计字段数失败,按 0 计: {}", e.getMessage());
            return 0L;
        }
    }
 
    /** 当前登录用户名(无上下文时返回 null) */
    private String username()
    {
        try { return SecurityUtils.getUsername(); } catch (Exception e) { return null; }
    }
}