查询委外加工入库接口

This commit is contained in:
张明 2025-05-12 11:17:19 +08:00
parent 6272269970
commit bcf3969eb9
1 changed files with 197 additions and 0 deletions

View File

@ -0,0 +1,197 @@
package nccloud.openapi.ic.subcontractReceipt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import nc.bs.dao.BaseDAO;
import nc.bs.logging.Log;
import nc.jdbc.framework.SQLParameter;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nccloud.api.rest.utils.OpenApiPageInfo;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
import org.json.JSONString;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nccloud.api.rest.utils.ApiResourceParamUtils;
import nc.impl.pubapp.pattern.database.SqlBuilderUtil;
import nc.vo.ic.m47.entity.SubcontInHeadVO;
@Path("ic/subcontractReceipt")
public class SubcontractReceiptResource extends AbstractNCCRestResource {
private Log log = Log.getInstance("hlcsalog");
private volatile BaseDAO baseDAO = null;
/**
* 委外加工入库查询校验必填参数并按条件查询数据库
* 第一次查询,为空查询,返回分页条件对应的数据,第一次查询返回code,name,pk
* 第二次查询可以按照主键查询 也可以按照code查询
* 1. 第一次查询 无限制条件 根据分页条件查询
* 2. 第二次查询 写一个视图
*
* @param json 请求JSON
* @return 查询结果
*/
@POST
@Path("querySubcontractReceipt")
@Consumes({"application/json"})
@Produces({"application/json"})
public JSONString querySubcontractReceipt(JSONString json) {
try {
// 解析JSON请求体
JSONObject jObject = JSON.parseObject(json.toJSONString());
if (jObject == null) {
return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
}
JSONObject bject = jObject.getJSONObject("ufinterface");
if (bject == null) {
return ResultMessageUtil.exceptionToJSON(new NullPointerException("ufinterface:null"));
}
// 处理分页参数
JSONObject pageInfo = bject.getJSONObject("pageInfo");
OpenApiPageInfo openApiPageInfo = new OpenApiPageInfo();
int pageIndex = 1, pageSize = 20;
if (pageInfo != null) {
pageIndex = pageInfo.getIntValue("pageIndex") > 0 ? pageInfo.getIntValue("pageIndex") : 1;
pageSize = pageInfo.getIntValue("pageSize") > 0 ? pageInfo.getIntValue("pageSize") : 20;
openApiPageInfo.setPageIndex(pageIndex);
openApiPageInfo.setPageSize(pageSize);
}
// 提取查询参数
Map<String, Object> param = extractAndValidateParams(jObject);
// 使用ApiResourceParamUtils生成查询条件
ApiResourceParamUtils apiUtils = new ApiResourceParamUtils();
String condition = apiUtils.parseParmToSql(new SubcontInHeadVO(), param, ApiResourceParamUtils.BYCODE);
if (condition.endsWith("and ")) {
condition = condition.substring(0, condition.length() - 4);
}
if (condition.trim().isEmpty()) {
condition = "1=1";
}
// 查询所有符合条件的主键
if (baseDAO == null) {
baseDAO = new BaseDAO();
}
String sql = "SELECT cgeneralhid FROM ic_subcontin_h WHERE " + condition;
List<String> allPks = (List<String>) baseDAO.executeQuery(sql,
new SQLParameter(),
new ColumnListProcessor());
// 处理分页
int total = allPks.size();
if (total == 0) {
openApiPageInfo.setTotal(0);
return ResultMessageUtil.toJSONByPage(null, openApiPageInfo, false);
}
// 计算当前页的记录
int fromIndex = (pageIndex - 1) * pageSize;
int toIndex = Math.min(fromIndex + pageSize, total);
if (fromIndex >= total) {
openApiPageInfo.setTotal(total);
return ResultMessageUtil.toJSONByPage(null, openApiPageInfo, false);
}
List<String> currPks = allPks.subList(fromIndex, toIndex);
// 使用SqlBuilderUtil生成IN子句处理批量查询
List<Map<String, Object>> resultList = new ArrayList<>();
if (!currPks.isEmpty()) {
SqlBuilderUtil sqlBuilderUtil = new SqlBuilderUtil();
String wherepart = sqlBuilderUtil.buildSQL("cgeneralhid", currPks.toArray(new String[0]), null);
String detailSql = "SELECT * FROM ic_subcontin_h WHERE " + wherepart;
resultList = (List<Map<String, Object>>) baseDAO.executeQuery(detailSql,
new SQLParameter(),
new MapListProcessor());
}
// 返回结果
openApiPageInfo.setTotal(total);
return ResultMessageUtil.toJSONByPage(resultList, openApiPageInfo, false);
} catch (Exception e) {
// 统一异常处理
return ResultMessageUtil.exceptionToJSON(new BusinessException("查询委外加工入库单据失败: " + e.getMessage(), e));
}
}
/**
* 提取并验证请求参数
*
* @param jObject JSON请求对象
* @return 参数Map
* @throws BusinessException 参数校验失败时抛出异常
*/
private Map<String, Object> extractAndValidateParams(JSONObject jObject) throws BusinessException {
Map<String, Object> param = new HashMap<>();
jObject = (JSONObject) jObject.get("ufinterface");
// 获取参数
String pk_group = jObject.getString("pk_group");
String name = jObject.getString("name");
String code = jObject.getString("code");
String createdate = jObject.getString("createdate");
String ncindustry = jObject.getString("ncindustry");
String pk_currtype = jObject.getString("pk_currtype");
String pk_exratescheme = jObject.getString("pk_exratescheme");
String vbillcode = jObject.getString("vbillcode");
String pk_org = jObject.getString("pk_org");
String dbilldate = jObject.getString("dbilldate");
// 必填字段校验
if (pk_group == null || pk_group.trim().isEmpty()) {
throw new BusinessException("集团主键(pk_group)为必填项");
}
if (name == null || name.trim().isEmpty()) {
throw new BusinessException("名称(name)为必填项");
}
if (code == null || code.trim().isEmpty()) {
throw new BusinessException("编码(code)为必填项");
}
if (createdate == null || createdate.trim().isEmpty()) {
throw new BusinessException("成立时间(createdate)为必填项");
}
if (ncindustry == null || ncindustry.trim().isEmpty()) {
throw new BusinessException("所属UAP行业(ncindustry)为必填项");
}
if (pk_currtype == null || pk_currtype.trim().isEmpty()) {
throw new BusinessException("本位币(pk_currtype)为必填项");
}
if (pk_exratescheme == null || pk_exratescheme.trim().isEmpty()) {
throw new BusinessException("外币汇率方案(pk_exratescheme)为必填项");
}
// 添加到参数Map
param.put("pk_group", pk_group);
param.put("name", name);
param.put("code", code);
param.put("createdate", createdate);
param.put("ncindustry", ncindustry);
param.put("pk_currtype", pk_currtype);
param.put("pk_exratescheme", pk_exratescheme);
if (vbillcode != null) param.put("vbillcode", vbillcode);
if (pk_org != null) param.put("pk_org", pk_org);
if (dbilldate != null) param.put("dbilldate", dbilldate);
return param;
}
@Override
public String getModule() {
return "ic";
}
}