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 WeighBusiness.BO; using WeighBusiness.Utils; using WeighBusiness.Utils.SqlUtils; namespace WeighBusiness.BL { public static class LittleCarBL { public static List GetAllLocalLittleCar() { return LocalQueryUtil.GetAllLocalLittleCar(); } public static bool Insert(LittleCar input) { string insertSql = InsertUtil.GetInsertSql(TableNames.小推车, new string[] { "ERP_ID", "Name", "Weight","RowVersion" }, new string[] { input.ERP_ID.ToString(), input.Name, input.Weight.ToString(),input.RowVersion.ToString()}); return ExcuteSql(insertSql); } 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 decimal? GetWeight(string name) { var table = SqlHelperEx.DoQuery(string.Format("select Weight from {0} where Name = '{1}'",TableNames.小推车,name)); if (table.Rows.Count == 0) return null; return (decimal?)table.Rows[0][0]; } 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 SyncLittleCar() { var method = "/MainSystem/B3FoodDeepProcess/Rpcs/BaseInfoRpc/GetLittleCarRowVersion"; var littleCarVersion = RpcFacade.Call>(method); var oldLittleCarVersion = LittleCarBL.GetAllLocalLittleCar(); var needInsertCarID = new List(); var needDeleteAndInsertCarID = new List(); if (littleCarVersion.Count > 0) { if (oldLittleCarVersion == null || oldLittleCarVersion.Count() <= 0) { littleCarVersion.ForEach(x => needInsertCarID.Add(x.Get("field3"))); } else { foreach (var map in littleCarVersion) { var car_ID = map.Get("field3"); var oldVer = oldLittleCarVersion.Where(x => x.ERP_ID == car_ID); if (oldVer != null && oldVer.Count() > 0 && oldVer.FirstOrDefault().RowVersion != map.Get("field4")) { needDeleteAndInsertCarID.Add(car_ID); } else if (oldVer == null || oldVer.Count() <= 0) { needInsertCarID.Add(car_ID); } } foreach (var oldVersion in oldLittleCarVersion) { if (!littleCarVersion.Any(x => x.Get("field3") == oldVersion.ERP_ID)) { LittleCarBL.Delete(oldVersion.ERP_ID.Value); } } } } else { LittleCarBL.Delete(); } if (needDeleteAndInsertCarID.Count() > 0) { foreach (var carID in needDeleteAndInsertCarID) { LittleCarBL.Delete(carID.Value); needInsertCarID.Add(carID); } } if (needInsertCarID.Count > 0) { string carMethod = "/MainSystem/B3FoodDeepProcess/Rpcs/BaseInfoRpc/GetLittleCar"; var cars = RpcFacade.Call>(carMethod, needInsertCarID.ToArray()); if (cars.Count() > 0) { foreach (var detail in cars) { var car = new LittleCar(); car.ERP_ID = detail.Get("field3"); car.Name = detail.Get("field1"); car.Weight = detail.Get("field2"); car.RowVersion = detail.Get("field4"); LittleCarBL.Insert(car); } } } } } }