顺丰店铺id重建接口

This commit is contained in:
Jack 2026-01-14 23:22:48 +08:00
parent adcb3242e1
commit bde0f1cc53
7 changed files with 262 additions and 40 deletions

View File

@ -1,5 +1,6 @@
package com.suisung.mall.common.utils; package com.suisung.mall.common.utils;
import cn.hutool.core.util.StrUtil;
import com.suisung.mall.common.pojo.to.AddressParseResultTO; import com.suisung.mall.common.pojo.to.AddressParseResultTO;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -100,6 +101,29 @@ public class AddressUtil {
return ""; return "";
} }
/**
* 分解地址字符串支持ID和名称两种格式
*
* @param input 输入字符串格式如"450000/450800/450881" "广西壮族自治区/贵港市/桂平市"
* @return 包含三个元素的数组不足三个的部分用空字符串填充
*/
public static String[] parseAreas2Arr(String input) {
if (StrUtil.isBlank(input)) {
return new String[]{"", "", ""};
}
String[] parts = input.split("/");
String[] result = {"", "", ""}; // 默认填充空字符串
// 将分割后的部分填入结果数组最多填3个
for (int i = 0; i < Math.min(parts.length, 3); i++) {
result[i] = parts[i].trim(); // 去除前后空白字符
}
return result;
}
/** /**
* 使用示例 * 使用示例
* <p> * <p>

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2025. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.suisung.mall.shop.store.controller.admin;
import cn.hutool.json.JSONObject;
import com.suisung.mall.common.api.CommonResult;
import com.suisung.mall.common.service.impl.BaseControllerImpl;
import com.suisung.mall.shop.store.service.ShopStoreSfSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@Slf4j
@Api(tags = "顺丰商家平台控制器")
@RestController
@RequestMapping("/admin/shop/store/sf-supplier")
public class ShopStoreSfSupplierAdminController extends BaseControllerImpl {
@Resource
private ShopStoreSfSupplierService shopStoreSfSupplierService;
/**
* 商家申请入驻商城平台
*
* @param jsonParam
* @return
*/
@ApiOperation(value = "后台-检测是否可重新生成sf店铺ID", notes = "检测是否可重新生成sf店铺ID")
@RequestMapping(value = "/check-recreate-shopid", method = RequestMethod.POST)
public CommonResult checkReCreateSfShopId(@RequestBody JSONObject jsonParam) {
return shopStoreSfSupplierService.checkReCreateSfShopId(jsonParam.getInt("store_id"));
}
@ApiOperation(value = "后台-重新生成sf店铺ID", notes = "后台-重新生成sf店铺ID")
@RequestMapping(value = "/recreate-shopid", method = RequestMethod.POST)
public CommonResult reCreateSfShopId(@RequestBody JSONObject jsonParam) {
return shopStoreSfSupplierService.reCreateSfShopId(jsonParam.getInt("store_id"), jsonParam.getStr("supplier_id"));
}
}

View File

@ -75,6 +75,15 @@ public interface ShopStoreSameCityTransportBaseService {
*/ */
Pair<Long, String> saveOrUpdateShopStoreSameCityTransportBase(ShopStoreSameCityTransportBase shopStoreSameCityTransportBase); Pair<Long, String> saveOrUpdateShopStoreSameCityTransportBase(ShopStoreSameCityTransportBase shopStoreSameCityTransportBase);
/**
* 更新顺丰信息
*
* @param storeId
* @param sfSupplierI
* @param sfShopIdd
* @return
*/
Boolean updateSfInfoByStoreId(Integer storeId, String sfSupplierI, String sfShopIdd);
/** /**
* 初始化默认的同城配送基础运费信息 * 初始化默认的同城配送基础运费信息

View File

@ -55,5 +55,13 @@ public interface ShopStoreSfOrderService extends IBaseService<ShopStoreSfOrder>
* @return * @return
*/ */
Boolean exists(Integer devId, String shopOrderId); Boolean exists(Integer devId, String shopOrderId);
/**
* 检查顺丰同城开发者id下某个订单是否已经存在
*
* @param sfShopId
* @return
*/
Boolean existsBySfShopId(String sfShopId);
} }

View File

@ -16,6 +16,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.suisung.mall.common.api.CommonResult; import com.suisung.mall.common.api.CommonResult;
import com.suisung.mall.common.api.ResultCode; import com.suisung.mall.common.api.ResultCode;
import com.suisung.mall.common.constant.CommonConstant; import com.suisung.mall.common.constant.CommonConstant;
@ -506,6 +507,28 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
} }
} }
/**
* 更新顺丰信息
*
* @param storeId
* @param sfSupplierId
* @param sfShopId
* @return
*/
@Override
public Boolean updateSfInfoByStoreId(Integer storeId, String sfSupplierId, String sfShopId) {
if (StrUtil.hasBlank(sfSupplierId, sfShopId) || CheckUtil.isEmpty(storeId)) {
return false;
}
LambdaUpdateWrapper<ShopStoreSameCityTransportBase> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(ShopStoreSameCityTransportBase::getStore_id, storeId);
updateWrapper.eq(ShopStoreSameCityTransportBase::getSupplier_id, sfSupplierId);
updateWrapper.eq(ShopStoreSameCityTransportBase::getShop_id, sfShopId);
return update(updateWrapper);
}
/** /**
* 初始化默认的同城配送基础运费信息 * 初始化默认的同城配送基础运费信息
@ -680,7 +703,6 @@ public class ShopStoreSameCityTransportBaseServiceImpl extends BaseServiceImpl<S
// 通过配送范围和起送金额决定使用哪个配送费优惠规则 // 通过配送范围和起送金额决定使用哪个配送费优惠规则
// 获取运费配送范围和优惠信息 // 获取运费配送范围和优惠信息
// List<ShopStoreSameCityTransport> transportList = shopStoreSameCityTransportService.selectShopStoreSameCityTransportList(storeId);
List<ShopStoreSameCityTransport> transportList = shopStoreSameCityTransportService.selectShopStoreSameCityTransportListByTransportBaseId(transportBase.getTransport_base_id()); List<ShopStoreSameCityTransport> transportList = shopStoreSameCityTransportService.selectShopStoreSameCityTransportListByTransportBaseId(transportBase.getTransport_base_id());
if (CollUtil.isEmpty(transportList)) { if (CollUtil.isEmpty(transportList)) {
// 没有配送范围和起配金额规则的时候直接以基础配送费来配送 // 没有配送范围和起配金额规则的时候直接以基础配送费来配送

View File

@ -9,6 +9,7 @@
package com.suisung.mall.shop.store.service.impl; package com.suisung.mall.shop.store.service.impl;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.suisung.mall.common.modules.store.ShopStoreSfOrder; import com.suisung.mall.common.modules.store.ShopStoreSfOrder;
@ -217,4 +218,17 @@ public class ShopStoreSfOrderServiceImpl extends BaseServiceImpl<ShopStoreSfOrde
return count(wrapper) > 0; return count(wrapper) > 0;
} }
/**
* 检查顺丰同城开发者id下某个订单是否已经存在
*
* @param sfShopId
* @return
*/
@Override
public Boolean existsBySfShopId(String sfShopId) {
LambdaQueryWrapper<ShopStoreSfOrder> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(ShopStoreSfOrder::getShop_id, sfShopId);
return count(wrapper) > 0;
}
} }

View File

@ -13,11 +13,12 @@ import com.suisung.mall.common.utils.AddressUtil;
import com.suisung.mall.common.utils.CheckUtil; import com.suisung.mall.common.utils.CheckUtil;
import com.suisung.mall.core.web.service.impl.BaseServiceImpl; import com.suisung.mall.core.web.service.impl.BaseServiceImpl;
import com.suisung.mall.shop.sfexpress.service.SFExpressApiService; import com.suisung.mall.shop.sfexpress.service.SFExpressApiService;
import com.suisung.mall.shop.store.mapper.ShopStoreSameCityTransportBaseMapper;
import com.suisung.mall.shop.store.mapper.ShopStoreSfSupplierMapper; import com.suisung.mall.shop.store.mapper.ShopStoreSfSupplierMapper;
import com.suisung.mall.shop.store.service.ShopMchEntryService; import com.suisung.mall.shop.store.service.ShopMchEntryService;
import com.suisung.mall.shop.store.service.ShopStoreSameCityTransportBaseService; import com.suisung.mall.shop.store.service.ShopStoreSameCityTransportBaseService;
import com.suisung.mall.shop.store.service.ShopStoreSfOrderService;
import com.suisung.mall.shop.store.service.ShopStoreSfSupplierService; import com.suisung.mall.shop.store.service.ShopStoreSfSupplierService;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
@ -36,9 +37,6 @@ public class ShopStoreSfSupplierServiceImpl extends BaseServiceImpl<ShopStoreSfS
@Resource @Resource
private ShopStoreSameCityTransportBaseService shopStoreSameCityTransportBaseService; private ShopStoreSameCityTransportBaseService shopStoreSameCityTransportBaseService;
@Lazy
@Resource
private ShopStoreSameCityTransportBaseMapper shopStoreSameCityTransportBaseMapper;
@Lazy @Lazy
@Resource @Resource
@ -48,6 +46,10 @@ public class ShopStoreSfSupplierServiceImpl extends BaseServiceImpl<ShopStoreSfS
@Resource @Resource
private ShopMchEntryService shopMchEntryService; private ShopMchEntryService shopMchEntryService;
@Lazy
@Resource
private ShopStoreSfOrderService shopStoreSfOrderService;
/** /**
* 根据区域ID查询顺丰供应商信息 * 根据区域ID查询顺丰供应商信息
* *
@ -109,16 +111,73 @@ public class ShopStoreSfSupplierServiceImpl extends BaseServiceImpl<ShopStoreSfS
} }
/** /**
* 检测是否可重新生成sf店铺ID * 重新生成新店铺Id之前检测是否可重新生成sf店铺ID
* *
* @param storeId 店铺ID * @param storeId 店铺ID
* @return * @return
*/ */
@Override @Override
public CommonResult checkReCreateSfShopId(Integer storeId) { public CommonResult checkReCreateSfShopId(Integer storeId) {
return null; if (CheckUtil.isEmpty(storeId)) {
return CommonResult.failed("缺少必要参数");
} }
ShopStoreSameCityTransportBase sameCityTransportBase = shopStoreSameCityTransportBaseService.getShopStoreSameCityTransportBaseById(Convert.toLong(storeId), 2);
if (sameCityTransportBase == null) {
return CommonResult.failed("请配置店铺配送设置,再重试");
}
ShopMchEntry shopMchEntry = shopMchEntryService.getShopMerchEntryByStoreId(storeId);
if (shopMchEntry == null || !CommonConstant.Enable.equals(shopMchEntry.getApproval_status())) {
return CommonResult.failed("未找到商家入驻信息,请检查");
}
// 获取旧的顺丰店铺ID和供应商ID
String oldSfShopId = sameCityTransportBase.getShop_id();
String oldSupplierId = sameCityTransportBase.getSupplier_id();
// 检查是否有相关订单
Boolean hasShopOrder = StrUtil.isNotBlank(oldSfShopId) && shopStoreSfOrderService.existsBySfShopId(oldSfShopId);
// 解析地址信息
String[] areaIds = AddressUtil.parseAreas2Arr(shopMchEntry.getStore_district());
// 如果没有供应商ID尝试从地区查找
if (StrUtil.isBlank(oldSupplierId)) {
ShopStoreSfSupplier sfSupplier = getByDistrictId(getArrayElement(areaIds, 2));
if (sfSupplier != null) {
oldSupplierId = sfSupplier.getSupplier_id();
}
}
// 构建响应数据
JSONObject resp = new JSONObject();
resp.put("hasShopOrder", hasShopOrder);
resp.put("supplier_id", oldSupplierId);
resp.put("province_id", getArrayElement(areaIds, 0));
resp.put("city_id", getArrayElement(areaIds, 1));
resp.put("district_id", getArrayElement(areaIds, 2));
// 设置状态和消息
int status = 1;
String message = "顺丰店铺Id 未生成,可重建操作";
if (StrUtil.isNotBlank(oldSfShopId)) {
if (hasShopOrder) {
status = 3;
message = "顺丰店铺Id 和相关订单已存在,请检查是否需重建?";
} else {
status = 2;
message = "顺丰店铺Id 已存在,请检查是否需重建?";
}
}
resp.put("status", status);
resp.put("message", message);
return CommonResult.success(resp);
}
/** /**
* 重新生成sf店铺ID * 重新生成sf店铺ID
* *
@ -138,51 +197,41 @@ public class ShopStoreSfSupplierServiceImpl extends BaseServiceImpl<ShopStoreSfS
return CommonResult.failed("请配置店铺配送设置,再重试"); return CommonResult.failed("请配置店铺配送设置,再重试");
} }
ShopMchEntry shopMchEntry = shopMchEntryService.getShopMerchEntryByStoreId(storeId); ShopMchEntry shopMchEntry = shopMchEntryService.getShopMerchEntryByStoreId(storeId);
if (shopMchEntry == null) { if (shopMchEntry == null || !CommonConstant.Enable.equals(shopMchEntry.getApproval_status())) {
return CommonResult.failed("找到商家入驻信息,请检查"); return CommonResult.failed("找到商家入驻信息,请检查");
} }
// 省市区名称 // 解析地址信息
String storeArea = shopMchEntry.getStore_area();
// 省市区Id
String storeDistrict = shopMchEntry.getStore_district();
AddressParseResultTO addressParseResultTO = AddressUtil.parseAddress(shopMchEntry.getStore_address()); AddressParseResultTO addressParseResultTO = AddressUtil.parseAddress(shopMchEntry.getStore_address());
String cityName = "桂平市"; String[] areaNames = AddressUtil.parseAreas2Arr(shopMchEntry.getStore_area());
String[] areaIds = AddressUtil.parseAreas2Arr(shopMchEntry.getStore_district());
// 获取城市名称
String cityName = getCityName(shopMchEntry.getStore_area(), addressParseResultTO);
String shopAddress = addressParseResultTO != null ? addressParseResultTO.getDetailAddress() : ""; String shopAddress = addressParseResultTO != null ? addressParseResultTO.getDetailAddress() : "";
String[] areaNames; // 调用顺丰API创建店铺
if (StrUtil.isNotBlank(shopMchEntry.getStore_area())) { Pair<Boolean, String> result = sFExpressApiService.createSfExpressShopInner(storeId, sfSupplierId,
areaNames = shopMchEntry.getStore_area().split("/"); shopMchEntry.getStore_name(), cityName, shopAddress, shopMchEntry.getContact_name(),
cityName = areaNames.length >= 3 ? areaNames[2] : shopMchEntry.getLogin_mobile(), shopMchEntry.getStore_longitude(), shopMchEntry.getStore_latitude());
areaNames.length > 0 ? areaNames[areaNames.length - 1] : cityName;
} else if (addressParseResultTO != null && StrUtil.isNotBlank(addressParseResultTO.getCity())) {
cityName = addressParseResultTO.getCity();
}
if (StrUtil.isBlank(cityName)) {
cityName = "桂平市";
logger.warn("[顺丰] 城市名为空,使用默认城市: {}", cityName);
} else {
logger.debug("[顺丰] 解析得到城市名: {}", cityName);
}
Pair<Boolean, String> result = sFExpressApiService.createSfExpressShopInner(storeId, sfSupplierId, shopMchEntry.getStore_name(),
cityName, shopAddress, shopMchEntry.getContact_name(), shopMchEntry.getLogin_mobile(),
shopMchEntry.getStore_longitude(), shopMchEntry.getStore_latitude());
if (Boolean.FALSE.equals(result.getFirst())) { if (Boolean.FALSE.equals(result.getFirst())) {
return CommonResult.failed(result.getSecond()); return CommonResult.failed(result.getSecond());
} }
// 更改店铺sf店铺ID // 更新店铺信息
sameCityTransportBase.setSupplier_id(sfSupplierId); shopStoreSameCityTransportBaseService.updateSfInfoByStoreId(storeId, sfSupplierId, result.getSecond());
sameCityTransportBase.setShop_id(result.getSecond());
shopStoreSameCityTransportBaseMapper.updateById(sameCityTransportBase);
// 保存或更新顺丰供应商信息
ShopStoreSfSupplier record = new ShopStoreSfSupplier(); ShopStoreSfSupplier record = new ShopStoreSfSupplier();
record.setProvince_id(getArrayElement(areaIds, 0));
record.setCity_id(getArrayElement(areaIds, 1));
record.setDistrict_id(getArrayElement(areaIds, 2));
record.setProvince_name(getArrayElement(areaNames, 0));
record.setCity_name(getArrayElement(areaNames, 1));
record.setDistrict_name(getArrayElement(areaNames, 2));
record.setSupplier_id(sfSupplierId); record.setSupplier_id(sfSupplierId);
addOrUpdate(record); addOrUpdate(record);
@ -190,4 +239,48 @@ public class ShopStoreSfSupplierServiceImpl extends BaseServiceImpl<ShopStoreSfS
return CommonResult.success(); return CommonResult.success();
} }
/**
* 获取城市名称
*
* @param storeArea 省市区名称
* @param addressParseResultTO 地址解析结果
* @return 城市名称
*/
private String getCityName(String storeArea, AddressParseResultTO addressParseResultTO) {
String defaultCity = "桂平市";
String cityName = defaultCity;
if (StrUtil.isNotBlank(storeArea)) {
String[] areaNames = AddressUtil.parseAreas2Arr(storeArea);
if (areaNames.length >= 3) {
cityName = areaNames[2];
} else if (areaNames.length > 0) {
cityName = areaNames[areaNames.length - 1];
}
} else if (addressParseResultTO != null && StrUtil.isNotBlank(addressParseResultTO.getCity())) {
cityName = addressParseResultTO.getCity();
}
if (StrUtil.isBlank(cityName)) {
cityName = defaultCity;
logger.warn("[顺丰] 城市名为空,使用默认城市: {}", cityName);
} else {
logger.debug("[顺丰] 解析得到城市名: {}", cityName);
}
return cityName;
}
/**
* 安全获取数组元素
*
* @param array 数组
* @param index 索引
* @return 元素值如果索引超出范围则返回null
*/
private String getArrayElement(String[] array, int index) {
return array != null && array.length > index ? array[index] : "";
}
} }