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 GoodsBL { public static List GetAllLocalPSInfoVersion() { var table = SqlHelperEx.DoQuery("select Goods_ID,RowVersion from Goods"); if (table.Rows.Count == 0) return new List(); var list = new List(); foreach (DataRow row in table.Rows) { var goods = new Goods(); var goods_ID = row[0].ToString(); if (!string.IsNullOrEmpty(goods_ID)) { goods.Goods_ID = (long)(int.Parse(goods_ID)); } goods.RowVersion = int.Parse(row[1].ToString()); list.Add(goods); } return list; } public static bool Insert(Goods goods) { string insertSql = InsertUtil.GetInsertSql(TableNames.存货, new string[] { "Goods_ID", "AllowDisplay", "RowVersion"}, new string[] { goods.Goods_ID.ToString(), goods.AllowDisplay.ToString(), goods.RowVersion.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 Goods_ID=" + ID.ToString()); ExcuteSql(sql2); } public static void Delete() { var sql2 = SqlUtil.GetDeleteSql(TableNames.存货); ExcuteSql(sql2); } public static void SyncGoods() { var method = "/MainSystem/B3_HaoYue/Rpcs/RpcFun/GetGoodsRowVersion"; var goodsVersion = RpcFacade.Call>>(method); var oldGoods = GetAllLocalPSInfoVersion(); var needInsertGoodsID = new List(); var needDeleteAndInsertGoodsID = new List(); if (goodsVersion.Count > 0) { if (oldGoods == null || oldGoods.Count() <= 0) { goodsVersion.ForEach(x => needInsertGoodsID.Add(x.Item1)); } else { foreach (var goods in goodsVersion) { var goods_ID = goods.Item1; var oldGS = oldGoods.Where(x => x.Goods_ID == goods_ID); if (oldGS != null && oldGS.Count() > 0 && oldGS.FirstOrDefault().RowVersion != goods.Item2) { needDeleteAndInsertGoodsID.Add(goods_ID); } else if (oldGS == null || oldGS.Count() <= 0) { needInsertGoodsID.Add(goods_ID); } } foreach (var oldVersion in oldGoods) { if (!goodsVersion.Any(x => x.Item1 == oldVersion.Goods_ID)) { Delete(oldVersion.Goods_ID); } } } } else { Delete(); } if (needDeleteAndInsertGoodsID.Count() > 0) { foreach (var gsID in needDeleteAndInsertGoodsID) { GoodsBL.Delete(gsID.Value); needInsertGoodsID.Add(gsID.Value); } } if (needInsertGoodsID.Count > 0) { if (goodsVersion.Count() > 0) { var list = new List>(); foreach (var gsID in needInsertGoodsID) { list.Add(goodsVersion.Where(x => x.Item1 == gsID).FirstOrDefault()); } foreach (var detail in list) { var goods = new Goods(); goods.Goods_ID = detail.Item1.Value; goods.RowVersion = detail.Item2.Value; goods.AllowDisplay = detail.Item3.Value; GoodsBL.Insert(goods); } } } } } }