diff --git a/src/main/java/in/gov/chennaicorporation/mobileservice/configuration/DataSourceConfig.java b/src/main/java/in/gov/chennaicorporation/mobileservice/configuration/DataSourceConfig.java index 044ea6de..4f1ab437 100644 --- a/src/main/java/in/gov/chennaicorporation/mobileservice/configuration/DataSourceConfig.java +++ b/src/main/java/in/gov/chennaicorporation/mobileservice/configuration/DataSourceConfig.java @@ -20,12 +20,13 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement public class DataSourceConfig { -// private static String host = "localhost:3306"; -// private static String dbpassword = "root"; + // private static String host = "localhost:3306"; + // private static String dbpassword = "root"; // private static String dbpassword = "gccroot"; // AWS - private static String host = "gcc-facial-db-instance-1.cf48eqcciziq.ap-south-1.rds.amazonaws.com:3306"; + private static String host = + "gcc-facial-db-instance-1.cf48eqcciziq.ap-south-1.rds.amazonaws.com:3306"; private static String dbpassword = "gcc-facial-password"; ////////////////////////////// (For GCC APP) //////////////////////// @@ -729,6 +730,16 @@ public class DataSourceConfig { return dataSource; } + @Bean(name = "mysqlWorksTestDataSource") + public DataSource mysqlWorksTestDataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); + dataSource.setUrl("jdbc:mysql://" + host + "/gcc_works_status_test"); + dataSource.setUsername("root"); + dataSource.setPassword(dbpassword); + return dataSource; + } + ////////////////////////////// ORACLE //////////////////////// @Bean(name = "oracleERPDataSource") public DataSource oracleERPDataSource() { diff --git a/src/main/java/in/gov/chennaicorporation/mobileservice/works/controller/WorksTestController.java b/src/main/java/in/gov/chennaicorporation/mobileservice/works/controller/WorksTestController.java new file mode 100644 index 00000000..077abdff --- /dev/null +++ b/src/main/java/in/gov/chennaicorporation/mobileservice/works/controller/WorksTestController.java @@ -0,0 +1,97 @@ +package in.gov.chennaicorporation.mobileservice.works.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import in.gov.chennaicorporation.mobileservice.works.service.WorksTestService; + +@RequestMapping("/gccofficialapp/works/test/api") +@RestController("worksTestRestapi") +public class WorksTestController { + + @Autowired + private WorksTestService worksTestService; + + /* + * @GetMapping("/getESTList") + * public List getTaskList( + * + * @RequestParam(value = "typeofworkid", required = true) String typeofworkid + * ) { + * return worksService.getTaskList(typeofworkid); + * } + */ + + @GetMapping("/getESTList") + public List getESTList( + @RequestParam(value = "typeofworkid", required = false) String typeofworkid, + @RequestParam(value = "loginId", required = true) String loginId) { + return worksTestService.getESTList(loginId); + } + + @GetMapping("/getESTWorkListData") + public List getESTWorkListData( + @RequestParam(value = "estid", required = true) String estid, + @RequestParam(value = "loginId", required = false) String cby, + @RequestParam(value = "typeofworkid", required = true) String typeofworkid) { + return worksTestService.getESTWorkListData(estid, typeofworkid); + } + + @GetMapping("/getTaskDataListWithData") + public List getTaskDataListWithData( + @RequestParam(value = "worktasktid", required = true) String wtid, + @RequestParam(value = "estid", required = true) String estid, + @RequestParam(value = "typeofworkid", required = true) String typeofworkid, + @RequestParam(value = "refId", required = true) String refId) { + return worksTestService.getTaskDataListWithData(wtid, estid, typeofworkid, refId); + } + + @PostMapping(value = "/addTaskStageActivityData") + public List addTaskStageActivityData(@RequestParam MultiValueMap formData, + @RequestParam(value = "estid", required = true) String estid, + @RequestParam(value = "tmid", required = true) String tmid, + @RequestParam(value = "worktasktid", required = true) String wtid, + @RequestParam(value = "activityfile", required = true) MultipartFile activityfile, + @RequestParam(value = "percentage", required = true) String percentage, + @RequestParam(value = "remarks", required = false) String remarks, + @RequestParam(value = "loginId", required = true) String cby, + @RequestParam(value = "zone", required = true) String zone, + @RequestParam(value = "ward", required = true) String ward, + @RequestParam(value = "latitude", required = true) String latitude, + @RequestParam(value = "longitude", required = true) String longitude, + @RequestParam(value = "refId", required = true) String refId) { + return worksTestService.addTaskStageActivityData(estid, tmid, wtid, activityfile, percentage, remarks, cby, + zone, + ward, latitude, longitude, "open", refId); + } + + @GetMapping("/getZoneReport") + public List getZoneReport( + @RequestParam(value = "loginId", required = false) String cby) { + return worksTestService.getZoneReport(); + } + + @GetMapping("/getWardReport") + public List getWardReport( + @RequestParam(value = "zone", required = true) String zone, + @RequestParam(value = "loginId", required = false) String cby) { + return worksTestService.getWardReport(zone); + } + + @GetMapping("/getListReport") + public List getListReport( + @RequestParam(value = "ward", required = true) String ward, + @RequestParam(value = "statusFilter", required = true) String statusFilter, + @RequestParam(value = "loginId", required = false) String cby) { + return worksTestService.getListReport(ward, statusFilter); + } + +} diff --git a/src/main/java/in/gov/chennaicorporation/mobileservice/works/service/WorksTestService.java b/src/main/java/in/gov/chennaicorporation/mobileservice/works/service/WorksTestService.java new file mode 100644 index 00000000..ec8740bf --- /dev/null +++ b/src/main/java/in/gov/chennaicorporation/mobileservice/works/service/WorksTestService.java @@ -0,0 +1,879 @@ +package in.gov.chennaicorporation.mobileservice.works.service; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.SecureRandom; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; + +import javax.imageio.ImageIO; +import javax.imageio.ImageWriteParam; +import javax.imageio.ImageWriter; +import javax.imageio.stream.ImageOutputStream; +import javax.sql.DataSource; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.core.env.Environment; +import org.springframework.dao.DataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.PreparedStatementCreator; +import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import in.gov.chennaicorporation.mobileservice.gccactivity.service.DateTimeUtil; + +@Service +public class WorksTestService { + private JdbcTemplate jdbcWorksTemplate; + + private final Environment environment; + private String fileBaseUrl; + + private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + private static final int STRING_LENGTH = 15; + private static final Random RANDOM = new SecureRandom(); + + @Autowired + public void setDataSource(@Qualifier("mysqlWorksTestDataSource") DataSource worksDataSource) { + this.jdbcWorksTemplate = new JdbcTemplate(worksDataSource); + } + + @Autowired + public WorksTestService(Environment environment) { + this.environment = environment; + this.fileBaseUrl = environment.getProperty("fileBaseUrl"); + } + + public static String generateRandomString() { + StringBuilder result = new StringBuilder(STRING_LENGTH); + for (int i = 0; i < STRING_LENGTH; i++) { + result.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length()))); + } + return result.toString(); + } + + public static String generateRandomStringForFile(int String_Lenth) { + StringBuilder result = new StringBuilder(String_Lenth); + for (int i = 0; i < STRING_LENGTH; i++) { + result.append(CHARACTERS.charAt(RANDOM.nextInt(CHARACTERS.length()))); + } + return result.toString(); + } + + private byte[] compressImage(BufferedImage image, float quality) throws IOException { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream); + + ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next(); + writer.setOutput(imageOutputStream); + + ImageWriteParam param = writer.getDefaultWriteParam(); + if (param.canWriteCompressed()) { + param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); + param.setCompressionQuality(quality); + } + + writer.write(null, new javax.imageio.IIOImage(image, null, null), param); + + writer.dispose(); + imageOutputStream.close(); + + return byteArrayOutputStream.toByteArray(); + } + + public String fileUpload(String name, String id, MultipartFile file) { + + int lastInsertId = 0; + // Set the file path where you want to save it + String uploadDirectory = environment.getProperty("file.upload.directory"); + String serviceFolderName = environment.getProperty("works_test_foldername"); + var year = DateTimeUtil.getCurrentYear(); + var month = DateTimeUtil.getCurrentMonth(); + var date = DateTimeUtil.getCurrentDay(); + + uploadDirectory = uploadDirectory + serviceFolderName + year + "/" + month + "/" + date; + + try { + // Create directory if it doesn't exist + Path directoryPath = Paths.get(uploadDirectory); + if (!Files.exists(directoryPath)) { + Files.createDirectories(directoryPath); + } + + // Datetime string + String datetimetxt = DateTimeUtil.getCurrentDateTime(); + // File name + System.out.println(file.getOriginalFilename()); + String fileName = name + "_" + id + "_" + datetimetxt + "_" + generateRandomStringForFile(10) + "_" + + file.getOriginalFilename(); + fileName = fileName.replaceAll("\\s+", ""); // Remove space on filename + + String filePath = uploadDirectory + "/" + fileName; + + String filepath_txt = "/" + serviceFolderName + year + "/" + month + "/" + date + "/" + fileName; + + // Create a new Path object + Path path = Paths.get(filePath); + + // Get the bytes of the file + byte[] bytes = file.getBytes(); + + // Compress the image + BufferedImage image = ImageIO.read(new ByteArrayInputStream(bytes)); + byte[] compressedBytes = compressImage(image, 0.5f); // Compress with 50% quality + + // Write the bytes to the file + Files.write(path, compressedBytes); + + System.out.println(filePath); + return filepath_txt; + + } catch (IOException e) { + e.printStackTrace(); + return "Failed to save file " + file.getOriginalFilename(); + } + } + + public String getWardByLoginId(String loginid, String type) { + String sqlQuery = "SELECT `ward`, `works` FROM `gcc_penalty_hoardings`.`hoading_user_list` WHERE `userid` = ? LIMIT 1"; + + List> results = jdbcWorksTemplate.queryForList(sqlQuery, loginid); + + if (!results.isEmpty()) { + Map row = results.get(0); + String wardClause; + String departmentClause = ""; + + Object wardValue = row.get("ward"); + Object worksValue = row.get("works"); + + if (wardValue == null || wardValue.toString().equals("0")) { + wardClause = "wu.`ward` NOT IN (0)"; + } else { + wardClause = "wu.`ward` IN ('" + wardValue + "')"; + } + + if (worksValue != null) { + departmentClause = " AND wu.`department` IN (" + worksValue + ")"; + } + + return wardClause + departmentClause; + } + + return "wu.`ward` IN ('-')"; + } + + public List> getESTList(String loginid) { + /* + * String sql = + * "SELECT wu.`Id` AS estid, `wu`.`sub_category` AS works, tw.id AS typeofworkid, `wu`.`estimate_no`, " + * + + * "`wu`.`estimate_date`, `wu`.`zone`, `wu`.`ward`, `wu`.`project_name`, `wu`.`location`, `wu`.`department`, " + * + + * "`wu`.`contractor_name`, `wu`.`contractor_period`, `wu`.`fund_source`, `wu`.`scheme`, `wu`.`category`, `wu`.`sub_category` AS `sub_category`, " + * + + * "`wu`.`est_amount`, `wu`.`tech_sanc_date`, `wu`.`admin_sanc_date`, `wu`.`tender_call_date`, `wu`.`tender_fin_date`, " + * + + * "`wu`.`loa_date`, `wu`.`agreement_date`, `wu`.`work_order_date`, `wu`.`work_comm_date` " + * + + * "FROM `works_update` `wu` LEFT JOIN EGW_TYPEOFWORK `tw` ON trim(`tw`.description) = trim(`wu`.`sub_category`)" + * ; + */ + + String where = getWardByLoginId(loginid, "ae"); + // System.out.println("ward: "+ward); + String sql = "SELECT " + + "wu.`estid` AS estid, " + + "tm.`description` AS works, " + + "tm.id AS typeofworkid, " + + "wu.`estimate_no`, " + + "wu.`estimate_date`, " + + "wu.`zone`, " + + "wu.`ward`, " + + "wu.`project_name`, " + + "wu.`location`, " + + "dm.`dept_name` AS `department`, " + + "wu.`contractor_name`, " + + "CAST(wu.contractor_period AS CHAR) AS contractor_period, " + + "fs.`name` AS `fund_source`, " + + "s.`name` AS `scheme`, " + + "wm.`name` AS `category`, " + + "tm.`description` AS `sub_category`, " + + "wu.`estimation_amount` AS `est_amount`, " + + "wu.`technical_sanction_date` AS `tech_sanc_date`, " + + "wu.`admin_sanction_date` AS `admin_sanc_date`, " + + "wu.`tender_called_date` AS `tender_call_date`, " + + "wu.`tender_finalized_date` AS `tender_fin_date`, " + + "wu.`loa_date` AS `loa_date`, " + + "wu.`agreement_date` AS `agreement_date`, " + + "wu.`work_order_date` AS `work_order_date`, " + + "wu.`work_commenced_date` AS `work_comm_date`, " + + "wu.`percentage` AS `percentage` " + + "FROM `works_details` wu " + // + "LEFT JOIN typeofwork_master tw ON trim(tw.description) = + // trim(wu.`sub_category`)" + + "LEFT JOIN department_master dm ON dm.id = wu.`department`" + + "LEFT JOIN fundsource_master fs ON fs.id = wu.`fund_source` " + + "LEFT JOIN scheme_master s ON s.id = wu.`scheme` " + + "LEFT JOIN workstype_master wm ON wm.id = wu.`category` " + + "LEFT JOIN typeofwork_master tm ON tm.id = wu.`sub_category` " + + "WHERE " + where; + + List> result = jdbcWorksTemplate.queryForList(sql); + /* + * List> estWorklistData = new ArrayList<>(); + * + * for (Map row : result) { + * // CHECK IN WORK_TASK + * + * // CHECK IN typeofwork_task_master + * + * // CHECK IN task_data last DATA for each typeofwork_task_master + * } + */ + + return jdbcWorksTemplate.queryForList(sql); + } + + /* + * public List> getTaskDataListWithData(String estid, String + * typeofworkid) { + * String sql = + * "SELECT `tdid`, `estid`, `tmid`, `percentage`, `cdate`, `cby`, `zone`, `ward`, `latitude`, `longitude`, `stage_file`, `status`, `isactive` FROM `task_data` WHERE 1" + * ; + * return jdbcWorksTemplate.queryForList(sql,typeofworkid); + * } + */ + + public List> getESTWorkListData(String estid, String typeofworkid) { + + // String sql = "SELECT `id` AS wtid, `estid`, `estimate_no`, `sub_cat_id`, + // `question_id`, `ans_id`," + // + " `remarks`, `cdate`, `isactive`, `isdelete`, `percentage` " + // + " FROM `erp_works_task` " + // + " WHERE `isactive`=1 AND `isdelete`=0 AND `estid`= ?"; + Map result = new HashMap<>(); + + String sql = "select id, api_name from addworks_question_master where subcategory_id = " + typeofworkid + + " and question_name in ('Zone','Ward','Road or Street Name')"; + List> result1 = jdbcWorksTemplate.queryForList(sql); + if (result1.isEmpty()) { + result.put("estWorklistData", new ArrayList<>()); + result.put("message", "No Activity Question ID found against this category"); + return Collections.singletonList(result); + } + int zoneQuestionId = 0; + int wardQuestionId = 0; + int roadQuestionId = 0; + for (Map row : result1) { + if (row.get("api_name").equals("ZONE")) { + zoneQuestionId = Integer.parseInt(row.get("id").toString()); + } + if (row.get("api_name").equals("WARD")) { + wardQuestionId = Integer.parseInt(row.get("id").toString()); + } + if (row.get("api_name").equals("ROAD")) { + roadQuestionId = Integer.parseInt(row.get("id").toString()); + } + } + + String sql1 = "SELECT " + + "ewt.id AS wtid, " + + "ewt.estid, " + + "ewt.estimate_no, " + + "ewt.sub_cat_id, " + + "ifnull(ewt.question_id,'') as question_id, " + + "ewt.ans_id, " + + "ewt.remarks, " + + "ewt.cdate, " + + "ewt.isactive, " + + "ewt.isdelete, " + + "ewt.percentage, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.answer END), '') AS road_name, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.ref_id END), '') AS roadRefId, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.answer END), '') AS zone, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.answer END), '') AS ward " + + "FROM erp_works_task ewt " + + "LEFT JOIN addworks_text_details atd " + + "ON ewt.ref_id = atd.ref_id " + + "AND ewt.estid = atd.estid " + + "WHERE atd.isactive = 1 " + + "AND atd.isdelete = 0 " + + "AND ewt.isactive = 1 " + + "AND ewt.isdelete = 0 " + + "AND ewt.estid = ? " + + "GROUP BY " + + "ewt.id, " + + "ewt.estid, " + + "ewt.estimate_no, " + + "ewt.sub_cat_id, " + + "ewt.question_id, " + + "ewt.ans_id, " + + "ewt.remarks, " + + "ewt.cdate, " + + "ewt.isactive, " + + "ewt.isdelete, " + + "ewt.percentage"; + + List> result2 = jdbcWorksTemplate.queryForList(sql1, roadQuestionId, roadQuestionId, + zoneQuestionId, wardQuestionId, estid); + + Map response = new HashMap<>(); + response.put("estWorklistData", result2); + response.put("message", "EST Activity Data List."); + + return Collections.singletonList(response); + } + + public List> getTaskDataListWithData(String wtid, String estid, String typeofworkid, + String refId) { + + // String sql = "SELECT `id`, `estid`, `estimate_no`, `sub_cat_id`, + // `question_id`, `ans_id`," + // + " `remarks`, `cdate`, `isactive`, `isdelete`, `percentage` FROM + // `erp_works_task` " + // + "WHERE `id`=? AND `estid`= ? AND `isactive`=1 AND `isdelete`=0"; + + // List> result = jdbcWorksTemplate.queryForList(sql, wtid, + // estid); + + Map result = new HashMap<>(); + + String sql = "select id, api_name from addworks_question_master where subcategory_id = ? " + + " and question_name in ('Zone','Ward','Road or Street Name')"; + List> result1 = jdbcWorksTemplate.queryForList(sql, typeofworkid); + if (result1.isEmpty()) { + result.put("estWorklistData", new ArrayList<>()); + result.put("message", "No Activity Question ID found against this category"); + return Collections.singletonList(result); + } + int zoneQuestionId = 0; + int wardQuestionId = 0; + int roadQuestionId = 0; + for (Map row : result1) { + if (row.get("api_name").equals("ZONE")) { + zoneQuestionId = Integer.parseInt(row.get("id").toString()); + } + if (row.get("api_name").equals("WARD")) { + wardQuestionId = Integer.parseInt(row.get("id").toString()); + } + if (row.get("api_name").equals("ROAD")) { + roadQuestionId = Integer.parseInt(row.get("id").toString()); + } + } + + String sql1 = "SELECT " + + "ewt.id AS wtid, " + + "ewt.estid, " + + "ewt.estimate_no, " + + "ewt.sub_cat_id, " + + "ifnull(ewt.question_id,'') as question_id, " + + "ewt.ans_id, " + + "ewt.remarks, " + + "ewt.cdate, " + + "ewt.isactive, " + + "ewt.isdelete, " + + "ewt.percentage, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.answer END), '') AS road_name, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.ref_id END), '') AS roadRefId, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.answer END), '') AS zone, " + + "IFNULL(MAX(CASE WHEN atd.question_id = ? THEN atd.answer END), '') AS ward " + + "FROM erp_works_task ewt " + + "LEFT JOIN addworks_text_details atd " + + "ON ewt.ref_id = atd.ref_id " + + "AND ewt.estid = atd.estid " + + "WHERE atd.isactive = 1 " + + "AND atd.isdelete = 0 " + + "AND ewt.isactive = 1 " + + "AND ewt.isdelete = 0 " + + "AND ewt.estid = ? and ewt.id = ? " + + "GROUP BY " + + "ewt.id, " + + "ewt.estid, " + + "ewt.estimate_no, " + + "ewt.sub_cat_id, " + + "ewt.question_id, " + + "ewt.ans_id, " + + "ewt.remarks, " + + "ewt.cdate, " + + "ewt.isactive, " + + "ewt.isdelete, " + + "ewt.percentage"; + + List> result2 = jdbcWorksTemplate.queryForList(sql1, roadQuestionId, roadQuestionId, + zoneQuestionId, wardQuestionId, estid, wtid); + + List> estTaskWorklistData = new ArrayList<>(); + + for (Map row : result2) { + + // Query for the task related to the current activity - need to change this + // query + String sqlForTask = "SELECT `tmid`, `stagename` AS `taskname`, `isactive`, `isdelete`, `orderby`, " + + "(" + + " SELECT `percentage` " + + " FROM `erp_task_data` " + + " WHERE `tmid` = `erp_works_stages`.`tmid` AND wtid=? AND `isactive`=1 " + + " ORDER BY `cdate` DESC " + + " LIMIT 1 " + + " ) AS percentage " + + "FROM `erp_works_stages` WHERE `isactive`=1 AND `isdelete`=0 AND `estid`=? AND `ref_id`= ? AND `isactive`=1 ORDER BY `orderby`"; + + List> taskresult = jdbcWorksTemplate.queryForList(sqlForTask, wtid, estid, refId); + + if (!taskresult.isEmpty()) { + // For each task in the taskresult, add taskdataresult + for (Map task : taskresult) { + // Query for the task-related data + String tmid = "" + task.get("tmid"); // Assuming tmid is a field in task + String sqlForTaskData = "SELECT `tdid`, `estid`, `ref_id`, `tmid`, `percentage`, `remarks`, " + + "DATE_FORMAT(`cdate`, '%d-%m-%Y %r') AS createddate, " + + "`cby`, `zone`, `ward`, `latitude`, `longitude`, " + + " CONCAT('" + fileBaseUrl + "/gccofficialapp/files', `stage_file`) AS image, " + + "`status`, `isactive` FROM `erp_task_data` WHERE `wtid`=? AND `tmid`=? AND `estid`=? AND `ref_id`= ? AND `isactive`=1"; + + List> taskdataresult = jdbcWorksTemplate.queryForList(sqlForTaskData, wtid, + tmid, estid, refId); + + // Add taskdataresult to each task + task.put("taskdata", taskdataresult); + } + } + // Add tasklist (with taskdata) to the row + row.put("tasklist", taskresult); + + // Add the row to SWDData + estTaskWorklistData.add(row); + } + + // Prepare the response + Map response = new HashMap<>(); + response.put("estTaskWorklistData", estTaskWorklistData); + response.put("message", "EST Activity Data List."); + + // Return the response wrapped in a singleton list + return Collections.singletonList(response); + } + + @Transactional + public List> addTaskStageActivityData( + String estid, + String tmid, + String wtid, + MultipartFile file, + String percentage, + String remarks, + String cby, + String zone, + String ward, + String latitude, + String longitude, + String status, + String refId) { + List> result = null; + Map response = new HashMap<>(); + + int lastInsertId = 0; + + String filetxt = ""; + + // Handle file upload if a file is provided + if (file != null && !file.isEmpty()) { + filetxt = fileUpload(estid + "_" + tmid, cby, file); + } + String finalfile = filetxt; + String sqlQuery = "INSERT INTO `erp_task_data`" + + "(`estid`, `tmid`, `percentage`, `remarks`, `cby`, `zone`, `ward`, `latitude`, `longitude`, `stage_file`, `status`,`wtid`, `ref_id`) " + + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"; + + KeyHolder keyHolder = new GeneratedKeyHolder(); + + LocalDateTime approvedDate = LocalDateTime.now(); + + String formattedDate = approvedDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); + + try { + status = "open"; + if (percentage.contains("100")) { + status = "close"; + } + + String statustxt = status; + + // Insert the data the required number of times + int affectedRows = jdbcWorksTemplate.update(new PreparedStatementCreator() { + @Override + public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { + PreparedStatement ps = connection.prepareStatement(sqlQuery, new String[] { "did" }); + ps.setString(1, estid); + ps.setString(2, tmid); + ps.setString(3, percentage); + ps.setString(4, remarks); + ps.setString(5, cby); + ps.setString(6, zone); + ps.setString(7, ward); + ps.setString(8, latitude); + ps.setString(9, longitude); + ps.setString(10, finalfile); + ps.setString(11, statustxt); + ps.setString(12, wtid); + ps.setString(13, refId); + return ps; + } + }, keyHolder); + + if (affectedRows > 0) { + Number generatedId = keyHolder.getKey(); + lastInsertId = (generatedId != null) ? generatedId.intValue() : 0; + response.put("insertId", lastInsertId); + response.put("status", "success"); + response.put("message", "Stage Task data inserted successfully!"); + System.out.println("Stage Task was inserted successfully! Insert ID: " + generatedId); + updateStatus(percentage, estid, wtid); + } else { + response.put("status", "error"); + response.put("message", "Failed to insert stage task data."); + } + } catch (DataAccessException e) { + System.out.println("Data Access Exception:"); + Throwable rootCause = e.getMostSpecificCause(); + if (rootCause instanceof SQLException) { + SQLException sqlException = (SQLException) rootCause; + System.out.println("SQL State: " + sqlException.getSQLState()); + System.out.println("Error Code: " + sqlException.getErrorCode()); + System.out.println("Message: " + sqlException.getMessage()); + response.put("status", "error"); + response.put("message", sqlException.getMessage()); + response.put("sqlState", sqlException.getSQLState()); + response.put("errorCode", sqlException.getErrorCode()); + } else { + System.out.println("Message: " + rootCause.getMessage()); + response.put("status", "error"); + response.put("message", rootCause.getMessage()); + } + } + + // String sqlUpdateQuery = "UPDATE erp_works ew " + // + "JOIN ( " + // + " SELECT " + // + " ew.estid, " + // + " ROUND(SUM(COALESCE(etd.percentage, 0)) / COUNT(*), 0) AS + // physical_progress " + // + " FROM erp_works ew " + // + " JOIN erp_works_task ewt ON ewt.estid = ew.estid " + // + " JOIN erp_works_stages ews ON ews.estid = ew.estid " + // + " LEFT JOIN ( " + // // + " -- Get only the latest active record per (estid, wtid, tmid) " + // + " SELECT etd.* " + // + " FROM erp_task_data etd " + // + " INNER JOIN ( " + // + " SELECT estid, wtid, tmid, MAX(tdid) AS max_tdid " + // + " FROM erp_task_data " + // + " WHERE isactive = 1 " + // + " GROUP BY estid, wtid, tmid " + // + " ) latest ON latest.estid = etd.estid " + // + " AND latest.wtid = etd.wtid " + // + " AND latest.tmid = etd.tmid " + // + " AND latest.max_tdid = etd.tdid " + // + " ) etd ON etd.estid = ew.estid " + // + " AND etd.wtid = ewt.id " + // + " AND etd.tmid = ews.tmid " + // + " GROUP BY ew.estid " + // + " HAVING physical_progress > 0 " + // + ") AS prog " + // + "ON ew.estid = prog.estid " + // + "SET " + // + " ew.percentage = prog.physical_progress, " + // + " ew.update_status = CASE " + // + " WHEN prog.physical_progress = 0 THEN 'pending' " + // + " WHEN prog.physical_progress < 100 THEN 'Inprogress' " + // + " ELSE 'Completed' " + // + " END"; + + String sqlUpdateQuery = "UPDATE works_details ew " + + "JOIN ( " + + " SELECT " + + " ew.estid, " + + " ROUND(SUM(COALESCE(etd.percentage, 0)) / COUNT(*), 0) AS physical_progress " + + " FROM works_details ew " + + " JOIN erp_works_task ewt ON ewt.estid = ew.estid " + + " JOIN erp_works_stages ews ON ews.estid = ew.estid " + + " LEFT JOIN ( " + // + " -- Get only the latest active record per (estid, wtid, tmid) " + + " SELECT etd.* " + + " FROM erp_task_data etd " + + " INNER JOIN ( " + + " SELECT estid, wtid, tmid, MAX(tdid) AS max_tdid " + + " FROM erp_task_data " + + " WHERE isactive = 1 " + + " GROUP BY estid, wtid, tmid " + + " ) latest ON latest.estid = etd.estid " + + " AND latest.wtid = etd.wtid " + + " AND latest.tmid = etd.tmid " + + " AND latest.max_tdid = etd.tdid " + + " ) etd ON etd.estid = ew.estid " + + " AND etd.wtid = ewt.id " + + " AND etd.tmid = ews.tmid " + + " GROUP BY ew.estid " + + " HAVING physical_progress > 0 " + + ") AS prog " + + "ON ew.estid = prog.estid " + + "SET " + + " ew.percentage = prog.physical_progress, " + + " ew.update_status = CASE " + + " WHEN prog.physical_progress = 0 THEN 'pending' " + + " WHEN prog.physical_progress < 100 THEN 'Inprogress' " + + " ELSE 'Completed' " + + " END"; + + System.out.println("Percentage Update : " + sqlUpdateQuery + " --> ESTID : " + estid); + jdbcWorksTemplate.update(sqlUpdateQuery); + + return Collections.singletonList(response); + } + + public boolean updateStatus(String percentage, String estid, String wtid) { + int statusAffected = 0; + + // String selectSql = "SELECT " + // + " ewt.id, " + // + " ROUND(SUM(COALESCE(etd.percentage, 0)) / COUNT(*), 0) AS + // physical_progress " + // + "FROM " + // + " erp_works ew " + // + "JOIN " + // + " erp_works_task ewt ON ewt.estid = ew.estid " + // + "JOIN " + // + " erp_works_stages ews ON ews.estid = ew.estid " + // + "LEFT JOIN ( " + // // + " -- Subquery to get only latest entry per estid/wtid/tmid based on + // highest + // // tdid " + // + " SELECT etd.* " + // + " FROM erp_task_data etd " + // + " INNER JOIN ( " + // + " SELECT estid, wtid, tmid, MAX(tdid) AS max_tdid " + // + " FROM erp_task_data " + // + " WHERE isactive = 1 " + // + " GROUP BY estid, wtid, tmid " + // + " ) latest ON latest.estid = etd.estid " + // + " AND latest.wtid = etd.wtid " + // + " AND latest.tmid = etd.tmid " + // + " AND latest.max_tdid = etd.tdid " + // + ") etd ON etd.estid = ew.estid " + // + " AND etd.wtid = ewt.id " + // + " AND etd.tmid = ews.tmid " + // + "WHERE " + // + " ewt.id = ? " + // + "GROUP BY " + // + " ew.estid " + // + "HAVING " + // + " physical_progress > 0"; + + String selectSql = "SELECT " + + " ewt.id, " + + " ROUND(SUM(COALESCE(etd.percentage, 0)) / COUNT(*), 0) AS physical_progress " + + "FROM " + + " works_details ew " + + "JOIN " + + " erp_works_task ewt ON ewt.estid = ew.estid " + + "JOIN " + + " erp_works_stages ews ON ews.estid = ew.estid " + + "LEFT JOIN ( " + // + " -- Subquery to get only latest entry per estid/wtid/tmid based on highest + // tdid " + + " SELECT etd.* " + + " FROM erp_task_data etd " + + " INNER JOIN ( " + + " SELECT estid, wtid, tmid, MAX(tdid) AS max_tdid " + + " FROM erp_task_data " + + " WHERE isactive = 1 " + + " GROUP BY estid, wtid, tmid " + + " ) latest ON latest.estid = etd.estid " + + " AND latest.wtid = etd.wtid " + + " AND latest.tmid = etd.tmid " + + " AND latest.max_tdid = etd.tdid " + + ") etd ON etd.estid = ew.estid " + + " AND etd.wtid = ewt.id " + + " AND etd.tmid = ews.tmid " + + "WHERE " + + " ewt.id = ? " + + "GROUP BY " + + " ew.estid " + + "HAVING " + + " physical_progress > 0"; + + List> rows = jdbcWorksTemplate.queryForList(selectSql, new Object[] { wtid }); + + for (Map row : rows) { + int physical_progress = ((Number) row.get("physical_progress")).intValue(); + + String updateSql = "UPDATE `erp_works_task` SET `percentage`=? WHERE `estid`=? AND `id`=?"; + statusAffected = jdbcWorksTemplate.update(updateSql, physical_progress, estid, wtid); + + System.out.println("Updated " + statusAffected + " row(s) for estid=" + estid + ", wtid=" + wtid); + } + + // System.out.println("UPDATE `erp_works_task` SET `percentage`="+percentage+" + // WHERE `estid`="+estid+" AND `id`="+wtid); + // System.out.println("UPDATE `erp_works_task` Status: " + statusAffected); + + // int rowsAffected = 0; + // if (statusAffected > 0) { + /* + * sqlQuery = "UPDATE `works_update` SET `percentage`=? WHERE `id`=?"; + * rowsAffected = jdbcWorksTemplate.update(sqlQuery, new + * Object[]{percentage,estid}); + * + * System.out.println("UPDATE `works_update` SET `percentage`=" + * +percentage+" WHERE `id`="+estid); + * System.out.println("UPDATE `works_update` Status: " + rowsAffected); + */ + // } + return true; + } + + public List> getZoneReport() { + + String sql = "SELECT " + + " CASE " + + " WHEN zone = '00' THEN 'HQ' " + + " ELSE zone " + + " END AS zone, " + + " COUNT(estid) AS Total, " + + " SUM(CASE WHEN percentage = 100 THEN 1 ELSE 0 END) AS CompletedTotal, " + + " SUM(CASE WHEN percentage > 0 AND percentage < 100 THEN 1 ELSE 0 END) AS OngoingTotal, " + + " SUM(CASE WHEN percentage = 0 THEN 1 ELSE 0 END) AS NotStartedTotal " + + "FROM " + + " `works_details` " + + "GROUP BY " + + " zone " + + "ORDER BY " + + " zone;"; + + List> result = jdbcWorksTemplate.queryForList(sql); + + // Prepare the response + Map response = new HashMap<>(); + response.put("ZoneData", result); + response.put("message", "EST Activity Zone Data List."); + + // Return the response wrapped in a singleton list + return Collections.singletonList(response); + } + + public List> getWardReport(String zone) { + + String sql = "SELECT " + + " CASE " + + " WHEN zone = '00' THEN 'HQ' " + + " ELSE zone " + + " END AS zone, " + + " CASE " + + " WHEN ward = '000' THEN 'HQ' " + + " ELSE ward " + + " END AS ward, " + + " COUNT(estid) AS Total, " + + " SUM(CASE WHEN percentage = 100 THEN 1 ELSE 0 END) AS CompletedTotal, " + + " SUM(CASE WHEN percentage > 0 AND percentage < 100 THEN 1 ELSE 0 END) AS OngoingTotal, " + + " SUM(CASE WHEN percentage = 0 THEN 1 ELSE 0 END) AS NotStartedTotal " + + "FROM " + + " `works_details` " + + "WHERE zone=? " + + "GROUP BY " + + " zone, ward " + + "ORDER BY " + + " ward"; + + List> result = jdbcWorksTemplate.queryForList(sql, zone); + + // Prepare the response + Map response = new HashMap<>(); + response.put("estWorklistData", result); + response.put("message", "EST Activity Data List."); + + // Return the response wrapped in a singleton list + return Collections.singletonList(response); + } + + public List> getListReport(String ward, String statusFilter) { + + StringBuilder sqlBuilder = new StringBuilder("SELECT " + + "wu.`estid` AS estid, " + + "tm.`description` AS works, " + + "tm.id AS typeofworkid, " + + "wu.`estimate_no`, " + + "wu.`estimate_date`, " + + "wu.`zone`, " + + "wu.`ward`, " + + "wu.`project_name`, " + + "wu.`location`, " + + "dm.`dept_name` AS `department`, " + + "wu.`contractor_name`, " + + "CAST(wu.contractor_period AS CHAR) AS contractor_period, " + + "fs.`name` AS `fund_source`, " + + "s.`name` AS `scheme`, " + + "wm.`name` AS `category`, " + + "tm.`description` AS `sub_category`, " + + "wu.`estimation_amount` AS `est_amount`, " + + "wu.`technical_sanction_date` AS `tech_sanc_date`, " + + "wu.`admin_sanction_date` AS `admin_sanc_date`, " + + "wu.`tender_called_date` AS `tender_call_date`, " + + "wu.`tender_finalized_date` AS `tender_fin_date`, " + + "wu.`loa_date` AS `loa_date`, " + + "wu.`agreement_date` AS `agreement_date`, " + + "wu.`work_order_date` AS `work_order_date`, " + + "wu.`work_commenced_date` AS `work_comm_date`, " + + "wu.`percentage` AS `percentage` " + + "FROM `works_details` wu " + + "LEFT JOIN department_master dm ON dm.id = wu.`department` " + + "LEFT JOIN fundsource_master fs ON fs.id = wu.`fund_source` " + + "LEFT JOIN scheme_master s ON s.id = wu.`scheme` " + + "LEFT JOIN workstype_master wm ON wm.id = wu.`category` " + + "LEFT JOIN typeofwork_master tm ON tm.id = wu.`sub_category` " + + "WHERE wu.`ward` = '" + ward + "' "); + + switch (statusFilter) { + case "completed": + sqlBuilder.append("AND wu.percentage = 100 "); + break; + case "ongoing": + sqlBuilder.append("AND wu.percentage > 0 AND wu.percentage < 100 "); + break; + case "not_started": + sqlBuilder.append("AND wu.percentage = 0 "); + break; + // default is no filtering (all) + } + + String sql = sqlBuilder.toString(); + List> result = jdbcWorksTemplate.queryForList(sql); + + // Prepare the response + Map response = new HashMap<>(); + response.put("estWorklistData", result); + response.put("message", "EST Data List."); + + // Return the response wrapped in a singleton list + return Collections.singletonList(response); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ddde7c02..762fa704 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ -#spring.profiles.active=local +# spring.profiles.active=local spring.profiles.active=production spring.application.name=Mobile Service @@ -89,3 +89,5 @@ nightshelterinspection_document_foldername = night_shelter_inspection_documents/ roadwarweb_foldername = road_war_web_files/ volunteer_foldername = volunteer_app_files/ +works_test_foldername = works_activity_test/ +