同步中通行政区划

pull/1121/head
梁宇奇 2025-07-14 20:38:42 +08:00
parent 008deabf2b
commit 3a13e1b580
8 changed files with 153 additions and 10 deletions

View File

@ -6,6 +6,7 @@ import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.core.BulkResponse;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
@ -42,8 +43,10 @@ import com.ruoyi.xkt.dto.useSearchHistory.UserSearchHistoryDTO;
import com.ruoyi.xkt.dto.userBrowsingHistory.UserBrowsingHisDTO;
import com.ruoyi.xkt.enums.*;
import com.ruoyi.xkt.manager.PaymentManager;
import com.ruoyi.xkt.manager.impl.ZtoExpressManagerImpl;
import com.ruoyi.xkt.mapper.*;
import com.ruoyi.xkt.service.*;
import com.ruoyi.xkt.thirdpart.zto.ZtoRegion;
import io.jsonwebtoken.lang.Assert;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -114,6 +117,8 @@ public class XktTask {
final UserNoticeMapper userNoticeMapper;
final UserSubscriptionsMapper userSubMapper;
final StoreMemberMapper storeMemberMapper;
final IExpressService expressService;
final ZtoExpressManagerImpl ztoExpressManager;
public void test() throws IOException {
System.err.println("aaa");
@ -1534,6 +1539,40 @@ public class XktTask {
this.userNoticeMapper.insert(userNoticeList);
}
/**
*
*/
public void syncRegionFromZto() {
log.info("-------------同步行政区划开始-------------");
try {
Map<Long, ZtoRegion> ztoRegionMap = ztoExpressManager.getAllRegion()
.stream()
.collect(Collectors.toMap(ZtoRegion::getId, Function.identity()));
List<ExpressRegion> expressRegions = new ArrayList<>(ztoRegionMap.size());
ztoRegionMap.values().forEach(ztoRegion -> {
ExpressRegion expressRegion = new ExpressRegion();
expressRegion.setId(ztoRegion.getId());
expressRegion.setRegionCode(ztoRegion.getNationalCode());
expressRegion.setRegionName(ztoRegion.getFullName());
if (ztoRegion.getParentId() != null) {
ZtoRegion ztoParentRegion = ztoRegionMap.get(ztoRegion.getParentId());
if (ztoParentRegion != null) {
expressRegion.setParentRegionCode(ztoParentRegion.getNationalCode());
expressRegion.setParentRegionName(ztoParentRegion.getFullName());
}
}
expressRegion.setRegionLevel(ztoRegion.getLayer());
expressRegion.setVersion(0L);
expressRegion.setDelFlag(Constants.UNDELETED);
expressRegions.add(expressRegion);
});
expressService.clearAndInsertAllRegion(expressRegions);
} catch (Exception e) {
log.error("同步行政区划异常", e);
fsNotice.sendMsg2DefaultChat("同步行政区划异常", StrUtil.emptyIfNull(e.getMessage()));
}
log.info("-------------同步行政区划结束-------------");
}
}

View File

@ -40,9 +40,11 @@ public class ZtoExpressManagerImpl implements ExpressManager, InitializingBean {
private static final String STRUCTURE_ADDRESS_URI = "zto.innovate.structureNamePhoneAddress";
public static final String ORDER_PRINT_URI = "zto.open.order.print";
private static final String ORDER_PRINT_URI = "zto.open.order.print";
public static final String TRACK_SUB_URI = "zto.merchant.waybill.track.subsrcibe";
private static final String TRACK_SUB_URI = "zto.merchant.waybill.track.subsrcibe";
private static final String QUERY_ALL_REGION_URI = "zto.sp.findAllForbServiceGateway";
@Value("${zto.appKey:}")
private String appKey;
@ -282,6 +284,30 @@ public class ZtoExpressManagerImpl implements ExpressManager, InitializingBean {
throw new ServiceException("中通智能解析失败");
}
/**
*
*
* @return
*/
public List<ZtoRegion> getAllRegion() {
ZopPublicRequest request = new ZopPublicRequest();
request.addParam("a", "a");
request.setUrl(gatewayUrl + QUERY_ALL_REGION_URI);
request.setEncryptionType(EncryptionType.MD5);
request.setReqTimeout(300000);
try {
String bodyStr = client.execute(request);
JSONObject bodyJson = JSONUtil.parseObj(bodyStr);
boolean success = BooleanUtil.isTrue(bodyJson.getBool("status"));
if (success) {
return bodyJson.getJSONArray("result").toList(ZtoRegion.class);
}
} catch (Exception e) {
log.error("中通获取行政区划异常", e);
}
throw new ServiceException("中通获取行政区划失败");
}
private ZtoCreateOrderParam trans2CreateOrderReq(ExpressShipReqDTO expressShipReqDTO) {
ZtoCreateOrderParam reqDTO = new ZtoCreateOrderParam();
//合作模式 1集团客户2非集团客户

View File

@ -181,5 +181,12 @@ public interface IExpressService {
*/
void modifyExpressFeeConfig(ExpressFeeConfigEditDTO editDTO);
/**
*
*
* @param allRegions
*/
void clearAndInsertAllRegion(List<ExpressRegion> allRegions);
}

View File

@ -386,4 +386,20 @@ public class ExpressServiceImpl implements IExpressService {
config.setUpdateTime(new Date());
expressFeeConfigMapper.updateById(config);
}
@Transactional(rollbackFor = Exception.class)
@Override
public void clearAndInsertAllRegion(List<ExpressRegion> allRegions) {
//清空行政区划
expressRegionMapper.delete(Wrappers.emptyWrapper());
//插入行政区划
for (ExpressRegion region : allRegions) {
expressRegionMapper.insert(region);
}
//清除缓存
redisCache.deleteObject(Constants.EXPRESS_REGION_MAP_CACHE_KEY);
redisCache.deleteObject(Constants.EXPRESS_REGION_NAME_MAP_CACHE_KEY);
redisCache.deleteObject(Constants.EXPRESS_REGION_LIST_CACHE_KEY);
redisCache.deleteObject(Constants.EXPRESS_REGION_TREE_CACHE_KEY);
}
}

View File

@ -36,7 +36,7 @@ public class ZopClient {
if (request.getTimestamp() != null) {
headers.put("x-timestamp", String.valueOf(request.getTimestamp()));
}
return ZtoHttpUtil.post(request.getUrl(), headers, queryString);
return ZtoHttpUtil.post(request.getUrl(), headers, queryString, request.getReqTimeout());
} else {
Map<String, String> headers = new HashMap<>();
String strToDigest = jsonBody + properties.getKey();
@ -45,7 +45,7 @@ public class ZopClient {
if (request.getTimestamp() != null) {
headers.put("x-timestamp", String.valueOf(request.getTimestamp()));
}
return ZtoHttpUtil.postJson(request.getUrl(), headers, jsonBody);
return ZtoHttpUtil.postJson(request.getUrl(), headers, jsonBody, request.getReqTimeout());
}
}

View File

@ -35,6 +35,8 @@ public class ZopPublicRequest {
*/
private Long timestamp;
private Integer reqTimeout;
public ZopPublicRequest() {
}
@ -119,4 +121,12 @@ public class ZopPublicRequest {
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Integer getReqTimeout() {
return reqTimeout;
}
public void setReqTimeout(Integer reqTimeout) {
this.reqTimeout = reqTimeout;
}
}

View File

@ -13,14 +13,19 @@ public class ZtoHttpUtil {
private static final int DEFAULT_TIMEOUT = 3000;
public static String post(String interfaceUrl, Map<String, String> headers, String queryString) throws IOException {
public static String post(String interfaceUrl, Map<String, String> headers, String queryString, Integer timeout) throws IOException {
URL url = new URL(interfaceUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
con.setDoOutput(true);
con.setConnectTimeout(DEFAULT_TIMEOUT);
con.setReadTimeout(DEFAULT_TIMEOUT);
if (timeout == null) {
con.setConnectTimeout(DEFAULT_TIMEOUT);
con.setReadTimeout(DEFAULT_TIMEOUT);
} else {
con.setConnectTimeout(timeout);
con.setReadTimeout(timeout);
}
for (Map.Entry<String, String> e : headers.entrySet()) {
con.setRequestProperty(e.getKey(), e.getValue());
}
@ -57,14 +62,19 @@ public class ZtoHttpUtil {
}
public static String postJson(String interfaceUrl, Map<String, String> headers, String json) throws IOException {
public static String postJson(String interfaceUrl, Map<String, String> headers, String json, Integer timeout) throws IOException {
URL url = new URL(interfaceUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=utf-8");
con.setDoOutput(true);
con.setConnectTimeout(DEFAULT_TIMEOUT);
con.setReadTimeout(DEFAULT_TIMEOUT);
if (timeout == null) {
con.setConnectTimeout(DEFAULT_TIMEOUT);
con.setReadTimeout(DEFAULT_TIMEOUT);
} else {
con.setConnectTimeout(timeout);
con.setReadTimeout(timeout);
}
for (Map.Entry<String, String> e : headers.entrySet()) {
con.setRequestProperty(e.getKey(), e.getValue());
}

View File

@ -0,0 +1,35 @@
package com.ruoyi.xkt.thirdpart.zto;
import lombok.Data;
/**
* @author liangyq
* @date 2025-07-14
*/
@Data
public class ZtoRegion {
/**
* id
*/
private Long id;
/**
*
*/
private Long parentId;
/**
*
*/
private String code;
/**
*
*/
private String nationalCode;
/**
*
*/
private String fullName;
/**
*
*/
private Integer layer;
}