yeg
2026-09-09 f38ca8a9f8454ac2eebdc268c89efd0e436b3cf2
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
package com.battery.business.report;
 
import java.sql.Timestamp;
import java.util.List;
import javax.sql.DataSource;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.ruoyi.common.exception.ServiceException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Service;
 
/** 化成模板仅存详情和循环字段结构,独立于对比模板及所有报表版本。 */
@Service
public class FormationFieldTemplateService
{
    private final JdbcTemplate jdbc;
    private final FormationReportService reports;
    public FormationFieldTemplateService(DataSource source, FormationReportService reports)
    { this.jdbc = new JdbcTemplate(source); this.reports = reports; }
 
    public record Template(long id, String name, String type, List<FormationField> fields, String creator, long updatedAt) { }
    public record Request(@NotBlank @Size(max = 100) String name,
            @NotNull @Size(min = 1, max = 100) List<@NotNull @Valid FormationField> fields)
    {
        /** 模板不接受整份报表、来源选择或数据值。 */
        @JsonAnySetter
        public void rejectUnknown(String field, Object value) { throw new ServiceException("化成模板不允许提交属性:" + field); }
    }
 
    /** 查看该类型全部模板,保存顺序即应用顺序。 */
    public List<Template> list()
    {
        return jdbc.query("select template_id,template_name,fields_json,creator_name,updated_at_utc from t_formation_report_field_template where template_type='formation' order by template_name,template_id",
                (rs, index) -> new Template(rs.getLong(1), rs.getString(2), "formation", JSON.parseArray(rs.getString(3), FormationField.class), rs.getString(4), rs.getTimestamp(5).getTime()));
    }
 
    /** 新增前解析共享定义身份,同名模板须显式更新。 */
    public long create(Request request, long userId, String userName)
    {
        var fields = reports.validateFields(request.fields(), true);
        var key = new GeneratedKeyHolder();
        try
        {
            jdbc.update(connection -> {
                var statement = connection.prepareStatement("insert into t_formation_report_field_template(template_name,template_type,fields_json,creator_id,creator_name,created_at_utc,updated_at_utc) values(?,'formation',?,?,?,?,?)", java.sql.Statement.RETURN_GENERATED_KEYS);
                statement.setString(1, request.name().trim()); statement.setString(2, JSON.toJSONString(fields));
                statement.setLong(3, userId); statement.setString(4, userName);
                var now = new Timestamp(System.currentTimeMillis()); statement.setTimestamp(5, now); statement.setTimestamp(6, now);
                return statement;
            }, key);
            return key.getKey().longValue();
        }
        catch (org.springframework.dao.DuplicateKeyException error) { throw new ServiceException("模板名称已存在"); }
    }
 
    /** 修改只影响后续主动应用,不访问报表版本;允许跨创建人协作。 */
    public void update(long id, Request request)
    {
        var fields = reports.validateFields(request.fields(), true);
        try
        {
            if (jdbc.update("update t_formation_report_field_template set template_name=?,fields_json=?,updated_at_utc=? where template_id=? and template_type='formation'",
                    request.name().trim(), JSON.toJSONString(fields), new Timestamp(System.currentTimeMillis()), id) == 0)
                throw new ServiceException("化成字段模板不存在");
        }
        catch (org.springframework.dao.DuplicateKeyException error) { throw new ServiceException("模板名称已存在"); }
    }
}