套餐启用增加生效时间
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package com.ruoyi.price.search;
|
||||
|
||||
import java.util.UUID;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.http.HttpHelper;
|
||||
import com.ruoyi.common.utils.sign.Md5Utils;
|
||||
import com.ruoyi.price.domain.vo.TmsPriceBillRequest;
|
||||
import com.ruoyi.price.domain.vo.TmsPriceQuoteRequest;
|
||||
@@ -14,7 +18,11 @@ public final class EsBizLogSupport
|
||||
public static final String STATUS_SUCCESS = "SUCCESS";
|
||||
public static final String STATUS_FAIL = "FAIL";
|
||||
|
||||
/** Request attribute: fail already written to ES in this request. */
|
||||
public static final String ATTR_FAIL_LOGGED = "bms.es.fail.logged";
|
||||
|
||||
private static final int ERROR_MAX = 2000;
|
||||
private static final int RAW_BODY_MAX = 8000;
|
||||
|
||||
private EsBizLogSupport()
|
||||
{
|
||||
@@ -26,6 +34,10 @@ public final class EsBizLogSupport
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (request instanceof CharSequence)
|
||||
{
|
||||
return cut(String.valueOf(request), RAW_BODY_MAX);
|
||||
}
|
||||
JSONObject obj = JSON.parseObject(JSON.toJSONString(request));
|
||||
if (obj == null)
|
||||
{
|
||||
@@ -64,15 +76,20 @@ public final class EsBizLogSupport
|
||||
}
|
||||
|
||||
public static String cut(String msg)
|
||||
{
|
||||
return cut(msg, ERROR_MAX);
|
||||
}
|
||||
|
||||
public static String cut(String msg, int max)
|
||||
{
|
||||
if (msg == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String text = msg.trim();
|
||||
if (text.length() > ERROR_MAX)
|
||||
if (text.length() > max)
|
||||
{
|
||||
return text.substring(0, ERROR_MAX);
|
||||
return text.substring(0, max);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
@@ -104,6 +121,39 @@ public final class EsBizLogSupport
|
||||
return "FAIL-" + Md5Utils.hash(raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Common entry for quote/bill API failures (validation / JSON parse / business / unexpected).
|
||||
* Dedups with {@link #ATTR_FAIL_LOGGED} so controller/service/advice can all call safely.
|
||||
*/
|
||||
public static void saveApiFail(IBillSearchStore store, HttpServletRequest httpRequest, Object boundTarget,
|
||||
Throwable e)
|
||||
{
|
||||
saveApiFail(store, httpRequest, boundTarget, errorText(e));
|
||||
}
|
||||
|
||||
public static void saveApiFail(IBillSearchStore store, HttpServletRequest httpRequest, Object boundTarget,
|
||||
String errorMessage)
|
||||
{
|
||||
if (store == null || isFailLogged(httpRequest))
|
||||
{
|
||||
return;
|
||||
}
|
||||
String rawBody = readRawBody(httpRequest);
|
||||
String logType = resolveLogType(httpRequest, boundTarget);
|
||||
if (StringUtils.isEmpty(logType))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Object payload = boundTarget != null ? boundTarget : rawBody;
|
||||
String bizId = resolveFailBizId(logType, boundTarget, rawBody);
|
||||
// saveFail also marks ATTR_FAIL_LOGGED
|
||||
saveFail(store, logType, bizId, payload, errorMessage);
|
||||
if (!isFailLogged(httpRequest))
|
||||
{
|
||||
markFailLogged(httpRequest);
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveFail(IBillSearchStore store, String logType, String bizId, Object request, Throwable e)
|
||||
{
|
||||
saveFail(store, logType, bizId, request, errorText(e));
|
||||
@@ -115,8 +165,157 @@ public final class EsBizLogSupport
|
||||
{
|
||||
return;
|
||||
}
|
||||
HttpServletRequest httpRequest = currentRequest();
|
||||
if (isFailLogged(httpRequest))
|
||||
{
|
||||
return;
|
||||
}
|
||||
String msg = cut(errorMessage);
|
||||
store.save(logType, bizId, requestJson(request), failResponseJson(msg), STATUS_FAIL, msg);
|
||||
markFailLogged(httpRequest);
|
||||
}
|
||||
|
||||
public static String resolveLogType(HttpServletRequest httpRequest, Object boundTarget)
|
||||
{
|
||||
if (boundTarget instanceof TmsPriceBillRequest)
|
||||
{
|
||||
return EsLogType.BILL;
|
||||
}
|
||||
if (boundTarget instanceof TmsPriceQuoteRequest)
|
||||
{
|
||||
return EsLogType.QUOTE;
|
||||
}
|
||||
if (httpRequest == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
String uri = httpRequest.getRequestURI();
|
||||
if (uri == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (uri.endsWith("/bill"))
|
||||
{
|
||||
return EsLogType.BILL;
|
||||
}
|
||||
if (uri.endsWith("/quote"))
|
||||
{
|
||||
return EsLogType.QUOTE;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String resolveFailBizId(String logType, Object boundTarget, String rawBody)
|
||||
{
|
||||
if (EsLogType.BILL.equals(logType))
|
||||
{
|
||||
if (boundTarget instanceof TmsPriceBillRequest)
|
||||
{
|
||||
return billFailBizId((TmsPriceBillRequest) boundTarget);
|
||||
}
|
||||
String orderNo = extractJsonField(rawBody, "orderNo");
|
||||
if (StringUtils.isNotEmpty(orderNo))
|
||||
{
|
||||
return orderNo.trim();
|
||||
}
|
||||
return "FAIL-" + UUID.randomUUID();
|
||||
}
|
||||
if (EsLogType.QUOTE.equals(logType))
|
||||
{
|
||||
if (boundTarget instanceof TmsPriceQuoteRequest)
|
||||
{
|
||||
return quoteFailBizId((TmsPriceQuoteRequest) boundTarget);
|
||||
}
|
||||
String seed = StringUtils.isNotEmpty(rawBody) ? rawBody : UUID.randomUUID().toString();
|
||||
return "FAIL-" + Md5Utils.hash(seed);
|
||||
}
|
||||
return "FAIL-" + UUID.randomUUID();
|
||||
}
|
||||
|
||||
public static boolean isPriceSyncWriteApi(HttpServletRequest httpRequest)
|
||||
{
|
||||
if (httpRequest == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (!"POST".equalsIgnoreCase(httpRequest.getMethod()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
String uri = httpRequest.getRequestURI();
|
||||
if (uri == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return uri.endsWith("/quote") || uri.endsWith("/bill");
|
||||
}
|
||||
|
||||
public static String readRawBody(HttpServletRequest httpRequest)
|
||||
{
|
||||
if (httpRequest == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
String body = HttpHelper.getBodyString(httpRequest);
|
||||
return cut(body, RAW_BODY_MAX);
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isFailLogged(HttpServletRequest httpRequest)
|
||||
{
|
||||
HttpServletRequest req = httpRequest != null ? httpRequest : currentRequest();
|
||||
return req != null && Boolean.TRUE.equals(req.getAttribute(ATTR_FAIL_LOGGED));
|
||||
}
|
||||
|
||||
public static void markFailLogged(HttpServletRequest httpRequest)
|
||||
{
|
||||
HttpServletRequest req = httpRequest != null ? httpRequest : currentRequest();
|
||||
if (req != null)
|
||||
{
|
||||
req.setAttribute(ATTR_FAIL_LOGGED, Boolean.TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpServletRequest currentRequest()
|
||||
{
|
||||
try
|
||||
{
|
||||
ServletRequestAttributes attrs =
|
||||
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return attrs == null ? null : attrs.getRequest();
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractJsonField(String rawBody, String field)
|
||||
{
|
||||
if (StringUtils.isEmpty(rawBody) || StringUtils.isEmpty(field))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
JSONObject obj = JSON.parseObject(rawBody);
|
||||
if (obj == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
Object value = obj.get(field);
|
||||
return value == null ? null : String.valueOf(value);
|
||||
}
|
||||
catch (Exception ignored)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String nvl(Object value)
|
||||
|
||||
Reference in New Issue
Block a user