#region Windchild BOM Model public class WindchildBomViewModel { public long BOMID { get; set; } /// <summary> /// BOM层级 /// </summary> public int Level { get { int le = 0; int leStep = 4; int partLen = Part.Length; int partlen2 = PartTrim.Length; le = partLen - partlen2; return le / leStep; } } public string PartTrim { get { return Part.Trim(); } } public string Part { get; set; } public string Part_Desc { get; set; } public string Ver { get; set; } public string Status { get; set; } public string Qey_Per { get; set; } public string UM { get; set; } public string QAD_Suffix { get; set; } public string Mfgr { get; set; } public string PurStatus { get; set; } public string AltType { get; set; } public string AltStatus { get; set; } public string CompType { get; set; } public string CriticaPart { get; set; } public string StandardPart { get; set; } public string Assembly_Technology { get; set; } public string Classification { get; set; } public string Position { get; set; } public string WR_Sourcing { get; set; } public string TJ_Sourcing { get; set; } public string QX_Sourcing { get; set; } public string MY_Sourcing { get; set; } public string Base_PN { get; set; } } public class WindChild_Bom_MstrModel { public long ID { get; set; } public string Part { get; set; } public string Ver { get; set; } public DateTime Added { get; set; } } public class WindChild_Bom_DetModel { public long ID { get; set; } public string Part { get; set; } public string Ver { get; set; } public long BOMID { get; set; } public long ParentID { get; set; } } public class WindChild_Part_ALTModel { public long ID { get; set; } public string Part { get; set; } public string Alt_Part { get; set; } } #endregion #region Windchild BOM DAL /// <summary> /// Windchild BOM 导入DAL /// </summary> public class WindchildBOMDAL { #region Windchild public void WindchildBomImport(string fileName) { #region 数据加载 var dt = PubUtils.ExcelToDataTable(fileName, true); List<WindchildBomViewModel> wbList = dt.ToList<WindchildBomViewModel>(); #endregion #region 变量定义 bool isSameBom = false;//是否同一Bom WindchildBomViewModel bom = new WindchildBomViewModel(); WindchildBomViewModel part1 = new WindchildBomViewModel(); WindchildBomViewModel part2 = new WindchildBomViewModel(); WindchildBomViewModel part3 = new WindchildBomViewModel(); WindchildBomViewModel part4 = new WindchildBomViewModel(); WindchildBomViewModel part5 = new WindchildBomViewModel(); //WindchildBomModel part6 = new WindchildBomModel(); string compType = "制造商部件";//类型 long currentBomID = 0; #endregion #region 分析处理 foreach (var item in wbList) { isSameBom = item.Level == 0; if (isSameBom) { #region Bom Mstr bom = item; long? bomID = GetBomMstrModel(bom)?.ID; DeleteBomTree(bomID); //保存 Bom Mstr SaveBomMstr(bom); currentBomID = GetBomMstrModel(bom).ID; part1 = null; part2 = null; part3 = null; part4 = null; part5 = null; #endregion } else { #region Bom Det item.BOMID = currentBomID; if (item.Level == 1) { part1 = item; SaveBomDet(null, item); } else if (item.Level == 2) { part2 = item; SaveBomTree(part1, compType, item); } else if (item.Level == 3) { part3 = item; SaveBomTree(part2, compType, item); } else if (item.Level == 4) { part4 = item; SaveBomTree(part3, compType, item); } else if (item.Level == 5) { part5 = item; SaveBomTree(part4, compType, item); } else if (item.Level == 6) { SaveBomTree(part5, compType, item); } #endregion } } #endregion } #endregion #region GetModel /// <summary> /// 是否存在BOM /// </summary> /// <param name="model">WindchildBomModel</param> /// <returns></returns> public bool IsExistBomMstr(WindchildBomViewModel model) { return GetBomMstrModel(model) != null; } /// <summary> /// 获取BOM明细 /// </summary> /// <param name="model"></param> /// <returns></returns> public WindChild_Bom_DetModel GetBomDetModel(WindchildBomViewModel model) { string sql = $"SELECT top 1 * FROM WindChild_Bom_Det WHERE BOMID = '{model.BOMID}' AND Part = '{model.PartTrim}' AND Ver = '{model.Ver}' ";// AND ParentID = '{model.ParentID}' DataTable dt = PubUtils.GetDataTable(sql); if (dt != null && dt.Rows.Count > 0) { return PubUtils.DataRowViewToModel<WindChild_Bom_DetModel>(dt.Rows[0]); } return null; } /// <summary> /// 获取Bom头 /// </summary> /// <param name="model"></param> /// <returns></returns> public WindChild_Bom_MstrModel GetBomMstrModel(WindchildBomViewModel model) { string sql = $"SELECT top 1 * FROM WindChild_Bom_Mstr WHERE Part = '{model.PartTrim}' AND Ver = '{model.Ver}' "; DataTable dt = PubUtils.GetDataTable(sql); if (dt != null && dt.Rows.Count > 0) { return PubUtils.DataRowViewToModel<WindChild_Bom_MstrModel>(dt.Rows[0]); } return null; } #endregion #region Operation /// <summary> /// 保存BOM /// </summary> /// <param name="part1"></param> /// <param name="compType"></param> /// <param name="item"></param> public bool SaveBomTree(WindchildBomViewModel part1, string compType, WindchildBomViewModel item) { if (item.CompType != compType) { //保存Part2 return SaveBomDet(part1, item); } else { //保存Part ALT return SavePartALT(part1, item); } } /// <summary> /// 保存制造商部件 /// </summary> /// <param name="parent"></param> /// <param name="current"></param> /// <returns></returns> public bool SavePartALT(WindchildBomViewModel parent, WindchildBomViewModel current) { string sql = "INSERT INTO [ERPExtend].[dbo].[WindChild_Part_ALT] \n" + "( \n" + "BOMID,Part,Alt_Part " + ") \n" + "VALUES \n" + "( \n" + $" '{parent.BOMID}','{parent.PartTrim}','{current.PartTrim}' " //Parent.Part都有 Ver BomID,ParentID + ")"; return PubUtils.ExeSql(sql) > 0; } /// <summary> /// 保存Bom明细 /// </summary> /// <param name="parent">Parent</param> /// <param name="current"></param> /// <returns></returns> public bool SaveBomDet(WindchildBomViewModel parent, WindchildBomViewModel current) { WindChild_Bom_DetModel pmModel = new WindChild_Bom_DetModel() { BOMID = current.BOMID }; if (parent != null) { pmModel = GetBomDetModel(parent); } if (pmModel is null) { pmModel = new WindChild_Bom_DetModel() { BOMID = current.BOMID }; } string sql = "INSERT INTO [ERPExtend].[dbo].[WindChild_Bom_Det] \n" + "( \n" + " BOMID, ParentID,Part, Ver," + " Part_Desc, [Status], Classification, Qey_Per, UM, \n" + " Position, \n" + " WR_Sourcing, \n" + " TJ_Sourcing, \n" + " QX_Sourcing, MY_Sourcing, Base_PN, QAD_Suffix, Mfgr, PurStatus,AltType, AltStatus, CompType, CriticaPart, StandardPart, Assembly_Technology, Added \n" + ") \n" + "VALUES \n" + "( \n" + $"'{pmModel.BOMID}','{pmModel.ID}', '{current.PartTrim}','{current.Ver}', \n" + $"'{current.Part_Desc}','{current.Status}','{current.Classification}','{current.Qey_Per}','{current.UM}','{current.Position}','{current.WR_Sourcing}','{current.TJ_Sourcing}', \n" + $"'{current.QX_Sourcing}','{current.MY_Sourcing}','{current.Base_PN}','{current.QAD_Suffix}','{current.Mfgr}','{current.PurStatus}','{current.AltType}','{current.AltStatus}','{current.CompType}','{current.CriticaPart}','{current.StandardPart}','{current.Assembly_Technology}', (getdate()) \n" + ")"; return PubUtils.ExeSql(sql) > 0; } /// <summary> /// 保存BOM头 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool SaveBomMstr(WindchildBomViewModel model) { string sql = "INSERT INTO [ERPExtend].[dbo].[WindChild_Bom_Mstr] \n" + "( \n" + " Part, \n" + " Ver, \n" + " Added \n" + ") \n" + "VALUES \n" + "( \n" + $" '{model.PartTrim}', \n" + $" '{model.Ver}', \n" + " (getdate()) \n" + ")"; return PubUtils.ExeSql(sql) > 0; } /// <summary> /// 删除Bom结构 /// </summary> /// <param name="bomID"></param> /// <returns></returns> public bool DeleteBomTree(long? bomID) { string sql = $"DELETE from WindChild_Bom_Mstr WHERE ID = '{bomID}' DELETE FROM WindChild_Bom_Det WHERE BOMID = '{bomID}' DELETE FROM WindChild_Part_ALT WHERE BOMID = '{bomID}' "; return PubUtils.ExeSql(sql) > 0; } #endregion } #endregion
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/