refactor(uapbd): 重构 QuerySync 服务
- 简化入参和返回结构 - 移除了不必要的导入和注释 - 优化了参数处理逻辑,支持 ts 特殊字段 -重构了查询方法,提高了代码复用性
This commit is contained in:
parent
9f5e7703ea
commit
0587b05343
|
@ -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<String, Object> 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<String> allPks = (List<String>) 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<Map<String, Object>> rows = (List<Map<String, Object>>) 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<String, Object> data) {
|
||||
private static void processTsParams(Map<String, Object> 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<String, Object> originalData, String viewName) throws DAOException {
|
||||
// 创建参数副本进行操作,避免修改传入的原始Map
|
||||
Map<String, Object> 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<Map<String, Object>> rows = (List<Map<String, Object>>) BASE_DAO.executeQuery(dataSql, new MapListProcessor());
|
||||
|
||||
return ResultMessageUtil.toJSON(rows);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("query")
|
||||
@Consumes({"application/json"})
|
||||
@Produces({"application/json"})
|
||||
public JSONString query(Map<String, Object> 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<String, Object> data) throws DAOException {
|
||||
return baseQuery(data, "V_UAPBD_QUERYSYNC_BOM");
|
||||
}
|
||||
|
||||
private JSONString queryCustomer(Map<String, Object> data) throws DAOException {
|
||||
return baseQuery(data, "V_UAPBD_QUERYSYNC_CUSTOMER");
|
||||
}
|
||||
|
||||
private JSONString queryDept(Map<String, Object> data) throws DAOException {
|
||||
return baseQuery(data, "V_UAPBD_QUERYSYNC_DEPT");
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询物料信息
|
||||
*/
|
||||
private JSONString queryMaterial(ApiUfinterface ufinterface) throws DAOException {
|
||||
Map<String, Object> 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<String, Object> data) throws DAOException {
|
||||
// Map<String, Object> 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<String, Object> 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<String, Object> data) throws DAOException {
|
||||
return baseQuery(data, "V_UAPBD_QUERYSYNC_PSNDOC");
|
||||
}
|
||||
|
||||
private JSONString queryStordoc(Map<String, Object> data) throws DAOException {
|
||||
return baseQuery(data, "V_UAPBD_QUERYSYNC_STORDOC");
|
||||
}
|
||||
|
||||
private JSONString querySupplier(Map<String, Object> data) throws DAOException {
|
||||
return baseQuery(data, "V_UAPBD_QUERYSYNC_SUPPLIER");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue