| | |
| | | import javax.sql.DataSource; |
| | | import java.sql.Timestamp; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** 对比模板独立存储;模板更新不访问或回写报表版本。 */ |
| | | @Repository |
| | | public class CompareFieldTemplateRepository { |
| | | private final JdbcTemplate jdbc; |
| | | public CompareFieldTemplateRepository(DataSource dataSource) { jdbc = new JdbcTemplate(dataSource); } |
| | | public record Template(long id, String name, String type, List<Map<String, Object>> fields, String creator, long updatedAt) {} |
| | | @SuppressWarnings("unchecked") private List<Map<String,Object>> fields(String json) { return (List<Map<String,Object>>)(List<?>) JSON.parseArray(json, Map.class); } |
| | | public record Template(long id, String name, String type, List<CompareField> fields, String creator, long updatedAt) {} |
| | | private List<CompareField> fields(String json) { return JSON.parseArray(json, CompareField.class); } |
| | | |
| | | /** 返回该类全部模板,不按创建人过滤;字段数组次序即展示次序。 */ |
| | | public List<Template> list() { |
| | | return jdbc.query("select template_id,template_name,template_type,fields_json,creator_name,updated_at_utc from t_compare_report_field_template where template_type='compare' order by template_name,template_id", |
| | | (rs, i) -> new Template(rs.getLong(1), rs.getString(2), rs.getString(3), fields(rs.getString(4)), rs.getString(5), rs.getTimestamp(6).getTime())); |
| | | } |
| | | public Template get(long id) { |
| | | List<Template> rows = jdbc.query("select template_id,template_name,template_type,fields_json,creator_name,updated_at_utc from t_compare_report_field_template where template_id=?", (rs,i) -> new Template(rs.getLong(1),rs.getString(2),rs.getString(3),fields(rs.getString(4)),rs.getString(5),rs.getTimestamp(6).getTime()), id); |
| | | if (rows.isEmpty()) throw new ServiceException("字段模板不存在"); |
| | | return rows.get(0); |
| | | } |
| | | public long insert(String name, List<Map<String,Object>> fields, long userId, String userName) { |
| | | |
| | | /** 保存已验证的结构;同类同名模板须由用户明确选择更新,不隐式覆盖。 */ |
| | | public long insert(String name, List<CompareField> fields, long userId, String userName) { |
| | | try { |
| | | var key = new org.springframework.jdbc.support.GeneratedKeyHolder(); |
| | | jdbc.update(c -> { var s=c.prepareStatement("insert into t_compare_report_field_template(template_name,template_type,fields_json,creator_id,creator_name,created_at_utc,updated_at_utc) values(?,'compare',?,?,?,?,?)", java.sql.Statement.RETURN_GENERATED_KEYS); s.setString(1,name); s.setString(2,JSON.toJSONString(fields)); s.setLong(3,userId); s.setString(4,userName); var now=new Timestamp(System.currentTimeMillis()); s.setTimestamp(5,now); s.setTimestamp(6,now); return s; }, key); |
| | | jdbc.update(connection -> { |
| | | var statement = connection.prepareStatement("insert into t_compare_report_field_template(template_name,template_type,fields_json,creator_id,creator_name,created_at_utc,updated_at_utc) values(?,'compare',?,?,?,?,?)", java.sql.Statement.RETURN_GENERATED_KEYS); |
| | | statement.setString(1, name); 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 e) { throw new ServiceException("模板名称已存在"); } |
| | | } |
| | | |
| | | /** 授权编辑只替换模板结构,不改变既有报表或模板创建人。 */ |
| | | public void update(long id, String name, List<CompareField> fields) { |
| | | try { |
| | | if (jdbc.update("update t_compare_report_field_template set template_name=?,fields_json=?,updated_at_utc=? where template_id=? and template_type='compare'", |
| | | name, JSON.toJSONString(fields), new Timestamp(System.currentTimeMillis()), id) == 0) |
| | | throw new ServiceException("字段模板不存在"); |
| | | } catch (org.springframework.dao.DuplicateKeyException e) { throw new ServiceException("模板名称已存在"); } |
| | | } |
| | | } |