@ -2,13 +2,16 @@ package in.gov.chennaicorporation.gccoffice.greencommittee.service;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.stream.Collectors ;
import javax.sql.DataSource ;
import javax.sql.DataSource ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
@ -18,6 +21,8 @@ public class ReportService {
@Autowired
@Autowired
private JdbcTemplate jdbcTemplate ;
private JdbcTemplate jdbcTemplate ;
@Value ( "${fileBaseUrl}" )
private String fileBaseUrl ;
@Autowired
@Autowired
public void setDataSource ( @Qualifier ( "mysqlGreenCommitteeDataSource" ) DataSource dataSource ) {
public void setDataSource ( @Qualifier ( "mysqlGreenCommitteeDataSource" ) DataSource dataSource ) {
@ -50,8 +55,7 @@ public class ReportService {
" LEFT JOIN reg_details rd2 ON rd2.ref_id = ri.ref_id " +
" LEFT JOIN reg_details rd2 ON rd2.ref_id = ri.ref_id " +
" WHERE ri.isactive = 1 " +
" WHERE ri.isactive = 1 " +
" ) t WHERE 1=1 "
) ;
" ) t WHERE 1=1 " ) ;
List < Object > params = new ArrayList < > ( ) ;
List < Object > params = new ArrayList < > ( ) ;
@ -62,8 +66,7 @@ public class ReportService {
" SELECT ref_id FROM committee_data WHERE meeting_id = ? " +
" SELECT ref_id FROM committee_data WHERE meeting_id = ? " +
" UNION " +
" UNION " +
" SELECT ref_id FROM reinspection WHERE meeting_id = ? " +
" SELECT ref_id FROM reinspection WHERE meeting_id = ? " +
" ) "
) ;
" ) " ) ;
params . add ( meetingid ) ;
params . add ( meetingid ) ;
params . add ( meetingid ) ;
params . add ( meetingid ) ;
}
}
@ -73,16 +76,14 @@ public class ReportService {
sql . append (
sql . append (
" AND ref_id IN ( " +
" AND ref_id IN ( " +
" SELECT ref_id FROM reg_details WHERE zone = ? " +
" SELECT ref_id FROM reg_details WHERE zone = ? " +
" ) "
) ;
" ) " ) ;
params . add ( zones ) ;
params . add ( zones ) ;
}
}
return jdbcTemplate . queryForList (
return jdbcTemplate . queryForList (
sql . toString ( ) ,
sql . toString ( ) ,
params . toArray ( ) ,
params . toArray ( ) ,
String . class
) ;
String . class ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
System . err . println ( "Error in getrefnums(): " + e . getMessage ( ) ) ;
System . err . println ( "Error in getrefnums(): " + e . getMessage ( ) ) ;
@ -91,7 +92,6 @@ public class ReportService {
}
}
}
}
public Map < String , Object > getlistdata ( String ref ) {
public Map < String , Object > getlistdata ( String ref ) {
Map < String , Object > regDetails = getRegDetails ( ref ) ;
Map < String , Object > regDetails = getRegDetails ( ref ) ;
@ -102,8 +102,7 @@ public class ReportService {
List < Map < String , Object > > inspectionList = new ArrayList < > ( ) ;
List < Map < String , Object > > inspectionList = new ArrayList < > ( ) ;
String sql =
"SELECT id.*, um.name AS inspector_name, " +
String sql = "SELECT id.*, um.name AS inspector_name, " +
" DATE_FORMAT(id.cdate, '%d-%m-%Y %l:%i %p') AS f_Cdate " +
" DATE_FORMAT(id.cdate, '%d-%m-%Y %l:%i %p') AS f_Cdate " +
"FROM inspection_data id " +
"FROM inspection_data id " +
"LEFT JOIN user_maping um ON um.userid = id.cby " +
"LEFT JOIN user_maping um ON um.userid = id.cby " +
@ -118,9 +117,7 @@ public class ReportService {
"ORDER BY FIELD(id.inspection_by, 'GCC', 'NGO', 'TNFD')" ;
"ORDER BY FIELD(id.inspection_by, 'GCC', 'NGO', 'TNFD')" ;
List < Map < String , Object > > rows = jdbcTemplate . queryForList (
List < Map < String , Object > > rows = jdbcTemplate . queryForList (
sql , ref , ref
) ;
sql , ref , ref ) ;
for ( Map < String , Object > row : rows ) {
for ( Map < String , Object > row : rows ) {
@ -132,8 +129,7 @@ public class ReportService {
"FROM inspection_tree_data itd " +
"FROM inspection_tree_data itd " +
"JOIN inspection_recommend_master rm ON rm.rid = itd.action " +
"JOIN inspection_recommend_master rm ON rm.rid = itd.action " +
"WHERE itd.inspection_data_id=? AND itd.isactive=1" ,
"WHERE itd.inspection_data_id=? AND itd.isactive=1" ,
inspId
) ;
inspId ) ;
row . put ( "recommendations" , recommend ) ;
row . put ( "recommendations" , recommend ) ;
@ -142,9 +138,7 @@ public class ReportService {
regDetails . put ( "inspections" , inspectionList ) ;
regDetails . put ( "inspections" , inspectionList ) ;
String reSql =
"SELECT r.id AS reins_id, r.committee_action, r.remarks AS reins_remarks, " +
String reSql = "SELECT r.id AS reins_id, r.committee_action, r.remarks AS reins_remarks, " +
" DATE_FORMAT(r.cdate, '%d-%m-%Y %l:%i %p') AS reins_date, " +
" DATE_FORMAT(r.cdate, '%d-%m-%Y %l:%i %p') AS reins_date, " +
" cm.event_name, DATE_FORMAT(cm.event_date, '%d-%m-%Y') AS event_date, crm.name as ins_name " +
" cm.event_name, DATE_FORMAT(cm.event_date, '%d-%m-%Y') AS event_date, crm.name as ins_name " +
"FROM reinspection r " +
"FROM reinspection r " +
@ -165,8 +159,7 @@ public class ReportService {
"FROM committee_data cd " +
"FROM committee_data cd " +
"LEFT JOIN create_meeting cm ON cm.id = cd.meeting_id " +
"LEFT JOIN create_meeting cm ON cm.id = cd.meeting_id " +
"WHERE cd.ref_id = ? AND cd.isactive = 1" ,
"WHERE cd.ref_id = ? AND cd.isactive = 1" ,
ref
) ;
ref ) ;
if ( ! committeeList . isEmpty ( ) ) {
if ( ! committeeList . isEmpty ( ) ) {
c1 = committeeList . get ( 0 ) ;
c1 = committeeList . get ( 0 ) ;
@ -177,8 +170,7 @@ public class ReportService {
"JOIN committee_recommend_master crm ON crm.id = ctd.committee_action " +
"JOIN committee_recommend_master crm ON crm.id = ctd.committee_action " +
"WHERE ctd.ref_id = ? " +
"WHERE ctd.ref_id = ? " +
"GROUP BY crm.name" ,
"GROUP BY crm.name" ,
ref
) ;
ref ) ;
c1 . put ( "com_complaints" , com_complaints ) ;
c1 . put ( "com_complaints" , com_complaints ) ;
}
}
@ -186,7 +178,6 @@ public class ReportService {
/ / IMPORTANT : always put committee key ( even if null )
/ / IMPORTANT : always put committee key ( even if null )
regDetails . put ( "committee" , c1 ) ;
regDetails . put ( "committee" , c1 ) ;
return regDetails ;
return regDetails ;
}
}
@ -205,4 +196,326 @@ public class ReportService {
return jdbcTemplate . queryForList ( sql , ref ) ;
return jdbcTemplate . queryForList ( sql , ref ) ;
}
}
public List < Map < String , Object > > getabscount ( String startDate , String endDate ) {
String baseSql = "SELECT " +
" zm.zone_name, " +
" COALESCE(COUNT(DISTINCT rd.ref_id), 0) AS total_requests, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='NGO' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id=id.reinspection_id) ) " +
" ) " +
" THEN 1 ELSE 0 END), 0) AS ngo_pending, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='GCC' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id=id.reinspection_id) ) " +
" ) " +
" THEN 1 ELSE 0 END), 0) AS gcc_pending, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='TNFD' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id=id.reinspection_id) ) " +
" ) " +
" THEN 1 ELSE 0 END), 0) AS tnfd_pending, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND ( " +
" SELECT COUNT(DISTINCT inspection_by) " +
" FROM inspection_data id " +
" WHERE id.ref_id = rd.ref_id AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id=id.reinspection_id) ) " +
" ) = 3 " +
" THEN 1 ELSE 0 END), 0) AS committee_pending, " +
" COALESCE(SUM(CASE WHEN rd.is_withdraw = 1 THEN 1 ELSE 0 END), 0) AS withdrawn, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" THEN 1 ELSE 0 END), 0) AS completed " +
"FROM zone_master zm " +
"LEFT JOIN reg_details rd " +
" ON rd.zone = zm.zone_name " +
" AND rd.is_active = 1 " ;
StringBuilder sql = new StringBuilder ( baseSql ) ;
List < Object > params = new ArrayList < > ( ) ;
if ( startDate ! = null & & ! startDate . isEmpty ( ) ) {
sql . append ( " AND DATE(rd.cdate) >= ? " ) ;
params . add ( startDate ) ;
}
if ( endDate ! = null & & ! endDate . isEmpty ( ) ) {
sql . append ( " AND DATE(rd.cdate) <= ? " ) ;
params . add ( endDate ) ;
}
sql . append (
" WHERE zm.is_active = 1 " +
" AND zm.is_delete = 0 " +
" GROUP BY zm.zone_name " +
" ORDER BY zm.zone_name " ) ;
return jdbcTemplate . queryForList ( sql . toString ( ) , params . toArray ( ) ) ;
}
public List < Map < String , Object > > getWardDetailsByZone (
String zone ,
String startDate ,
String endDate ) {
String baseSql = "SELECT " +
" wm.zone_id, " +
" wm.ward_name, " +
" COALESCE(COUNT(DISTINCT rd.ref_id), 0) AS total_requests, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='NGO' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
" ) " +
" THEN 1 ELSE 0 END), 0) AS ngo_pending, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='GCC' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
" ) " +
" THEN 1 ELSE 0 END), 0) AS gcc_pending, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='TNFD' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
" ) " +
" THEN 1 ELSE 0 END), 0) AS tnfd_pending, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IS NOT NULL " +
" AND rd.is_withdraw = 0 " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
" AND ( " +
" SELECT COUNT(DISTINCT inspection_by) " +
" FROM inspection_data id " +
" WHERE id.ref_id = rd.ref_id AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
" ) = 3 " +
" THEN 1 ELSE 0 END), 0) AS committee_pending, " +
" COALESCE(SUM(CASE WHEN rd.is_withdraw=1 THEN 1 ELSE 0 END), 0) AS withdrawn, " +
" COALESCE(SUM(CASE " +
" WHEN rd.ref_id IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
" THEN 1 ELSE 0 END), 0) AS completed " +
"FROM ward_master wm " +
"JOIN zone_master zm ON zm.id = wm.zone_id AND zm.zone_name = ? " +
"LEFT JOIN reg_details rd " +
" ON rd.ward = wm.ward_name " +
" AND rd.is_active = 1 " ;
StringBuilder sql = new StringBuilder ( baseSql ) ;
List < Object > params = new ArrayList < > ( ) ;
params . add ( zone ) ;
if ( startDate ! = null & & ! startDate . isEmpty ( ) ) {
sql . append ( " AND DATE(rd.cdate) >= ? " ) ;
params . add ( startDate ) ;
}
if ( endDate ! = null & & ! endDate . isEmpty ( ) ) {
sql . append ( " AND DATE(rd.cdate) <= ? " ) ;
params . add ( endDate ) ;
}
sql . append (
" WHERE wm.is_active=1 AND wm.is_delete=0 " +
" GROUP BY wm.zone_id, wm.ward_name " +
" ORDER BY wm.ward_name " ) ;
return jdbcTemplate . queryForList ( sql . toString ( ) , params . toArray ( ) ) ;
}
public List < String > getPendingRefIds (
String zone ,
String ward ,
String type ,
String startDate ,
String endDate ) {
String baseSql ;
switch ( type . toUpperCase ( ) ) {
case "NGO" :
baseSql = "SELECT rd.ref_id FROM reg_details rd " +
"WHERE rd.zone=? " +
"AND rd.is_active=1 AND rd.is_withdraw=0 " +
"AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
"AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
"AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='NGO' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
")" ;
break ;
case "GCC" :
baseSql = "SELECT rd.ref_id FROM reg_details rd " +
"WHERE rd.zone=? " +
"AND rd.is_active=1 AND rd.is_withdraw=0 " +
"AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
"AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
"AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='GCC' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
")" ;
break ;
case "TNFD" :
baseSql = "SELECT rd.ref_id FROM reg_details rd " +
"WHERE rd.zone=? " +
"AND rd.is_active=1 AND rd.is_withdraw=0 " +
"AND rd.ref_id NOT IN (SELECT ref_id FROM committee_data WHERE isactive=1) " +
"AND rd.ref_id NOT IN (SELECT ref_id FROM reinspection WHERE isactive=1) " +
"AND rd.ref_id NOT IN ( " +
" SELECT id.ref_id FROM inspection_data id " +
" WHERE id.inspection_by='TNFD' AND id.isactive=1 " +
" AND ( (rd.reinspection_id IS NULL AND id.reinspection_id IS NULL) " +
" OR (rd.reinspection_id = id.reinspection_id) ) " +
")" ;
break ;
case "WITHDRAW" :
baseSql = "SELECT rd.ref_id FROM reg_details rd " +
"WHERE rd.zone=? AND rd.is_withdraw=1" ;
break ;
default :
return Collections . emptyList ( ) ;
}
StringBuilder sql = new StringBuilder ( baseSql ) ;
List < Object > params = new ArrayList < > ( ) ;
params . add ( zone ) ;
if ( ward ! = null & & ! ward . isEmpty ( ) ) {
sql . append ( " AND rd.ward = ? " ) ;
params . add ( ward ) ;
}
if ( startDate ! = null & & ! startDate . isEmpty ( ) ) {
sql . append ( " AND DATE(rd.cdate) >= ? " ) ;
params . add ( startDate ) ;
}
if ( endDate ! = null & & ! endDate . isEmpty ( ) ) {
sql . append ( " AND DATE(rd.cdate) <= ? " ) ;
params . add ( endDate ) ;
}
return jdbcTemplate . query (
sql . toString ( ) ,
params . toArray ( ) ,
( rs , rowNum ) - > rs . getString ( "ref_id" ) ) ;
}
public Map < String , Object > getfulldetailsdata ( String refId ) {
Map < String , Object > response = new HashMap < > ( ) ;
/ / 1 — Application details
Map < String , Object > app = jdbcTemplate . queryForMap (
"SELECT *,DATE_FORMAT(cdate, '%d-%m-%Y %l:%i %p') as r_Cdate,IFNULL(reinspection_id, '') AS com_reinspection_id FROM reg_details WHERE ref_id=? AND is_active=1" ,
refId ) ;
/ / 2 — Complaint details ( grouped )
List < Map < String , Object > > complaints = jdbcTemplate . queryForList (
"SELECT cn.name AS nature, SUM(cd.no_of_trees) AS count " +
"FROM complaint_details cd JOIN comp_nature cn ON cn.id=cd.comp_nature_id " +
"WHERE cd.ref_id=? GROUP BY cn.name" ,
refId ) ;
String compDetails = complaints . stream ( )
. map ( c - > c . get ( "nature" ) + "-" + c . get ( "count" ) )
. collect ( Collectors . joining ( "," ) ) ;
/ / 3 — Complaint images
List < Map < String , Object > > imagePaths = jdbcTemplate . queryForList (
"SELECT img_path FROM img_uploads WHERE ref_id=? AND is_active=1" ,
new Object [ ] { refId } ) . stream ( )
. map ( row - > {
Map < String , Object > img = new HashMap < > ( ) ;
img . put (
"img_path" ,
fileBaseUrl + "/gccofficialapp/files" + row . get ( "img_path" ) . toString ( ) ) ;
return img ;
} )
. collect ( Collectors . toList ( ) ) ;
app . put ( "complaints" , complaints ) ;
app . put ( "comp_details" , compDetails ) ;
app . put ( "image_paths" , imagePaths ) ;
response . put ( "data" , app ) ;
return response ;
}
}
}