目录
(1)医院查询接口-功能实现
(2)上传科室接口-功能实现
(1)医院查询接口-功能实现
接口文档:
4.4.查询医院
医院信息
4.4.1.提交地址
http://localhost/api/hosp/hospital/show
在ApiController:添加新的查询医院的方法:
package com.atguigu.yygh.hosp.controller.api;
import com.atguigu.yygh.common.exception.HospitalException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
@RequestMapping("/api/hosp") //这个路径要跟医院系统的路径相对应
public class ApiController {
//注入service
@Autowired
private HospitalService hospitalService;
//注入HospitalSetService
@Autowired
private HospitalSetService hospitalSetService;
//上传医院的接口
@PostMapping("saveHospital")
public Result saveHosp(HttpServletRequest request){
//获取传递过来的医院信息
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
//签名校验
//1获取医院系统中传递过来的签名,这个签名做了MD5加密
String hospSign = (String)paramMap.get("sign");
//2根据传递过来的医院编号,查询数据库,查询签名
String hoscode = (String)paramMap.get("hoscode");
//调用hospitalSetService中的方法根据医院编码获取签名
String singKey= hospitalSetService.getSignKey(hoscode);
//3把数据库查询出来的签名进行MD5的加密
String signMd5 = MD5.encrypt(singKey);
//4判断签名是否一致
if (!hospSign.equals(signMd5)){
throw new HospitalException(ResultCodeEnum.SIGN_ERROR);//抛出我们自定义的异常
}
//传输过程中“+”转换为了“ ”,因此我们要转换回来
String logoDataString = (String)paramMap.get("logoData");
String logoData = logoDataString.replaceAll(" ", "+");
paramMap.put("logoData", logoData);
//调用service的方法把它加入到数据库
hospitalService.save(paramMap);
return Result.ok();
}
//查询医院的的接口
@ApiOperation(value = "获取医院信息")
@PostMapping("hospital/show")
public Result hospital(HttpServletRequest request) {
//获取传递过来的医院信息
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
//获取传过来的医院编号
String hoscode = (String)paramMap.get("hoscode");
//签名校验
//1获取医院系统中传递过来的签名,这个签名做了MD5加密
String hospSign = (String)paramMap.get("sign");
//2根据传递过来的医院编号,查询数据库,查询签名
//调用hospitalSetService中的方法根据医院编码获取签名
String singKey= hospitalSetService.getSignKey(hoscode);
//3把数据库查询出来的签名进行MD5的加密
String signMd5 = MD5.encrypt(singKey);
//4判断签名是否一致
if (!hospSign.equals(signMd5)){
throw new HospitalException(ResultCodeEnum.SIGN_ERROR);//抛出我们自定义的异常
}
//调用Service方法实现根据医院编号进行查询
Hospital hospital= hospitalService.getByHoscode(hoscode);
return Result.ok(hospital);
}
}
HospitalService :
package com.atguigu.yygh.hosp.service;
import com.atguigu.yygh.model.hosp.Hospital;
import java.util.Map;
public interface HospitalService {
//上传医院接口的方法
void save(Map<String, Object> paramMap);
//根据医院编号进行查询
Hospital getByHoscode(String hoscode);
}
实现类:HospitalServiceImpl :
//根据医院编号进行查询
@Override
public Hospital getByHoscode(String hoscode) {
Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
return hospital;
}
点击医院管理: 数据就查询出来了:
(2)上传科室接口-功能实现
科室管理主要做3个接口:上传接口、查询接口、删除接口
接口数据分析
{
"hoscode": "1000_0",
"depcode": "200050923",
"depname": "门诊部核酸检测门诊(东院)",
"intro": "门诊部核酸检测门诊(东院)",
"bigcode": "44f162029abb45f9ff0a5f743da0650d",
"bigname": "体检科"
} |
之前上传的科室实体类:
package com.atguigu.yygh.model.hosp;
import com.atguigu.yygh.model.base.BaseMongoEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* <p>
* Department
* </p>
*
* @author qy
*/
@Data
@ApiModel(description = "Department")
@Document("Department")
public class Department extends BaseMongoEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "医院编号")
@Indexed //普通索引
private String hoscode;
@ApiModelProperty(value = "科室编号")
@Indexed(unique = true) //唯一索引
private String depcode;
@ApiModelProperty(value = "科室名称")
private String depname;
@ApiModelProperty(value = "科室描述")
private String intro;
@ApiModelProperty(value = "大科室编号")
private String bigcode;
@ApiModelProperty(value = "大科室名称")
private String bigname;
}
创建reposity、service、controller:
创建:DepartmentRepository
package com.atguigu.yygh.hosp.repository;
import com.atguigu.yygh.model.hosp.Department;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DepartmentRepository extends MongoRepository<Department,String> {
}
创建:DepartmentService 接口
package com.atguigu.yygh.hosp.service;
public interface DepartmentService {
}
实现类:DepartmentServiceImpl:
package com.atguigu.yygh.hosp.service.impl;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DepartmentServiceImpl implements DepartmentService {
//service操作Reposity操作MongoDB
//注入reposity
@Autowired
private DepartmentRepository departmentRepository;
}
controller使用ApiController:
医院系统上传科室对应的方法:
记得接口要对应:
在ApiController:添加上传科室的方法:首先注入departmentService
//上传科室的接口
@ApiOperation(value = "上传科室")
@PostMapping("saveDepartment")
public Result saveDepartment(HttpServletRequest request) {
//获取传递过来的科室的信息
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
//获取传过来的医院编号
String hoscode = (String)paramMap.get("hoscode");
//签名校验
//1获取医院系统中传递过来的签名,这个签名做了MD5加密
String hospSign = (String)paramMap.get("sign");
//2根据传递过来的医院编号,查询数据库,查询签名
//调用hospitalSetService中的方法根据医院编码获取签名
String singKey= hospitalSetService.getSignKey(hoscode);
//3把数据库查询出来的签名进行MD5的加密
String signMd5 = MD5.encrypt(singKey);
//4判断签名是否一致
if (!hospSign.equals(signMd5)){
throw new HospitalException(ResultCodeEnum.SIGN_ERROR);//抛出我们自定义的异常
}
//调用DepartmentService里面的方法
departmentService.save(paramMap);
return Result.ok();
}
DepartmentService 接口:
package com.atguigu.yygh.hosp.service;
import java.util.Map;
public interface DepartmentService {
//上传科室的接口
void save(Map<String, Object> paramMap);
}
DepartmentService Impl实现类:
package com.atguigu.yygh.hosp.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
@Service
public class DepartmentServiceImpl implements DepartmentService {
//service操作Reposity操作MongoDB
//注入reposity
@Autowired
private DepartmentRepository departmentRepository;
//上传科室的接口
@Override
public void save(Map<String, Object> paramMap) {
//将paramMap转换为department对象
String paramMapString = JSONObject.toJSONString(paramMap);
Department department = JSONObject.parseObject(paramMapString, Department.class);
//调用reposity的方法根据医院编号和科室编号进行查询
Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
//判断是否为空
if (departmentExist!=null){
departmentExist.setUpdateTime(new Date());
departmentExist.setIsDeleted(0);
departmentRepository.save(department);//修改
}else{
department.setCreateTime(new Date());
department.setUpdateTime(new Date());
department.setIsDeleted(0);
departmentRepository.save(department);//添加
}
}
}
DepartmentRepository :添加方法
package com.atguigu.yygh.hosp.repository;
import com.atguigu.yygh.model.hosp.Department;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DepartmentRepository extends MongoRepository<Department,String> {
//根据医院编号和科室编号进行查询 方法不用我们自己实现MongoRepository会给我们实现
Department getDepartmentByHoscodeAndDepcode(String hoscode, String depcode);
}
启动项目:
复制department.json的数据进行上传:
刷新mongodb可视化客户端:发现成功添加数据: