diff --git a/src/main/java/com/iemr/mmu/repo/stoptb/NikshayExportRepository.java b/src/main/java/com/iemr/mmu/repo/stoptb/NikshayExportRepository.java index 28268b3d..2a8089d0 100644 --- a/src/main/java/com/iemr/mmu/repo/stoptb/NikshayExportRepository.java +++ b/src/main/java/com/iemr/mmu/repo/stoptb/NikshayExportRepository.java @@ -264,10 +264,10 @@ public Long findLatestSuspectedId(Long benRegId) { return getJdbcTemplate().query(sql, (ResultSet rs) -> rs.next() ? rs.getLong("id") : null, benRegId); } - public void updateNikshayId(Long suspectedId, String nikshayId, String modifiedBy) { - String sql = "UPDATE tb_suspected SET nikshay_id = ?, modified_by = ?, " + public void updateNikshayId(Long suspectedId, String nikshayId, boolean createdByAmrit, String modifiedBy) { + String sql = "UPDATE tb_suspected SET nikshay_id = ?, nikshay_created_by_amrit = ?, modified_by = ?, " + "last_mod_date = CURRENT_TIMESTAMP WHERE id = ?"; - getJdbcTemplate().update(sql, nikshayId, modifiedBy, suspectedId); + getJdbcTemplate().update(sql, nikshayId, createdByAmrit, modifiedBy, suspectedId); } /** Called when a beneficiary had no tb_suspected row yet — creates one to @@ -275,16 +275,17 @@ public void updateNikshayId(Long suspectedId, String nikshayId, String modifiedB * because, unlike created_date on most other AMRIT tables, tb_suspected's * has no DB-side default. */ public Long insertSuspectedWithNikshayId(Long benRegId, LocalDate visitDate, String nikshayId, - String createdBy) { - String sql = "INSERT INTO tb_suspected (benRegID, visit_date, nikshay_id, created_by, created_date) " - + "VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)"; + boolean createdByAmrit, String createdBy) { + String sql = "INSERT INTO tb_suspected (benRegID, visit_date, nikshay_id, nikshay_created_by_amrit, " + + "created_by, created_date) VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)"; KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(connection -> { PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); ps.setLong(1, benRegId); ps.setTimestamp(2, Timestamp.valueOf(visitDate.atStartOfDay())); ps.setString(3, nikshayId); - ps.setString(4, createdBy); + ps.setBoolean(4, createdByAmrit); + ps.setString(5, createdBy); return ps; }, keyHolder); return keyHolder.getKey().longValue(); diff --git a/src/main/java/com/iemr/mmu/service/stoptb/NikshayImportService.java b/src/main/java/com/iemr/mmu/service/stoptb/NikshayImportService.java index ebc81e4e..faf35e12 100644 --- a/src/main/java/com/iemr/mmu/service/stoptb/NikshayImportService.java +++ b/src/main/java/com/iemr/mmu/service/stoptb/NikshayImportService.java @@ -78,6 +78,7 @@ public record ImportRowResult(int rowIndex, Long benRegId, String firstName, Str } public record ImportSummary(int csvRowCount, int updated, int failed, int needsReview, + int createdByAmrit, int alreadyOnNikshay, List needsReviewRows, List failedRows) { } @@ -104,6 +105,8 @@ public ImportSummary importResults(LocalDate visitDate, InputStream csvInputStre } int updated = 0; + int createdByAmritCount = 0; + int alreadyOnNikshayCount = 0; List needsReview = new ArrayList<>(); List failedRows = new ArrayList<>(); @@ -141,8 +144,14 @@ public ImportSummary importResults(LocalDate visitDate, InputStream csvInputStre if ("success".equalsIgnoreCase(status) || "skipped".equalsIgnoreCase(status)) { String[] tokens = generatedId.isEmpty() ? new String[0] : generatedId.split("\\s+"); if (tokens.length == 1) { - writeNikshayId(visitDate, benRegId, tokens[0], modifiedBy); + boolean createdByAmrit = "success".equalsIgnoreCase(status); + writeNikshayId(visitDate, benRegId, tokens[0], createdByAmrit, modifiedBy); updated++; + if (createdByAmrit) { + createdByAmritCount++; + } else { + alreadyOnNikshayCount++; + } } else { String note = tokens.length == 0 ? "Row marked " + status + " but has no generatedId." : "Multiple possible existing Nikshay IDs (" + generatedId @@ -157,8 +166,8 @@ public ImportSummary importResults(LocalDate visitDate, InputStream csvInputStre } } - return new ImportSummary(records.size(), updated, failedRows.size(), needsReview.size(), needsReview, - failedRows); + return new ImportSummary(records.size(), updated, failedRows.size(), needsReview.size(), + createdByAmritCount, alreadyOnNikshayCount, needsReview, failedRows); } private static Long parseBenRegId(String raw) { @@ -186,12 +195,14 @@ private static String normalizePhone(String raw) { return digits.matches("[1-9][0-9]{9}") ? digits : null; } - private void writeNikshayId(LocalDate visitDate, Long benRegId, String nikshayId, String modifiedBy) { + private void writeNikshayId(LocalDate visitDate, Long benRegId, String nikshayId, boolean createdByAmrit, + String modifiedBy) { Long suspectedId = nikshayExportRepository.findLatestSuspectedId(benRegId); if (suspectedId != null) { - nikshayExportRepository.updateNikshayId(suspectedId, nikshayId, modifiedBy); + nikshayExportRepository.updateNikshayId(suspectedId, nikshayId, createdByAmrit, modifiedBy); } else { - nikshayExportRepository.insertSuspectedWithNikshayId(benRegId, visitDate, nikshayId, modifiedBy); + nikshayExportRepository.insertSuspectedWithNikshayId(benRegId, visitDate, nikshayId, createdByAmrit, + modifiedBy); } } }