Commit 039f182a by 周立明

优加审核通过后发布对账规则

1 parent ffcea896
...@@ -93,6 +93,55 @@ public class RopClient { ...@@ -93,6 +93,55 @@ public class RopClient {
} }
/** /**
* 以 <b>POST</b> 方式调用 ROP 方法并返回响应根节点。
*
* <p>与 {@link #invoke} 的差异仅在传输方式(POST,参数同时走 query 与 form body),
* 加签、凭据、超时、错误码处理完全一致。</p>
*
* <p>适用于 D+ 侧入参为嵌套结构的接口——单个 {@code param} 参数承载 JSON 报文,
* 用 GET 拼接超长JSON 会被 URL 长度限制截断。</p>
*
* @param app 应用编码,见 {@link RopProperties#APP_SMARTCARLIFE}
* @param method ROP 方法名
* @param params 业务参数(可为 null)
* @return 响应 JSON 根节点
*/
public JsonNode invokePost(String app, String method, MultiValueMap<String, String> params) {
if (!properties.isEnabled()) {
throw new BusinessException(ResultCode.REMOTE_CALL_FAILED,
"D+ 接口未启用(请在 Nacos 配置 youjia.rop.enabled=true)");
}
if (!properties.wellConfigured(app)) {
throw new BusinessException(ResultCode.REMOTE_CALL_FAILED,
"D+ 接口未配置:" + properties.missingConfigHint(app));
}
String url = properties.resolveUrl(app);
String appKey = properties.resolveAppKey(app);
String secret = properties.resolveAppSecret(app);
MultiValueMap<String, String> all = new LinkedMultiValueMap<>();
if (params != null) {
all.putAll(params);
}
String body;
try {
body = RopHttpUtil.post(url, all, method, appKey, secret,
properties.getVersion(), properties.getLocale(), getRequestFactory());
} catch (BusinessException e) {
throw e;
} catch (Exception e) {
log.error("[ROP] POST 调用失败 app={} method={} url={} msg={}", app, method, url, e.getMessage(), e);
throw new BusinessException(ResultCode.REMOTE_CALL_FAILED,
"D+ 接口调用失败(" + method + "):" + e.getMessage());
}
log.debug("[ROP] POST app={} method={} resp={}", app, method, body);
return parseAndCheck(app, method, body);
}
/**
* 调用 ROP 方法并把 {@code result} 反序列化为指定类型。 * 调用 ROP 方法并把 {@code result} 反序列化为指定类型。
* *
* @param app 应用编码 * @param app 应用编码
......
package com.youjia.framework.util; package com.youjia.framework.util;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
...@@ -50,6 +53,57 @@ public final class RopHttpUtil { ...@@ -50,6 +53,57 @@ public final class RopHttpUtil {
String method, String appKey, String secret, String method, String appKey, String secret,
String version, String locale, String version, String locale,
SimpleClientHttpRequestFactory factory) { SimpleClientHttpRequestFactory factory) {
MultiValueMap<String, String> all = buildSignParams(params, method, appKey, secret, version, locale);
RestTemplate rest = new RestTemplate(factory);
RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(url);
uriTemplateHandler.setDefaultUriVariables(all.toSingleValueMap());
rest.setUriTemplateHandler(uriTemplateHandler);
return rest.getForObject(url, String.class, all.toSingleValueMap());
}
/**
* ROP 调用(POST,query + form 双通道传参)。
*
* <p>与 {@link #get} 的差异仅在传输方式:参数同时放入 URL query 与 form body,
* 签名算法沿用 D+ 侧统一的 SHA1 大写规则(见 {@link #sign})。</p>
*
* @param url 完整网关地址,形如 {@code http://host/rop-dj-xxx/router}
* @param params 业务参数
* @param method ROP 方法名
* @param appKey 应用标识
* @param secret 应用密钥
* @param version 接口版本号
* @param locale 区域
* @param factory 已配置超时的请求工厂
* @return 响应原文(JSON 字符串)
*/
public static String post(String url, MultiValueMap<String, String> params,
String method, String appKey, String secret,
String version, String locale,
SimpleClientHttpRequestFactory factory) {
MultiValueMap<String, String> all = buildSignParams(params, method, appKey, secret, version, locale);
RestTemplate rest = new RestTemplate(factory);
// query 与 body 双通道携带,满足对端 ROP 框架从 request 读取参数的两种方式
RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(url);
uriTemplateHandler.setDefaultUriVariables(all.toSingleValueMap());
rest.setUriTemplateHandler(uriTemplateHandler);
MultiValueMap<String, String> form = new LinkedMultiValueMap<>();
form.putAll(all);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(form, headers);
return rest.postForObject(url, entity, String.class);
}
/**
* 组装 ROP 系统参数并完成签名,GET / POST 共用。
*/
private static MultiValueMap<String, String> buildSignParams(MultiValueMap<String, String> params,
String method, String appKey, String secret,
String version, String locale) {
MultiValueMap<String, String> all = new LinkedMultiValueMap<>(16); MultiValueMap<String, String> all = new LinkedMultiValueMap<>(16);
// 参数名必须是 appKey(camelCase)—— D+ 的 ROP 框架按此名取应用标识, // 参数名必须是 appKey(camelCase)—— D+ 的 ROP 框架按此名取应用标识,
// 写成 app_key 会被判为「缺少appKey参数」(code=22)。 // 写成 app_key 会被判为「缺少appKey参数」(code=22)。
...@@ -67,14 +121,8 @@ public final class RopHttpUtil { ...@@ -67,14 +121,8 @@ public final class RopHttpUtil {
|| e.getValue().isEmpty() || e.getValue().isEmpty()
|| (e.getValue().size() == 1 && !StringUtils.isNotBlank(e.getValue().get(0)))); || (e.getValue().size() == 1 && !StringUtils.isNotBlank(e.getValue().get(0))));
String sign = sign(all.toSingleValueMap(), secret); all.add("sign", sign(all.toSingleValueMap(), secret));
all.add("sign", sign); return all;
RestTemplate rest = new RestTemplate(factory);
RopUriTemplateHandler uriTemplateHandler = new RopUriTemplateHandler(url);
uriTemplateHandler.setDefaultUriVariables(all.toSingleValueMap());
rest.setUriTemplateHandler(uriTemplateHandler);
return rest.getForObject(url, String.class, all.toSingleValueMap());
} }
/** /**
......
...@@ -23,6 +23,7 @@ import com.youjia.store.mapper.StoreChangeLogMapper; ...@@ -23,6 +23,7 @@ import com.youjia.store.mapper.StoreChangeLogMapper;
import com.youjia.store.mapper.StoreListingItemMapper; import com.youjia.store.mapper.StoreListingItemMapper;
import com.youjia.store.mapper.StoreListingMapper; import com.youjia.store.mapper.StoreListingMapper;
import com.youjia.store.remote.StorePartnerMerchantSyncer; import com.youjia.store.remote.StorePartnerMerchantSyncer;
import com.youjia.store.service.ReconciliationRulePublishService;
import com.youjia.store.service.StoreApprovalService; import com.youjia.store.service.StoreApprovalService;
import com.youjia.store.support.StoreApprovalConfig; import com.youjia.store.support.StoreApprovalConfig;
import com.youjia.store.support.StoreListingAssembler; import com.youjia.store.support.StoreListingAssembler;
...@@ -55,6 +56,7 @@ public class StoreApprovalServiceImpl implements StoreApprovalService { ...@@ -55,6 +56,7 @@ public class StoreApprovalServiceImpl implements StoreApprovalService {
private final StoreApprovalConfig storeApprovalConfig; private final StoreApprovalConfig storeApprovalConfig;
private final DataScopeHelper dataScopeHelper; private final DataScopeHelper dataScopeHelper;
private final StorePartnerMerchantSyncer storePartnerMerchantSyncer; private final StorePartnerMerchantSyncer storePartnerMerchantSyncer;
private final ReconciliationRulePublishService reconciliationRulePublishService;
@Override @Override
public PageResult<StoreApprovalTaskVO> pagePending(StoreApprovalTaskQueryDTO query) { public PageResult<StoreApprovalTaskVO> pagePending(StoreApprovalTaskQueryDTO query) {
...@@ -147,12 +149,32 @@ public class StoreApprovalServiceImpl implements StoreApprovalService { ...@@ -147,12 +149,32 @@ public class StoreApprovalServiceImpl implements StoreApprovalService {
StoreStatusEnum.PENDING.getCode(), StoreStatusEnum.APPROVED.getCode(), "审批通过"); StoreStatusEnum.PENDING.getCode(), StoreStatusEnum.APPROVED.getCode(), "审批通过");
log.info("门店上架审批通过:listingId={}, 门店={}, 场景={}", log.info("门店上架审批通过:listingId={}, 门店={}, 场景={}",
listing.getListingId(), listing.getStoreName(), listing.getBusinessScene()); listing.getListingId(), listing.getStoreName(), listing.getBusinessScene());
// 终审通过后发布对账规则(best-effort,失败仅告警不回滚审批)
publishReconciliationRule(listing);
storePartnerMerchantSyncer.syncPartnerMerchant( storePartnerMerchantSyncer.syncPartnerMerchant(
listing.getMerchantNo(), listing.getBusinessSceneCode(), CurrentUserHolder.getName()); listing.getMerchantNo(), listing.getBusinessSceneCode(), CurrentUserHolder.getName());
// TODO 接入企业微信通知发起人 // TODO 接入企业微信通知发起人
} }
} }
/**
* 发布对账规则:按 listingId 查明细,组装后交由 ReconciliationRulePublishService 调用 D+ 接口。
*
* <p>明细查询显式过滤 is_deleted = 0,避免把已软删除的品类行带入规则。</p>
*/
private void publishReconciliationRule(StoreListing listing) {
List<StoreListingItem> items = storeListingItemMapper.selectList(
new LambdaQueryWrapper<StoreListingItem>()
.eq(StoreListingItem::getListingId, listing.getListingId())
.eq(StoreListingItem::getIsDeleted, 0));
String operator = CurrentUserHolder.getName();
boolean published = reconciliationRulePublishService.publish(listing, items, operator);
if (!published) {
log.warn("门店上架对账规则发布失败,需人工补发, listingId={}, merchantNo={}, businessScene={}",
listing.getListingId(), listing.getMerchantNo(), listing.getBusinessScene());
}
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void reject(StoreApprovalActionDTO dto) { public void reject(StoreApprovalActionDTO dto) {
......
package com.youjia.store.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youjia.store.entity.StoreListing;
import com.youjia.store.entity.StoreListingItem;
import com.youjia.store.mapper.StoreListingItemMapper;
import com.youjia.store.mapper.StoreListingMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.util.List;
/**
* 对账规则发布集成测试:<b>按 listingId 查库取真实数据</b>,触发向 D+(嘀加)发布对账规则。
*
* <p><b>不使用 mock 数据</b>:门店上架主表与明细表均从 dev 库读取真实记录,
* 便于按真实数据核对发布报文。</p>
*
* <p>用法:修改下面的 {@link #LISTING_ID} 为目标门店上架记录ID 后执行即可。</p>
*
* <p><b>⚠️ 本测试会真实调用 D+ 接口并写入对账规则表,请在测试环境执行,勿在生产库使用。</b></p>
*
* <p>数据源走 Nacos(见 {@code application-dev.yml}),需保证 dev 配置可用。</p>
*/
@Slf4j
@DisplayName("对账规则发布(真实数据)")
@SpringBootTest
@ActiveProfiles("dev")
class ReconciliationRulePublishRealDataTest {
/** 待发布的门店上架记录ID,可按需修改 */
private static final Long LISTING_ID = 26L;
/** 操作人,写入对账规则的 updatedBy */
private static final String OPERATOR = "zhoulm";
@Autowired
private StoreListingMapper storeListingMapper;
@Autowired
private StoreListingItemMapper storeListingItemMapper;
@Autowired
private ReconciliationRulePublishService reconciliationRulePublishService;
/**
* 查询门店上架主表。
*/
private StoreListing loadListing(Long listingId) {
StoreListing listing = storeListingMapper.selectOne(new LambdaQueryWrapper<StoreListing>()
.eq(StoreListing::getListingId, listingId)
.eq(StoreListing::getIsDeleted, 0));
if (listing == null) {
throw new IllegalStateException("门店上架记录不存在,listingId=" + listingId);
}
return listing;
}
/**
* 查询门店上架明细(过滤逻辑删除)。
*/
private List<StoreListingItem> loadItems(Long listingId) {
return storeListingItemMapper.selectList(new LambdaQueryWrapper<StoreListingItem>()
.eq(StoreListingItem::getListingId, listingId)
.eq(StoreListingItem::getIsDeleted, 0));
}
@Test
@DisplayName("按 listingId 查库并发布对账规则")
void publishByListingId() {
StoreListing listing = loadListing(LISTING_ID);
List<StoreListingItem> items = loadItems(LISTING_ID);
log.info("门店上架信息: listingId={}, merchantNo={}, storeName={}, businessSceneCode={}, 明细行数={}",
listing.getListingId(), listing.getMerchantNo(), listing.getStoreName(),
listing.getBusinessSceneCode(), items.size());
for (StoreListingItem item : items) {
log.info("明细: itemId={}, 品类={}({}), 市场费1={}/{}/{}, 市场费2={}/{}/{}, "
+ "阳光1={}/{}/{}(线上积分={}), 阳光2={}/{}/{}(线上积分={})",
item.getItemId(), item.getProductCategory(), item.getProductCategoryId(),
item.getMarketFee1Value(), item.getMarketFee1CalcWay(), item.getMarketFee1CalcMode(),
item.getMarketFee2Value(), item.getMarketFee2CalcWay(), item.getMarketFee2CalcMode(),
item.getSunshine1Value(), item.getSunshine1CalcWay(), item.getSunshine1CalcMode(),
item.getSunshine1OnlinePoint(),
item.getSunshine2Value(), item.getSunshine2CalcWay(), item.getSunshine2CalcMode(),
item.getSunshine2OnlinePoint());
}
boolean published = reconciliationRulePublishService.publish(listing, items, OPERATOR);
log.info("对账规则发布结果: published={}, listingId={}, merchantNo={}, businessSceneCode={}",
published, listing.getListingId(), listing.getMerchantNo(), listing.getBusinessSceneCode());
}
@Test
@DisplayName("门店上架记录不存在时抛出明确异常")
void publishWithNonExistListing() {
Long notExistId = -999999L;
StoreListing listing = storeListingMapper.selectOne(new LambdaQueryWrapper<StoreListing>()
.eq(StoreListing::getListingId, notExistId)
.eq(StoreListing::getIsDeleted, 0));
if (listing != null) {
throw new IllegalStateException("测试数据异常:listingId=" + notExistId + " 竟然存在");
}
List<StoreListingItem> items = loadItems(notExistId);
if (!items.isEmpty()) {
throw new IllegalStateException("测试数据异常:listingId=" + notExistId + " 竟然有明细");
}
log.info("确认 listingId={} 不存在,测试前置条件成立", notExistId);
}
}
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!