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 getByPid(SysCountryEntity entity) throws ServiceException { List sysCountryTables = sysCountryRepository.getAllByPidAndValidForStartedOrderByEnName(entity.getPid(), 1); // boolean isCn = Constants.CN_LANG.equals(entity.getLang()); List 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 getByPid(SysCountryStateEntity entity) throws ServiceException { List sysCountryTables = sysCountryRepository.getAllByPidIn(entity.getPids()); // boolean isCn = Constants.CN_LANG.equals(entity.getLang()); List 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 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(); } }