Back-end implementation of hospital list for hospital management

Back-end-hospital management implementation

1. Hospital list

1.1. Hospital list api interface

1.1.1, add controller method

Insert picture description here
@RestController
@RequestMapping("/admin/hosp/hospital")
@CrossOrigin
public class HospitalController {

    @Autowired
    private HospitalService hospitalService;

    //医院信息列表
    @GetMapping("limit/{page}/{limit}")
    public Result listHospital(@PathVariable Integer page,
                               @PathVariable Integer limit,
                               HospitalQueryVo hospitalQueryVo){
        //调用业务层方法
        Page<Hospital> pageModel = hospitalService.selectHospPage(page,limit,hospitalQueryVo);
        return Result.ok(pageModel);
    }

}

1.1.2, service layer method

interface:

Insert picture description here
//医院信息列表
Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo);

Implementation class:

Insert picture description here
//医院信息列表(分页带模糊查询)
@Override
public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
    //创建pageable,用于设置分页信息
    Pageable pageable = PageRequest.of(page,limit);

    //设置条件匹配规则
    ExampleMatcher matcher = ExampleMatcher.matching()
            .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配为:模糊查询
            .withIgnoreCase(true);//查询时忽略大小写
    //将hospitalQueryVo对象转换成Hospital对象
    Hospital hospital = new Hospital();
    BeanUtils.copyProperties(hospitalQueryVo,hospital);

    //创建Example对象,用于设置查询条件
    Example<Hospital> example = Example.of(hospital,matcher);
    Page<Hospital> all = hospitalRepository.findAll(example, pageable);
    return all;
}

1.2, service-cmn module provides interface

Since the interface for querying information about the hospital is in the service_cmn microservice, we need to write the required interface in the service-cmn module for the service_hosp module to call.

1.2.1. Background of interface writing

  • Since our hospital level and provincial/city address are the data dictionary value values, we need to obtain the data dictionary name according to the data dictionary value when displaying the hospital level and hospital address in the list.
  • By learning the data dictionary, we know that the corresponding data dictionary name can be obtained according to the superior code and value value. If the value value can be kept unique (not necessarily unique), we can also directly obtain the data dictionary name through the value value. For the level data, we use the data of the National Bureau of Statistics. The data code is the id and value of the data dictionary, so the value can uniquely determine a data dictionary, as shown in the figure:
Insert picture description here

1.2.2, write interface

Write the interface in service_cmn and return the getName value for the service_hosp module to call.
Control layer:

Insert picture description here
//根据dictCode和value查询
@GetMapping("getName/{dictCode}/{value}")
public String getName(@PathVariable String dictCode,
                      @PathVariable String value){
    String dictName = dictService.getName(dictCode,value);
    return dictName;
}

//根据value查询
@GetMapping("getName/{value}")
public String getName(@PathVariable String value){
    String dictName = dictService.getName("",value);
    return dictName;
}

Business layer interface:

//查询getName
String getName(String dictCode, String value);

Business layer implementation class:;

Insert picture description here
//查询getName
@Override
public String getName(String dictCode, String value) {
	//如果dictCode为空,根据value查询
	if(StringUtils.isEmpty(dictCode)){
	    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
	    wrapper.eq("value",value);
	    Dict dict = baseMapper.selectOne(wrapper);
	    return dict.getName();
	}else {//如果value为空,首先根据dictCode查询出Dict对象
	    Dict Dict = getDictByDictCode(dictCode);
	    Long parentId = Dict.getId();
	    //在根据查询出来的parent_id和value查询
	    Dict finalDict = baseMapper.selectOne(new QueryWrapper<Dict>()
	            .eq("parent_id", parentId)
	            .eq("value", value));
	    return finalDict.getName();
	}
private Dict getDictByDictCode(String dictCode){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code", dictCode);
        Dict codeDict = baseMapper.selectOne(wrapper);
        return codeDict;
    }

1.3. Service call-case

1.3.1. Requirement background: call the interface in service_cmn in service_hosp

Because all the information of the hospital is stored in the dict table of the yygh_cmn database in the mysql database. This data is the database under the service_cmn module.

Insert picture description here

The data of the hospital platform system is stored in mongoDB, and part of the hospital information stored in mongoDB is numbered without a real name. This data is the data in the service_hosp module:

Insert picture description here

so the service_cmn interface must be called in service_hosp to query the hospital-related data, and finally display the real name of the hospital on the browser instead of displaying its number.

Create a service_client module, and create sub-modules under this module for remote invocation.

Insert picture description here


Introduce dependency in the pom.xml file of service_client:

Insert picture description here
<dependencies>
    <dependency>
        <groupId>com.study</groupId>
        <artifactId>common_util</artifactId>
    </dependency>
    <dependency>
        <groupId>com.study</groupId>
        <artifactId>model</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <scope>provided </scope>
    </dependency>

     <!-- 服务调用feign -->
     <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
          <version>2.2.6.RELEASE</version>
      </dependency>
</dependencies>

1.3.2, create a module service_client_cmn under the service_client module

Declare the called service_cmn interface in service_client_cmn:

Insert picture description here
@FeignClient("service-cmn")
public interface DictFeignClient {

    @GetMapping("/admin/cmn/dict/getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value);

    @GetMapping("/admin/cmn/dict/getName/{value}")
    public String getName(@PathVariable("value") String value);
}

1.3.3, import service_cmn_client module coordinates in service_hosp

Because the code for the service call is written in service_cmn_client, and finally service_hosp calls the interface in service_cmn. Therefore, service_cmn_client dependency must be introduced in service_hosp.

Insert picture description here

1.3.4, inject DictFeignClient in the hospitalServiceImpl of service_hosp

Insert picture description here

1.3.5. Write code in hospitalServiceImpl of service_hosp to implement remote service calls

Mainly call the interface in service_cmn remotely to query the grade information of the hospital. Packaged in the hospital for platform display.

Insert picture description here
  //医院信息列表(分页带模糊查询)
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable,用于设置分页信息
        Pageable pageable = PageRequest.of(page,limit);

        //设置条件匹配规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//改变默认字符串匹配为:模糊查询
                .withIgnoreCase(true);//查询时忽略大小写
        //将hospitalQueryVo对象转换成Hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);

        //创建Example对象,用于设置查询条件
        Example<Hospital> example = Example.of(hospital,matcher);
        //调用方法实现查询,返回查询到底所有的医院的信息(list集合)
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //获取查询的list集合,遍历及进行医院等级的封装
        pages.getContent().stream().forEach(item->{
            this.getHospitalHosType(item);
        });
        return pages;
    }

    //远程调用服务,获取医院等级
    private Hospital getHospitalHosType(Hospital hospital){
        String hosTypeString = dictFeignClient.getName("HosType", hospital.getHostype());
        //查询 省 市 区信息
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        //将查询出来的细腻些存放到hospital中
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hosTypeString",hosTypeString);
        return hospital;
    }

1.4. Add a data dictionary display interface to query the province information, the city under the first-level province

1.4.1, write controller

Insert picture description here
//根据dictCode获取其下级节点(例如根据省份的dictCode的值获取其下级节点(北京市,上海市))
@ApiModelProperty(value = "根据dictCode获取其下级节点")
@GetMapping("findByDictCode/{dictCode}")
public Result findByDictCode(@PathVariable String dictCode){
    List<Dict> list = dictService.findByDictCode(dictCode);
    return Result.ok(list);
}

1.4.2, business layer

interface:

Insert picture description here
//根据dictCode获取其下级节点(例如根据省份的dictCode的值获取其下级节点(北京市,上海市))
 List<Dict> findByDictCode(String dictCode);

Implementation class:

Insert picture description here
//根据dictCode获取其下级节点(例如根据省份的dictCode的值获取其下级节点(北京市,上海市))
@Override
public List<Dict> findByDictCode(String dictCode) {
    //根据传递过来的dictCode查询dict对象
    Dict dict = this.getDictByDictCode(dictCode);
    //获取该对象的id,调用方法已写好的方法,查询其下级节点
    List<Dict> childData = findChildData(dict.getId());
    return childData;
}


private Dict getDictByDictCode(String dictCode){
    QueryWrapper<Dict> wrapper = new QueryWrapper<>();
    wrapper.eq("dict_code", dictCode);
    Dict codeDict = baseMapper.selectOne(wrapper);
    return codeDict;
}

//根据数据id查询子数据列表
@Override
@Cacheable(value = "dict", keyGenerator = "keyGenerator")//作用是将该方法返回的值放到缓存中
public List<Dict> findChildData(Long id) {
    QueryWrapper<Dict> query = new QueryWrapper<>();
    query.eq("parent_id", id);
    List<Dict> dictList = baseMapper.selectList(query);//查询父节点为id的所有子数据对象
    //遍历集合中的每一个对象,并调用方法,判断该对象是否有子数据
    for (Dict dict : dictList) {
        Long dictId = dict.getId();
        boolean flag = this.isChildren(dictId);//如果对象是否有子数据,返回true
        dict.setHasChildren(flag);//将hasChildren属性设置为flag的值
    }
    return dictList;
}