using System; using System.Collections.Generic; using System.Configuration; 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 GoodsWeightFloatBL { public static List GetGoodsWeightFloatRowVersion() { var table = SqlHelperEx.DoQuery("select GoodsWeightFloat_ID,RowVersion from {0}".FormatWith(TableNames.存货称重浮动配置)); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var plan = new GoodsWeightFloat(); plan.GoodsWeightFloat_ID = (long)(int.Parse(row[0].ToString())); plan.RowVersion = int.Parse(row[1].ToString()); list.Add(plan); } return list; } public static GoodsWeightFloat_Detail GetGoodsWeightFloatData(long? goodsId) { var table = SqlHelperEx.DoQuery("select StandardWeight,UpWeight from {0} where Goods_ID = {1}".FormatWith(TableNames.存货称重浮动配置明细, goodsId)); if (table.Rows.Count == 0) return null; foreach (DataRow row in table.Rows) { var floatDetail = new GoodsWeightFloat_Detail(); floatDetail.StandardWeight = DataTypeUtil.GetDecimalNullNum(row[0]); floatDetail.UpWeight = DataTypeUtil.GetDecimalNullNum(row[1]); return floatDetail; } return null; } public static void Insert(List floats, List floatDetails) { using (var she = new SqlHelperEx()) { she.CreateTransaction(); bool success = false; string errorMessage; if (floatDetails.Count() > 0) { foreach (var detail in floats) { string insertSql = InsertUtil.GetInsertSql(TableNames.存货称重浮动配置, new string[] { "GoodsWeightFloat_ID","RowVersion" }, new string[] { detail.GoodsWeightFloat_ID.ToString(), detail.RowVersion.ToString() }); she.ExecuteNonQuery(insertSql, out success, out errorMessage); if (!success && !string.IsNullOrEmpty(errorMessage)) { she.Rollback(); throw new ApplicationException(errorMessage); } } } if (floatDetails.Count() > 0) { foreach (var detail in floatDetails) { string insertSql = InsertUtil.GetInsertSql(TableNames.存货称重浮动配置明细, new string[] { "GoodsWeightFloat_ID", "Goods_ID", "DetailID", "StandardWeight", "UpWeight" }, new string[] { detail.GoodsWeightFloat_ID.ToString(), detail.Goods_ID.ToString(), detail.DetailID.ToString(),detail.StandardWeight == null?"null":detail.StandardWeight.ToString(),detail.UpWeight==null?"null":detail.UpWeight.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(); } } } public static string Delete(long ID) { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.存货称重浮动配置, " where GoodsWeightFloat_ID=" + ID.ToString()); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql3 = SqlUtil.GetDeleteSql(TableNames.存货称重浮动配置明细, " where GoodsWeightFloat_ID=" + ID.ToString()); she.ExecuteNonQuery(sql3, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static void SyncGoodsWeightFloat() { var domain_ID = ConfigurationManager.AppSettings["Domain_ID"]; var method = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetGoodsWeightFloatRowVersion"; if (domain_ID != null && !string.IsNullOrEmpty(domain_ID)) { var goodsWeightFloatRowVersions = RpcFacade.Call>>(method, long.Parse(domain_ID)); var oldGoodsWeightFloatRowVersions = GetGoodsWeightFloatRowVersion(); var needInsertGoodsWeightFloatID = new List(); var needDeleteAndInsertGoodsWeightFloatID = new List(); if (goodsWeightFloatRowVersions.Count > 0) { if (oldGoodsWeightFloatRowVersions.Count() <= 0) { goodsWeightFloatRowVersions.ForEach(x => needInsertGoodsWeightFloatID.Add(x.Item1)); } else { foreach (var floatRowVersion in goodsWeightFloatRowVersions) { var float_ID = floatRowVersion.Item1; var oldWeightFloats = oldGoodsWeightFloatRowVersions.Where(x => x.GoodsWeightFloat_ID == float_ID); if (oldWeightFloats.Count() > 0 && oldWeightFloats.FirstOrDefault().RowVersion != floatRowVersion.Item2) { needDeleteAndInsertGoodsWeightFloatID.Add(floatRowVersion.Item1); } else if (oldWeightFloats.Count() <= 0) { needInsertGoodsWeightFloatID.Add(float_ID); } } foreach (var oldVersion in oldGoodsWeightFloatRowVersions) { if (!goodsWeightFloatRowVersions.Any(x => x.Item1 == oldVersion.GoodsWeightFloat_ID)) { Delete(oldVersion.GoodsWeightFloat_ID); } } } } else { Delete(); } if (needDeleteAndInsertGoodsWeightFloatID.Count() > 0) { foreach (var floatID in needDeleteAndInsertGoodsWeightFloatID) { var error = Delete(floatID.Value); if (!string.IsNullOrEmpty(error)) { throw new ApplicationException(error); } needInsertGoodsWeightFloatID.Add(floatID); } } if (needInsertGoodsWeightFloatID.Count > 0) { var getGoodsWeightFloatDetail = "/MainSystem/B3_HaoYue/Rpcs/B3CowButcherManageRpc/GetGoodsWeightFloatInDetailData"; var goodsWeightFloatDetails = RpcFacade.Call>>(getGoodsWeightFloatDetail, needInsertGoodsWeightFloatID.ToArray(),0); var list = new List(); var floatList = new List(); foreach (var floatId in needInsertGoodsWeightFloatID) { var wFloat = new GoodsWeightFloat(); wFloat.GoodsWeightFloat_ID = floatId.Value; var result = goodsWeightFloatRowVersions.Where(x => x.Item1 == floatId).First(); wFloat.RowVersion = result.Item2.Value; list.Add(wFloat); } if (goodsWeightFloatDetails.Count() > 0) { foreach (var detail in goodsWeightFloatDetails) { var floatDetail = new GoodsWeightFloat_Detail(); floatDetail.GoodsWeightFloat_ID = detail.Item1.Value; floatDetail.Goods_ID = detail.Item2.Value; floatDetail.DetailID = detail.Item3.Value; floatDetail.StandardWeight = detail.Item4; floatDetail.UpWeight = detail.Item5; floatList.Add(floatDetail); } } Insert(list, floatList); } } } public static string Delete() { bool success; string errorMessage; using (var she = new SqlHelperEx()) { she.CreateTransaction(); var sql = SqlUtil.GetDeleteSql(TableNames.存货称重浮动配置); she.ExecuteNonQuery(sql, out success, out errorMessage); if (success) { var sql2 = SqlUtil.GetDeleteSql(TableNames.存货称重浮动配置明细); she.ExecuteNonQuery(sql2, out success, out errorMessage); } if (!success) she.Rollback(); else she.Commit(); } return errorMessage; } public static ProductTeam GetProductTeam(long? employeeId) { var table = SqlHelperEx.DoQuery("select ProductTeam_ID,Name,ProduceType from {0} where Employee_ID = {1}".FormatWith(TableNames.生产班组, employeeId == null ? "null" : employeeId.Value.ToString())); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var team = new ProductTeam(); team.ProductTeam_ID = (long)(int.Parse(row[0].ToString())); team.Name = row[1].ToString(); team.ProduceType = row[2].ToString(); return team; } } return null; } } }