|
|
@@ -1,107 +1,85 @@
|
|
|
package com.crm.manager.service.impl;
|
|
|
|
|
|
-import com.crm.manager.dao.mapper.SysCountryMapper;
|
|
|
+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.Constants;
|
|
|
import com.crm.rely.backend.core.constant.CustomConstant;
|
|
|
-import com.crm.rely.backend.core.dto.base.PageDto;
|
|
|
-import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
|
|
|
-import com.crm.rely.backend.core.dto.country.SysCountrySearchListDto;
|
|
|
-import com.crm.rely.backend.core.entity.base.SingleLongEntity;
|
|
|
-import com.crm.rely.backend.core.entity.country.*;
|
|
|
+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 org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+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 {
|
|
|
|
|
|
- @Autowired
|
|
|
- private SysCountryRepository sysCountryRepository;
|
|
|
+ private static DatabaseReader databaseReader;
|
|
|
|
|
|
@Autowired
|
|
|
- private SysCountryMapper sysCountryMapper;
|
|
|
+ private SysCountryRepository sysCountryRepository;
|
|
|
|
|
|
@Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void add(SysCountryAddEntity entity) throws ServiceException{
|
|
|
- SysCountryTable table = new SysCountryTable();
|
|
|
- BeanUtils.copyProperties(entity, table);
|
|
|
- sysCountryRepository.save(table);
|
|
|
- }
|
|
|
+ public List<SysCountryDto> getByPid(SysCountryEntity entity) throws ServiceException {
|
|
|
|
|
|
- @Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void delete(SysCountryDeleteEntitys entity) throws ServiceException {
|
|
|
- List<SysCountryTable> tables = sysCountryRepository.findAllByIdIn(entity.getIds());
|
|
|
- if (tables == null) {
|
|
|
- throw ServiceException.exception(Constants.INFO_NOT_FOUND);
|
|
|
- }
|
|
|
- //sysCountryRepository.delete(tables);
|
|
|
- for (SysCountryTable table : tables) {
|
|
|
- sysCountryRepository.delete(table);
|
|
|
- }
|
|
|
- }
|
|
|
+ List<SysCountryTable> sysCountryTables =
|
|
|
+ sysCountryRepository.getAllByPidAndValidForStartedOrderByEnName(entity.getPid(), 1);
|
|
|
|
|
|
- @Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public void update(SysCountryUpdateEntity entity) throws ServiceException {
|
|
|
- SysCountryTable table = sysCountryRepository.findFirstById(entity.getId());
|
|
|
- if (table == null) {
|
|
|
- throw ServiceException.exception(Constants.INFO_NOT_FOUND);
|
|
|
+// 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);
|
|
|
}
|
|
|
- BeanUtils.copyProperties(entity, table);
|
|
|
- sysCountryRepository.save(table);
|
|
|
|
|
|
+ return sysCountryDtos;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ResultWithPagerDto<SysCountrySearchListDto> searchPageList(SysCountrySearchListEntity entity) throws ServiceException {
|
|
|
- PageDto pageDto = null;
|
|
|
- if(entity.getPid() == null || entity.getPid() == 0){
|
|
|
- entity.setPid(0l);
|
|
|
- Integer count = sysCountryMapper.countList(entity);
|
|
|
- pageDto = PageDto.format(entity, count);
|
|
|
- if (count == null || count <= 0) {
|
|
|
- return ResultWithPagerDto.success(pageDto);
|
|
|
- }
|
|
|
- }else{
|
|
|
- entity.setPage(null);
|
|
|
- }
|
|
|
+ public List<SysCountryDto> getByPid(SysCountryStateEntity entity) throws ServiceException {
|
|
|
|
|
|
- List<SysCountryTable> sysCountryTables = sysCountryMapper.pageList(entity);
|
|
|
- List<SysCountrySearchListDto> sysCountrySearchListDtos = new ArrayList<>(sysCountryTables.size());
|
|
|
+ List<SysCountryTable> sysCountryTables = sysCountryRepository.getAllByPidIn(entity.getPids());
|
|
|
|
|
|
- for (SysCountryTable sysCountryTable : sysCountryTables) {
|
|
|
- SysCountrySearchListDto sysCountrySearchListDto = transformSearchListDto(sysCountryTable);
|
|
|
- sysCountrySearchListDtos.add(sysCountrySearchListDto);
|
|
|
- }
|
|
|
-
|
|
|
- return ResultWithPagerDto.success(pageDto, sysCountrySearchListDtos);
|
|
|
- }
|
|
|
+// boolean isCn = Constants.CN_LANG.equals(entity.getLang());
|
|
|
|
|
|
- private SysCountrySearchListDto transformSearchListDto(SysCountryTable sysCountryTable) {
|
|
|
- SysCountrySearchListDto sysCountrySearchListDto = new SysCountrySearchListDto();
|
|
|
- BeanUtils.copyProperties(sysCountryTable, sysCountrySearchListDto);
|
|
|
- return sysCountrySearchListDto;
|
|
|
- }
|
|
|
+ List<SysCountryDto> sysCountryDtos = new ArrayList<>();
|
|
|
|
|
|
- @Override
|
|
|
- public SysCountrySearchListDto searchSingle(SingleLongEntity entity) throws ServiceException {
|
|
|
- SysCountrySearchListDto dto = sysCountryMapper.getFirstById(entity.getId());
|
|
|
- if (dto == null) {
|
|
|
- throw ServiceException.exception(Constants.INFO_NOT_FOUND);
|
|
|
+ for (SysCountryTable sysCountryTable : sysCountryTables) {
|
|
|
+ SysCountryDto sysCountryDto = new SysCountryDto();
|
|
|
+ BeanUtils.copyProperties(sysCountryTable, sysCountryDto);
|
|
|
+
|
|
|
+ //判断是否是中国 不是则返回英文名称
|
|
|
+// if (!isCn) {
|
|
|
+// sysCountryDto.setName(sysCountryTable.getEnName());
|
|
|
+// }
|
|
|
+ sysCountryDtos.add(sysCountryDto);
|
|
|
}
|
|
|
|
|
|
- return dto;
|
|
|
+ return sysCountryDtos;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -120,4 +98,50 @@ public class SysCountryServiceImpl implements SysCountryService {
|
|
|
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();
|
|
|
+ }
|
|
|
}
|