| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package com.crm.manager.service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.crm.manager.dao.repository.SysCountryRepository;
- import com.crm.manager.service.SysCountryService;
- import com.crm.rely.backend.core.constant.CustomConstant;
- import com.crm.rely.backend.core.dto.country.SysCountryDto;
- import com.crm.rely.backend.core.entity.country.SysCountryEntity;
- import com.crm.rely.backend.core.entity.country.SysCountryStateEntity;
- import com.crm.rely.backend.core.pojo.table.SysCountryTable;
- import com.crm.rely.backend.exception.ServiceException;
- import com.crm.rely.backend.util.HttpUtil;
- import com.google.common.base.Strings;
- import com.maxmind.geoip2.DatabaseReader;
- import com.maxmind.geoip2.model.CountryResponse;
- import com.maxmind.geoip2.record.Country;
- import org.jsoup.Connection;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.io.InputStream;
- import java.net.InetAddress;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Service
- public class SysCountryServiceImpl implements SysCountryService {
- private static DatabaseReader databaseReader;
- @Autowired
- private SysCountryRepository sysCountryRepository;
- @Override
- public List<SysCountryDto> getByPid(SysCountryEntity entity) throws ServiceException {
- List<SysCountryTable> sysCountryTables =
- sysCountryRepository.getAllByPidAndValidForStartedOrderByEnName(entity.getPid(), 1);
- // boolean isCn = Constants.CN_LANG.equals(entity.getLang());
- List<SysCountryDto> sysCountryDtos = new ArrayList<>();
- for (SysCountryTable sysCountryTable : sysCountryTables) {
- SysCountryDto sysCountryDto = new SysCountryDto();
- BeanUtils.copyProperties(sysCountryTable, sysCountryDto);
- //判断是否是中国 不是则返回英文名称
- // if (!isCn) {
- // sysCountryDto.setName(sysCountryTable.getEnName());
- // }
- sysCountryDtos.add(sysCountryDto);
- }
- return sysCountryDtos;
- }
- @Override
- public List<SysCountryDto> getByPid(SysCountryStateEntity entity) throws ServiceException {
- List<SysCountryTable> sysCountryTables = sysCountryRepository.getAllByPidIn(entity.getPids());
- // boolean isCn = Constants.CN_LANG.equals(entity.getLang());
- List<SysCountryDto> sysCountryDtos = new ArrayList<>();
- for (SysCountryTable sysCountryTable : sysCountryTables) {
- SysCountryDto sysCountryDto = new SysCountryDto();
- BeanUtils.copyProperties(sysCountryTable, sysCountryDto);
- //判断是否是中国 不是则返回英文名称
- // if (!isCn) {
- // sysCountryDto.setName(sysCountryTable.getEnName());
- // }
- sysCountryDtos.add(sysCountryDto);
- }
- return sysCountryDtos;
- }
- @Override
- public SysCountryTable getByCode(String code) throws ServiceException {
- //判断code不为空
- if (Strings.isNullOrEmpty(code)) {
- throw new ServiceException(CustomConstant.COUNTRY_NOT_NULL);
- }
- SysCountryTable sysCountryTable = sysCountryRepository.getByCode(code);
- if (sysCountryTable == null) {
- throw new ServiceException(CustomConstant.COUNTRY_NOT_EXIST_ERROR);
- }
- return sysCountryTable;
- }
- private static DatabaseReader getReader() {
- try {
- if (databaseReader == null) {
- // File file = new File(SysCountryServiceImpl.class.getClassLoader().getResource
- // ("country/GeoLite2-Country.mmdb").getFile());
- InputStream inputStream = SysCountryServiceImpl.class.getClassLoader().getResourceAsStream("country" +
- "/GeoLite2-Country.mmdb");
- databaseReader = new DatabaseReader.Builder(inputStream).build();
- inputStream.close();
- return databaseReader;
- } else {
- return databaseReader;
- }
- } catch (Exception e) {
- e.printStackTrace();
- return databaseReader;
- }
- }
- @Override
- public String getCodeByIp(String ip) throws Exception {
- Country country = null;
- try {
- InetAddress inetAddress = InetAddress.getByName(ip);
- CountryResponse countryResponse = getReader().country(inetAddress);
- country = countryResponse.getCountry();
- } catch (Exception e) {
- country = null;
- }
- return country != null ? country.getIsoCode() : null;
- }
- @Override
- public String getCodeByIpAndMaxmind(String ip) throws Exception {
- Map<String, String> header = new HashMap<>(1);
- header.put("Authorization","Basic MTE2MDY3Mzozd0RJbHJfRGFlQ1ZzNjZYSEo5M3FZbXB1T2s1RUtnMDhkT3BfbW1r");
- Connection.Response response = HttpUtil.get(String.format("https://geoip.maxmind.com/geoip/v2.1/country/%s?pretty",ip),header);
- JSONObject jsonObject = JSON.parseObject(response.body());
- return jsonObject.getJSONObject("country").get("iso_code").toString();
- }
- }
|