using System; using System.Collections.Generic; using System.Collections.ObjectModel; 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 RoadMapBL { public static List GetAllLocalRoadMapVersion() { return LocalQueryUtil.GetAllLocalRoadMapVersion(); } public static void DeleteOut(long ID) { var sql = SqlUtil.GetDeleteSql(TableNames.工艺产出明细, "where RoadMap_ID=" + ID.ToString()); ExcuteSql(sql); } public static void DeleteInput(long ID) { var sql2 = SqlUtil.GetDeleteSql(TableNames.工艺投入明细, "where RoadMap_ID=" + ID.ToString()); ExcuteSql(sql2); } public static void DeleteOut() { var sql = SqlUtil.GetDeleteSql(TableNames.工艺产出明细); ExcuteSql(sql); } public static void DeleteInput() { var sql2 = SqlUtil.GetDeleteSql(TableNames.工艺投入明细); ExcuteSql(sql2); } 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 bool InputInsert(RoadMap_InputDetail input) { string insertSql = InsertUtil.GetInsertSql(TableNames.工艺投入明细, new string[] { "Detail_ID", "Goods_ID", "Goods_Name", "Goods_Code", "RoadMap_ID", "RowVersion","Routing_ID","ProductShift_ID","RoadMap_Name" }, new string[] { input.Detail_ID.ToString(), input.Goods_ID.ToString(), input.Goods_Name, input.Goods_Code, input.RoadMap_ID.ToString(), input.RowVersion,input.Routing_ID.ToString(),input.ProductShift_ID.ToString(), input.RoadMap_Name}); return ExcuteSql(insertSql); } public static bool OutInsert(RoadMap_OutputDetail input) { string insertSql = InsertUtil.GetInsertSql(TableNames.工艺产出明细, new string[] { "Detail_ID", "Goods_ID", "Goods_Name", "Goods_Code", "RoadMap_Name", "ProductShift_ID", "ProductShift_Name", "RoadMap_ID", "RowVersion","Routing_ID" }, new string[] { input.Detail_ID.ToString(), input.Goods_ID.ToString(), input.Goods_Name, input.Goods_Code, input.RoadMap_Name, input.ProductShift_ID.ToString(),input.ProductShift_Name,input.RoadMap_ID.ToString(),input.RowVersion,input.Routing_ID.ToString() }); return ExcuteSql(insertSql); } public static int GetAllLocalRoadMap_InputPageCount(long? Routing_ID, long shiftId,string roadMap_Name) { var sql = "select count(Detail_ID) from {0} where Routing_ID={1} and ProductShift_ID = {2} and RoadMap_Name = '{3}'".FormatWith(TableNames.工艺投入明细,Routing_ID,shiftId, roadMap_Name); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return 0; var counts = (int)table.Rows[0][0]; var num1 = counts / 6; var num2 = counts % 6; return num2 > 0 ? num1 + 1 : num1; } public static ObservableCollection GetRoadMapInputDetail(long batchNumberId, long? Routing_ID, long shiftId,string roadMap_Name, RoadMap_OutputDetail outDetail,bool isBatch,int pageIndex,bool isbox) { var details = LocalQueryUtil.GetAllLocalRoadMap_InputDetail(batchNumberId,Routing_ID, roadMap_Name, shiftId, isBatch, pageIndex); details.ForEach(x => x.IsBatch = isBatch); if (outDetail!=null && outDetail.Goods_ID != null && details.Count() > 0) { var list = BomSetBL.GetBomSetDetailNum(outDetail.Goods_ID.Value); if (list.Count > 0) { foreach (var bomDetail in list) { var detail = details.Where(x => x.Goods_ID == bomDetail.Goods_ID); if (detail != null && detail.Count() > 0) { var inputDetail = detail.FirstOrDefault(); inputDetail.StandardNum = bomDetail.DetailNumber; if (inputDetail.StandardNum != 0) { inputDetail.Copies = Math.Round((inputDetail.InputNum / inputDetail.StandardNum) ?? 0, 1); } if(bomDetail.DetailNumber != null && bomDetail.Number != 0){ var result = (outDetail.ProductNum/bomDetail.Number * bomDetail.DetailNumber) ?? 0; if (isbox) { inputDetail.TotalNum = Math.Round(result, 3); } else { if (bomDetail.InputStyle == InputStyle.料包) { inputDetail.TotalNum = Math.Round(result, 1); } else { inputDetail.TotalNum = Math.Round(result, 3); } } } } } } } return details; } public static List GetAllLocalRoadMap_InputDetails(long? Routing_ID, long shiftId,string roadMap_Name) { var sql = @"select detail.Detail_ID,detail.Goods_ID from {0} detail where Routing_ID={1} and ProductShift_ID = {2} and RoadMap_Name = '{3}'".FormatWith(TableNames.工艺投入明细, Routing_ID, shiftId,roadMap_Name); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var detail = new RoadMap_InputDetail(); detail.Detail_ID = DataTypeUtil.GetLongNum(row[0]); detail.Goods_ID = DataTypeUtil.GetLongNum(row[1]); list.Add(detail); } return list; } public static List GetAllLocalRoadMap_OutputDetails(long? Routing_ID, long shiftId) { var sql = @"select Detail_ID,Goods_ID from {0} where Routing_ID={1} and ProductShift_ID = {2}".FormatWith(TableNames.工艺产出明细, Routing_ID, shiftId); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var detail = new RoadMap_OutputDetail(); detail.Detail_ID = DataTypeUtil.GetLongNum(row[0]); detail.Goods_ID = DataTypeUtil.GetLongNum(row[1]); list.Add(detail); } return list; } public static ObservableCollection GetRoadMapOutputDetail(long? Routing_ID, long shiftId,long bomSet_ID,decimal? produceCount) { var details = LocalQueryUtil.GetAllLocalRoadMap_OutputDetail(Routing_ID, shiftId); if (details.Count() > 0) { var bomSets = BomSetBL.GetBomSet(bomSet_ID, details.Select(x => x.Goods_ID.Value).ToArray()); foreach (var detail in details) { var sets = bomSets.Where(x => x.Goods_ID == detail.Goods_ID); if (sets != null && sets.Count() > 0) { var set = sets.FirstOrDefault(); if (set.Numbers.Count() > 0) { decimal? number = produceCount; foreach (var item in set.Numbers) { if (item.Item1 != 0) { number = number.Value / item.Item1 * item.Item2 + (item.Item3??0); } else { number = null; break; } } if (set.InputStyle == InputStyle.料包) { detail.ProductNum = Math.Round((number ?? 0), 1); } else { detail.ProductNum = Math.Round((number ?? 0), 3); } } } else { detail.ProductNum = produceCount; } } } return details; } public static int GetAllLocalRoadMap_OutputPageCount(long? Routing_ID, long shiftId) { var sql = "select count(Detail_ID) from {0} where Routing_ID={1} and ProductShift_ID = {2}".FormatWith(TableNames.工艺产出明细,Routing_ID, shiftId); var table = SqlHelperEx.DoQuery(sql); if (table.Rows.Count == 0) return 0; var counts = (int)table.Rows[0][0]; var num1 = counts / 6; var num2 = counts % 6; return num2 > 0 ? num1 + 1 : num1; } public static ObservableCollection GetRoadMapOutputDetail2(long? batchNumberId,long? Routing_ID, long shiftId, long bomSet_ID, decimal? produceCount,int outPutPageIndex) { var details = LocalQueryUtil.GetAllLocalRoadMap_OutputDetail2(Routing_ID, batchNumberId, shiftId,outPutPageIndex); if (details.Count() > 0) { var bomSets = BomSetBL.GetBomSet(bomSet_ID, details.Select(x => x.Goods_ID.Value).ToArray()); foreach (var detail in details) { var sets = bomSets.Where(x => x.Goods_ID == detail.Goods_ID); if (sets != null && sets.Count() > 0) { var set = sets.FirstOrDefault(); if (set.Numbers.Count() > 0) { decimal? number = produceCount; foreach (var item in set.Numbers) { if (item.Item1 != 0) { number = number.Value / item.Item1 * item.Item2 + (item.Item3??0); } else { number = null; break; } } if (set.InputStyle == InputStyle.料包) { detail.ProductNum = Math.Round((number ?? 0), 1); } else { detail.ProductNum = Math.Round((number ?? 0), 3); } } } else { detail.ProductNum = produceCount; } } } return details; } public static void SyncRoadMap() { string method = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetRoadMapVersion"; var roadMapVersion = RpcFacade.Call>(method); var oldRoadMapVersion = RoadMapBL.GetAllLocalRoadMapVersion(); var needInsertMapID = new List(); var needDeleteAndInsertMapID = new List(); if (roadMapVersion.Count > 0) { if (oldRoadMapVersion == null || oldRoadMapVersion.Count() <= 0) { roadMapVersion.ForEach(x => needInsertMapID.Add(x.Get("ID"))); } else { foreach (var map in roadMapVersion) { var roadMap_ID = map.Get("ID"); var oldVer = oldRoadMapVersion.Where(x => x.RoadMap_ID == roadMap_ID); if (oldVer != null && oldVer.Count() > 0 && oldVer.FirstOrDefault().RowVersion != map.Get("RowVersion")) { needDeleteAndInsertMapID.Add(roadMap_ID); } else if (oldVer == null || oldVer.Count() <= 0) { needInsertMapID.Add(roadMap_ID); } } foreach (var oldVersion in oldRoadMapVersion) { if (!roadMapVersion.Any(x => x.Get("ID") == oldVersion.RoadMap_ID)) { RoadMapBL.DeleteInput(oldVersion.RoadMap_ID.Value); RoadMapBL.DeleteOut(oldVersion.RoadMap_ID.Value); } } } } else { RoadMapBL.DeleteInput(); RoadMapBL.DeleteOut(); } if (needDeleteAndInsertMapID.Count() > 0) { foreach (var mapID in needDeleteAndInsertMapID) { RoadMapBL.DeleteInput(mapID.Value); RoadMapBL.DeleteOut(mapID.Value); needInsertMapID.Add(mapID); } } if (needInsertMapID.Count > 0) { string inputMethod = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetRoadMapInputDetail"; var inpuDetails = RpcFacade.Call>(inputMethod,null, needInsertMapID); string outMethod = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetRoadMapOutDetail"; var outDetails = RpcFacade.Call>(outMethod,null, needInsertMapID); if (inpuDetails.Count() > 0) { foreach (var detail in inpuDetails) { var input = new RoadMap_InputDetail(); input.Detail_ID = detail.Get("ID"); input.Goods_ID = detail.Get("Goods_ID"); input.Goods_Name = detail.Get("Goods_Name"); input.Goods_Code = detail.Get("Goods_Code"); input.RoadMap_ID = detail.Get("RoadMap_ID"); input.RowVersion = detail.Get("RowVersion"); input.Routing_ID = detail.Get("Routing_ID"); input.ProductShift_ID = detail.Get("ProductShift_ID"); input.RoadMap_Name = detail.Get("RoadMap_Name"); RoadMapBL.InputInsert(input); } } if (outDetails.Count() > 0) { foreach (var detail in outDetails) { var output = new RoadMap_OutputDetail(); output.Detail_ID = detail.Get("ID"); output.Goods_ID = detail.Get("Goods_ID"); output.Goods_Name = detail.Get("Goods_Name"); output.Goods_Code = detail.Get("Goods_Code"); output.RoadMap_Name = detail.Get("RoadMap_Name"); output.ProductShift_ID = detail.Get("ProductShift_ID"); output.ProductShift_Name = detail.Get("ProductShift_Name"); output.RoadMap_ID = detail.Get("RoadMap_ID"); output.RowVersion = detail.Get("RowVersion"); output.Routing_ID = detail.Get("Routing_ID"); RoadMapBL.OutInsert(output); } } } } public static List GetFinishedRoadMapID(long? detailID) { var sql4 = @"select RoadMap_ID from {0} where ProduceBatch_ID = {1}".FormatWith(TableNames.已完毕路线, detailID); var table4 = SqlHelperEx.DoQuery(sql4); var list = new List(); if (table4.Rows.Count > 0) { foreach (DataRow row in table4.Rows) { list.Add(DataTypeUtil.GetLongNullNum(row[0])); } } return list; } } }