using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using FireBirdUtil.SqlUtils; using Forks.JsonRpc.Client; using Forks.JsonRpc.Client.Data; using Utils.Datas; using WeighBusiness.BO; using WeighBusiness.Utils; using WeighBusiness.Utils.SqlUtils; namespace WeighBusiness.BL { public static class BomSetBL { public static List GetBomSetRowVersion() { return LocalQueryUtil.GetBomSetRowVersion(); } public class BomSetData { public long BomSet_ID { get; set; } public long BomSet_Detail_ID { get; set; } public decimal? Number { get; set; } public long? Goods_ID { get; set; } public decimal? DetailNumber { get; set; } public InputStyle? InputStyle { get; set; } private List> mNumbers = new List>(); public List> Numbers { get { return mNumbers; } set { mNumbers = value; } } } static List resultList = new List(); private static List GetDetails(BomSetData setData) { var unitPrecentStr = string.Empty; var sql = string.Format("select b.Goods_ID,b.InputStyle,b.Number,a.Number,b.FixedNumber from BomSet a inner join BSDetail b on a.BomSet_ID = b.BomSet_ID where a.BomSet_ID = {1}", unitPrecentStr, setData.BomSet_ID); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var bomSetData = new BomSetData(); bomSetData.Goods_ID = (long)(int.Parse(row[0].ToString())); var inputStyle = row[1].ToString(); if (!string.IsNullOrEmpty(inputStyle)) { var num = int.Parse(inputStyle); if (num == 1) { bomSetData.InputStyle = InputStyle.料包; list.Add(bomSetData); } else if (num == 2) { bomSetData.InputStyle = InputStyle.直接; } } var detailNumber = DataTypeUtil.GetDecimalNullNum(row[2]); var number = DataTypeUtil.GetDecimalNullNum(row[3]); var fixedNumber = DataTypeUtil.GetDecimalNullNum(row[4]); if (setData.Numbers.Count() > 0) { foreach (var item in setData.Numbers) { bomSetData.Numbers.Add(new Tuple(item.Item1, item.Item2,item.Item3)); } } bomSetData.Numbers.Add(new Tuple(number, detailNumber,fixedNumber)); resultList.Add(bomSetData); } return list; } private static void GetDetailsByPackage(List datalist) { var sql = string.Format("select BomSet_ID,Goods_ID from BSApplyDetail where Goods_ID in ({0})",string.Join(",",datalist.Select(x=>x.Goods_ID))); var table = SqlHelperEx.DoQuery(sql); var list = new List(); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var bomSetData = new BomSetData(); bomSetData.BomSet_ID = (long)(int.Parse(row[0].ToString())); bomSetData.Goods_ID = (long)(int.Parse(row[1].ToString())); var result = datalist.Where(x => x.Goods_ID == bomSetData.Goods_ID); if (result != null && result.Count() > 0) { foreach (var item in result.FirstOrDefault().Numbers) { bomSetData.Numbers.Add(new Tuple(item.Item1,item.Item2,item.Item3)); } } list.Add(bomSetData); } } if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { if (list[i] != null) { var details = GetDetails(list[i]); if (details.Count > 0) { GetDetailsByPackage(details); } } } } } public static List GetBomSet(long bomSet_ID, params long[] Goods_IDs) { var backList = new List(); resultList.Clear(); if(Goods_IDs.Length <= 0) return new List(); var list = GetDetails(new BomSetData() { BomSet_ID = bomSet_ID }); if (list.Count > 0) { GetDetailsByPackage(list); } for (var i = 0; i < Goods_IDs.Length; i++) { var result = resultList.Where(x => x.Goods_ID == Goods_IDs[i]); if (result != null && result.Count() > 0) { backList.Add(result.FirstOrDefault()); } } return backList; } public static void Insert(List bomSets, List bomSet_Details, List bomSet_ApplyDetails) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); bool success = false; string errorMessage; if (bomSets != null && bomSets.Count() > 0) { foreach (var bomSet in bomSets) { string insertSql = InsertUtil.GetInsertSql(TableNames.BOM设置, new string[] { "BomSet_ID", "BomSet_Date", "Number", "RowVersion" }, new string[] { bomSet.BomSet_ID.ToString(), bomSet.Date == null ? "null" : bomSet.Date.Value.ToString(), bomSet.Number == null ? "null" : bomSet.Number.ToString(), bomSet.RowVersion.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (bomSet_Details != null && bomSet_Details.Count() > 0) { foreach (var bomSet_Detail in bomSet_Details) { string insertSql = InsertUtil.GetInsertSql(TableNames.BOM清单, new string[] { "BomSet_ID", "BomSet_Detail_ID", "Number", "Goods_ID", "InputStyle", "UnitPrecent", "FixedNumber" }, new string[] { bomSet_Detail.BomSet_ID.ToString(), bomSet_Detail.BomSet_Detail_ID.ToString(), bomSet_Detail.Number == null ? "null" : bomSet_Detail.Number.ToString(), bomSet_Detail.Goods_ID.ToString(), bomSet_Detail.InputStyle == null ? "null" : ((int)(bomSet_Detail.InputStyle)).ToString(), bomSet_Detail.UnitPrecent == null ? "null" : bomSet_Detail.UnitPrecent.ToString(), bomSet_Detail.FixedNumber == null ? "null" : bomSet_Detail.FixedNumber.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (bomSet_ApplyDetails != null && bomSet_ApplyDetails.Count() > 0) { foreach (var bomSet_ApplyDetail in bomSet_ApplyDetails) { string insertSql = InsertUtil.GetInsertSql(TableNames.BOM产品清单, new string[] { "BomSet_ID", "BomSet_Detail_ID", "Goods_ID" }, new string[] { bomSet_ApplyDetail.BomSet_ID.ToString(), bomSet_ApplyDetail.BomSet_Detail_ID.ToString(), bomSet_ApplyDetail.Goods_ID.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (!success) { she.Rollback(); } else { she.Commit(); } } } private static bool ExcuteSql(string sql) { bool success; using (var she = new SqlHelperEx()) { she.CreateTransaction(); she.ExecuteNonQuery(sql, out success); if (!success) she.Rollback(); else she.Commit(); } return success; } public static string Delete(long ID) { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.BOM设置, " where BomSet_ID=" + ID.ToString()); she.ExecuteNonQuery(sql, out success,out errorMessage); if (success) { var sql2 = SqlUtil.GetDeleteSql(TableNames.BOM清单, " where BomSet_ID=" + ID.ToString()); she.ExecuteNonQuery(sql2, out success,out errorMessage); } if (success) { var sql3 = SqlUtil.GetDeleteSql(TableNames.BOM产品清单, " where BomSet_ID=" + ID.ToString()); she.ExecuteNonQuery(sql3, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static string Delete() { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.BOM设置); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql2 = SqlUtil.GetDeleteSql(TableNames.BOM清单); she.ExecuteNonQuery(sql2, out success, out errorMessage); } if (success) { var sql3 = SqlUtil.GetDeleteSql(TableNames.BOM产品清单); she.ExecuteNonQuery(sql3, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static List GetBomSetDetailNum(long goods_ID) { var sql = string.Format("select b.BomSet_ID from BomSet a inner join BSAPPLYDetail b on a.BomSet_ID = b.BomSet_ID where b.Goods_ID = {0}", goods_ID); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); var bomSet_ID = (long)(int.Parse(table.Rows[0][0].ToString())); var sql2 = string.Format("select Goods_ID,a.Number,b.Number,a.InputStyle from BSDetail a left outer join BomSet b on a.BomSet_ID = b.BomSet_ID where a.BomSet_ID = {0}", bomSet_ID); var table2 = SqlHelperEx.DoQuery(sql2); if (table2.Rows.Count == 0) return new List(); foreach (DataRow row in table2.Rows) { var bomSetData = new BomSetData(); bomSetData.Goods_ID = (long)(int.Parse(row[0].ToString())); var detailNumber = row[1].ToString(); if (!string.IsNullOrEmpty(detailNumber)) { bomSetData.DetailNumber = decimal.Parse(detailNumber); } var number = row[2].ToString(); if (!string.IsNullOrEmpty(number)) { bomSetData.Number = decimal.Parse(number); } var inputStyle = row[3].ToString(); if (!string.IsNullOrEmpty(inputStyle)) { var num = int.Parse(inputStyle); if (num == 1) { bomSetData.InputStyle = InputStyle.料包; } else if (num == 2) { bomSetData.InputStyle = InputStyle.直接; } } list.Add(bomSetData); } return list; } public static void SyncBomSet() { var method = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSetRowVersion"; var bomSetRowVersions = RpcFacade.Call>(method); var oldBomSetRowVersions = BomSetBL.GetBomSetRowVersion(); var needInsertBomSetID = new List(); var needDeleteAndInsertBomSetID = new List(); if (bomSetRowVersions.Count > 0) { if (oldBomSetRowVersions == null || oldBomSetRowVersions.Count() <= 0) { bomSetRowVersions.ForEach(x => needInsertBomSetID.Add(x.Get("BomSet_ID"))); } else { foreach (var bsRowVersion in bomSetRowVersions) { var bomSet_ID = bsRowVersion.Get("BomSet_ID"); var oldBomSet = oldBomSetRowVersions.Where(x => x.BomSet_ID == bomSet_ID); if (oldBomSet != null && oldBomSet.Count() > 0 && oldBomSet.FirstOrDefault().RowVersion != bsRowVersion.Get("RowVersion")) { needDeleteAndInsertBomSetID.Add(bomSet_ID); } else if (oldBomSet == null || oldBomSet.Count() <= 0) { needInsertBomSetID.Add(bomSet_ID); } } foreach (var oldVersion in oldBomSetRowVersions) { if (!bomSetRowVersions.Any(x => x.Get("BomSet_ID") == oldVersion.BomSet_ID)) { BomSetBL.Delete(oldVersion.BomSet_ID); } } } } else { BomSetBL.Delete(); } if (needDeleteAndInsertBomSetID.Count() > 0) { foreach (var bomSetID in needDeleteAndInsertBomSetID) { var error = BomSetBL.Delete(bomSetID.Value); if (!string.IsNullOrEmpty(error)) { throw new ApplicationException(error); } needInsertBomSetID.Add(bomSetID); } } if (needInsertBomSetID.Count > 0) { var getBomSet = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSet"; var bomSets = RpcFacade.Call>(getBomSet, needInsertBomSetID.ToArray()); var getBomSetDetail = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSetDetail"; var bomSetDetails = RpcFacade.Call>(getBomSetDetail, needInsertBomSetID.ToArray()); var getBomSetApplyDetail = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetBomSetApplyDetail"; var bomSetApplyDetails = RpcFacade.Call>(getBomSetApplyDetail, needInsertBomSetID.ToArray()); var list = new List(); var detaillist = new List(); var applyDetaillist = new List(); if (bomSets.Count() > 0) { foreach (var detail in bomSets) { var bomSet = new BomSet(); bomSet.BomSet_ID = detail.Get("BomSet_ID"); bomSet.Date = detail.Get("Date"); bomSet.Number = detail.Get("Number"); bomSet.RowVersion = detail.Get("RowVersion"); list.Add(bomSet); } } if (bomSetDetails.Count() > 0) { foreach (var detail in bomSetDetails) { var bomSet = new BomSet_Detail(); bomSet.BomSet_ID = detail.Get("BomSet_ID"); bomSet.BomSet_Detail_ID = detail.Get("BomSet_Detail_ID"); bomSet.Goods_ID = detail.Get("Goods_ID"); bomSet.Number = detail.Get("Number"); var inputStyle = detail.Get("InputStyle"); if (!string.IsNullOrEmpty(inputStyle)) { if(inputStyle == "料包"){ bomSet.InputStyle = InputStyle.料包; } else if (inputStyle == "直接") { bomSet.InputStyle = InputStyle.直接; } } bomSet.UnitPrecent = detail.Get("UnitPrecent"); bomSet.FixedNumber = detail.Get("FixedNumber"); detaillist.Add(bomSet); } } if (bomSetApplyDetails.Count() > 0) { foreach (var detail in bomSetApplyDetails) { var bomSet = new BomSet_ApplyDetail(); bomSet.BomSet_ID = detail.Get("BomSet_ID"); bomSet.BomSet_Detail_ID = detail.Get("BomSet_Detail_ID"); bomSet.Goods_ID = detail.Get("Goods_ID"); applyDetaillist.Add(bomSet); } } BomSetBL.Insert(list, detaillist, applyDetaillist); } } } }