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("模板名称已存在"); }
|
}
|
}
|