201 lines
7.9 KiB
Java
201 lines
7.9 KiB
Java
package nccloud.api.uapbd;
|
||
|
||
|
||
import com.alibaba.fastjson.JSONObject;
|
||
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 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;
|
||
|
||
import javax.ws.rs.Consumes;
|
||
import javax.ws.rs.POST;
|
||
import javax.ws.rs.Path;
|
||
import javax.ws.rs.Produces;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.StringJoiner;
|
||
|
||
|
||
@Path("/uapbd/querySync")
|
||
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();
|
||
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("缺失接口类型,请检查参数"));
|
||
}
|
||
}
|
||
|
||
private JSONString queryCustomer(ApiUfinterface ufinterface) throws DAOException {
|
||
return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_CUSTOMER", "pk_customer");
|
||
}
|
||
|
||
private JSONString queryDept(ApiUfinterface apiUfinterface) throws DAOException {
|
||
return baseQuery(apiUfinterface, "V_UAPBD_QUERYSYNC_DEPT", "pk_defdoc");
|
||
}
|
||
|
||
|
||
private JSONString queryStordoc(ApiUfinterface apiUfinterface) throws DAOException {
|
||
return baseQuery(apiUfinterface, "V_UAPBD_QUERYSYNC_STORDOC", "pk_stordoc");
|
||
}
|
||
|
||
private JSONString querySupplier(ApiUfinterface ufinterface) throws DAOException {
|
||
return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_SUPPLIER", "pk_supplier");
|
||
}
|
||
|
||
/**
|
||
* 查询物料信息
|
||
*/
|
||
private JSONString queryMaterial(ApiUfinterface ufinterface) throws DAOException {
|
||
Map<String, Object> data = ufinterface.getData().getParamdata();
|
||
|
||
// 处理ts参数
|
||
if (data.containsKey("ts")) {
|
||
Object tsObj = data.get("ts");
|
||
String tsCondition = null;
|
||
|
||
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] + "'";
|
||
}
|
||
}
|
||
}
|
||
|
||
// 将构建好的条件放入extraCondition
|
||
if (tsCondition != null) {
|
||
// 检查是否已有其他extraCondition
|
||
String existingExtraCondition = data.containsKey("extraCondition") ?
|
||
(String) data.get("extraCondition") : "";
|
||
|
||
if (existingExtraCondition.isEmpty()) {
|
||
data.put("extraCondition", tsCondition);
|
||
} else {
|
||
data.put("extraCondition", existingExtraCondition + " AND " + tsCondition);
|
||
}
|
||
}
|
||
|
||
// 从参数中移除ts,避免它被直接用于查询条件
|
||
data.remove("ts");
|
||
}
|
||
|
||
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 queryMaterialClass(ApiUfinterface ufinterface) throws DAOException {
|
||
return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_MATERIALCLASS", "pk_marbasclass");
|
||
}
|
||
|
||
/**
|
||
* 查询人员信息
|
||
*/
|
||
private JSONString queryPsndoc(ApiUfinterface ufinterface) throws DAOException {
|
||
return baseQuery(ufinterface, "V_UAPBD_QUERYSYNC_PSNDOC", "pk_psndoc");
|
||
}
|
||
|
||
} |