using HH.Redis.ReisModel;
|
using HH.WMS.BLL;
|
using HH.WMS.BLL.Basic;
|
using HH.WMS.BLL.Common;
|
using HH.WMS.BLL.InStock;
|
using HH.WMS.Common;
|
using HH.WMS.Entitys;
|
using HH.WMS.Entitys.Basic;
|
using HH.WMS.Entitys.Common;
|
using HH.WMS.WebApi.Areas.Common.Controllers;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Converters;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Http;
|
using System.Web.Http;
|
using System.Xml;
|
|
namespace HH.WMS.WebApi.Areas.InStock.Controllers
|
{
|
public class XmlUpController : BaseController
|
{
|
#region Xml文件导入
|
/// <summary>
|
/// Xml文件导入
|
/// </summary>
|
/// <param name="entity">审批实体</param>
|
/// <returns></returns>
|
/// <History>[HANHE(XDL)] CREATED BY 2018-11-30</History>
|
[HttpPost]
|
public OperateResult ImportXmlDataString(dynamic entityStr)
|
{
|
string tokenId = entityStr.tokenId.ToString();
|
try
|
{
|
return ValidateToken(tokenId, t =>
|
{
|
//最终实体
|
List<TN_WM_B_UNIQUE_BARCODEEntity> uniqueEntityList = new List<TN_WM_B_UNIQUE_BARCODEEntity>();
|
|
string stream = entityStr.streamStr.ToString();
|
|
var newStr = stream.Replace("[\"", "").Replace("\"]", "").Replace("\",\"", ",");
|
string[] splitXml = newStr.Split(',');
|
//当存在多文件时,使用循环
|
foreach (var splitnewStr in splitXml)
|
{
|
XmlDocument xmldoc = new XmlDocument();
|
xmldoc.LoadXml(splitnewStr.Replace("\\", "").Trim());
|
|
XmlNodeList listNode = xmldoc.SelectSingleNode("DataList").ChildNodes; //获取Product节点下的所有节点列表
|
|
//到Product一级
|
foreach (XmlNode node in listNode)
|
{
|
XmlElement xe = (XmlElement)node;
|
var productName = xe.GetAttribute("productName"); //产品名称
|
var pzwh = xe.GetAttribute("pzwh"); //批准文号
|
|
XmlNodeList xnfChild = xe.ChildNodes;
|
//到Batch一级
|
foreach (XmlNode child in xnfChild)
|
{
|
XmlElement xmlchild = (XmlElement)child;
|
|
List<string> dataCodeList = new List<string>();
|
|
XmlNodeList xnfDataChild = xmlchild.ChildNodes;
|
//到Data一级
|
foreach (XmlNode datachild in xnfDataChild)
|
{
|
XmlElement xmldatachild = (XmlElement)datachild;
|
var dataCode = xmldatachild.GetAttribute("code"); //所有Data
|
dataCodeList.Add(dataCode);
|
}
|
|
var res = BLLCreator.Create<TN_WM_B_UNIQUE_BARCODEBLL>().GetValue(dataCodeList[0]);
|
if (res != null)
|
{
|
return OperateResult.Error("该XML已上传,请选择其他未上传的XML文件!");
|
}
|
|
//创建实体
|
uniqueEntityList = DoCreateEntity(productName, pzwh, xmlchild, dataCodeList, t);
|
}
|
}
|
}
|
|
// OperateResult or = BLLCreator.Create<TN_WM_MONGOBLL>().AddList(uniqueEntityList, "TN_WM_B_UNIQUE_BARCODE");
|
|
//SqlBulkCopy不支持事务,没法trans
|
OperateResult or = BLLCreator.Create<TN_WM_B_UNIQUE_BARCODEBLL>().AddList(uniqueEntityList);
|
|
if (or.Success)//mongo不支持事务,没法trans
|
{
|
or = BLLCreator.Create<TN_WM_MONGOBLL>().AddList(TempList, "MONGO_KEYTYPE_MAP");
|
}
|
return or;
|
|
});
|
}
|
catch (Exception ex)
|
{
|
return OperateResult.Error(ex.Message);
|
}
|
}
|
/// <summary>
|
/// 拼接实体
|
/// </summary>
|
/// <param name="productName">产品名称</param>
|
/// <param name="pzwh">批准文号</param>
|
/// <param name="xmlchild">xml节点</param>
|
/// <param name="dataCodeList">所有Data的List集合</param>
|
/// <param name="userEntity">用户实体</param>
|
/// <returns></returns>
|
private List<TN_WM_B_UNIQUE_BARCODEEntity> DoCreateEntity(string productName, string pzwh, XmlElement xmlchild, List<string> dataCodeList, RedisUserEntity userEntity)
|
{
|
|
var specification = xmlchild.GetAttribute("specification"); //包装规格
|
var batchNo = xmlchild.GetAttribute("batchNo"); //批次编码
|
|
AutoBomItemEntity itemEntity = new AutoBomItemEntity();
|
List<AutoBomItemEntity> itemEnityList = BLLCreator.Create<TN_WMS_ITEMBLL>().GetItemByNameSpec(productName, specification);
|
Log.Info("XML导入获取mongo实体", JsonConvert.SerializeObject(itemEnityList));
|
|
bool noExist = false;
|
if (itemEnityList != null)
|
{
|
if (itemEnityList.Any())
|
{
|
if (itemEnityList.Count == 1)//mongo返回单条
|
{
|
itemEntity = itemEnityList[0];
|
}
|
else
|
{
|
List<TN_WM_INCREASE_INVENTORY_DTLEntity> inDtlEntity = BLLCreator.Create<TN_WM_INCREASE_INVENTORYBLL>().GetDtlByBatch(batchNo);
|
Log.Info("XML导入获取入库单实体", JsonConvert.SerializeObject(inDtlEntity));
|
|
if (inDtlEntity.Count > 0)
|
{
|
noExist = true;
|
}
|
else
|
{
|
if (inDtlEntity.Count == 1)
|
{
|
string itemCode = inDtlEntity[0].CN_S_ITEM_CODE;
|
itemEntity = BLLCreator.Create<TN_WMS_ITEMBLL>().GetItem(itemCode);
|
}
|
else
|
{
|
var entity = inDtlEntity.Where(x => x.CN_S_ITEM_NAME.Contains(productName)).ToList();
|
Log.Info("XML导入匹配产品名称", JsonConvert.SerializeObject(entity));
|
|
if (entity.Count != 1)
|
{
|
Exception ex = new Exception("在入库单明细表中未找到或找到多条生产批次是<" + batchNo + ">的数据!");
|
throw ex;
|
}
|
else
|
{
|
string itemCode = inDtlEntity[0].CN_S_ITEM_CODE;
|
itemEntity = BLLCreator.Create<TN_WMS_ITEMBLL>().GetItem(itemCode);
|
}
|
}
|
}
|
}
|
}
|
else
|
{
|
noExist = true;
|
}
|
|
}
|
else
|
{
|
noExist = true;
|
}
|
if (noExist)//不存在、抛异常
|
{
|
Exception ex = new Exception("未找到物料<" + productName + ">的Mongo实体!");
|
throw ex;
|
}
|
|
List<TN_WM_B_UNIQUE_BARCODEEntity> uniqueEntityList = new List<TN_WM_B_UNIQUE_BARCODEEntity>();
|
|
var tagRatio = xmlchild.GetAttribute("tagRatio");//级别
|
|
int position = Convert.ToInt32(tagRatio.Split(':')[1]);
|
|
if (tagRatio.Split(':').Length == 2)
|
{
|
uniqueEntityList = ForeachData(productName, pzwh, xmlchild, dataCodeList, "", itemEntity, userEntity);
|
}
|
if (tagRatio.Split(':').Length == 3)
|
{
|
string[] tagRatios = tagRatio.Split(':');
|
var foreachCount = Convert.ToInt32(tagRatios[0]) + Convert.ToInt32(tagRatios[1]) + Convert.ToInt32(tagRatios[2]);
|
|
var boci = 0;
|
List<string> foreachList = new List<string>();
|
for (int i = 1; i <= (dataCodeList.Count) / foreachCount; i++)
|
{
|
foreachList = dataCodeList.GetRange(boci * foreachCount, foreachCount);
|
var list = ForeachData(productName, pzwh, xmlchild, foreachList, foreachList[foreachList.Count - 1], itemEntity, userEntity);
|
uniqueEntityList.AddRange(list);
|
boci++;
|
}
|
|
|
}
|
|
return uniqueEntityList;
|
}
|
|
|
List<MONGO_KEYTYPE_MAP> TempList = new List<MONGO_KEYTYPE_MAP>() { };
|
/// <summary>
|
/// 内部循环
|
/// </summary>
|
/// <param name="productName">产品名称</param>
|
/// <param name="pzwh">批准文号</param>
|
/// <param name="xmlchild">xml节点</param>
|
/// <param name="dataCodeList">所有Data的List集合</param>
|
/// <param name="endCode">结束点</param>
|
/// <param name="itemEnity">Item实体</param>
|
/// <param name="userEntity">用户实体</param>
|
/// <returns></returns>
|
private List<TN_WM_B_UNIQUE_BARCODEEntity> ForeachData(string productName, string pzwh, XmlElement xmlchild, List<string> dataCodeList, string endCode, AutoBomItemEntity itemEnity, RedisUserEntity userEntity)
|
{
|
List<TN_WM_B_UNIQUE_BARCODEEntity> uniqueEntityList = new List<TN_WM_B_UNIQUE_BARCODEEntity>();
|
var batchNo = xmlchild.GetAttribute("batchNo"); //批次编码
|
var specification = xmlchild.GetAttribute("specification"); //包装规格
|
var minPackUnit = xmlchild.GetAttribute("minPackUnit"); //
|
var minTagUnit = xmlchild.GetAttribute("minTagUnit"); //
|
var tagPackRatio = xmlchild.GetAttribute("tagPackRatio"); //
|
var produceDate = xmlchild.GetAttribute("produceDate"); //
|
var operatorName = xmlchild.GetAttribute("operator"); //
|
var oprDate = xmlchild.GetAttribute("oprDate"); //
|
var count = xmlchild.GetAttribute("count"); //
|
var countUnit = xmlchild.GetAttribute("countUnit"); //
|
var tagRatio = xmlchild.GetAttribute("tagRatio"); //
|
|
int position = Convert.ToInt32(tagRatio.Split(':')[1]);
|
int xiangPos = 1; //标记箱码位置
|
int foreachIndex = 0; //标记循环位置
|
foreach (var dataCode in dataCodeList)
|
{
|
string[] tagRatios = tagRatio.Split(':');
|
var qty = 0; var unit = "";
|
|
int packingJb = tagRatios.Length;
|
if (foreachIndex != xiangPos * position + (xiangPos - 1))
|
{
|
if (dataCode == endCode)
|
{
|
if (packingJb == 2)// 盒/箱两级的情况下,这个条件就是箱码的包装数量和单位
|
{
|
qty = Convert.ToInt32(tagRatios[1]) / (Convert.ToInt32(tagRatios[0]));
|
unit = qty + "盒/箱";
|
}
|
if (packingJb == 3)// 支/盒/箱三级的情况下,这个条件就是箱码的包装数量和单位
|
{
|
qty = Convert.ToInt32(tagRatios[2]) / (Convert.ToInt32(tagRatios[0]));
|
unit = qty + minPackUnit + "/" + minTagUnit;
|
}
|
|
}
|
TN_WM_B_UNIQUE_BARCODEEntity bar = new TN_WM_B_UNIQUE_BARCODEEntity();
|
|
bar.CN_S_CODE = dataCode;
|
bar.CN_S_CODE_TYPE = "";
|
bar.CN_S_PARENT_CODE = (dataCode == endCode) ? "" : dataCodeList[xiangPos * position + (xiangPos - 1)];
|
bar.CN_S_PRODUCT_NAME = productName;
|
bar.CN_S_PZWH = pzwh;
|
bar.CN_S_BATCH_NO = batchNo;
|
bar.CN_S_SPECIFICATION = specification;
|
bar.CN_S_MIN_PACK_UNIT = minPackUnit;
|
bar.CN_S_MIN_TAG_UNIT = minTagUnit;
|
bar.CN_S_TAG_PACK_RATIO = tagPackRatio;
|
bar.CN_T_PRODUCT_DATE = Convert.ToDateTime(produceDate);
|
bar.CN_S_OPERATOR = operatorName;
|
bar.CN_T_OPR_DATE = Convert.ToDateTime(oprDate);
|
bar.CN_F_QUANTITY = Convert.ToInt32(count);
|
bar.CN_S_QUANTITY_UNIT = countUnit;
|
bar.CN_S_TAG_RATIO = tagRatio;
|
bar.CN_S_CREATOR = userEntity.CN_S_LOGIN;
|
bar.CN_S_CREATOR_BY = userEntity.CN_S_NAME;
|
bar.CN_T_CREATE = DateTime.Now;
|
bar.CN_S_MODIFY = userEntity.CN_S_LOGIN;
|
bar.CN_S_MODIFY_BY = userEntity.CN_S_NAME;
|
bar.CN_T_MODIFY = DateTime.Now;
|
|
bar.CN_S_PACKING_UNIT = unit;
|
bar.CN_F_PACKING_QTY = qty;
|
bar.CN_S_ITEM_CODE = itemEnity.CN_S_ITEM_CODE;
|
bar.CN_S_ITEM_NAME = itemEnity.CN_S_ITEM_NAME;
|
bar.CN_S_MEASURE_UNIT = itemEnity.CN_S_MEASURE_UNIT;
|
bar.CN_S_MODEL = itemEnity.CN_S_MODEL;
|
bar.CN_S_ITEM_STATE = "待检";//Constants.DefaultItemState;
|
bar.CN_S_OWNER = Constants.DefaultOwner;
|
uniqueEntityList.Add(bar);
|
|
}
|
else
|
{
|
if (dataCode != endCode)
|
{
|
if (packingJb == 2)// 盒/箱三级的情况下,这个条件就是箱码的包装数量和单位
|
{
|
qty = Convert.ToInt32(tagRatios[1]) / (Convert.ToInt32(tagRatios[0]));
|
unit = qty + "盒/箱"; //minPackUnit + "/" + minTagUnit;
|
}
|
if (packingJb == 3)// 支/盒/箱三级的情况下,这个条件就是箱码的包装数量和单位
|
{
|
qty = Convert.ToInt32(tagRatios[2]) / (Convert.ToInt32(tagRatios[0]));
|
unit = qty + minPackUnit + "/" + minTagUnit;
|
}
|
}
|
TN_WM_B_UNIQUE_BARCODEEntity bar = new TN_WM_B_UNIQUE_BARCODEEntity();
|
|
bar.CN_S_CODE = dataCode;
|
bar.CN_S_CODE_TYPE = "";
|
bar.CN_S_PARENT_CODE = endCode;
|
bar.CN_S_PRODUCT_NAME = productName;
|
bar.CN_S_PZWH = pzwh;
|
bar.CN_S_BATCH_NO = batchNo;
|
bar.CN_S_SPECIFICATION = specification;
|
bar.CN_S_MIN_PACK_UNIT = minPackUnit;
|
bar.CN_S_MIN_TAG_UNIT = minTagUnit;
|
bar.CN_S_TAG_PACK_RATIO = tagPackRatio;
|
bar.CN_T_PRODUCT_DATE = Convert.ToDateTime(produceDate);
|
bar.CN_S_OPERATOR = operatorName;
|
bar.CN_T_OPR_DATE = Convert.ToDateTime(oprDate);
|
bar.CN_F_QUANTITY = Convert.ToInt32(count);
|
bar.CN_S_QUANTITY_UNIT = countUnit;
|
bar.CN_S_TAG_RATIO = tagRatio;
|
bar.CN_S_CREATOR = userEntity.CN_S_LOGIN;
|
bar.CN_S_CREATOR_BY = userEntity.CN_S_NAME;
|
bar.CN_T_CREATE = DateTime.Now;
|
bar.CN_S_MODIFY = userEntity.CN_S_LOGIN;
|
bar.CN_S_MODIFY_BY = userEntity.CN_S_NAME;
|
bar.CN_T_MODIFY = DateTime.Now;
|
|
bar.CN_S_PACKING_UNIT = unit;
|
bar.CN_F_PACKING_QTY = qty;
|
bar.CN_S_ITEM_CODE = itemEnity.CN_S_ITEM_CODE;
|
bar.CN_S_ITEM_NAME = itemEnity.CN_S_ITEM_NAME;
|
bar.CN_S_MEASURE_UNIT = itemEnity.CN_S_MEASURE_UNIT;
|
bar.CN_S_MODEL = itemEnity.CN_S_MODEL;
|
bar.CN_S_ITEM_STATE = "待检";
|
bar.CN_S_OWNER = Constants.DefaultOwner;
|
uniqueEntityList.Add(bar);
|
|
if ((xiangPos + 1) * position == dataCodeList.Count)
|
{
|
continue;
|
}
|
xiangPos++;
|
}
|
|
TempList.Add(new MONGO_KEYTYPE_MAP { Key = dataCode, Type = "2" });
|
|
foreachIndex++;
|
}
|
|
return uniqueEntityList;
|
}
|
|
#endregion
|
|
#region 查询数据集
|
/// <summary>
|
/// 获取Xml列表
|
/// </summary>
|
/// <param name="searchModel">查询实体(页码,显示条数,条件等)</param>
|
/// <returns></returns>
|
/// <History>[HANHE(XDL)] CREATED BY 2018-11-30</History>
|
[HttpPost]
|
public string GetXmlList(SearchModel searchModel)
|
{
|
try
|
{
|
return ValidateToken(searchModel.TokenId, t =>
|
{
|
//根据tokenId 获取当前登陆信息
|
OperateResult resultInfo = GetTokenInfo(searchModel.TokenId);
|
if (!resultInfo.Success) return JsonConvert.SerializeObject(resultInfo);
|
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
|
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
OperateResult pagingList = BLLCreator.Create<TN_WM_B_UNIQUE_BARCODEBLL>().GetXmlList(searchModel);
|
return JsonConvert.SerializeObject(pagingList, timeFormat);
|
});
|
}
|
catch (Exception ex)
|
{
|
return JsonConvert.SerializeObject(OperateResult.Error(ex.ToString()));
|
throw;
|
}
|
|
}
|
#endregion
|
|
}
|
}
|