using HH.WCS.Mobox3.Template.device;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Web.Http;
using System.Xml;
using System.Xml.Linq;
using static HH.WCS.Mobox3.Template.api.ApiHelper;
using static HH.WCS.Mobox3.Template.api.ApiModel;
using static HH.WCS.Mobox3.Template.api.OtherModel;
using static HH.WCS.Mobox3.Template.device.S7Helper;
namespace HH.WCS.Mobox3.Template.api {
///
/// 测试用,如果项目中要和设备对接,前期设备无法测试,用接口模拟
///
[RoutePrefix("api")]
public class TestController : System.Web.Http.ApiController {
//如果好用,请收藏地址,帮忙分享。
public class HEAD {
///
/// 唯一标识
///
public string KEY { get; set; }
///
/// 过账日期
///
public string GZRQ { get; set; }
///
/// 过账人员
///
public string GZRY { get; set; }
///
/// 工厂
///
public string GC { get; set; }
}
public class ITEM1 {
///
/// 序列号
///
public string XLH { get; set; }
}
public class ITEM {
///
/// 物料凭证编号
///
public string WLPZBH { get; set; }
///
/// 物料凭证年度
///
public string WLPZND { get; set; }
///
/// 物料凭证中的项目
///
public string WLPZZDXM { get; set; }
///
/// 物料号
///
public string WLH { get; set; }
///
/// 基本计量单位
///
public string JBJLDW { get; set; }
///
/// 数量
///
public string SL { get; set; }
///
/// 采购凭证号
///
public string CGPZH { get; set; }
///
/// 采购凭证项目编号
///
public string CGPZXMBH { get; set; }
///
/// 库存地点
///
public string KCDD { get; set; }
///
/// 检验批编号
///
public string JYPBH { get; set; }
///
/// 会计凭证输入日期
///
public string KJPZSSRQ { get; set; }
///
/// 会计凭证输入时间
///
public string KJPZSRSJ { get; set; }
///
/// 批次
///
public string PC { get; set; }
///
///
///
public List ITEM1 { get; set; }
}
public class WLPZ {
///
///
///
public HEAD HEAD { get; set; }
///
///
///
public List- ITEM { get; set; }
}
public class ROOT {
///
///
///
public WLPZ WLPZ { get; set; }
}
public class Root {
///
///
///
public ROOT ROOT { get; set; }
}
/*
唯一标识
过账日期
过账人员
工厂
-
物料凭证编号
物料凭证年度
物料凭证中的项目
物料号
基本计量单位
数量
采购凭证号
采购凭证项目编号
库存地点
检验批编号
会计凭证输入日期
会计凭证输入时间
批次
序列号
*/
static void ProcessXmlElements(XmlNode parentNode) {
if (parentNode.HasChildNodes) {
// 用于存储 ITEM 和 ITEM1 元素
XmlNodeList itemNodes = parentNode.SelectNodes("ITEM | ITEM1");
if (itemNodes.Count > 0) {
// 创建一个新的父节点来容纳 ITEM 和 ITEM1 数组
XmlElement arrayElement = parentNode.OwnerDocument.CreateElement(itemNodes[0].Name + "Array");
foreach (XmlNode itemNode in itemNodes) {
// 将 ITEM 和 ITEM1 元素移动到新的数组父节点下
arrayElement.AppendChild(itemNode.CloneNode(true));
parentNode.RemoveChild(itemNode);
}
// 将新的数组父节点添加到原父节点中
parentNode.AppendChild(arrayElement);
}
// 递归处理子节点
foreach (XmlNode childNode in parentNode.ChildNodes) {
if (childNode.NodeType == XmlNodeType.Element) {
ProcessXmlElements(childNode);
}
}
}
}
static void EnsureArrayFormat(JObject json) {
foreach (var property in json.Properties()) {
if (property.Name == "ITEM" || property.Name == "ITEM1") {
if (property.Value is JObject && !(property.Value is JArray)) {
// 若为单个对象,包装成数组
property.Value = new JArray(property.Value);
}
}
if (property.Value is JObject) {
// 递归处理子对象
EnsureArrayFormat((JObject)property.Value);
}
else if (property.Value is JArray) {
// 递归处理数组中的每个元素
foreach (var item in (JArray)property.Value) {
if (item is JObject) {
EnsureArrayFormat((JObject)item);
}
}
}
}
}
[HttpPost]
public IHttpActionResult PostXml([FromBody] XElement xmlRequest) {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlRequest.ToString());
// 将 XmlDocument 转换为 JSON 字符串
JObject jsonObject = JsonConvert.DeserializeObject(JsonConvert.SerializeXmlNode(xmlDoc));
// 处理特定字段,强制转换为数组
EnsureArrayFormat(jsonObject);
// 反序列化为RootWithArray
var data = JsonConvert.DeserializeObject(jsonObject.ToString());
//string json = JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.Indented);
//var data1 = JsonConvert.DeserializeObject(json);
// 处理SAP XML请求逻辑
var responseXml = new XElement("Response",
new XElement("Status", "Success"),
new XElement("Message", "XML processed")
);
var response = new HttpResponseMessage()
{
Content = new StringContent(content: responseXml.ToString(), encoding: Encoding.UTF8, mediaType: "application/xml" // 关键:声明内容类型
)
};
//return response;
//return Request.CreateResponse(HttpStatusCode.OK, responseXml);
return Ok(responseXml);
}
[HttpGet]
public HttpResponseMessage GetXml(string id) {
// 根据ID返回XML数据
var xmlData = $@"
{id}
Sample Data
";
//string xml = JsonConvert.DeserializeXmlNode(json, "root").OuterXml;
var response = new HttpResponseMessage()
{
Content = new StringContent(xmlData.ToString(), Encoding.UTF8, "application/xml")
};
//return Content(HttpStatusCode.OK, xmlData, Configuration.Formatters.XmlFormatter);
return response;
}
[HttpGet]
public HttpResponseMessage GetXml1(string id) {
// json转xml返回
var json = JsonConvert.SerializeObject(new { id = 123, name = "jack" });
string xml = JsonConvert.DeserializeXmlNode(json, "root").OuterXml;
var response = new HttpResponseMessage()
{
Content = new StringContent(xml, Encoding.UTF8, "application/xml")
};
return response;
}
}
}