using System; using System.Collections.Generic; using System.Linq; using System.Text; using FireBirdUtil.SqlUtils; using Forks.JsonRpc.Client; using Forks.JsonRpc.Client.Data; using WeighBusiness.BO; using WeighBusiness.Utils; using WeighBusiness.Utils.SqlUtils; namespace WeighBusiness.BL { public static class DishBL { public static List GetAllLocalDish() { return LocalQueryUtil.GetAllLocalDish(); } public static bool Insert(Dish input) { string insertSql = InsertUtil.GetInsertSql(TableNames.盘, new string[] { "ERP_ID", "Name", "Weight", "RowVersion" }, new string[] { input.ERP_ID.ToString(), input.Name, input.Weight == null ? "null" : input.Weight.ToString(), input.RowVersion.Value.ToString() }); return ExcuteSql(insertSql); } 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 void Delete(long ID) { var sql2 = SqlUtil.GetDeleteSql(TableNames.盘, "where ERP_ID=" + ID.ToString()); ExcuteSql(sql2); } public static void Delete() { var sql2 = SqlUtil.GetDeleteSql(TableNames.盘); ExcuteSql(sql2); } public static void SyncDish() { var method = "/MainSystem/B3FoodDeepProcess/Rpcs/BaseInfoRpc/GetDishRowVersion"; var dishVersion = RpcFacade.Call>(method); var oldDishVersion = DishBL.GetAllLocalDish(); var needInsertDishID = new List(); var needDeleteAndInsertDishID = new List(); if (dishVersion.Count > 0) { if (oldDishVersion == null || oldDishVersion.Count() <= 0) { dishVersion.ForEach(x => needInsertDishID.Add(x.Get("field3"))); } else { foreach (var dish in dishVersion) { var car_ID = dish.Get("field3"); var oldDish = oldDishVersion.Where(x => x.ERP_ID == car_ID); if (oldDish != null && oldDish.Count() > 0 && oldDish.FirstOrDefault().RowVersion != dish.Get("field4")) { needDeleteAndInsertDishID.Add(car_ID); } else if (oldDish == null || oldDish.Count() <= 0) { needInsertDishID.Add(car_ID); } } foreach (var oldVersion in oldDishVersion) { if (!dishVersion.Any(x => x.Get("field3") == oldVersion.ERP_ID)) { DishBL.Delete(oldVersion.ERP_ID.Value); } } } } else { DishBL.Delete(); } if (needDeleteAndInsertDishID.Count() > 0) { foreach (var dishID in needDeleteAndInsertDishID) { DishBL.Delete(dishID.Value); needInsertDishID.Add(dishID); } } if (needInsertDishID.Count > 0) { string carMethod = "/MainSystem/B3FoodDeepProcess/Rpcs/BaseInfoRpc/GetDish"; var cars = RpcFacade.Call>(carMethod, needInsertDishID.ToArray()); if (cars.Count() > 0) { foreach (var detail in cars) { var dish = new Dish(); dish.ERP_ID = detail.Get("field3"); dish.Name = detail.Get("field1"); dish.Weight = detail.Get("field2"); dish.RowVersion = detail.Get("field4"); DishBL.Insert(dish); } } } } } }