Skip to content
Snippets Groups Projects
Commit 06dc4011 authored by Yan0305's avatar Yan0305
Browse files

Add ,edit and delete device

parent 44d93fba
No related branches found
No related tags found
1 merge request!69Add ,edit and delete device
......@@ -67,7 +67,7 @@ public class ViewController {
public String loadMainView_3(Model model) {
System.out.println("hospitalLoadMain2");
//System.out.println("loadMainView_2");
Result result = superAdminService.selectAllHospital();
Result result = hospitalService.selectAllHospital();
// Add data to the model
System.out.println("result"+result.getData());
model.addAttribute("tableData", result.getData());
......
......@@ -18,7 +18,6 @@ public class DeviceController {
/**
* select all
*
* @return
*/
@PostMapping("/selectAll")
......@@ -27,18 +26,53 @@ public class DeviceController {
return result;
}
/**
* insert
* @return
*/
@PostMapping("/insert")
public Result insertDeviceInform(@RequestBody Device device){
System.out.println(device);
Result result= deviceService.insertDevice(device);
System.out.println(result);
return result;
}
/**
*delete device information by id
* @param ids
* @return
*/
@DeleteMapping("/deleteDeviceById")
public Result deletePatientById(@RequestBody List<Integer> ids){
Result result = deviceService.deleteById(ids, "hospital");
return result;
}
/**
* fuzzy query
*
* @param
* @return
*/
@PostMapping("/select")
public Result selectByItem(@RequestBody Device device) {
public Result selectByItem(@RequestBody Device device){
System.out.println(device);
Result result = deviceService.selectByItem(device);
return result;
}
/**
* Update data
* @param device
* @return
*/
@PutMapping("/update")
public Result updateById(@RequestBody Device device){
System.out.println("update" + device);
Result result = deviceService.update(device);
return result;
}
}
......@@ -19,6 +19,45 @@ public class DeviceMapper {
@Autowired
JdbcTemplate jdbcTemplate;
/**
* update device data
* @param device
* @return
*/
public int update(Device device) {
String sql = "UPDATE device SET name = ?,type = ?, freetime=?,hospitalId =?, id =? WHERE id = ?";
int update = jdbcTemplate.update(sql, preparedStatement -> {
preparedStatement.setString(1, device.getName());
preparedStatement.setString(2, device.getType());
preparedStatement.setDate(3, device.getFreeTime());
preparedStatement.setInt(4, device.getHospitalId());
preparedStatement.setInt(5, device.getId());
preparedStatement.setInt(6, device.getId());
});
return update;
}
/**
* Batch deletion based on id
* @param ids
* @return
*/
public int[] deleteByIdAndType(List<Integer> ids, String type) {
String sql = "DELETE FROM device WHERE id=?";
String sql_1="ALTER TABLE device DROP COLUMN id;";
String sql_2="ALTER TABLE device 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;
}
/**
* select device
......@@ -49,6 +88,10 @@ public class DeviceMapper {
List<Object> params = new ArrayList<>();
//Dynamic concatenation of query conditions
if (device.getHospitalId() > -1) {
sql.append(" AND hospitalId = ?");
params.add(device.getHospitalId());
}
if (device.getName() != null && device.getName() != "") {
sql.append(" AND name LIKE ?");
params.add("%" + device.getName() + "%");
......@@ -72,7 +115,7 @@ public class DeviceMapper {
*/
public Object getDeviceByName(String name){
try {
// 1. 查询 device 表
// 查询 device 表
String sql = "select * from device where name=?";
return jdbcTemplate.queryForObject(sql, new Object[]{name}, new BeanPropertyRowMapper<>(Device.class));
} catch (Exception e) {
......@@ -82,4 +125,32 @@ public class DeviceMapper {
return null;
}
/**
* insert device
* @param device
*/
public Result insertDeviceInform(Device device) {
//insert data
SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate)
.withTableName("device")
.usingGeneratedKeyColumns("id");
Map<String, Object> parameters = new HashMap<>();
parameters.put("Id",device.getId());
parameters.put("hospitalId",device.getHospitalId());
parameters.put("name",device.getName());
parameters.put("type",device.getType());
parameters.put("freeTime", device.getFreeTime());
Number number = insert.executeAndReturnKey(parameters);
if(number.longValue() > 0){
return Result.success(ResponseCode.SUCCESS);
}else {
return Result.error(ResponseCode.ERROR);
}
}
}
......@@ -7,12 +7,32 @@ import java.util.List;
public interface DeviceService {
/**
* @param device
* @return
*/
Result insertDevice(Device device);
/**
* delete information by id
* @param ids
* @return
*/
Result deleteById(List<Integer> ids, String type);
/**
* Query device information
* @return
*/
Result selectAllDevice(int hospitalId);
/**
* Update data
* @param device
* @return
*/
Result update(Device device);
/**
* fuzzy query
* @param device
......
......@@ -18,6 +18,18 @@ public class DeviceServiceImpl implements DeviceService {
@Autowired
private DeviceMapper deviceMapper;
@Override
public Result insertDevice(Device device) {
Result result = deviceMapper.insertDeviceInform(device);
return result;
}
@Override
public Result deleteById(List<Integer> ids, String type) {
deviceMapper.deleteByIdAndType(ids,type);
return Result.success(ResponseCode.SUCCESS);
}
@Override
public Result selectAllDevice(int hospitalId) {
List<Device> selectVOS = deviceMapper.selectAllDevice(hospitalId);
......@@ -31,6 +43,15 @@ public class DeviceServiceImpl implements DeviceService {
return Result.success(deviceVOS);
}
@Override
public Result update(Device device) {
int update = deviceMapper.update(device);
if (update > 0) {
return Result.success(ResponseCode.SUCCESS);
}
return Result.error(ResponseCode.ERROR);
}
@Override
public Result selectByItem(Device device) {
List<Device> selectVos = deviceMapper.selectByItem(device);
......
//add data
//add data of admin
$(document).on("click","#addButton",function (){
const overlay = document.getElementById("overlay_add");
const formContainerAdd = document.getElementById("formContainer_add");
......@@ -14,7 +14,6 @@ $(document).on("click","#addDevice",function (){
"name":document.getElementById("addName").value,
"type":document.getElementById("addType").value,
"freeTime":document.getElementById("addFreeTime").value,
"type":"hospital",
"role":"HOSPITAL"
}
console.log(data)
......@@ -50,8 +49,7 @@ $(document).on("click","#edit",function (){
}
var data={
"id":row.data('id'),
"type":"hospital",
"hospitalId":row.data('hospitalId'),
"hospitalId":localStorage.getItem('hospitalId'),
"name":row.data('name'),
"type":row.data('type'),
"freeTime":row.data('freeTime'),
......@@ -140,7 +138,7 @@ $(document).on("click","#freeze",function (){
var data={
"id":row.data('id'),
"type":"hospital",
"hospitalId":row.data('hospitalId'),
"hospitalId":localStorage.getItem('hospitalId'),
"name":row.data('name'),
"age":row.data('age'),
"phone":row.data('phone'),
......@@ -227,8 +225,8 @@ $(document).on("click", "#searchButton", function () {
var data={
'name':document.getElementById("searchName").value,
'type':document.getElementById("searchType").value,
'hospitalId':localStorage.getItem("hospitalId")
// 'email':document.getElementById("searchAddress").value,
'type':"hospital",
}
$.ajax({
contentType: "application/json",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment