SysCountryServiceImpl.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.crm.manager.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.crm.manager.dao.repository.SysCountryRepository;
  5. import com.crm.manager.service.SysCountryService;
  6. import com.crm.rely.backend.core.constant.CustomConstant;
  7. import com.crm.rely.backend.core.dto.country.SysCountryDto;
  8. import com.crm.rely.backend.core.entity.country.SysCountryEntity;
  9. import com.crm.rely.backend.core.entity.country.SysCountryStateEntity;
  10. import com.crm.rely.backend.core.pojo.table.SysCountryTable;
  11. import com.crm.rely.backend.exception.ServiceException;
  12. import com.crm.rely.backend.util.HttpUtil;
  13. import com.google.common.base.Strings;
  14. import com.maxmind.geoip2.DatabaseReader;
  15. import com.maxmind.geoip2.model.CountryResponse;
  16. import com.maxmind.geoip2.record.Country;
  17. import org.jsoup.Connection;
  18. import org.springframework.beans.BeanUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21. import java.io.InputStream;
  22. import java.net.InetAddress;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. @Service
  28. public class SysCountryServiceImpl implements SysCountryService {
  29. private static DatabaseReader databaseReader;
  30. @Autowired
  31. private SysCountryRepository sysCountryRepository;
  32. @Override
  33. public List<SysCountryDto> getByPid(SysCountryEntity entity) throws ServiceException {
  34. List<SysCountryTable> sysCountryTables =
  35. sysCountryRepository.getAllByPidAndValidForStartedOrderByEnName(entity.getPid(), 1);
  36. // boolean isCn = Constants.CN_LANG.equals(entity.getLang());
  37. List<SysCountryDto> sysCountryDtos = new ArrayList<>();
  38. for (SysCountryTable sysCountryTable : sysCountryTables) {
  39. SysCountryDto sysCountryDto = new SysCountryDto();
  40. BeanUtils.copyProperties(sysCountryTable, sysCountryDto);
  41. //判断是否是中国 不是则返回英文名称
  42. // if (!isCn) {
  43. // sysCountryDto.setName(sysCountryTable.getEnName());
  44. // }
  45. sysCountryDtos.add(sysCountryDto);
  46. }
  47. return sysCountryDtos;
  48. }
  49. @Override
  50. public List<SysCountryDto> getByPid(SysCountryStateEntity entity) throws ServiceException {
  51. List<SysCountryTable> sysCountryTables = sysCountryRepository.getAllByPidIn(entity.getPids());
  52. // boolean isCn = Constants.CN_LANG.equals(entity.getLang());
  53. List<SysCountryDto> sysCountryDtos = new ArrayList<>();
  54. for (SysCountryTable sysCountryTable : sysCountryTables) {
  55. SysCountryDto sysCountryDto = new SysCountryDto();
  56. BeanUtils.copyProperties(sysCountryTable, sysCountryDto);
  57. //判断是否是中国 不是则返回英文名称
  58. // if (!isCn) {
  59. // sysCountryDto.setName(sysCountryTable.getEnName());
  60. // }
  61. sysCountryDtos.add(sysCountryDto);
  62. }
  63. return sysCountryDtos;
  64. }
  65. @Override
  66. public SysCountryTable getByCode(String code) throws ServiceException {
  67. //判断code不为空
  68. if (Strings.isNullOrEmpty(code)) {
  69. throw new ServiceException(CustomConstant.COUNTRY_NOT_NULL);
  70. }
  71. SysCountryTable sysCountryTable = sysCountryRepository.getByCode(code);
  72. if (sysCountryTable == null) {
  73. throw new ServiceException(CustomConstant.COUNTRY_NOT_EXIST_ERROR);
  74. }
  75. return sysCountryTable;
  76. }
  77. private static DatabaseReader getReader() {
  78. try {
  79. if (databaseReader == null) {
  80. // File file = new File(SysCountryServiceImpl.class.getClassLoader().getResource
  81. // ("country/GeoLite2-Country.mmdb").getFile());
  82. InputStream inputStream = SysCountryServiceImpl.class.getClassLoader().getResourceAsStream("country" +
  83. "/GeoLite2-Country.mmdb");
  84. databaseReader = new DatabaseReader.Builder(inputStream).build();
  85. inputStream.close();
  86. return databaseReader;
  87. } else {
  88. return databaseReader;
  89. }
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. return databaseReader;
  93. }
  94. }
  95. @Override
  96. public String getCodeByIp(String ip) throws Exception {
  97. Country country = null;
  98. try {
  99. InetAddress inetAddress = InetAddress.getByName(ip);
  100. CountryResponse countryResponse = getReader().country(inetAddress);
  101. country = countryResponse.getCountry();
  102. } catch (Exception e) {
  103. country = null;
  104. }
  105. return country != null ? country.getIsoCode() : null;
  106. }
  107. @Override
  108. public String getCodeByIpAndMaxmind(String ip) throws Exception {
  109. Map<String, String> header = new HashMap<>(1);
  110. header.put("Authorization","Basic MTE2MDY3Mzozd0RJbHJfRGFlQ1ZzNjZYSEo5M3FZbXB1T2s1RUtnMDhkT3BfbW1r");
  111. Connection.Response response = HttpUtil.get(String.format("https://geoip.maxmind.com/geoip/v2.1/country/%s?pretty",ip),header);
  112. JSONObject jsonObject = JSON.parseObject(response.body());
  113. return jsonObject.getJSONObject("country").get("iso_code").toString();
  114. }
  115. }