fix(查询sap库存):新增导出接口

This commit is contained in:
liaohechuan 2025-03-07 10:20:53 +08:00
parent 8f5b7b5329
commit 3affe33ce8
2 changed files with 57 additions and 103 deletions

View File

@ -1,5 +1,6 @@
package com.foreverwin.iep.integration.dto;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.math.BigDecimal;
@ -16,55 +17,66 @@ public class MesReferSapInvResponseDTO {
/**
* 物料编号
*/
@ExcelProperty("物料")
private String matnr;
/**
* 工厂
*/
@ExcelProperty("站点")
private String werks;
/**
* 存储地点
*/
@ExcelProperty("存储地点")
private String lgort;
/**
* 批次编号
*/
@ExcelProperty("批次")
private String charg;
/**
* 特殊库存标识
*/
@ExcelProperty("特殊库存标识")
private String sobkz;
/**
* 销售和分销凭证号
*/
@ExcelProperty("销售订单")
private String vbeln;
/**
* 销售和分销凭证的项目号
*/
@ExcelProperty("销售订单行号")
private String posnr;
/**
* 非限制库存数量
*/
@ExcelProperty("非限制库存数量")
private BigDecimal labst;
/**
* 质检库存
*/
@ExcelProperty("质检库存")
private BigDecimal insme;
/**
* 冻结库存
*/
@ExcelProperty("冻结库存")
private BigDecimal speme;
/**
* 单位
*/
@ExcelProperty("单位")
private String meins;
}

View File

@ -2,9 +2,12 @@ package com.foreverwin.iep.integration.controller;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.foreverwin.iep.integration.constant.Msg;
import com.foreverwin.iep.integration.domain.InvokeResponse;
import com.foreverwin.iep.integration.dto.MesReferSapInvResponseDTO;
@ -12,14 +15,15 @@ import com.foreverwin.iep.integration.handler.def.sap.SapUtil;
import com.foreverwin.iep.integration.utils.JS;
import com.foreverwin.mom.common.core.util.R;
import com.foreverwin.mom.common.log.annotation.SysLog;
import com.foreverwin.mom.wms.util.ExcelCellWidthStyleStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@ -37,104 +41,8 @@ public class MesReferSapInvController {
@ApiOperation(value = "查询sap库存", notes = "查询sap库存")
@SysLog("查询sap库存")
@GetMapping("/api/mesReferSapInv")
public JSONObject mesReferSapInv(@RequestParam(name = "matnr", required = false) String matnr,
@RequestParam(name = "werks", required = false) String werks,
@RequestParam(name = "lgort", required = false) String lgort,
@RequestParam(name = "charg", required = false) String charg,
@RequestParam(name = "sobkz", required = false) String sobkz,
@RequestParam(name = "vbeln", required = false) String vbeln,
@RequestParam(name = "posnr", required = false) String posnr) {
// 封装请求报文
JSONObject jsonObject = new JSONObject();
jsonObject.put("RFC_FUNCTION", "ZMM_MES_GET_STOCK");
JSONObject conTextJsonObject = new JSONObject();
conTextJsonObject.put("EXCHANGE_SYSTEM", "MES");
conTextJsonObject.put("EXCHANGE_ID", StrUtil.uuid());
conTextJsonObject.put("EXCHANGE_DOC", "");
jsonObject.put("REQUEST_CONTEXT", conTextJsonObject);
JSONObject bodyJsonObject = new JSONObject();
JSONObject inputJsonObject = new JSONObject();
JSONArray inputJsonArray = new JSONArray();
inputJsonObject.put("WERKS", werks);
if (StrUtil.isBlank(matnr)) {
inputJsonObject.put("MATNR", "");
} else {
inputJsonObject.put("MATNR", matnr);
}
if (StrUtil.isBlank(lgort)) {
inputJsonObject.put("LGORT", "");
} else {
inputJsonObject.put("LGORT", lgort);
}
if (StrUtil.isBlank(charg)) {
inputJsonObject.put("CHARG", "");
} else {
inputJsonObject.put("CHARG", charg);
}
if (StrUtil.isBlank(sobkz)) {
inputJsonObject.put("SOBKZ", "");
} else {
inputJsonObject.put("SOBKZ", sobkz);
}
if (StrUtil.isBlank(vbeln)) {
inputJsonObject.put("VBELN", "");
} else {
inputJsonObject.put("VBELN", vbeln);
}
if (StrUtil.isBlank(posnr)) {
inputJsonObject.put("POSNR", "");
} else {
inputJsonObject.put("POSNR", posnr);
}
inputJsonArray.add(inputJsonObject);
bodyJsonObject.put("IT_INPUT", inputJsonArray);
jsonObject.put("REQUEST_BODY", bodyJsonObject);
// messageContent内容
JSONObject messageContentJson = SapUtil.appendHead(jsonObject);
List<MesReferSapInvResponseDTO> mesReferSapInvResponseDTOList = new ArrayList<>();
// 调用 SAP 接口
InvokeResponse invokeResponse = SapUtil.sendSap(messageContentJson.toString());
if(!Msg.E.equals(invokeResponse.getResult())){
// 请求处理成功
JSONObject resJsonObject = JS.parseObject(invokeResponse.getMessageContent());
JSONObject responseContextJson = resJsonObject.getJSONObject("RESPONSE_CONTEXT");
if ("200".equals(responseContextJson.getString("RFC_CODE"))) {
JSONObject responseBodyJson = resJsonObject.getJSONObject("RESPONSE_BODY");
if (ObjectUtil.isNotEmpty(responseBodyJson)) {
JSONArray etOutputJsonArray = responseBodyJson.getJSONArray("ET_OUTPUT");
if (CollectionUtils.isNotEmpty(etOutputJsonArray)) {
for (int i = 0; i < etOutputJsonArray.size(); i++) {
MesReferSapInvResponseDTO mesReferSapInvResponseDTO = new MesReferSapInvResponseDTO();
mesReferSapInvResponseDTO.setMatnr(etOutputJsonArray.getJSONObject(i).getString("MATNR"));
mesReferSapInvResponseDTO.setWerks(etOutputJsonArray.getJSONObject(i).getString("WERKS"));
mesReferSapInvResponseDTO.setLgort(etOutputJsonArray.getJSONObject(i).getString("LGORT"));
mesReferSapInvResponseDTO.setCharg(etOutputJsonArray.getJSONObject(i).getString("CHARG"));
mesReferSapInvResponseDTO.setSobkz(etOutputJsonArray.getJSONObject(i).getString("SOBKZ"));
mesReferSapInvResponseDTO.setVbeln(etOutputJsonArray.getJSONObject(i).getString("VBELN"));
mesReferSapInvResponseDTO.setPosnr(etOutputJsonArray.getJSONObject(i).getString("POSNR"));
mesReferSapInvResponseDTO.setLabst(etOutputJsonArray.getJSONObject(i).getBigDecimal("LABST"));
mesReferSapInvResponseDTO.setInsme(etOutputJsonArray.getJSONObject(i).getBigDecimal("INSME"));
mesReferSapInvResponseDTO.setSpeme(etOutputJsonArray.getJSONObject(i).getBigDecimal("SPEME"));
mesReferSapInvResponseDTO.setMeins(etOutputJsonArray.getJSONObject(i).getString("MEINS"));
mesReferSapInvResponseDTOList.add(mesReferSapInvResponseDTO);
}
}
}
}
}
JSONObject resutJsonObject = new JSONObject();
resutJsonObject.put("data", mesReferSapInvResponseDTOList);
return resutJsonObject;
}
@ApiOperation(value = "查询sap库存", notes = "查询sap库存")
@SysLog("查询sap库存")
@GetMapping("/readMesReferSapInv")
public R<List<MesReferSapInvResponseDTO>> readMesReferSapInv(MesReferSapInvResponseDTO mesReferSapInvDTO) {
@PostMapping("/readMesReferSapInv")
public R<IPage<MesReferSapInvResponseDTO>> readMesReferSapInv(Page page, @RequestBody MesReferSapInvResponseDTO mesReferSapInvDTO) {
// 封装请求报文
JSONObject jsonObject = new JSONObject();
jsonObject.put("RFC_FUNCTION", "ZMM_MES_GET_STOCK");
@ -144,6 +52,8 @@ public class MesReferSapInvController {
conTextJsonObject.put("EXCHANGE_DOC", "");
jsonObject.put("REQUEST_CONTEXT", conTextJsonObject);
JSONObject bodyJsonObject = new JSONObject();
bodyJsonObject.put("PAGE", page.getCurrent());
bodyJsonObject.put("SIZE", page.getSize());
JSONObject inputJsonObject = new JSONObject();
JSONArray inputJsonArray = new JSONArray();
inputJsonObject.put("WERKS", mesReferSapInvDTO.getWerks());
@ -186,6 +96,7 @@ public class MesReferSapInvController {
// 调用 SAP 接口
InvokeResponse invokeResponse = SapUtil.sendSap(messageContentJson.toString());
long total = 0;
if(!Msg.E.equals(invokeResponse.getResult())){
// 请求处理成功
JSONObject resJsonObject = JS.parseObject(invokeResponse.getMessageContent());
@ -193,6 +104,7 @@ public class MesReferSapInvController {
if ("200".equals(responseContextJson.getString("RFC_CODE"))) {
JSONObject responseBodyJson = resJsonObject.getJSONObject("RESPONSE_BODY");
if (ObjectUtil.isNotEmpty(responseBodyJson)) {
total = responseBodyJson.getLong("TOTAL");
JSONArray etOutputJsonArray = responseBodyJson.getJSONArray("ET_OUTPUT");
if (CollectionUtils.isNotEmpty(etOutputJsonArray)) {
for (int i = 0; i < etOutputJsonArray.size(); i++) {
@ -221,6 +133,36 @@ public class MesReferSapInvController {
return R.failed("sap返回异常:"+invokeResponse.getMessageContent());
}
return R.ok(mesReferSapInvResponseDTOList);
IPage<MesReferSapInvResponseDTO> mesReferSapInvResponseDTOIPage = new Page<>();
mesReferSapInvResponseDTOIPage.setTotal(total);
mesReferSapInvResponseDTOIPage.setRecords(mesReferSapInvResponseDTOList);
mesReferSapInvResponseDTOIPage.setSize(page.getSize());
mesReferSapInvResponseDTOIPage.setCurrent(page.getCurrent());
return R.ok(mesReferSapInvResponseDTOIPage);
}
@ApiOperation(value = "sap库存导出", notes = "sap库存导出")
@SysLog("sap库存导出")
@ResponseBody
@GetMapping("/exportMesReferSapInv")
public void exportMesReferSapInv(MesReferSapInvResponseDTO mesReferSapInvDTO) throws Exception {
Page page = new Page();
page.setSize(1);
page.setCurrent(1);
// 先查询一次,获取总条数
IPage<MesReferSapInvResponseDTO> mesReferSapInvResponseDTOIPage = this.readMesReferSapInv(page, mesReferSapInvDTO).getData();
page.setSize(mesReferSapInvResponseDTOIPage.getTotal());
List<MesReferSapInvResponseDTO> mesReferSapInvResponseDTOList = this.readMesReferSapInv(page, mesReferSapInvDTO).getData().getRecords();
String fileName = "sap查询库存报表";
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
// 这里需要设置不关闭流
ExcelCellWidthStyleStrategy widthStyleStrategy = new ExcelCellWidthStyleStrategy();
EasyExcel.write(response.getOutputStream(), MesReferSapInvResponseDTO.class).registerWriteHandler(widthStyleStrategy).autoCloseStream(Boolean.FALSE).sheet(fileName)
.doWrite(mesReferSapInvResponseDTOList);
}
}