目录
- 一、上传科室接口
- 1. 添加科室基础类
- 2. 上传科室
- 二、查询科室接口
- 三、删除科室接口
一、上传科室接口
1. 添加科室基础类
A、添加 model
说明:由于实体对象没有逻辑,我们已经统一导入
com.fancy.yygh.model.hosp.Department
B、添加 repository
添加 com.fancy.yygh.hosp.repository.DepartmentRepository
package com.fancy.yygh.hosp.repository;
@Repository
public interface DepartmentRepository extends MongoRepository<Department,String> {
}
C、添加 service 接口及实现类
添加 com.fancy.yygh.hosp.service.DepartmentService 接口
package com.fancy.yygh.hosp.service;
public interface DepartmentService {
}
添加com.fancy.yygh.hosp.service.impl.DepartmentServiceImpl 接口实现
package com.fancy.yygh.hosp.service.impl;
@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentRepository departmentRepository;
}
2. 上传科室
A、上传科室
医院编号是平台分配的,全局唯一,科室编号为医院自己的编号,相对医院唯一,上传科室接口可以多次调用,如果医院编号与科室编号组合唯一为更新操作
B、接口数据分析
{
"hoscode": "1000_0",
"depcode": "200050923",
"depname": "门诊部核酸检测门诊(东院)",
"intro": "门诊部核酸检测门诊(东院)",
"bigcode": "44f162029abb45f9ff0a5f743da0650d",
"bigname": "体检科"
}
说明:一个大科室下可以有多个小科室,如图:
C、添加 service 接口
在 DepartmentService 类添加接口
/**
* 上传科室信息
* @param paramMap
*/
void save(Map<String, Object> paramMap);
说明:参数使用 Map,减少对象封装,有利于签名校验,后续会体验到
在 DepartmentServiceImpl 类添加实现
@Override
public void save(Map<String, Object> paramMap) {
Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
if(null != targetDepartment) {
//copy不为null的值,该方法为自定义方法
BeanUtils.copyBean(department, targetDepartment, Department.class);
departmentRepository.save(targetDepartment);
} else {
department.setCreateTime(new Date());
department.setUpdateTime(new Date());
department.setIsDeleted(0);
departmentRepository.save(department);
}
}
D、添加 repository 接口
在DepartmentRepository添加方法
Department getDepartmentByHoscodeAndDepcode(String hoscode, String depcode);
E、添加 controller 接口
在 ApiController 类添加接口
@Autowired
private DepartmentService departmentService;
@ApiOperation(value = "上传科室")
@PostMapping("saveDepartment")
public Result saveDepartment(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);
}
departmentService.save(paramMap);
return Result.ok();
}
二、查询科室接口
一个医院有多个科室,因此我们采取分页查询方式
A、添加 service 接口
在 DepartmentService 类添加接口
/**
* 分页查询
* @param page 当前页码
* @param limit 每页记录数
* @param departmentQueryVo 查询条件
* @return
*/
Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo);
在 DepartmentServiceImpl 类添加实现
@Override
public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//0为第一页
Pageable pageable = PageRequest.of(page-1, limit, sort);
Department department = new Department();
BeanUtils.copyProperties(departmentQueryVo, department);
department.setIsDeleted(0);
//创建匹配器,即如何使用查询条件
ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式:模糊查询
.withIgnoreCase(true); //改变默认大小写忽略方式:忽略大小写
//创建实例
Example<Department> example = Example.of(department, matcher);
Page<Department> pages = departmentRepository.findAll(example, pageable);
return pages;
}
B、添加 controller 接口
在 ApiController 类添加接口
@ApiOperation(value = "获取分页列表")
@PostMapping("department/list")
public Result department(HttpServletRequest request) {
Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
String hoscode = (String)paramMap.get("hoscode");
//非必填
String depcode = (String)paramMap.get("depcode");
int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
if(StringUtils.isEmpty(hoscode)) {
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//签名校验
if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
departmentQueryVo.setHoscode(hoscode);
departmentQueryVo.setDepcode(depcode);
Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
return Result.ok(pageModel);
}
三、删除科室接口
根据医院编号与科室编号删除科室
A、添加 service 接口
在 DepartmentService 类添加接口
/**
* 删除科室
* @param hoscode
* @param depcode
*/
void remove(String hoscode, String depcode);
在 DepartmentServiceImpl 类添加实现
@Override
public void remove(String hoscode, String depcode) {
Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
if(null != department) {
//departmentRepository.delete(department);
departmentRepository.deleteById(department.getId());
}
}
B、添加 controller 接口
在 ApiController 类添加接口
@ApiOperation(value = "删除科室")
@PostMapping("department/remove")
public Result removeDepartment(HttpServletRequest request) {
Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
String hoscode = (String)paramMap.get("hoscode");
//必填
String depcode = (String)paramMap.get("depcode");
if(StringUtils.isEmpty(hoscode)) {
throw new YyghException(ResultCodeEnum.PARAM_ERROR);
}
//签名校验
if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
throw new YyghException(ResultCodeEnum.SIGN_ERROR);
}
departmentService.remove(hoscode, depcode);
return Result.ok();
}