AddressEndPoint.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.yihu.ehr.basic.address.controller;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.yihu.ehr.basic.address.service.AddressService;
  4. import com.yihu.ehr.constants.ApiVersion;
  5. import com.yihu.ehr.constants.ErrorCode;
  6. import com.yihu.ehr.constants.ServiceApi;
  7. import com.yihu.ehr.exception.ApiException;
  8. import com.yihu.ehr.entity.address.Address;
  9. import com.yihu.ehr.model.geography.MGeography;
  10. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  11. import com.yihu.ehr.util.id.BizObject;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import io.swagger.annotations.ApiParam;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.http.MediaType;
  17. import org.springframework.web.bind.annotation.*;
  18. import java.util.List;
  19. /**
  20. * @author zlf
  21. * @version 1.0
  22. * @created 2015.08.10 17:57
  23. */
  24. @RestController
  25. @RequestMapping(ApiVersion.Version1_0)
  26. @Api(value = "AddressEndPoint", description = "获取常用地址,根据选择地址判断数据库中是否存在,否则保存为新地址", tags = {"基础信息-地址信息管理"})
  27. public class AddressEndPoint extends EnvelopRestEndPoint {
  28. @Autowired
  29. private AddressService geographyService;
  30. @RequestMapping(value = ServiceApi.Geography.Address, method = RequestMethod.GET)
  31. @ApiOperation(value = "根据地址编号查询地址")
  32. public MGeography getAddressById(
  33. @ApiParam(name = "id", value = "地址编号", defaultValue = "")
  34. @PathVariable(value = "id") String id) {
  35. Address address = geographyService.getAddressById(id);
  36. return convertToModel(address, MGeography.class);
  37. }
  38. @RequestMapping(value = ServiceApi.Geography.AddressCanonical, method = RequestMethod.GET)
  39. @ApiOperation(value = "根据地址编号获取地址中文字符串全拼")
  40. public String getCanonicalAddress(
  41. @ApiParam(name = "id", value = "地址代码", defaultValue = "")
  42. @PathVariable(value = "id") String id) {
  43. Address address = geographyService.getAddressById(id);
  44. String addressStr = "";
  45. if(address != null){
  46. addressStr = geographyService.getCanonicalAddress(address);
  47. }
  48. return addressStr;
  49. }
  50. /**
  51. * 地址检查并保存
  52. * @return
  53. */
  54. @RequestMapping(value = ServiceApi.Geography.Geographies, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  55. @ApiOperation(value = "地址检查,如果地址在数据库中不存在,这新增这条记录,否则返回地址id")
  56. public String saveAddress(
  57. @ApiParam(name = "json_data", value = "地址json字符串")
  58. @RequestBody String jsonData) throws Exception{
  59. // ObjectMapper objectMapper = new ObjectMapper();
  60. // Geography geography = objectMapper.readValue(jsonData, Geography.class);
  61. Address geography = toEntity(jsonData,Address.class);
  62. if (geography.getCountry() == null) {
  63. geography.setCountry("中国");
  64. }
  65. List<Address> geographies = geographyService.isGeographyExist(geography);
  66. if(geographies==null || geographies.size()==0){
  67. geography.setId(getObjectId(BizObject.Geography));
  68. String addressId = geographyService.saveAddress(geography);
  69. return addressId;
  70. }else {
  71. return geographies.get(0).getId();
  72. }
  73. }
  74. /**
  75. * 根据省市县查询地址
  76. * @param province
  77. * @param city
  78. * @param district
  79. * @return
  80. */
  81. @RequestMapping(value = ServiceApi.Geography.Geographies , method = RequestMethod.GET)
  82. @ApiOperation(value = "根据省市县查询地址并返回地址编号列表")
  83. public List<String> search(
  84. @ApiParam(name = "province", value = "省", defaultValue = "")
  85. @RequestParam(value = "province",required = true) String province,
  86. @ApiParam(name = "city", value = "市", defaultValue = "")
  87. @RequestParam(value = "city",required = false) String city,
  88. @ApiParam(name = "district", value = "县", defaultValue = "")
  89. @RequestParam(value = "district",required = false) String district) {
  90. List<String> idList = geographyService.search(province,city,district);
  91. return idList;
  92. }
  93. /**
  94. * 删除地址
  95. * @param id
  96. * @return
  97. */
  98. @RequestMapping(value = ServiceApi.Geography.GeographiesDelete , method = RequestMethod.DELETE)
  99. @ApiOperation(value = "根据地址编号删除地址")
  100. public boolean delete(
  101. @ApiParam(name = "id" , value = "地址代码" ,defaultValue = "")
  102. @PathVariable (value = "id") String id) {
  103. Address address = geographyService.getAddressById(id);
  104. if(address == null){
  105. throw new ApiException(ErrorCode.NOT_FOUND, "获取地址失败");
  106. }
  107. geographyService.deleteAddress(address);
  108. return true;
  109. }
  110. @RequestMapping(value = ServiceApi.Geography.GeographiesNull , method = RequestMethod.GET)
  111. @ApiOperation(value = "判断是否是个空地址")
  112. public boolean isNullAddress(
  113. @ApiParam(name = "json_data", value = "地址json字符串")
  114. @RequestParam( value = "json_data") String jsonData) throws Exception{
  115. ObjectMapper objectMapper = new ObjectMapper();
  116. Address geography = objectMapper.readValue(jsonData,Address.class);
  117. return geographyService.isNullAddress(geography);
  118. }
  119. }