Skip to content
Snippets Groups Projects
Commit 1ce941f7 authored by Yan0305's avatar Yan0305
Browse files

Add and delete patient and nurse

parent c129f72e
No related branches found
No related tags found
1 merge request!68Add and delete patient and nurse
......@@ -6,6 +6,8 @@ import com.cardiff.client_project.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/nurse")
public class NurseController {
......@@ -49,4 +51,27 @@ public class NurseController {
return result;
}
/**
*add nurse
* @param
* @return
*/
@PostMapping("/insert")
public Result insertNurseInform(@RequestBody Nurse nurse){
System.out.println(nurse);
Result result= nurseService.insertNurse(nurse);
System.out.println(result);
return result;
}
/**
*delete nurse information by id
* @param ids
* @return
*/
@DeleteMapping("/deleteNurseById")
public Result deleteNurseById(@RequestBody List<Integer> ids){
Result result = nurseService.deleteById(ids, "hospital");
return result;
}
}
......@@ -36,7 +36,6 @@ public class PatientController {
return result;
}
/**
* Update data
* @param patient
......@@ -48,4 +47,28 @@ public class PatientController {
Result result = patientService.update(patient);
return result;
}
/**
*add patient
* @param
* @return
*/
@PostMapping("/insert")
public Result insertPatientInform(@RequestBody Patient patient){
System.out.println(patient);
Result result= patientService.insertPatient(patient);
System.out.println(result);
return result;
}
/**
*delete patient information by id
* @param ids
* @return
*/
@DeleteMapping("/deletePatientById")
public Result deletePatientById(@RequestBody List<Integer> ids){
Result result = patientService.deleteById(ids, "hospital");
return result;
}
}
\ No newline at end of file
......@@ -100,13 +100,13 @@ public class NurseMapper {
*/
public Object getNurseByName(String name){
try {
// 1. 查询 nurse
//Query nurse table
String sql = "select * from nurse where name=?";
return jdbcTemplate.queryForObject(sql, new Object[]{name}, new BeanPropertyRowMapper<>(Nurse.class));
} catch (Exception e) {
e.printStackTrace();
}
// 如果所有查询都为空,返回 null
// If all queries are null, return null
return null;
}
......@@ -121,4 +121,62 @@ public class NurseMapper {
return type;
}
/**
* insert nurse
* @param nurse
*/
public Result insertNurseInform(Nurse nurse) {
//Check whether the account exists(If the name and email address are the same, the account is confirmed to exist)
String sql = "SELECT count(id) FROM nurse where name = ? and email = ?";
Integer count= jdbcTemplate.queryForObject(sql, new Object[]{nurse.getName(), nurse.getEmail()}, Integer.class);
System.out.println(count);
if(count > 0){
return Result.error(ResponseCode.ACCOUNT_EXISTS_ERROR);
}
//insert data
SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("nurse")
.usingGeneratedKeyColumns("id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("Id",nurse.getId());
parameters.put("hospitalId",nurse.getHospitalId());
parameters.put("name",nurse.getName());
parameters.put("age", nurse.getAge());
parameters.put("phone",nurse.getPhone());
parameters.put("email",nurse.getEmail());
parameters.put("address", nurse.getAddress());
parameters.put("status",nurse.getStatus());
Number number = insert.executeAndReturnKey(parameters);
if(number.longValue() > 0){
return Result.success(ResponseCode.SUCCESS);
}else {
return Result.error(ResponseCode.ERROR);
}
}
/**
* Batch deletion based on id
* @param ids
* @return
*/
public int[] deleteByIdAndType(List<Integer> ids, String type) {
String sql = "DELETE FROM nurse WHERE id=?";
String sql_1="ALTER TABLE nurse DROP COLUMN id;";
String sql_2="ALTER TABLE nurse ADD COLUMN id INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;";
List<Object[]> idList = new ArrayList<>();
// Build parameter list
for (Integer id : ids) {
idList.add(new Object[]{id});
}
// Batch delete
int[] item = jdbcTemplate.batchUpdate(sql, idList);
jdbcTemplate.update(sql_1);
jdbcTemplate.update(sql_2);
return item;
}
}
......@@ -124,6 +124,63 @@ public class PatientMapper {
return type;
}
/**
* insert patient
* @param patient
*/
public Result insertPatientInform(Patient patient) {
//Check whether the account exists(If the name and email address are the same, the account is confirmed to exist)
String sql = "SELECT count(id) FROM patient where name = ? and email = ?";
Integer count= jdbcTemplate.queryForObject(sql, new Object[]{patient.getName(), patient.getEmail()}, Integer.class);
System.out.println(count);
if(count > 0){
return Result.error(ResponseCode.ACCOUNT_EXISTS_ERROR);
}
//insert data
SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("patient")
.usingGeneratedKeyColumns("id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("Id",patient.getId());
parameters.put("hospitalId",patient.getHospitalId());
parameters.put("name",patient.getName());
parameters.put("age", patient.getAge());
parameters.put("phone",patient.getPhone());
parameters.put("email",patient.getEmail());
parameters.put("roleId",patient.getRoleId());
parameters.put("type",patient.getType());
//parameters.put("address", patient.getAddress());
parameters.put("status",patient.getStatus());
Number number = insert.executeAndReturnKey(parameters);
if(number.longValue() > 0){
return Result.success(ResponseCode.SUCCESS);
}else {
return Result.error(ResponseCode.ERROR);
}
}
/**
* Batch deletion based on id
* @param ids
* @return
*/
public int[] deleteByIdAndType(List<Integer> ids, String type) {
String sql = "DELETE FROM patient WHERE id=?";
String sql_1="ALTER TABLE patient DROP COLUMN id;";
String sql_2="ALTER TABLE patient ADD COLUMN id INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;";
List<Object[]> idList = new ArrayList<>();
for (Integer id : ids) {
idList.add(new Object[]{id});
}
int[] item = jdbcTemplate.batchUpdate(sql, idList);
jdbcTemplate.update(sql_1);
jdbcTemplate.update(sql_2);
return item;
}
}
......@@ -6,6 +6,20 @@ import java.util.List;
public interface NurseService {
/**
* insert nurse
* @param nurse
* @return
*/
Result insertNurse(Nurse nurse);
/**
* delete information by id
* @param ids
* @return
*/
Result deleteById(List<Integer> ids, String type);
/**
* Query nurse information
* @return
......
......@@ -7,6 +7,20 @@ import java.util.List;
public interface PatientService {
/**
* insert patient
* @param patient
* @return
*/
Result insertPatient(Patient patient);
/**
* delete information by id
* @param ids
* @return
*/
Result deleteById(List<Integer> ids, String type);
/**
* Query patient information
* @return
......
......@@ -17,6 +17,17 @@ public class NurseServiceImp implements NurseService {
@Autowired
private NurseMapper nurseMapper;
@Override
public Result insertNurse(Nurse nurse) {
Result result = nurseMapper.insertNurseInform(nurse);
return result;
}
@Override
public Result deleteById(List<Integer> ids, String type) {
nurseMapper.deleteByIdAndType(ids,type);
return Result.success(ResponseCode.SUCCESS);
}
@Override
public Result selectAllNurse(int hospitalId) {
......
......@@ -18,6 +18,20 @@ public class PatientServiceImp implements PatientService {
@Autowired
private PatientMapper patientMapper;
@Override
public Result insertPatient(Patient patient) {
patient.setRoleId(Authority.PATIENT);
patient.setType("patient");
Result result = patientMapper.insertPatientInform(patient);
return result;
}
@Override
public Result deleteById(List<Integer> ids, String type) {
patientMapper.deleteByIdAndType(ids,type);
return Result.success(ResponseCode.SUCCESS);
}
@Override
public Result selectAllPatient(int hospitalId) {
List<Patient> selectVOS = patientMapper.selectAllPatient(hospitalId);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment