Skip to content
Snippets Groups Projects
Commit 85bd2c9d authored by Burhan Akbar's avatar Burhan Akbar
Browse files

update available beds jdbc template and mapping (demo)

parent bdcf338c
No related branches found
No related tags found
1 merge request!40Update beds with sql db
/*
Navicat Premium Data Transfer
Source Server : wyl
Source Server Type : MySQL
Source Server Version : 80031
Source Host : localhost:3306
Source Schema : health_care
Target Server Type : MySQL
Target Server Version : 80031
File Encoding : 65001
Date: 26/11/2024 14:50:12
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for common_admin
-- ----------------------------
DROP TABLE IF EXISTS `common_admin`;
CREATE TABLE `common_admin` (
`id` int NOT NULL AUTO_INCREMENT,
`email` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`roleId` int NULL DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`status` int NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of common_admin
-- ----------------------------
INSERT INTO `common_admin` VALUES (1, '264978365@qq.com', '$2a$10$5UzuKUxgR8H0RzLYrP.BUu8.76yKwMD4XkvJIu/xxB7AAHWfphFQm', 'commonAdmin', 1, '22222', 'jjy', 1);
INSERT INTO `common_admin` VALUES (2, '26497@qq.com', '$2a$10$4uTgtSP1B6TLvKUicCuA0.lGwDKfNxtF2vJTXqX4WfekNa5C9Ny4u', 'commonAdmin', 1, '111', 'test', 1);
-- ----------------------------
-- Table structure for device
-- ----------------------------
DROP TABLE IF EXISTS `device`;
CREATE TABLE `device` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`freetime` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of device
-- ----------------------------
-- ----------------------------
-- Table structure for hospital
-- ----------------------------
DROP TABLE IF EXISTS `hospital`;
CREATE TABLE `hospital` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`amountPatient` int NULL DEFAULT NULL,
`maxAmount` int NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`address` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`status` int NULL DEFAULT NULL,
`roleId` int NULL DEFAULT NULL,
`email` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of hospital
-- ----------------------------
INSERT INTO `hospital` VALUES (1, 'jjy', 100, 300, 'hospital', 'Cardiffe', '22222', 0, 3, '264978365@qq.com');
INSERT INTO `hospital` VALUES (2, 'park00000', 5, 10, 'hospital', 'cardiff', '1234', 0, 3, '2649783657@qq.com');
INSERT INTO `hospital` VALUES (3, 'test', 0, 0, 'hospital', 'cardiff', '123', 0, 3, '264@qq.com');
INSERT INTO `hospital` VALUES (4, 'ttttttt', 0, 0, 'hospital', 'cardiff', '123', 0, 3, '26492@qq.com');
-- ----------------------------
-- Table structure for hospital_device
-- ----------------------------
DROP TABLE IF EXISTS `hospital_device`;
CREATE TABLE `hospital_device` (
`hospitalId` int NULL DEFAULT NULL,
`deviceId` int NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of hospital_device
-- ----------------------------
-- ----------------------------
-- Table structure for patient
-- ----------------------------
DROP TABLE IF EXISTS `patient`;
CREATE TABLE `patient` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`roleId` int NULL DEFAULT NULL,
`status` int NULL DEFAULT NULL,
`hospitalId` int NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`age` int NULL DEFAULT NULL,
`phone` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
`email` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of patient
-- ----------------------------
INSERT INTO `patient` VALUES (1, 'jyy', '$2a$10$obgmyiqAuhIkBjuw3bROr.LkCh.Nb70e4dL8lwKjSsPxS8gSpQxSC', 2, 0, 0, 'patient', 0, NULL, NULL);
-- ----------------------------
-- Table structure for patient_hospital
-- ----------------------------
DROP TABLE IF EXISTS `patient_hospital`;
CREATE TABLE `patient_hospital` (
`hosptialId` int NULL DEFAULT NULL,
`patientId` int NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of patient_hospital
-- ----------------------------
-- ----------------------------
-- Table structure for role
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`roleId` int NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES (0, 'SUPER');
INSERT INTO `role` VALUES (1, 'ADMIN');
INSERT INTO `role` VALUES (2, 'PATIENT');
INSERT INTO `role` VALUES (3, 'HOSPITAL');
-- ----------------------------
-- Table structure for super_admin
-- ----------------------------
DROP TABLE IF EXISTS `super_admin`;
CREATE TABLE `super_admin` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL,
`roleId` int NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb3 COLLATE = utf8mb3_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of super_admin
-- ----------------------------
INSERT INTO `super_admin` VALUES (1, '2649783657@qq.com', '$2a$10$5UzuKUxgR8H0RzLYrP.BUu8.76yKwMD4XkvJIu/xxB7AAHWfphFQm', 0);
-- ----------------------------
-- Procedure structure for reset_primary_keys
-- ----------------------------
DROP PROCEDURE IF EXISTS `reset_primary_keys`;
delimiter ;;
CREATE PROCEDURE `reset_primary_keys`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE current_id INT DEFAULT 0;
DECLARE cur CURSOR FOR
SELECT id FROM common_admin ORDER BY id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
-- 重新更新每一条记录的 ID
read_loop: LOOP
FETCH cur INTO current_id;
IF done THEN
LEAVE read_loop;
END IF;
-- 根据新的排序修改 ID
UPDATE common_admin SET id = current_id - 1 WHERE id = current_id;
END LOOP;
CLOSE cur;
END
;;
delimiter ;
SET FOREIGN_KEY_CHECKS = 1;
......@@ -10,6 +10,7 @@ import java.util.List;
@RestController
@RequestMapping("/api/hospitals")
@PreAuthorize("hasAnyRole('ADMIN', 'HOSPITAL', 'SUPER')")
public class HospitalController {
@Autowired
......
package com.cardiff.client_project.mapper;
import com.cardiff.client_project.pojo.dto.HospitalDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.stream.Collectors;
@Repository
public class HospitalMapper {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<HospitalDTO> findAvailableBeds(String name, String location, Integer minBeds) {
String sql = """
SELECT
h.id AS hospital_id,
h.name AS hospital_name,
h.address AS location,
h.phone,
h.maxAmount AS total_beds,
(h.maxAmount - h.amountPatient) AS available_beds,
ROUND(((h.amountPatient * 1.0) / h.maxAmount * 100), 1) AS occupancy_rate
FROM
hospital h
WHERE
h.status = 1
AND (h.maxAmount - h.amountPatient) > ?
AND (? IS NULL OR h.name LIKE ?)
AND (? IS NULL OR h.address LIKE ?)
ORDER BY
available_beds DESC,
occupancy_rate ASC
""";
return jdbcTemplate.query(sql,
ps -> {
int paramIndex = 1;
ps.setInt(paramIndex++, minBeds != null ? minBeds : 0);
ps.setString(paramIndex++, name);
ps.setString(paramIndex++, name != null ? "%" + name + "%" : "%");
ps.setString(paramIndex++, location);
ps.setString(paramIndex++, location != null ? "%" + location + "%" : "%");
},
(rs, rowNum) -> {
HospitalDTO hospital = new HospitalDTO();
hospital.setId(rs.getInt("hospital_id"));
hospital.setName(rs.getString("hospital_name"));
hospital.setLocation(rs.getString("location"));
hospital.setPhone(rs.getString("phone"));
hospital.setTotalBeds(rs.getInt("total_beds"));
hospital.setAvailableBeds(rs.getInt("available_beds"));
hospital.setOccupancyRate(rs.getDouble("occupancy_rate"));
return hospital;
});
}
public int insertHospital(HospitalDTO hospitalDTO) {
String sql = """
INSERT INTO hospital (name, address, phone, maxAmount, amountPatient, status)
VALUES (?, ?, ?, ?, 0, 1)
""";
return jdbcTemplate.update(sql,
hospitalDTO.getName(),
hospitalDTO.getLocation(),
hospitalDTO.getPhone(),
hospitalDTO.getTotalBeds()
);
}
public int updateHospital(HospitalDTO hospitalDTO) {
String sql = """
UPDATE hospital
SET name = ?,
address = ?,
phone = ?,
maxAmount = ?
WHERE id = ?
""";
return jdbcTemplate.update(sql,
hospitalDTO.getName(),
hospitalDTO.getLocation(),
hospitalDTO.getPhone(),
hospitalDTO.getTotalBeds(),
hospitalDTO.getId()
);
}
public int[] deleteHospitalByIds(List<Integer> ids) {
String sql = "DELETE FROM hospital WHERE id = ?";
List<Object[]> batchArgs = ids.stream()
.map(id -> new Object[]{id})
.collect(Collectors.toList());
return jdbcTemplate.batchUpdate(sql, batchArgs);
}
public List<HospitalDTO> findAllHospitals() {
String sql = """
SELECT
h.id AS hospital_id,
h.name AS hospital_name,
h.address AS location,
h.phone,
h.maxAmount AS total_beds,
(h.maxAmount - h.amountPatient) AS available_beds,
ROUND(((h.amountPatient * 1.0) / h.maxAmount * 100), 1) AS occupancy_rate
FROM hospital h
WHERE h.status = 1
ORDER BY h.name
""";
return jdbcTemplate.query(sql, (rs, rowNum) -> {
HospitalDTO hospital = new HospitalDTO();
hospital.setId(rs.getInt("hospital_id"));
hospital.setName(rs.getString("hospital_name"));
hospital.setLocation(rs.getString("location"));
hospital.setPhone(rs.getString("phone"));
hospital.setTotalBeds(rs.getInt("total_beds"));
hospital.setAvailableBeds(rs.getInt("available_beds"));
hospital.setOccupancyRate(rs.getDouble("occupancy_rate"));
return hospital;
});
}
public int updateBedCount(int hospitalId, int currentPatients) {
String sql = """
UPDATE hospital
SET amountPatient = ?
WHERE id = ?
AND maxAmount >= ?
""";
return jdbcTemplate.update(sql,
currentPatients,
hospitalId,
currentPatients);
}
}
\ No newline at end of file
package com.cardiff.client_project.pojo.dto;
import lombok.Data;
@Data
public class HospitalDTO {
private int id;
private String name;
private String location;
private String phone;
private int totalBeds;
private int availableBeds;
private double occupancyRate;
}
\ No newline at end of file
......@@ -76,4 +76,17 @@ public class HospitalServiceImpl implements HospitalService {
return Result.error("Error fetching hospitals: " + e.getMessage());
}
}
@Override
public Result updateBedCount(int hospitalId, int currentPatients) {
try {
int result = hospitalMapper.updateBedCount(hospitalId, currentPatients);
if (result > 0) {
return Result.success("Bed count updated successfully");
}
return Result.error("Failed to update bed count - check capacity limits");
} catch (Exception e) {
return Result.error("Error updating bed count: " + e.getMessage());
}
}
}
\ No newline at end of file
......@@ -217,47 +217,50 @@ $(document).on("dblclick","#searchButton",function (){
//use jQuery's load() to load content dynamically,Events cannot be bound in dom mode
$(document).on("click", "#searchButton", function () {
let status=document.getElementById("searchStatus").value;
if(status==="active"){
status=1;
}
else if(status==="inactive"){
status=0;
}else {
status=1;
let status = document.getElementById("searchStatus").value;
if (status === "active") {
status = 1;
} else if (status === "inactive") {
status = 0;
} else {
status = ""; // Allow all statuses if none selected
}
var data={
'name':document.getElementById("searchName").value,
'phone':document.getElementById("searchPhone").value,
'email':document.getElementById("searchAddress").value,
'type':"hospital",
'status':status
}
var data = {
'name': document.getElementById("searchName").value,
'phone': document.getElementById("searchPhone").value,
'address': document.getElementById("searchAddress").value,
'type': "hospital",
'status': status
};
console.log("Search data:", data); // Debugging line
$.ajax({
contentType: "application/json",
url: "/superAdmin/select",
type: "POST",
data: JSON.stringify(data),
dataType: "Json",
success: function (data) {
if(data.code!=0){
updateTable(data.data);
// location.reload()
}else {
alert(data.msg)
dataType: "json",
success: function (response) {
if (response.code !== 0) {
updateTable(response.data);
} else {
alert(response.msg);
}
},
error: function (xhr) {
alert('Error during search: ' + xhr.responseText);
}
});
});
function updateTable(data) {
let tbody = $("table tbody"); // choose table's tbody
tbody.empty(); // clean old data
console.log(data)
// create new table
let tbody = $("table tbody");
tbody.empty();
console.log("Table data:", data); // Debugging line
data.forEach(row => {
const tr = `
<tr
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment