using Rhea.Common; using System; using System.Collections.Generic; using System.Linq; using Tiger.IBusiness; using Tiger.Business.SqlSugarHepler; using Tiger.IBusiness.WMS.T100ToWMS; using Tiger.Model; using Tiger.Model.Entitys.WMS.DTOS; using Tiger.Model.Extensions; namespace Tiger.Business.WMS.T100ToWMS { /// ///出货通知单业务类 /// public class SaleOutInfoBusiness : ISaleOutInfoBusiness { /// ///业务方法 /// public int SaveSaleOutInfo(List input) { if (input == null || input.Count() == 0) { return 0; } var masterData = input.ToObject>();// 实体映射 var detailSource = new List(); foreach (var item in input) { if (!item.Details.Any()) { continue; } //把主表的公共数据赋值到明细 foreach (var detail in item.Details) { detail.BILLCODE = item.BILLCODE; detail.CREATE_TIME = item.BILLDATE; detail.DELIVERYDATE = item.BILLDATE; detail.AUTH_ORG = item.AUTH_ORG; } detailSource.AddRange(item.Details); } var detailData = detailSource.ToObject>();// 实体映射 int res = 0; //var db = Business.Biz.Db; var db = Biz.DataSource["WMS57"].Client; var BILLCODELst = masterData.Select(i => i.BILLCODE).Distinct().ToList(); #region 区分主表需要插入及更新的数据 var hostoryMasterData = db.Queryable().Where(i => BILLCODELst.Contains(i.BILLCODE) && i.GHOST_ROW == false).ToList();// 获取主表旧数据 var resMaster = hostoryMasterData.Select(i => i.BILLCODE).Distinct().ToList();//表中已有的单号 var masterNeedInsert = masterData.Where(i => !resMaster.Contains(i.BILLCODE) && i.GHOST_ROW == false).ToList();// 需要插入的数据 var masterNeedUpdate = masterData.Where(i => resMaster.Contains(i.BILLCODE) && i.GHOST_ROW == false).ToList();// 需要更新的数据 // var masterNeedUpdate = hostoryMasterData.Except(masterData).ToList();// 需要更新的数据 //更新 foreach (var itemnew in masterNeedUpdate) { itemnew.ID = hostoryMasterData.Where(i => i.BILLCODE == itemnew.BILLCODE).FirstOrDefault()?.ID; } //删除明细表 foreach (var ith in hostoryMasterData) { db.Deleteable().Where(i => i.BILLCODE == ith.BILLCODE).ExecuteCommand(); } #endregion 区分主表需要插入及更新的数据 #region 区分明细表需要插入及更新的数据 // var hostoryDetailData = db.Queryable().Where(i => BILLCODELst.Contains(i.BILLCODE)).ToList();// 获取明细表旧数据 // var resDetail = hostoryDetailData.Select(i => i.BILLCODE).Distinct().ToList(); // var detailNeedInsert = detailData.Where(i => !resDetail.Contains(i.BILLCODE)).ToList();// 需要插入的数据 // var detailNeedUpdate = detailData.Where(i => resDetail.Contains(i.BILLCODE)).ToList();// 需要更新的数据 // var detailNeedUpdate = hostoryDetailData.Except(detailData).ToList();// 需要更新的数据 #endregion 区分明细表需要插入及更新的数据 try { BizSqlsugar.CreateTran(() => { if (masterNeedInsert.Any()) { res = BizSqlsugar.InsertDataTable(masterNeedInsert, db); } if (detailData.Any()) { res += BizSqlsugar.InsertDataTable(detailData, db); } if (masterNeedUpdate.Any()) { res += BizSqlsugar.UpdateDataTable(masterNeedUpdate, db); } /* if (detailNeedUpdate.Any()) { res += BizSqlsugar.UpdateDataTable(detailNeedUpdate, db); }*/ }, db); } catch (Exception ex) { new ApiAction().CatchExceptionWithLog(ex, $"接口新增或更新数据异常!"); } return res; } } }