ProvinceAtlasController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package jnpf.base.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  4. import com.google.common.base.Joiner;
  5. import io.swagger.v3.oas.annotations.Operation;
  6. import io.swagger.v3.oas.annotations.Parameter;
  7. import io.swagger.v3.oas.annotations.Parameters;
  8. import io.swagger.v3.oas.annotations.tags.Tag;
  9. import jnpf.base.ActionResult;
  10. import jnpf.base.entity.ProvinceAtlasEntity;
  11. import jnpf.base.model.province.AtlasJsonModel;
  12. import jnpf.base.model.province.MapParams;
  13. import jnpf.base.model.province.ProvinceListTreeVO;
  14. import jnpf.base.service.ProvinceAtlasService;
  15. import jnpf.constant.MsgCode;
  16. import jnpf.util.JsonUtil;
  17. import jnpf.util.NoDataSourceBind;
  18. import jnpf.util.ServletUtil;
  19. import jnpf.util.StringUtil;
  20. import jnpf.util.wxutil.HttpUtil;
  21. import net.sourceforge.pinyin4j.PinyinHelper;
  22. import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  23. import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  24. import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  25. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.web.bind.annotation.*;
  28. import javax.imageio.ImageIO;
  29. import jakarta.servlet.http.HttpServletResponse;
  30. import java.awt.image.BufferedImage;
  31. import java.io.InputStream;
  32. import java.io.OutputStream;
  33. import java.math.BigDecimal;
  34. import java.net.HttpURLConnection;
  35. import java.net.URL;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. /**
  39. * 行政区划-地图
  40. *
  41. * @author JNPF开发平台组
  42. * @version V3.4.2
  43. * @copyright 引迈信息技术有限公司
  44. * @date 2023/1/29 10:41:25
  45. */
  46. @Tag(name = "行政区划-地图", description = "atlas")
  47. @RestController
  48. @RequestMapping("/api/system/atlas")
  49. public class ProvinceAtlasController extends SuperController<ProvinceAtlasService, ProvinceAtlasEntity> {
  50. @Autowired
  51. private ProvinceAtlasService provinceAtlasService;
  52. public static final String ATLAS_URL = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=";
  53. //树形递归
  54. private static boolean addChild(ProvinceListTreeVO node, List<ProvinceListTreeVO> list) {
  55. for (int i = 0; i < list.size(); i++) {
  56. ProvinceListTreeVO n = list.get(i);
  57. if (n.getId().equals(node.getParentId())) {
  58. if (n.getChildren() == null) {
  59. n.setChildren(new ArrayList<>());
  60. }
  61. List<ProvinceListTreeVO> children = n.getChildren();
  62. children.add(node);
  63. n.setChildren(children);
  64. return true;
  65. }
  66. if (n.getChildren() != null && n.getChildren().size() > 0) {
  67. List<ProvinceListTreeVO> children = n.getChildren();
  68. if (addChild(node, children)) {
  69. return true;
  70. }
  71. }
  72. }
  73. return false;
  74. }
  75. /**
  76. * 获取所有列表
  77. *
  78. * @return ignore
  79. */
  80. @Operation(summary = "获取所有列表")
  81. @GetMapping
  82. public ActionResult<List<ProvinceListTreeVO>> list() {
  83. List<ProvinceAtlasEntity> list = provinceAtlasService.getList();
  84. List<ProvinceListTreeVO> listVOS = JsonUtil.getJsonToList(list, ProvinceListTreeVO.class);
  85. listVOS.forEach(item -> {
  86. if( StringUtil.isNotEmpty(item.getAtlasCenter())){
  87. String[] split = item.getAtlasCenter().split(",");
  88. item.setCenterLong(new BigDecimal(split[0]));
  89. item.setCenterLat(new BigDecimal(split[1]));
  90. }
  91. });
  92. for (int i = 0; i < listVOS.size(); i++) {
  93. ProvinceListTreeVO item = listVOS.get(i);
  94. if (!StringUtil.isEmpty(item.getParentId())) {
  95. if (addChild(item, listVOS) && listVOS.size() > 0) {
  96. listVOS.remove(item);
  97. i--;
  98. }
  99. }
  100. }
  101. return ActionResult.success(listVOS);
  102. }
  103. /**
  104. * 获取列表
  105. *
  106. * @param pid 主键
  107. * @return
  108. */
  109. @Operation(summary = "获取列表")
  110. @Parameters({
  111. @Parameter(name = "pid", description = "主键", required = true)
  112. })
  113. @GetMapping("/list/{pid}")
  114. public ActionResult<List<ProvinceListTreeVO>> getListByPid(@PathVariable("pid") String pid) {
  115. List<ProvinceAtlasEntity> list = provinceAtlasService.getListByPid(pid);
  116. List<ProvinceListTreeVO> listVOS = JsonUtil.getJsonToList(list, ProvinceListTreeVO.class);
  117. return ActionResult.success(listVOS);
  118. }
  119. /**
  120. * 获取地图json
  121. *
  122. * @param code 编码
  123. * @return
  124. */
  125. @Operation(summary = "获取地图json")
  126. @Parameters({
  127. @Parameter(name = "code", description = "编码", required = true)
  128. })
  129. @GetMapping("/geojson")
  130. public ActionResult<JSONObject> geojson(@RequestParam("code") String code) {
  131. ProvinceAtlasEntity oneByCode = provinceAtlasService.findOneByCode(code);
  132. if(oneByCode == null){
  133. return ActionResult.fail(MsgCode.SYS022.get());
  134. }
  135. List<ProvinceAtlasEntity> listByPid = provinceAtlasService.getListByPid(oneByCode.getId());
  136. String url = ATLAS_URL + code;
  137. if (CollectionUtils.isNotEmpty(listByPid)) {
  138. url += "_full";
  139. }
  140. JSONObject rstObj;
  141. try {
  142. rstObj = HttpUtil.httpRequest(url, "GET", null);
  143. } catch (Exception e) {
  144. return ActionResult.fail(MsgCode.SYS023.get());
  145. }
  146. if (rstObj == null) {
  147. return ActionResult.fail(MsgCode.SYS024.get());
  148. }
  149. return ActionResult.success(rstObj);
  150. }
  151. /**
  152. * 同步行政区划信息
  153. *
  154. * @return
  155. */
  156. @Operation(summary = "同步行政区划信息")
  157. @GetMapping("/crePy")
  158. public ActionResult crePy() {
  159. List<ProvinceAtlasEntity> list = provinceAtlasService.list();
  160. for (ProvinceAtlasEntity p : list) {
  161. String url = ATLAS_URL + p.getId();
  162. JSONObject rstObj;
  163. try {
  164. rstObj = HttpUtil.httpRequest(url, "GET", null);
  165. if (rstObj == null) {
  166. provinceAtlasService.removeById(p);
  167. } else {
  168. //将获取到的信息写入表
  169. AtlasJsonModel jsonToBean = JsonUtil.getJsonToBean(rstObj, AtlasJsonModel.class);
  170. List<BigDecimal> center = jsonToBean.getFeatures().get(0).getProperties().getCenter();
  171. p.setAtlasCenter(Joiner.on(",").join(center));
  172. //生成拼音
  173. // String getpy = getpy(p.getFullName());
  174. // p.setQuickQuery(getpy);
  175. provinceAtlasService.updateById(p);
  176. }
  177. } catch (Exception e) {
  178. e.printStackTrace();
  179. return ActionResult.fail(MsgCode.SYS023.get());
  180. }
  181. }
  182. return ActionResult.success();
  183. }
  184. private String getpy(String name) {
  185. HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
  186. outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
  187. outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
  188. StringBuilder result = new StringBuilder();
  189. char[] charArray = name.toCharArray();
  190. for (char c : charArray) {
  191. try {
  192. String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
  193. result.append(pinyinArray[0]);
  194. } catch (BadHanyuPinyinOutputFormatCombination e) {
  195. e.printStackTrace();
  196. }
  197. }
  198. return result.toString();
  199. }
  200. }