尚医通
一、上传医院接口
参考《尚医通API接口文档.docx》业务接口4.1上传医院
参考《医院接口模拟系统.docx》进行接口测试与数据上传
- 这里要整合
hospital-manage
老师已经做好发来的模块
1、 集成mongodb
1.1 添加依赖
service-hosp模块pom.xml添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1.2 添加配置
在application.properties文件添加配置
#mongodb地址
spring.data.mongodb.host=47.113.247.182
spring.data.mongodb.port=27017
spring.data.mongodb.database=yygh_hosp
或
spring.data.mongodb.uri=mongodb://47.113.247.182:27017/yygh_hosp
说明:改为自己安装mongodb的ip地址
2、 添加医院基础类
2.1 添加model
说明:由于实体对象没有逻辑,我们已经统一导入
com.atguigu.yygh.model.hosp.Hospital
@Data
@ApiModel(description = "Hospital")
@Document("Hospital")
public class Hospital extends BaseMongoEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "医院编号")
@Indexed(unique = true) //唯一索引
private String hoscode;
@ApiModelProperty(value = "医院名称")
@Indexed //普通索引
private String hosname;
@ApiModelProperty(value = "医院类型")
private String hostype;
@ApiModelProperty(value = "省code")
private String provinceCode;
@ApiModelProperty(value = "市code")
private String cityCode;
@ApiModelProperty(value = "区code")
private String districtCode;
@ApiModelProperty(value = "详情地址")
private String address;
@ApiModelProperty(value = "医院logo")
private String logoData;
@ApiModelProperty(value = "医院简介")
private String intro;
@ApiModelProperty(value = "坐车路线")
private String route;
@ApiModelProperty(value = "状态 0:未上线 1:已上线")
private Integer status;
//预约规则
@ApiModelProperty(value = "预约规则")
private BookingRule bookingRule;
public void setBookingRule(String bookingRule) {
this.bookingRule = JSONObject.parseObject(bookingRule, BookingRule.class);
}
}
2.2 添加Repository
com.achang.yygh.model.hosp.Hospital
@Repository
public interface HospitalRepository extends MongoRepository<Hospital,String> {
}
2.3 添加service接口及实现类
1,添加com.achang.yygh.hosp.service.HospitalService接口
public interface HospitalService {
}
2,添加com.achang.yygh.hosp.service.impl.HospitalServiceImpl接口实现
@Service
public class HospitalServiceImpl implements HospitalService {
@Autowired
private HospitalRepository hospitalRepository;
}
2.4 添加controller
添加com.achang.yygh.hosp.api.ApiController
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
@Autowired
private HositalService hospitalService;
}
说明:平台对外开发的接口都写在该Controller类
3、 上传医院
参考《尚医通API接口文档.doc》业务接口4.1上传医院
医院编号是平台分配的,全局唯一,上传医院接口可以多次调用,如果存在相同编号的为更新操作
3.1 接口数据分析
{
"hoscode": "1000_0",
"hosname": "北京协和医院",
"hostype": "1",
"provinceCode": "110000",
"cityCode": "110100",
"districtCode": "110102",
"address": "大望路",
"intro": "北京协和医院是集医疗、教学、科研于一体的大型三级甲等综合医院,是国家卫生计生委...目标而继续努力。",
"route": "东院区乘车路线:106、...更多乘车路线详见须知。",
"logoData": "iVBORw0KGgoAAAA...NSUhEUg==",
"bookingRule": {
"cycle": "1",
"releaseTime": "08:30",
"stopTime": "11:30",
"quitDay": "-1",
"quitTime": "15:30",
"rule": [
"西院区预约号取号地点:西院区门诊楼一层大厅挂号窗口取号",
"东院区预约号取号地点:东院区老门诊楼一层大厅挂号窗口或新门诊楼各楼层挂号/收费窗口取号"
]
}
}
说明:
1,数据分为医院基本信息与预约规则信息
2,医院logo转换为base64字符串
3,预约规则信息属于医院基本信息的一个属性
4,预约规则rule,以数组形式传递
5,数据传递过来我们还要验证签名,只允许平台开通的医院可以上传数据,保证数据安全性
3.2 添加service接口
1、在HospitalService 类添加接口
public interface HospitalService {
//上传医院接口
void saveHospital(Map<String, Object> map);
}
说明:参数使用Map,减少对象封装,有利于签名校验,后续会体验到
2、在HospitalServiceImpl类添加实现
@Service
public class HospitalServiceImpl implements HospitalService {
@Autowired
private HospitalRepository hospitalRepository;
//上传医院接口
@Override
public void saveHospital(Map<String, Object> map) {
//把map集合转化成对象 Hospital
//先转成 Map -> 字符串
String mapString = JSONObject.toJSONString(map);
//将字符串 -> 对象
Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);
//获取到医院秘钥
String hoscode = hospital.getHoscode();
//根据秘钥获取,判断值是否存在,他会很智能的感觉方法命名来自动实现
Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);
//如果存在,进行修改
if (hospitalExist!=null){
//设置状态
hospital.setStatus(hospitalExist.getStatus());
hospital.setCreateTime(hospitalExist.getCreateTime());
hospital.setUpdateTime(new Date());
hospital.setIsDeleted(0);
//修改操作
hospitalRepository.save(hospital);
}else {
//不存在,进行添加
//设置状态
//0:未上线 1:已上线
hospital.setStatus(0);
hospital.setCreateTime(new Date());
hospital.setUpdateTime(new Date());
hospital.setIsDeleted(0);
//修改操作
hospitalRepository.save(hospital);
}
}
}
说明:
` Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap),Hospital.class);
Map转换为Hospital对象时,预约规则bookingRule为一个对象属性,rule为一个数组属性,因此在转换时我们要重新对应的set方法,不然转换不会成功
@Data
@ApiModel(description = "Hospital")
@Document("Hospital")
public class Hospital extends BaseMongoEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "医院编号")
@Indexed(unique = true) //唯一索引
private String hoscode;
@ApiModelProperty(value = "医院名称")
@Indexed //普通索引
private String hosname;
@ApiModelProperty(value = "医院类型")
private String hostype;
@ApiModelProperty(value = "省code")
private String provinceCode;
@ApiModelProperty(value = "市code")
private String cityCode;
@ApiModelProperty(value = "区code")
private String districtCode;
@ApiModelProperty(value = "详情地址")
private String address;
@ApiModelProperty(value = "医院logo")
private String logoData;
@ApiModelProperty(value = "医院简介")
private String intro;
@ApiModelProperty(value = "坐车路线")
private String route;
@ApiModelProperty(value = "状态 0:未上线 1:已上线")
private Integer status;
//预约规则
@ApiModelProperty(value = "预约规则")
private BookingRule bookingRule;
public void setBookingRule(String bookingRule) {
this.bookingRule = JSONObject.parseObject(bookingRule, BookingRule.class);
}
}
- 设置的时候,直接将他转为BookingRule类对象,并赋值↑
public class BookingRule {
@ApiModelProperty(value = "预约周期")
private Integer cycle;
...
@ApiModelProperty(value = "预约规则")
private List<String>rule;
/**
*
* @param rule
*/
public void setRule(String rule) {
if(!StringUtils.isEmpty(rule)) {
this.rule = JSONArray.parseArray(rule, String.class);
}
}
}
- 设置的时候,直接将他转换数组类型,并赋值↑
3.3 添加repository接口
在HospitalRepository类添加接口
- 这里方法命名为
getHospitalByHoscode()
,mongodb有一个自动实现规则,遵守命名规则,他就会自动实现方法
这里以get开头,然后并指定Hospital类型,再通过ByHoscode得到
@Repository
public interface HospitalRepository extends MongoRepository<Hospital,String> {
//判断是否存在数据
Hospital getHospitalByHoscode(String hoscode);
}
3.4 添加controller接口
在ApiController类添加接口
@RestController
@RequestMapping("/api/hosp/")
public class ApiController {
@Autowired
private HospitalService hospitalService;
//上传医院
@PostMapping("/saveHospital")
//通过HttpServletRequest来获得从来的数据
public Result saveHospital(HttpServletRequest request){
//获取到传来的数据
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> map = HttpRequestHelper.switchMap(parameterMap);
//调用service的方法
hospitalService.saveHospital(map);
return Result.ok();
}
}
3.5 添加工具帮助类
@Slf4j
public class HttpRequestHelper {
public static Map<String, Object> switchMap(Map<String, String[]> paramMap) {
Map<String, Object> resultMap = new HashMap<>();
for (Map.Entry<String, String[]> param : paramMap.entrySet()) {
resultMap.put(param.getKey(), param.getValue()[0]);
}
return resultMap;
}
}
3.6 使用Swagger2测试上传接口
- 启动8201、于老师给做好整合的hospital-manage模块,下面输入上面给的json字符串然后保存添加
- 使用可视化Mongodb软件(
NoSQLBooster for MongoDB
)
4、 参数签名
参考《尚医通API接口文档.doc》业务接口3.1传参说明
4.1 封装签名方法
在service-util模块HttpRequestHelper类添加方法
public static void main(String[] args) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("d", "4");
paramMap.put("b", "2");
paramMap.put("c", "3");
paramMap.put("a", "1");
paramMap.put("timestamp", getTimestamp());
log.info(getSign(paramMap, "111111111"));
}
/**
* 请求数据获取签名
* @param paramMap
* @param signKey
* @return
*/
public static String getSign(Map<String, Object> paramMap, String signKey) {
if(paramMap.containsKey("sign")) {
paramMap.remove("sign");
}
TreeMap<String, Object> sorted = new TreeMap<>(paramMap);
StringBuilder str = new StringBuilder();
for (Map.Entry<String, Object> param : sorted.entrySet()) {
str.append(param.getValue()).append("|");
}
str.append(signKey);
log.info("加密前:"+ str.toString());
String md5Str = MD5.encrypt(str.toString());
log.info("加密后:"+ md5Str);
return md5Str;
}
/**
* 签名校验
* @param paramMap
* @param signKey
* @return
*/
public static boolean isSignEquals(Map<String, Object> paramMap, String signKey) {
String sign = (String)paramMap.get("sign");
String md5Str = getSign(paramMap, signKey);
if(!sign.equals(md5Str)) {
return false;
}
return true;
}
/**
* 获取时间戳
* @return
*/
public static long getTimestamp() {
return new Date().getTime();
}
4.2 上传医院添加签名校验
我们在医院设置的时候,为每个医院生成了医院编码与签名key,因此我在验证签名时要根据医院编码去动态获取签名key,然后再做签名校验
4.2 .1 添加获取签名key接口
1,在HospitalSetService类添加接口
/**
* 获取签名key
* @param hoscode
* @return
*/
String getSignKey(String hoscode);
2,在HospitalSetServiceImpl类实现接口
@Override
public String getSignKey(String hoscode) {
HospitalSet hospitalSet = this.getByHoscode(hoscode);
if(null == hospitalSet) {
throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
}
if(hospitalSet.getStatus().intValue() == 0) {
throw new YyghException(ResultCodeEnum.HOSPITAL_LOCK);
}
return hospitalSet.getSignKey();
}
/**
* 根据hoscode获取医院设置
* @param hoscode
* @return
*/
private HospitalSet getByHoscode(String hoscode) {
return hospitalSetMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
}
4.2.2 修改ApiController类上传医院接口
修改ApiController类上传医院接口
@ApiOperation(value = "上传医院")
@PostMapping("saveHospital")
public Result saveHospital(HttpServletRequest request) {
Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验
String hoscode = (String)paramMap.get("hoscode");
if(StringUtils.isEmpty(hoscode)) {
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//签名校验
if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
hospitalService.save(paramMap);
return Result.ok();
}
5、 图片base64编码
5.1 图片base64说明
图片的base64编码就是可以将一张图片数据编码成一串字符串,使用该字符串代替图像地址url
在前端页面中常见的base64图片的引入方式:
<img src="data:image/png;base64,【base图片编码】iVBORw0..>
- 优点
(1)base64格式的图片是文本格式,占用内存小,转换后的大小比例大概为1/3,降低了资源服务器的消耗;
(2)网页中使用base64格式的图片时,不用再请求服务器调用图片资源,减少了服务器访问次数。
- 缺点
(1)base64格式的文本内容较多,存储在数据库中增大了数据库服务器的压力;
(2)网页加载图片虽然不用访问服务器了,但因为base64格式的内容太多,所以加载网页的速度会降低,可能会影响用户的体验。
说明:医院logo图片小,因此上传医院logo是可以使用base64格式保存
5.2 图片base64工具类
在common-util模块添加工具类
添加com.atguigu.yygh.common.util.ImageBase64Util类
import org.apache.commons.codec.binary.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class ImageBase64Util {
public static void main(String[] args) {
String imageFile= "D:\\yygh_work\\xh.png";// 待处理的图片
System.out.println(getImageString(imageFile));
}
public static String getImageString(String imageFile){
InputStream is = null;
try {
byte[] data = null;
is = new FileInputStream(new File(imageFile));
data = new byte[is.available()];
is.read(data);
return new String(Base64.encodeBase64(data));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
is = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "";
}
}
5.3 上传医院接口修正
图片转换为base64字符串时,该字符串中包含大量的加号“+”,服务器在解析数据时会把加号当成连接符,转换为空格,因此我们要做一下特殊处理
修改ApiController类上传接口
6、集成测试
参考《医院接口模拟系统.docx》进行接口测试与数据上传,后续不做说明,需要测试时即可使用
二、查询医院接口
参考《尚医通API接口文档.docx》业务接口4.4查询医院
1、添加service接口
1,在HospitalService 类添加接口
public interface HospitalService {
//查询医院
Hospital getByHoscode(String hoscode);
}
2,在HospitalServiceImpl类添加实现
//根据hoscode,获取hospital对象
@Override
public Hospital getByHoscode(String hoscode) {
return hospitalRepository.getHospitalByHoscode(hoscode);
}
2、添加controller接口
在ApiController类添加接口
//查询医院
@PostMapping("/hospital/show")
public Result show(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String) parameterMap.get("hoscode");
if (!StringUtils.isEmpty(hoscode)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
//签名校验
if (!HttpRequestHelper.isSignEquals(parameterMap,hospitalSetService.getSignKey(hoscode))){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
Hospital hospital = hospitalService.getByHoscode(hoscode);
return Result.ok(hospital);
}
- isSignEquals()
/**
* 请求数据获取签名
* @param paramMap
* @return
*/
public static String getSign(Map<String, Object> paramMap, String signKey) {
if(paramMap.containsKey("sign")) {
paramMap.remove("sign");
}
TreeMap<String, Object> sorted = new TreeMap<>(paramMap);
StringBuilder str = new StringBuilder();
for (Map.Entry<String, Object> param : sorted.entrySet()) {
str.append(param.getValue()).append("|");
}
str.append(signKey);
log.info("加密前:" + str.toString());
String md5Str = MD5.encrypt(str.toString());
log.info("加密后:" + md5Str);
return md5Str;
}
/**
* 签名校验
* @param paramMap
* @return
*/
public static boolean isSignEquals(Map<String, Object> paramMap, String signKey) {
String sign = (String)paramMap.get("sign");
String md5Str = getSign(paramMap, signKey);
if(!sign.equals(md5Str)) {
return false;
}
return true;
}
三、上传科室接口
参考《尚医通API接口文档.docx》业务接口4.2上传科室
1、添加科室基础类
1.1 添加model
说明:由于实体对象没有逻辑,我们已经统一导入
com.achang.yygh.model.hosp.Department
@Data
@ApiModel(description = "Department")
@Document("Department")//对应mongodb的集合,也就是mysql的表
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;
}
1.2 添加repository
@Repository
public interface DepartmentRepository extends MongoRepository<Department,String> {
}
1.3 添加service接口及实现类
- impl
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
}
- 接口
public interface DepartmentService {
}
2、上传科室
参考《尚医通API接口文档.doc》业务接口4.2上传科室
医院编号是平台分配的,全局唯一,科室编号为医院自己的编号,相对医院唯一,上传科室接口可以多次调用,如果医院编号与科室编号组合唯一为更新操作
2.1 接口数据分析
{
"hoscode": "1000_0",
"depcode": "200050923",
"depname": "门诊部核酸检测门诊(东院)",
"intro": "门诊部核酸检测门诊(东院)",
"bigcode": "44f162029abb45f9ff0a5f743da0650d",
"bigname": "体检科"
}
说明:一个大科室下可以有多个小科室,如图:
2.2 添加service接口
1,在DepartmentService 类添加接口
public interface DepartmentService {
//上传科室
void saveDepartment(Map<String, Object> parameterMap);
}
说明:参数使用Map,减少对象封装,有利于签名校验,后续会体验到
2,在DepartmentServiceImpl类添加impl实现
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
//上传科室
@Override
public void saveDepartment(Map<String, Object> parameterMap) {
//将map集合转换成字符串
String jsonString = JSONObject.toJSONString(parameterMap);
//字符串 转成 对象
Department department = JSONObject.parseObject(jsonString, Department.class);
String hoscode = department.getHoscode();
String depcode = department.getDepcode();
//判断数据库中是否已经存在信息,根据hoscode,depcode查询数据库,方法命名遵循mongoDB规范,他就自动实现
Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
if (targetDepartment!=null){
//数据库已经有数据,修改操作
targetDepartment.setIsDeleted(0);
targetDepartment.setUpdateTime(new Date());
departmentRepository.save(targetDepartment);
}else {
//数据库没有数据,保存操作
department.setUpdateTime(new Date());
department.setCreateTime(new Date());
department.setIsDeleted(0);
departmentRepository.save(department);
}
}
}
2.3 添加repository接口
在Department Repository添加方法
根据hoscode,depcode查询数据库,方法命名遵循mongoDB规范,他就自动实现
@Repository
public interface DepartmentRepository extends MongoRepository<Department,String> {
//根据hoscode,depcode,查询数据库对应数据
Department getDepartmentByHoscodeAndDepcode(String hoscode, String depcode);
}
2.4 添加controller接口
在ApiController类添加接口
@RestController
@RequestMapping("/api/hosp/")
public class ApiController {
@Autowired
private HospitalService hospitalService;
@Autowired
private HospitalSetService hospitalSetService;
@Autowired
private DepartmentService departmentService;
//上传科室
@PostMapping("/saveDepartment")
public Result saveDepartment(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String) parameterMap.get("hoscode");
String sign = (String) parameterMap.get("sign");
String signKey = hospitalSetService.getSignKey(hoscode);
String signKeyMD5 = MD5.encrypt(signKey);
if (hoscode==null){
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//签名校验
if (!signKeyMD5.equals(sign)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
//上传科室
departmentService.saveDepartment(parameterMap);
return Result.ok();
}
}
- 测试
四、查询科室接口
参考《尚医通API接口文档.docx》业务接口4.5查询医院
一个医院有多个科室,因此我们采取分页查询方式
- 接口
public interface DepartmentService {
//多条件科室分页查询
Page<Department> getPage(int pageNum, int pageSize, DepartmentQueryVo departmentQueryVo);
}
- impl
package com.achang.yygh.hosp.service.impl;
import com.achang.yygh.hosp.repository.DepartmentRepository;
import com.achang.yygh.hosp.service.DepartmentService;
import com.achang.yygh.model.hosp.Department;
import com.achang.yygh.vo.hosp.DepartmentQueryVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import java.util.Date;
import java.util.Map;
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
//多条件科室分页查询
@Override
public Page<Department> getPage(int pageNum, int pageSize, DepartmentQueryVo departmentQueryVo) {
//构建排序规则
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//构建分页规则
PageRequest pageRequest = PageRequest.of(pageNum, pageSize, sort);
//构建匹配规则
ExampleMatcher matching = ExampleMatcher.matching();
//改变默认字符串匹配方式:模糊查询
ExampleMatcher stringMatcher = matching.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
//改变默认大小写忽略方式:忽略大小写
stringMatcher.withIgnoreCase(true);
Department department = new Department();
BeanUtils.copyProperties(departmentQueryVo,department);
department.setIsDeleted(0);
//0为第一页
Pageable pageable = PageRequest.of(pageNum-1, pageSize, sort);
Example<Department> example = Example.of(department, stringMatcher);
return departmentRepository.findAll(example, pageable);
}
}
- DepartmentQueryVo
@Data
@ApiModel(description = "Department")
public class DepartmentQueryVo {
@ApiModelProperty(value = "医院编号")
private String hoscode;
@ApiModelProperty(value = "科室编号")
private String depcode;
@ApiModelProperty(value = "科室名称")
private String depname;
@ApiModelProperty(value = "大科室编号")
private String bigcode;
@ApiModelProperty(value = "大科室名称")
private String bigname;
}
- controller
package com.achang.yygh.hosp.controller.api;
import com.achang.exception.YyghException;
import com.achang.helper.HttpRequestHelper;
import com.achang.result.Result;
import com.achang.result.ResultCodeEnum;
import com.achang.utils.MD5;
import com.achang.yygh.hosp.service.DepartmentService;
import com.achang.yygh.hosp.service.HospitalService;
import com.achang.yygh.hosp.service.HospitalSetService;
import com.achang.yygh.model.hosp.Department;
import com.achang.yygh.model.hosp.Hospital;
import com.achang.yygh.model.hosp.HospitalSet;
import com.achang.yygh.vo.hosp.DepartmentQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.data.domain.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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 {
@Autowired
private HospitalService hospitalService;
@Autowired
private HospitalSetService hospitalSetService;
@Autowired
private DepartmentService departmentService;
//多条件科室分页查询
@PostMapping("/department/list")
public Result list(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String)parameterMap.get("hoscode");
if (hoscode==null){
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
String depcode = (String)parameterMap.get("depcode");
int pageNum = StringUtils.isEmpty(parameterMap.get("pageNum"))?1:Integer.parseInt((String)parameterMap.get("pageNum"));
int pageSize = StringUtils.isEmpty(parameterMap.get("pageSize"))?10:Integer.parseInt((String) parameterMap.get("pageSize"));
String hospSignKey = (String) parameterMap.get("sign");
String signKey = hospitalSetService.getSignKey(hoscode);
String signKeyMD5 = MD5.encrypt(signKey);
if (!hospSignKey.equals(signKeyMD5)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
departmentQueryVo.setDepcode(depcode);
departmentQueryVo.setHoscode(hoscode);
//多条件科室分页查询
Page<Department> pageList = departmentService.getPage(pageNum,pageSize,departmentQueryVo);
return Result.ok(pageList);
}
}
- 效果测试
五、删除科室接口
参考《尚医通API接口文档.docx》业务接口4.7删除科室
根据医院编号与科室编号删除科室
1、添加service接口
1,在DepartmentService 类添加接口
public interface DepartmentService {
//删除科室
void delete(String hoscode, String depcode);
}
2,在DepartmentServiceImpl类添加实现
@Service
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
//删除科室
@Override
public void delete(String hoscode, String depcode) {
Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
if (department!=null){
//epartmentRepository.deleteById(department.getId());
departmentRepository.delete(department);
}
}
}
2、添加controller接口
在ApiController类添加接口
//删除科室
@PostMapping("department/remove")
public Result remove(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String) parameterMap.get("hoscode");
if (hoscode==null){
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
String depcode = (String) parameterMap.get("depcode");
String hospSignKey = (String) parameterMap.get("sign");
String hospSetSignKey = hospitalSetService.getSignKey(hoscode);
String hospSetSignKeyMD5 = MD5.encrypt(hospSetSignKey);
//签名校验
if (!hospSetSignKeyMD5.equals(hospSignKey)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
//删除科室
departmentService.delete(hoscode,depcode);
return Result.ok();
}
- 测试
六、上传排班接口
参考《尚医通API接口文档.docx》业务接口4.3上传排班
医院编号是平台分配的,全局唯一,排班编号为医院自己的编号,相对医院唯一,上传排班接口可以多次调用,如果医院编号与排班编号组合唯一为更新操作
1、添加service接口
1,在ScheduleService 类添加接口
public interface ScheduleService {
//上传科室
void save(Map<String, Object> parameterMap);
}
2,在ScheduleServiceImpl类添加实现
@Service
public class ScheduleServiceImpl implements ScheduleService {
@Autowired
private ScheduleRepository scheduleRepository;
//上传科室
@Override
public void save(Map<String, Object> parameterMap) {
//Map 转成 字符串
String jsonString = JSONObject.toJSONString(parameterMap);
//字符串 转成 对象
Schedule schedule = JSONObject.parseObject(jsonString, Schedule.class);
Schedule targetSchedule = scheduleRepository.getScheduleByHoscodeAndHosScheduleId(schedule.getHoscode(), schedule.getHosScheduleId());
if (targetSchedule==null){
//添加
schedule.setCreateTime(new Date());
schedule.setUpdateTime(new Date());
schedule.setIsDeleted(0);
scheduleRepository.save(schedule);
}else {
//修改
BeanUtils.copyProperties(schedule,targetSchedule);
scheduleRepository.save(targetSchedule);
}
}
}
2、添加controller接口
在ApiController类添加接口
//上传排班
@PostMapping("/saveSchedule")
public Result saveSchedule(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String) parameterMap.get("hoscode");
//判断hoscode是否有值
if (hoscode==null){
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
String sign = (String) parameterMap.get("sign");
//根据hoscode获取数据库中的sign
String signKey = hospitalSetService.getSignKey(hoscode);
String signKeyMD5 = MD5.encrypt(signKey);
//数据校验
if (!signKeyMD5.equals(sign)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
//上传科室
scheduleService.save(parameterMap);
return Result.ok();
}
3、添加scheduleRegistory
@Repository
public interface ScheduleRepository extends MongoRepository<Schedule,String> {
//根据hoscode,depcode,查询数据库对应数据
Schedule getScheduleByHoscodeAndHosScheduleId(String hoscode, String hosScheduleId);
}
4、测试
七、查询排班接口
参考《尚医通API接口文档.docx》业务接口4.6查询医院
一个科室有多个科室,因此我们采取分页查询方式
1、添加service接口
1,在ScheduleService 类添加接口
public interface ScheduleService {
//多条件分页查询
Page<Schedule> selectPage(Integer pageNum, Integer pageSize, ScheduleQueryVo scheduleQueryVo);
}
2,在ScheduleServiceImpl类添加实现
//多条件分页查询
@Override
public Page<Schedule> selectPage(Integer pageNum, Integer pageSize, ScheduleQueryVo scheduleQueryVo) {
//构建排序规则
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//构建分页规则,0为第一页
PageRequest pageRequest = PageRequest.of(pageNum-1, pageSize, sort);
//构建匹配规则
ExampleMatcher matching = ExampleMatcher.matching();
//改变默认字符串匹配方式:模糊查询
ExampleMatcher stringMatcher = matching.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
//改变默认大小写忽略方式:忽略大小写
stringMatcher.withIgnoreCase(true);
Schedule schedule = new Schedule();
schedule.setIsDeleted(0);
BeanUtils.copyProperties(scheduleQueryVo,schedule);
Example<Schedule> example = Example.of(schedule, stringMatcher);
return scheduleRepository.findAll(example, pageRequest);
}
2、添加controller接口
在ApiController类添加接口
//多条件分页查询排班
@PostMapping("/schedule/list")
public Result listPageSchedule(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String) parameterMap.get("hoscode");
String depcode = (String)parameterMap.get("depcode");
if (hoscode==null){
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//页数
Integer pageNum = StringUtils.isEmpty((Integer) parameterMap.get("pageNum"))?1:(Integer) parameterMap.get("pageNum");
//页显示数
Integer pageSize = StringUtils.isEmpty((Integer) parameterMap.get("pageSize"))?10:(Integer) parameterMap.get("pageSize");
String hopSign = (String) parameterMap.get("sign");
String hopsetSignKey = hospitalSetService.getSignKey(hoscode);
String hopsetSignKeyMD5 = MD5.encrypt(hopsetSignKey);
//数据校验
if (!hopsetSignKeyMD5.equals(hopSign)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
scheduleQueryVo.setHoscode(hoscode);
scheduleQueryVo.setDepcode(depcode);
Page<Schedule> pageModel = scheduleService.selectPage(pageNum,pageSize,scheduleQueryVo);
return Result.ok(pageModel);
}
3、测试
八、删除排班接口
参考《尚医通API接口文档.docx》业务接口4.8删除科室
根据医院编号与排班编号删除科室
1、添加service接口
- 接口
public interface ScheduleService {
//删除科室
void delete(String hoscode, String hosScheduleId);
}
- impl
//删除科室
@Override
public void delete(String hoscode, String hosScheduleId) {
Schedule schedule = scheduleRepository.getScheduleByHoscodeAndHosScheduleId(hoscode, hosScheduleId);
if (schedule!=null){
scheduleRepository.delete(schedule);
}
}
2、添加controller
//删除科室
@PostMapping("/schedule/remove")
public Result scheduleRemove(HttpServletRequest request){
Map<String, String[]> map = request.getParameterMap();
Map<String, Object> parameterMap = HttpRequestHelper.switchMap(map);
String hoscode = (String) parameterMap.get("hoscode");
if (hoscode==null){
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
String hosScheduleId = (String) parameterMap.get("hosScheduleId");
String sign = (String) parameterMap.get("sign");
String signKey = hospitalSetService.getSignKey(hoscode);
String signKeyMD5 = MD5.encrypt(signKey);
//数据校验
if (!signKeyMD5.equals(sign)){
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
//删除科室
scheduleService.delete(hoscode,hosScheduleId);
return Result.ok();
}