From 5383cc04ba481d22a84748b4f7c2f5481cb2ee7c Mon Sep 17 00:00:00 2001 From: yoganandhank Date: Fri, 16 Feb 2024 05:31:44 +0000 Subject: [PATCH] Updated Allreports second changes --- service/AllReports.java | 324 +++++++++++++++++++++++++--------------- 1 file changed, 204 insertions(+), 120 deletions(-) diff --git a/service/AllReports.java b/service/AllReports.java index b7af982..125a1c8 100644 --- a/service/AllReports.java +++ b/service/AllReports.java @@ -1,10 +1,15 @@ package in.gov.chennaicorporation.gccoffice.pgr.service; + +import java.sql.Statement; + import java.sql.Connection; + import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -15,156 +20,235 @@ import org.springframework.stereotype.Service; import in.gov.chennaicorporation.gccoffice.pgr.dbconnection.DBConnection; +import in.gov.chennaicorporation.gccoffice.pgr.dto.ComplaintGroup; +import in.gov.chennaicorporation.gccoffice.pgr.dto.ComplaintType; //import in.gov.chennaicorporation.gccoffice.pgr.repository.PgrReportRepository; - - - @Service public class AllReports{ @Autowired DBConnection dbcon; -public String getQuery(String sFrDate, String sToDate, String sRptGroup, String sRptType, String cbCompStatus,String mode) { - - String sQry = "SELECT z.name AS zone_name, dn.name AS division_name, location.name AS location, " + - "dept_name, TO_CHAR(complaintdate, 'dd-mm-yyyy') COMPDATE, TO_CHAR(complaintdate, 'HH24:MI:SS') Time, " + - "cs.statusname AS status, usr.user_name, cd.complaintnumber, " + - "decode(cd.ID_MODE,1,'Internet',2,'Phone',3,'Paper form',4,'WHATSAPP',5,'FLOOD CONTROL',6,'MOBILE APPLICATION',7,'SOCIAL MEDIA',8,'NEWS PAPER') AS \"MODE\","+ - "COALESCE(ct.complainttypename, 'Unspecified') AS complainttypename, cg.complaintgroupname " + - "FROM eggr_complaintdetails@erp cd " + - "INNER JOIN eggr_redressaldetails@erp rd ON cd.complaintid = rd.complaintid " + - "INNER JOIN eggr_complaintstatus@erp cs ON rd.complaintstatusid = cs.complaintstatusid " + - "INNER JOIN eggr_complainttypes@erp ct ON TRIM(cd.complainttype) = ct.complainttypeid " + - "INNER JOIN eg_user@erp usr ON rd.redressalofficerid = usr.id_user " + - "LEFT JOIN eg_department@erp d ON cd.deptid = d.id_dept " + - "INNER JOIN eggr_complaintgroup@erp cg ON cg.id_complaintgroup = ct.complaintgroup_id " + - "INNER JOIN eg_boundary@erp dn ON dn.id_bndry = cd.bndry " + - "INNER JOIN eg_boundary@erp z ON dn.parent = z.id_bndry " + - "INNER JOIN eg_boundary@ERP LOCATION ON LOCATION.id_bndry = cd.locbndryid " + - "WHERE " + - "(cd.deptid IS NULL OR (cd.deptid < 21 OR cd.deptid > 30)) " + - "AND complaintdate BETWEEN TO_DATE(?, 'yyyy-MM-dd') " + - "AND (TO_DATE(?, 'yyyy-MM-dd') + 1)"; - - // Add conditions based on parameters - List conditions = new ArrayList<>(); - List params = new ArrayList<>(); - params.add(sFrDate); - params.add(sToDate); - - - - if (cbCompStatus != null && cbCompStatus.equals("Pending")) { - conditions.add("cs.complaintstatusid NOT IN (5,6,8,10,11)"); - } else if (cbCompStatus != null && cbCompStatus.equals("Completed")) { - conditions.add("cs.complaintstatusid IN (5,6,8,10,11)"); - } - if (cbCompStatus != null && cbCompStatus.equals("Reopened")) { - conditions.add("cs.complaintstatusid IN (9)"); - } - - if (cbCompStatus != null && cbCompStatus.equals("Forwarded")) { - conditions.add("cs.complaintstatusid IN (3)"); - } - - if (sRptGroup != null && !sRptGroup.equals("0")) { - conditions.add("cg.complaintgroupname = ?"); - params.add(sRptGroup); - } + //ComplaintTypes options method + public List getAllComplaintTypes() { + List complaintTypes = new ArrayList<>(); + Connection con = DBConnection.getConnection(); + Statement stmt = null; + ResultSet rs = null; - if (sRptType != null && !sRptType.equals("0")) { - conditions.add("ct.complainttypename = ?"); - params.add(sRptType); - } + try { + stmt = con.createStatement(); + rs = stmt.executeQuery(getComplainttypeQuery()); + + while (rs.next()) { + ComplaintType complaintType = new ComplaintType(); + complaintType.setId(rs.getString("complainttypeid")); + complaintType.setName(rs.getString("complainttypename")); + complaintTypes.add(complaintType); + } - - if (mode != null && !mode.equals("0")) { - conditions.add("cd.ID_MODE = ?"); - params.add(mode); - } - - - - if (!conditions.isEmpty()) { - sQry += " AND " + String.join(" AND ", conditions); + return complaintTypes; + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + if (rs != null) { + rs.close(); + } + if (stmt != null) { + stmt.close(); + } + if (con != null) { + con.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } } - sQry += " ORDER BY zone_name, division_name, complaintdate "; - - System.out.println("allreports Query:" + sQry); - return sQry; + return Collections.emptyList(); // Return an empty list if there's an error + } + + private String getComplainttypeQuery() { + return "SELECT DISTINCT ct.complainttypeid, ct.complainttypename " + + "FROM EGGR_COMPLAINTTYPES@erp ct"; + } + //complaint group options method - - public List> check(String sFrDate, String sToDate, String sRptGroup, String sRptType, String cbCompStatus,String mode) { + public List getAllComplaintgroups() { + List complaintGroups = new ArrayList<>(); Connection con = DBConnection.getConnection(); - PreparedStatement stmt3 = null; + Statement stmt = null; + ResultSet rs = null; try { - String query = getQuery(sFrDate, sToDate, sRptGroup, sRptType, cbCompStatus,mode); - stmt3 = con.prepareStatement(query); - - // Set parameters - int parameterIndex = 1; - stmt3.setString(parameterIndex++, sFrDate); - stmt3.setString(parameterIndex++, sToDate); - - if (sRptGroup != null && !sRptGroup.equals("0")) { - stmt3.setString(parameterIndex++, sRptGroup); - } - - if (sRptType != null && !sRptType.equals("0")) { - stmt3.setString(parameterIndex++, sRptType); - } - - - - if (cbCompStatus != null && !cbCompStatus.equals("0")) { - // No need to set a parameter for the status condition - } - if (mode != null && !mode.equals("0")) { - stmt3.setString(parameterIndex++, mode); - } - - ResultSet rs3 = stmt3.executeQuery(); - - List> rows = new ArrayList<>(); - - while (rs3.next()) { - Map row = new HashMap<>(); - ResultSetMetaData metaData = rs3.getMetaData(); - int columnCount = metaData.getColumnCount(); - for (int i = 1; i <= columnCount; i++) { - String columnName = metaData.getColumnName(i); - Object columnValue = rs3.getObject(i); - row.put(columnName, columnValue); - } - rows.add(row); + stmt = con.createStatement(); + rs = stmt.executeQuery(getComplaintgroupQuery()); + + while (rs.next()) { + ComplaintGroup complaintGroup = new ComplaintGroup(); + complaintGroup.setId(rs.getString("id_complaintgroup")); + complaintGroup.setName(rs.getString("complaintgroupname")); + complaintGroups.add(complaintGroup); } - return rows; - } catch (Exception e) { + return complaintGroups; + } catch (SQLException e) { e.printStackTrace(); } finally { try { - if (stmt3 != null) { - stmt3.close(); + if (rs != null) { + rs.close(); + } + if (stmt != null) { + stmt.close(); + } + if (con != null) { + con.close(); } - con.close(); } catch (SQLException e) { e.printStackTrace(); } } - return null; - } -} + return Collections.emptyList(); // Return an empty list if there's an error + } + private String getComplaintgroupQuery() { + return "SELECT DISTINCT id_complaintgroup, complaintgroupname " + + "FROM eggr_complaintgroup@erp"; + } - + public String getQuery(String sFrDate, String sToDate, String sRptGroup, String sRptType, String cbCompStatus,String mode) { + String sQry = "SELECT z.name AS zone_name, dn.name AS division_name, location.name AS location, " + + "dept_name, TO_CHAR(complaintdate, 'dd-mm-yyyy') COMPDATE, TO_CHAR(complaintdate, 'HH24:MI:SS') Time, " + + "cs.statusname AS status, usr.user_name, cd.complaintnumber, " + + "decode(cd.ID_MODE,1,'Internet',2,'Phone',3,'Paper form',4,'WHATSAPP',5,'FLOOD CONTROL',6,'MOBILE APPLICATION',7,'SOCIAL MEDIA',8,'NEWS PAPER') AS \"MODE\","+ + "COALESCE(ct.complainttypename, 'Unspecified') AS complainttypename, cg.complaintgroupname " + + "FROM eggr_complaintdetails@erp cd " + + "INNER JOIN eggr_redressaldetails@erp rd ON cd.complaintid = rd.complaintid " + + "INNER JOIN eggr_complaintstatus@erp cs ON rd.complaintstatusid = cs.complaintstatusid " + + "INNER JOIN eggr_complainttypes@erp ct ON TRIM(cd.complainttype) = ct.complainttypeid " + + "INNER JOIN eg_user@erp usr ON rd.redressalofficerid = usr.id_user " + + "LEFT JOIN eg_department@erp d ON cd.deptid = d.id_dept " + + "INNER JOIN eggr_complaintgroup@erp cg ON cg.id_complaintgroup = ct.complaintgroup_id " + + "INNER JOIN eg_boundary@erp dn ON dn.id_bndry = cd.bndry " + + "INNER JOIN eg_boundary@erp z ON dn.parent = z.id_bndry " + + "INNER JOIN eg_boundary@ERP LOCATION ON LOCATION.id_bndry = cd.locbndryid " + + "WHERE " + + "(cd.deptid IS NULL OR (cd.deptid < 21 OR cd.deptid > 30)) " + + "AND complaintdate BETWEEN TO_DATE(?, 'yyyy-MM-dd') " + + "AND (TO_DATE(?, 'yyyy-MM-dd') + 1)"; + + // Add conditions based on parameters + List conditions = new ArrayList<>(); + List params = new ArrayList<>(); + params.add(sFrDate); + params.add(sToDate); + + if (cbCompStatus != null && cbCompStatus.equals("Pending")) { + conditions.add("cs.complaintstatusid NOT IN (5,6,8,10,11)"); + } else if (cbCompStatus != null && cbCompStatus.equals("Completed")) { + conditions.add("cs.complaintstatusid IN (5,6,8,10,11)"); + } + if (cbCompStatus != null && cbCompStatus.equals("Reopened")) { + conditions.add("cs.complaintstatusid IN (9)"); + } + + if (cbCompStatus != null && cbCompStatus.equals("Forwarded")) { + conditions.add("cs.complaintstatusid IN (3)"); + } + + if (sRptGroup != null && !sRptGroup.equals("0")) { + conditions.add("cg.complaintgroupname = ?"); + params.add(sRptGroup); + } + + if (sRptType != null && !sRptType.equals("0")) { + conditions.add("ct.complainttypename = ?"); + params.add(sRptType); + } + if (mode != null && !mode.equals("0")) { + conditions.add("cd.ID_MODE = ?"); + params.add(mode); + } + + if (!conditions.isEmpty()) { + sQry += " AND " + String.join(" AND ", conditions); + } + + sQry += " ORDER BY zone_name, division_name, complaintdate "; + + System.out.println("allreports Query:" + sQry); + return sQry; + } + + + + public List> check(String sFrDate, String sToDate, String sRptGroup, String sRptType, String cbCompStatus,String mode) { + Connection con = DBConnection.getConnection(); + PreparedStatement stmt3 = null; + + try { + String query = getQuery(sFrDate, sToDate, sRptGroup, sRptType, cbCompStatus,mode); + stmt3 = con.prepareStatement(query); + + // Set parameters + int parameterIndex = 1; + stmt3.setString(parameterIndex++, sFrDate); + stmt3.setString(parameterIndex++, sToDate); + + if (sRptGroup != null && !sRptGroup.equals("0")) { + stmt3.setString(parameterIndex++, sRptGroup); + } + + if (sRptType != null && !sRptType.equals("0")) { + stmt3.setString(parameterIndex++, sRptType); + } + + if (cbCompStatus != null && !cbCompStatus.equals("0")) { + // No need to set a parameter for the status condition + } + if (mode != null && !mode.equals("0")) { + stmt3.setString(parameterIndex++, mode); + } + + ResultSet rs3 = stmt3.executeQuery(); + + List> rows = new ArrayList<>(); + + while (rs3.next()) { + Map row = new HashMap<>(); + ResultSetMetaData metaData = rs3.getMetaData(); + int columnCount = metaData.getColumnCount(); + for (int i = 1; i <= columnCount; i++) { + String columnName = metaData.getColumnName(i); + Object columnValue = rs3.getObject(i); + row.put(columnName, columnValue); + } + rows.add(row); + } + + return rows; + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (stmt3 != null) { + stmt3.close(); + } + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + return null; + } + - +} +