| | |
| | | public CompareReportRepository(DataSource dataSource) { jdbc = new JdbcTemplate(dataSource); } |
| | | |
| | | public record Summary(long id, String name, String creator, long createdAt) { } |
| | | public record Version(Summary report, int versionNo, String snapshotJson) { } |
| | | public record Version(Summary report, int versionNo, String snapshotJson, String operatorName, long versionCreatedAt, String changeSummary) { } |
| | | public record History(int versionNo, String operatorName, long createdAt, String changeSummary) { } |
| | | |
| | | /** 查询所有已保存报表摘要,允许授权用户跨创建人查看。 */ |
| | | public List<Summary> list() |
| | |
| | | (rs, index) -> new Summary(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getTimestamp(4).getTime())); |
| | | } |
| | | |
| | | /** 重读首个完整版本;来源不存在不影响结果。 */ |
| | | /** 重读最新完整版本;来源不存在不影响结果。 */ |
| | | public Version get(long id) |
| | | { return get(id, null); } |
| | | |
| | | /** 读取指定历史版,版本号为空时读取最新版本。 */ |
| | | public Version get(long id, Integer versionNo) |
| | | { |
| | | var rows = jdbc.query("select r.report_id, r.report_name, r.creator_name, r.created_at_utc, v.version_no, v.snapshot_json " |
| | | + "from t_compare_report r join t_compare_report_version v on v.report_id=r.report_id and v.version_no=1 where r.report_id=?", |
| | | var rows = jdbc.query("select r.report_id, r.report_name, r.creator_name, r.created_at_utc, v.version_no, v.snapshot_json, v.operator_name, v.created_at_utc, v.change_summary " |
| | | + "from t_compare_report r join t_compare_report_version v on v.report_id=r.report_id where r.report_id=? " |
| | | + (versionNo == null ? "and v.version_no=(select max(v2.version_no) from t_compare_report_version v2 where v2.report_id=r.report_id)" : "and v.version_no=?"), |
| | | (rs, index) -> new Version(new Summary(rs.getLong(1), rs.getString(2), rs.getString(3), rs.getTimestamp(4).getTime()), |
| | | rs.getInt(5), rs.getString(6)), id); |
| | | if (rows.isEmpty()) throw new ServiceException("对比报表不存在"); |
| | | rs.getInt(5), rs.getString(6), rs.getString(7), rs.getTimestamp(8).getTime(), rs.getString(9)), |
| | | versionNo == null ? new Object[] { id } : new Object[] { id, versionNo }); |
| | | if (rows.isEmpty()) throw new ServiceException("对比报表或版本不存在"); |
| | | return rows.get(0); |
| | | } |
| | | |
| | | /** 历史摘要只来自正式版本,不返回测量快照。 */ |
| | | public List<History> history(long id) |
| | | { |
| | | return jdbc.query("select version_no,operator_name,created_at_utc,change_summary from t_compare_report_version where report_id=? order by version_no desc", |
| | | (rs, index) -> new History(rs.getInt(1), rs.getString(2), rs.getTimestamp(3).getTime(), rs.getString(4)), id); |
| | | } |
| | | |
| | | /** 业务事务内锁定报表,版本号分配与旧基准拒绝共用此锁。 */ |
| | | public void lock(long id) |
| | | { |
| | | if (jdbc.queryForList("select report_id from t_compare_report where report_id=? for update", Long.class, id).isEmpty()) |
| | | throw new ServiceException("对比报表不存在"); |
| | | } |
| | | |
| | | /** 只追加版本并更新列表名称;业务事务保证两步同时成功。 */ |
| | | public void append(long id, int versionNo, String name, String snapshot, long operatorId, String operatorName, String summary) |
| | | { |
| | | jdbc.update("insert into t_compare_report_version(report_id,version_no,snapshot_json,operator_id,operator_name,created_at_utc,change_summary) values(?,?,?,?,?,?,?)", |
| | | id, versionNo, snapshot, operatorId, operatorName, new Timestamp(System.currentTimeMillis()), summary); |
| | | jdbc.update("update t_compare_report set report_name=? where report_id=?", name, id); |
| | | } |
| | | |
| | | /** 由业务事务调用,身份与首个版本任一写入失败必须一起回滚。 */ |
| | | public long insert(String name, String snapshot, long operatorId, String operatorName, long createdAt) |
| | | { |