using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using Tiger.Business.SqlSugarHepler;
|
using Tiger.IBusiness;
|
using Tiger.Model;
|
using Tiger.Model.Entitys.WMS.DTOS;
|
using Tiger.Model.Extensions;
|
|
namespace Tiger.Business.WMS
|
{
|
/// <summary>
|
///
|
/// </summary>
|
public class TrasferInfoBusiness : ITrasferInfoBusiness
|
{
|
/// <summary>
|
/// 存储T100推送过来的调拨信息
|
/// </summary>
|
/// <returns></returns>
|
/// <exception cref="NotImplementedException"></exception>
|
public int SaveTrasferInfo(List<TrasferInfoDTO> input, string type = "1")
|
{
|
var masterData = input.ToObject<List<BIZ_WMS_TRANSFER>>();// 实体映射
|
if (masterData != null && type == "2")
|
{
|
foreach (var item in masterData)
|
{
|
item.SOURCE_ORDER = item.ORDER_NO;
|
}
|
}
|
var detailSource = new List<TrasferInfoDetailDTO>();
|
var time = DateTime.Now;
|
foreach (var item in input)
|
{
|
if (!item.details.Any())
|
{
|
continue;
|
}
|
foreach (var detail in item.details)
|
{
|
detail.AUTH_ORG = item.AUTH_ORG;
|
detail.BILLCODE = item.BILLCODE;
|
detail.CREATE_TIME = time;
|
detail.LINESTATUS = string.IsNullOrWhiteSpace(detail.LINESTATUS) ? "0" : detail.LINESTATUS;
|
}
|
detailSource.AddRange(item.details);
|
}
|
var detailData = detailSource.ToObject<List<BIZ_WMS_TRANSFER_DTL>>();// 实体映射
|
|
int res = 0;
|
//var db = input.First()?.CorporateCode == "88" ? Biz.Db : Biz.DataSource["WMS57"].Client;// 区分正式库还是测试库
|
//var db = Biz.Db;// 区分正式库还是测试库
|
var db = Biz.DataSource["WMS57"].Client; // 区分正式库还是测试库
|
|
var BILLCODELst = masterData.Select(i => i.ORDER_NO).Distinct().ToList();
|
|
#region 区分主表需要插入及更新的数据
|
|
var hostoryMasterData = db.Queryable<BIZ_WMS_TRANSFER>().Where(i => BILLCODELst.Contains(i.ORDER_NO)).ToList();// 获取主表旧数据
|
var resMaster = hostoryMasterData.Select(i => i.ORDER_NO).Distinct().ToList();
|
var masterNeedInsert = masterData.Where(i => !resMaster.Contains(i.ORDER_NO)).ToList();// 需要插入的数据
|
var masterNeedUpdate = masterData.Where(i => resMaster.Contains(i.ORDER_NO)).ToList();// 需要更新的数据
|
foreach (var item in masterNeedUpdate)
|
{
|
item.ID = hostoryMasterData.Where(i => i.ORDER_NO == item.ORDER_NO).FirstOrDefault()?.ID;
|
}
|
|
#endregion 区分主表需要插入及更新的数据
|
|
#region 区分明细表需要插入及更新的数据
|
|
var hostoryDetailData = db.Queryable<BIZ_WMS_TRANSFER_DTL>().Where(i => BILLCODELst.Contains(i.ORDER_NO)).ToList();// 获取明细表旧数据 剔除状态为2的数据
|
var resDetail = hostoryDetailData.Select(i => i.ITEM_CODE).Distinct().ToList();// 获取所有的物料编号
|
var ignoreCode = hostoryDetailData.Where(i => i.STATUS == 2 || i.STATUS == 3).Select(i => i.ITEM_CODE).Distinct().ToList();// 需要忽略的物料编号
|
var needInsertData = detailData.Where(i => !resDetail.Contains(i.ITEM_CODE) && !ignoreCode.Contains(i.ITEM_CODE)).ToList();// 需要插入的数据
|
if (type == "1")
|
{
|
foreach (var item in needInsertData)
|
{
|
item.QTY_OUT = 0;
|
}
|
}
|
|
var needUpdateDetail = detailData.Where(i => resDetail.Contains(i.ITEM_CODE)).ToList();// 确认需要更新的数据
|
|
foreach (var item in needUpdateDetail)
|
{
|
if (item.STATUS == 0 && type == "1")
|
{
|
item.QTY_OUT = 0;
|
}
|
item.AUTH_ORG = input[0].AUTH_ORG;
|
}
|
|
var exceptCode = detailData.Select(i => i.ITEM_CODE).Distinct().ToList();
|
var deletecode = resDetail.Except(exceptCode).ToList();
|
|
var needDeleteData = hostoryDetailData.Where(i => deletecode.Contains(i.ITEM_CODE) && BILLCODELst.Contains(i.ORDER_NO)).ToList();
|
|
foreach (var item in needUpdateDetail)
|
{
|
item.ID = hostoryDetailData.Where(i => i.ITEM_CODE == item.ITEM_CODE).FirstOrDefault()?.ID;
|
}
|
//var needInsertData = detailData.Except(hostoryDetailData).ToList();// 需要插入的数据
|
//var needDeleteData = hostoryDetailData.Except(detailData).ToList();// 需要删除的数据
|
|
#endregion 区分明细表需要插入及更新的数据
|
|
try
|
{
|
BizSqlsugar.CreateTran(() =>
|
{
|
if (masterNeedInsert.Any())
|
{
|
res = BizSqlsugar.InsertDataTable(masterNeedInsert, db);
|
}
|
if (masterNeedUpdate.Any())
|
{
|
res += BizSqlsugar.UpdateDataTable(masterNeedUpdate, db);
|
}
|
if (detailData.Any())
|
{
|
if (needDeleteData.Any())
|
{
|
db.Deleteable(needDeleteData).ExecuteCommand();
|
}
|
if (needUpdateDetail.Any())
|
{
|
res += db.Updateable(needUpdateDetail).ExecuteCommand();
|
}
|
if (needInsertData.Any())
|
{
|
res += BizSqlsugar.InsertDataTable(needInsertData, db);
|
}
|
}
|
}, db);
|
}
|
catch (Exception)
|
{
|
}
|
|
return res;
|
}
|
}
|
}
|