From 0587b053431a471af289adb14f01df252d86e716 Mon Sep 17 00:00:00 2001 From: maolei Date: Thu, 15 May 2025 16:16:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(uapbd):=20=E9=87=8D=E6=9E=84=20QuerySy?= =?UTF-8?q?nc=20=E6=9C=8D=E5=8A=A1=20-=20=E7=AE=80=E5=8C=96=E5=85=A5?= =?UTF-8?q?=E5=8F=82=E5=92=8C=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=84=20-=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=BA=86=E4=B8=8D=E5=BF=85=E8=A6=81=E7=9A=84?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=92=8C=E6=B3=A8=E9=87=8A=20-=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BA=86=E5=8F=82=E6=95=B0=E5=A4=84=E7=90=86=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=94=AF=E6=8C=81=20ts=20=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=20-=E9=87=8D=E6=9E=84=E4=BA=86=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=96=B9=E6=B3=95=EF=BC=8C=E6=8F=90=E9=AB=98=E4=BA=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=A4=8D=E7=94=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../public/nccloud/api/uapbd/QuerySync.java | 237 ++++++++---------- 1 file changed, 109 insertions(+), 128 deletions(-) diff --git a/uapbd/src/public/nccloud/api/uapbd/QuerySync.java b/uapbd/src/public/nccloud/api/uapbd/QuerySync.java index e8bdec0..6f7ca70 100644 --- a/uapbd/src/public/nccloud/api/uapbd/QuerySync.java +++ b/uapbd/src/public/nccloud/api/uapbd/QuerySync.java @@ -1,18 +1,12 @@ package nccloud.api.uapbd; -import com.alibaba.fastjson.JSONObject; +// import com.alibaba.fastjson.JSONObject; // 可能不再需要,取决于IJsonForAPI的实现和ResultMessageUtil import nc.bs.dao.BaseDAO; import nc.bs.dao.DAOException; -import nc.jdbc.framework.processor.ColumnListProcessor; import nc.jdbc.framework.processor.MapListProcessor; -import nccloud.api.rest.utils.IJsonForAPI; -import nccloud.api.rest.utils.JsonFactoryForAPI; -import nccloud.api.rest.utils.OpenApiPageInfo; +import nc.vo.pub.lang.UFDate; import nccloud.api.rest.utils.ResultMessageUtil; -import nccloud.api.rest.utils.vo.ApiQueryParam; -import nccloud.api.rest.utils.vo.ApiUfinterface; -import nccloud.api.uapbd.common.utils.OpenApiPagenationUtils; import nccloud.ws.rest.resource.AbstractNCCRestResource; import org.json.JSONString; @@ -20,9 +14,11 @@ import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.StringJoiner; +// import java.util.StringJoiner; // 不再需要 @Path("/uapbd/querySync") @@ -30,97 +26,10 @@ public class QuerySync extends AbstractNCCRestResource { public static final BaseDAO BASE_DAO = new BaseDAO(); - @Override - public String getModule() { - return "uapbd"; - } - - private JSONString baseQuery(ApiUfinterface apiUfinterface, String viewName, String pkColumnName) throws DAOException { - Map data = apiUfinterface.getData().getParamdata(); - // 处理特殊字段 - processExtraParams(data); - JSONObject pageInfo = (JSONObject) JSONObject.toJSON(apiUfinterface.getPageInfo()); - data.remove("type"); // 移除类型参数,因为它仅用于路由,向下传递会影响查询 - - if (pageInfo == null) { - pageInfo = new JSONObject(); - pageInfo.put("pageIndex", "0"); - pageInfo.put("pageSize", "10"); - }// 默认分页 - - // 获取额外条件 - String extraCondition = null; - if (data.containsKey("extraCondition")) { - extraCondition = (String) data.get("extraCondition"); - data.remove("extraCondition"); // 移除额外条件参数,防止影响查询 - } - - String condition = QuerySyncSqlUtils.buildUniversalCondition(data); - - // 如果存在额外条件,拼接到条件后面 - if (extraCondition != null && !extraCondition.isEmpty()) { - condition = condition + " AND " + extraCondition; - } - - String countSql = "SELECT " + pkColumnName + " FROM " + viewName + " WHERE " + condition; - @SuppressWarnings("unchecked") - List allPks = (List) BASE_DAO.executeQuery(countSql, new ColumnListProcessor()); - OpenApiPageInfo openApiPageInfo = new OpenApiPageInfo(); - String[] currPks = OpenApiPagenationUtils.getCurrentPagePksAndPageInfo(allPks, pageInfo, openApiPageInfo); - - if (currPks == null || currPks.length == 0) { - return ResultMessageUtil.toJSONByPage(new JSONObject[0], openApiPageInfo, false); - } - StringJoiner stringJoiner = new StringJoiner(","); - for (String pk : currPks) { - stringJoiner.add("'" + pk + "'"); - } - String pksCondition = "(" + stringJoiner + ")"; - - String dataSql = "SELECT * FROM " + viewName + " WHERE " + pkColumnName + " IN " + pksCondition; - @SuppressWarnings("unchecked") - List> rows = (List>) BASE_DAO.executeQuery(dataSql, new MapListProcessor()); - - return ResultMessageUtil.toJSONByPage(rows, openApiPageInfo, false); - } - - - @POST - @Path("query") - @Consumes({"application/json"}) - @Produces({"application/json"}) - public JSONString query(JSONString json) { - IJsonForAPI iJsonForAPI = JsonFactoryForAPI.create(); - ApiQueryParam apiQueryParam = iJsonForAPI.fromJson(json.toJSONString(), ApiQueryParam.class); - // 从中抓取接口枚举类型 - ApiUfinterface ufinterface = apiQueryParam.getUfinterface(); - if (ufinterface.getData().getParamdata().containsKey("type")) { - String type = (String) ufinterface.getData().getParamdata().get("type"); - try { - return switch (type) { - case "queryStordoc" -> queryStordoc(ufinterface); - case "queryDept" -> queryDept(ufinterface); - case "queryCustomer" -> queryCustomer(ufinterface); - case "querySupplier" -> querySupplier(ufinterface); - case "queryMaterial" -> queryMaterial(ufinterface); - case "queryPsndoc" -> queryPsndoc(ufinterface); - case "queryBom" -> queryBom(ufinterface); - case "queryMaterialClass" -> queryMaterialClass(ufinterface); - default -> ResultMessageUtil.exceptionToJSON(new Exception("不支持的查询类型: " + type)); - }; - } catch (Exception e) { - // 捕获并返回异常信息 - return ResultMessageUtil.exceptionToJSON(e); - } - } else { - return ResultMessageUtil.exceptionToJSON(new Exception("缺失接口类型,请检查参数")); - } - } - /** * 处理类似TS的特殊字段 */ - private static void processExtraParams(Map data) { + private static void processTsParams(Map data) { // 处理ts参数 if (data.containsKey("ts")) { Object tsObj = data.get("ts"); @@ -129,14 +38,7 @@ public class QuerySync extends AbstractNCCRestResource { if (tsObj instanceof String) { String ts = (String) tsObj; if (!ts.contains(",")) { // 单个时间戳 - // 从时间戳中提取日期部分,构建当天零点到该时间的范围 - String dayStart = ts.split(" ")[0] + " 00:00:00"; - tsCondition = "ts >= '" + dayStart + "' AND ts <= '" + ts + "'"; - } else { // 已经是时间范围,格式如:startTime,endTime - String[] timeRange = ts.split(","); - if (timeRange.length == 2) { - tsCondition = "ts >= '" + timeRange[0] + "' AND ts <= '" + timeRange[1] + "'"; - } + tsCondition = "ts >= '" + ts + "' AND ts <= '" + new UFDate(new Date()) + "'"; } } @@ -158,51 +60,130 @@ public class QuerySync extends AbstractNCCRestResource { } } - private JSONString queryCustomer(ApiUfinterface ufinterface) throws DAOException { - return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_CUSTOMER", "pk_customer"); + @Override + public String getModule() { + return "uapbd"; } - private JSONString queryDept(ApiUfinterface apiUfinterface) throws DAOException { - return baseQuery(apiUfinterface, "V_UAPBD_QUERYSYNC_DEPT", "pk_defdoc"); + private JSONString baseQuery(Map originalData, String viewName) throws DAOException { + // 创建参数副本进行操作,避免修改传入的原始Map + Map queryParams = new HashMap<>(originalData); + + // 处理ts + processTsParams(queryParams); // queryParams 会被修改 + queryParams.remove("type"); // 移除类型参数,因为它仅用于路由 + + // 获取额外条件 + String extraCondition = null; + if (queryParams.containsKey("extraCondition")) { + extraCondition = (String) queryParams.get("extraCondition"); + queryParams.remove("extraCondition"); // 移除额外条件参数,防止影响 buildUniversalCondition + } + + String baseGeneratedCondition = QuerySyncSqlUtils.buildUniversalCondition(queryParams); + + String finalCondition = ""; + if (baseGeneratedCondition != null && !baseGeneratedCondition.trim().isEmpty()) { + finalCondition = baseGeneratedCondition.trim(); + } + + if (extraCondition != null && !extraCondition.trim().isEmpty()) { + if (finalCondition.isEmpty()) { + finalCondition = extraCondition.trim(); + } else { + finalCondition = finalCondition + " AND " + extraCondition.trim(); + } + } + + String dataSql; + if (finalCondition.isEmpty()) { + dataSql = "SELECT * FROM " + viewName; + } else { + dataSql = "SELECT * FROM " + viewName + " WHERE " + finalCondition; + } + + @SuppressWarnings("unchecked") + List> rows = (List>) BASE_DAO.executeQuery(dataSql, new MapListProcessor()); + + return ResultMessageUtil.toJSON(rows); } + @POST + @Path("query") + @Consumes({"application/json"}) + @Produces({"application/json"}) + public JSONString query(Map data) { // 直接接收Map作为参数 + // IJsonForAPI iJsonForAPI = JsonFactoryForAPI.create(); // 不再需要解析复杂JSON + // ApiQueryParam apiQueryParam = iJsonForAPI.fromJson(json.toJSONString(), ApiQueryParam.class); //移除 + // ApiUfinterface ufinterface = apiQueryParam.getUfinterface(); //移除 - private JSONString queryStordoc(ApiUfinterface apiUfinterface) throws DAOException { - return baseQuery(apiUfinterface, "V_UAPBD_QUERYSYNC_STORDOC", "pk_stordoc"); + if (data.containsKey("type")) { // 直接从data Map获取type + String type = (String) data.get("type"); + try { + // 将data Map传递给具体的查询方法 + return switch (type) { + case "queryStordoc" -> queryStordoc(data); + case "queryDept" -> queryDept(data); + case "queryCustomer" -> queryCustomer(data); + case "querySupplier" -> querySupplier(data); + case "queryMaterial" -> queryMaterial(data); + case "queryPsndoc" -> queryPsndoc(data); +// case "queryBom" -> queryBom(data); + case "queryMaterialClass" -> queryMaterialClass(data); + default -> ResultMessageUtil.exceptionToJSON(new Exception("不支持的查询类型: " + type)); + }; + } catch (Exception e) { + // 捕获并返回异常信息 + return ResultMessageUtil.exceptionToJSON(e); + } + } else { + return ResultMessageUtil.exceptionToJSON(new Exception("缺失接口类型,请检查参数")); + } } - private JSONString querySupplier(ApiUfinterface ufinterface) throws DAOException { - return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_SUPPLIER", "pk_supplier"); + /** + * 查询BOM维护信息 + */ + private JSONString queryBom(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_BOM"); + } + + private JSONString queryCustomer(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_CUSTOMER"); + } + + private JSONString queryDept(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_DEPT"); } /** * 查询物料信息 */ - private JSONString queryMaterial(ApiUfinterface ufinterface) throws DAOException { - Map data = ufinterface.getData().getParamdata(); - return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_MATERIAL", "pk_material"); - } - - - /** - * 查询BOM维护信息 - */ - private JSONString queryBom(ApiUfinterface ufinterface) throws DAOException { - return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_BOM", "cpickmid"); + private JSONString queryMaterial(Map data) throws DAOException { + // Map data = ufinterface.getData().getParamdata(); // 此行不再需要 + return baseQuery(data, "V_UAPBD_QUERYSYNC_MATERIAL"); } /** * 查询物料分类 */ - private JSONString queryMaterialClass(ApiUfinterface ufinterface) throws DAOException { - return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_MATERIALCLASS", "pk_marbasclass"); + private JSONString queryMaterialClass(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_MATERIALCLASS"); } /** * 查询人员信息 */ - private JSONString queryPsndoc(ApiUfinterface ufinterface) throws DAOException { - return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_PSNDOC", "pk_psndoc"); + private JSONString queryPsndoc(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_PSNDOC"); + } + + private JSONString queryStordoc(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_STORDOC"); + } + + private JSONString querySupplier(Map data) throws DAOException { + return baseQuery(data, "V_UAPBD_QUERYSYNC_SUPPLIER"); } } \ No newline at end of file