增加产品导入功能
This commit is contained in:
@@ -34,6 +34,14 @@ public interface BmsRegionMapper
|
||||
*/
|
||||
public BmsRegion selectRegionByCode2(String countryCode2);
|
||||
|
||||
/**
|
||||
* 根据中文名精确查询区域
|
||||
*
|
||||
* @param countryNameCn 中文名
|
||||
* @return 区域信息
|
||||
*/
|
||||
public BmsRegion selectRegionByNameCn(String countryNameCn);
|
||||
|
||||
/**
|
||||
* 新增区域
|
||||
*
|
||||
|
||||
+19
-22
@@ -1,16 +1,15 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.system.domain.BmsRegion;
|
||||
@@ -21,7 +20,7 @@ import com.ruoyi.system.service.IBmsRegionSyncService;
|
||||
|
||||
/**
|
||||
* 区域数据同步 服务层实现
|
||||
* 从本地 countries.json 文件加载国家数据
|
||||
* 通过 jsdelivr + mledoze/countries 同步国家数据(含中文翻译)
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@@ -30,7 +29,12 @@ public class BmsRegionSyncServiceImpl implements IBmsRegionSyncService
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(BmsRegionSyncServiceImpl.class);
|
||||
|
||||
private static final String DATA_FILE = "countries.json";
|
||||
/** 国家数据源(mledoze/countries,jsdelivr CDN,国内可直连,无需鉴权) */
|
||||
@Value("${bms.region.sync.url:https://cdn.jsdelivr.net/gh/mledoze/countries@master/dist/countries.json}")
|
||||
private String dataSourceUrl;
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
private BmsRegionMapper regionMapper;
|
||||
@@ -77,18 +81,13 @@ public class BmsRegionSyncServiceImpl implements IBmsRegionSyncService
|
||||
|
||||
try
|
||||
{
|
||||
log.info("开始同步国家数据,读取本地文件: {}", DATA_FILE);
|
||||
|
||||
// 从classpath读取国家数据文件
|
||||
ClassPathResource resource = new ClassPathResource(DATA_FILE);
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
byte[] bytes = inputStream.readAllBytes();
|
||||
inputStream.close();
|
||||
String jsonData = new String(bytes, StandardCharsets.UTF_8);
|
||||
log.info("开始同步国家数据,远程数据源: {}", dataSourceUrl);
|
||||
|
||||
// 从远程接口拉取国家数据
|
||||
String jsonData = restTemplate.getForObject(dataSourceUrl, String.class);
|
||||
if (StringUtils.isEmpty(jsonData))
|
||||
{
|
||||
throw new RuntimeException("国家数据文件为空");
|
||||
throw new RuntimeException("远程国家数据为空");
|
||||
}
|
||||
|
||||
// 解析JSON
|
||||
@@ -180,20 +179,18 @@ public class BmsRegionSyncServiceImpl implements IBmsRegionSyncService
|
||||
region.setCountryNameEn((String) name.get("common"));
|
||||
}
|
||||
|
||||
// 中文名 translations.cmn.common,如无则用英文名
|
||||
// 中文名 translations.zho.common(mledoze/countries 标准字段名为 zho)
|
||||
Map<String, Object> translations = (Map<String, Object>) country.get("translations");
|
||||
if (translations != null)
|
||||
{
|
||||
Map<String, Object> cmn = (Map<String, Object>) translations.get("cmn");
|
||||
if (cmn != null)
|
||||
Map<String, Object> zho = (Map<String, Object>) translations.get("zho");
|
||||
if (zho != null)
|
||||
{
|
||||
region.setCountryNameCn((String) cmn.get("common"));
|
||||
region.setCountryNameCn((String) zho.get("common"));
|
||||
}
|
||||
}
|
||||
if (StringUtils.isEmpty(region.getCountryNameCn()))
|
||||
{
|
||||
region.setCountryNameCn(region.getCountryNameEn());
|
||||
}
|
||||
// 注:取不到中文名时不再用英文名兜底,避免污染业务匹配。
|
||||
// 若确需兜底,可改为 region.setCountryNameCn(StringUtils.EMPTY);
|
||||
|
||||
// ISO代码
|
||||
region.setCountryCode2((String) country.get("cca2"));
|
||||
|
||||
Reference in New Issue
Block a user