From 3ca866f218004aa973f60ff5aedf5fa63417a139 Mon Sep 17 00:00:00 2001 From: yibo <361071264@qq.com> Date: Fri, 31 Aug 2018 14:30:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- B3ClientService/OfflinRpc/BaseInfoRpc.cs | 218 ++++++++++++++--------- 1 file changed, 132 insertions(+), 86 deletions(-) diff --git a/B3ClientService/OfflinRpc/BaseInfoRpc.cs b/B3ClientService/OfflinRpc/BaseInfoRpc.cs index e863289..85841a7 100644 --- a/B3ClientService/OfflinRpc/BaseInfoRpc.cs +++ b/B3ClientService/OfflinRpc/BaseInfoRpc.cs @@ -16,45 +16,58 @@ namespace BWP.B3ClientService.Rpcs public static class SyncBaseInfoRpc { [Rpc(RpcFlags.SkipAuth)] - public static string SyncWorkUnit() + public static string SyncWorkUnit(string par) { - return GetBaseInfoJosn(); + return GetBaseInfoJosn(par); } [Rpc(RpcFlags.SkipAuth)] - public static string SyncProductBatch() + public static string SyncStore(string par) { - return SyncProductBatchByType(0); + return GetBaseInfoJosn(par); } - [Rpc(RpcFlags.SkipAuth)] - public static string SyncProductBatchByType(int type) + static string GetBaseInfoJosn(string par) + where T : B3Frameworks.BO.BaseInfo, new() { - var query = new DQueryDom(new JoinAlias(typeof(ProductBatch))); + var local = JsonConvert.DeserializeObject>>(par); + var query = new DQueryDom(new JoinAlias(typeof(T))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("Name")); - query.Columns.Add(DQSelectColumn.Field("Date")); - query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("BatchType", type), DQCondition.EQ("IsLocked", false), DQCondition.EQ("Stopped", false))); - var list = query.EExecuteList().Select(x => new MinProductBatch { ID = x.Item1, Name = x.Item2, Date = x.Item3 }); - return JsonConvert.SerializeObject(list); + query.Columns.Add(DQSelectColumn.Field("RowVersion")); + query.Where.Conditions.Add(DQCondition.EQ("Stopped", false)); + var server = query.EExecuteList(); + var insert = server.Where(x => !local.Any(y => y.Item1 == x.Item1)).Select(x => new { ID = x.Item1, Name = x.Item2, RowVersion = x.Item3 }); + var update = server.Where(x => local.Any(y => y.Item1 == x.Item1 && y.Item2 != x.Item3)).Select(x => new { ID = x.Item1, Name = x.Item2, RowVersion = x.Item3 }); + var delete = local.Where(x => !server.Any(y => y.Item1 == x.Item1)).Select(x => x.Item1); + + return JsonConvert.SerializeObject(new Tuple, IEnumerable, IEnumerable>(insert, update, delete)); } - static string GetBaseInfoJosn(IDQExpression withCondition = null) - where T : B3Frameworks.BO.BaseInfo, new() + [Rpc(RpcFlags.SkipAuth)] + public static string SyncProductBatchByType(int type, string par) { - var query = new DQueryDom(new JoinAlias(typeof(T))); + var local = JsonConvert.DeserializeObject>>(par); + var query = new DQueryDom(new JoinAlias(typeof(ProductBatch))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("Name")); - query.Where.Conditions.Add(DQCondition.EQ("Stopped", false)); - if (withCondition != null) - query.Where.Conditions.Add(withCondition); - var list = query.EExecuteList().Select(x => new MinBaseInfo { ID = x.Item1, Name = x.Item2 }); - return JsonConvert.SerializeObject(list); + query.Columns.Add(DQSelectColumn.Field("Date")); + query.Columns.Add(DQSelectColumn.Field("RowVersion")); + query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("BatchType", type), DQCondition.EQ("Stopped", false), DQCondition.GreaterThanOrEqual("Date", DateTime.Today.AddDays(-1)))); + var server = query.EExecuteList(); + var insert = server.Where(x => !local.Any(y => y.Item1 == x.Item1)).Select(x => new { ID = x.Item1, Name = x.Item2, Date = x.Item3, RowVersion = x.Item4 }); + var update = server.Where(x => local.Any(y => y.Item1 == x.Item1 && y.Item2 != x.Item4)).Select(x => new { ID = x.Item1, Name = x.Item2, Date = x.Item3, RowVersion = x.Item4 }); + var delete = local.Where(x => !server.Any(y => y.Item1 == x.Item1)).Select(x => x.Item1); + + return JsonConvert.SerializeObject(new Tuple, IEnumerable, IEnumerable>(insert, update, delete)); } [Rpc(RpcFlags.SkipAuth)] - public static string SyncClientGoodsSetByClient(short type) + public static string SyncClientGoodsSetByClient(short type, string par, string p2) { + var local = JsonConvert.DeserializeObject>>(par); + var g2 = JsonConvert.DeserializeObject>>(p2); + var main = new JoinAlias(typeof(ClientGoodsSet)); var detail = new JoinAlias(typeof(ClientGoodsSet_Detail)); var goods = new JoinAlias(typeof(Goods)); @@ -62,9 +75,10 @@ namespace BWP.B3ClientService.Rpcs query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID")); query.From.AddJoin(JoinType.Left, new DQDmoSource(goods), DQCondition.EQ(detail, "Goods_ID", goods, "ID")); query.Where.Conditions.Add(DQCondition.EQ("ApplyClient", type)); + query.Where.Conditions.Add(DQCondition.EQ("Stopped", false)); query.Columns.Add(DQSelectColumn.Field("ID")); - query.Columns.Add(DQSelectColumn.Field("Stopped")); + query.Columns.Add(DQSelectColumn.Field("RowVersion")); query.Columns.Add(DQSelectColumn.Field("Name")); query.Columns.Add(DQSelectColumn.Field("ID", detail)); query.Columns.Add(DQSelectColumn.Field("Goods_ID", detail)); @@ -75,9 +89,15 @@ namespace BWP.B3ClientService.Rpcs query.Columns.Add(DQSelectColumn.Field("Code", goods)); query.Columns.Add(DQSelectColumn.Field("Spec", goods)); query.Columns.Add(DQSelectColumn.Field("MainUnit", goods)); + query.Columns.Add(DQSelectColumn.Field("RowVersion", goods)); - var list = new List(); - var goodsList = new List(); + var insert = new List(); + var update = new List(); + var serverIDs = new List(); + + var gInsert = new List(); + var gUpdate = new List(); + var gsID = new List(); using (var session = Dmo.NewSession()) { using (var reader = session.ExecuteReader(query)) @@ -85,69 +105,105 @@ namespace BWP.B3ClientService.Rpcs while (reader.Read()) { var id = (long)reader[0]; - var first = list.FirstOrDefault(x => x.ID == id); - if (first == null) + var rowVersion = (int)reader[1]; + if (!serverIDs.Contains(id)) + serverIDs.Add(id); + + MinClientGoodsSet first = null; + if (!local.Any(x => x.Item1 == id))//insert + { + first = insert.FirstOrDefault(x => x.ID == id); + if (first == null) + { + first = new MinClientGoodsSet(); + insert.Add(first); + first.ID = id; + first.RowVersion = rowVersion; + first.Name = (string)reader[2]; + } + } + else if (local.Any(x => x.Item1 == id && x.Item2 != rowVersion))//upate + { + first = update.FirstOrDefault(x => x.ID == id); + if (first == null) + { + first = new MinClientGoodsSet(); + update.Add(first); + first.ID = id; + first.RowVersion = rowVersion; + first.Name = (string)reader[2]; + } + } + var goodsID = (long)reader[4]; + if (first != null) + { + var d = new ClientGoodsSet_Detail(); + first.Details.Add(d); + d.ClientGoodsSet_ID = id; + d.ID = (long)reader[3]; + d.Goods_ID = goodsID; + d.StandardWeight = (decimal?)reader[5]; + d.StandardWeightUp = (decimal?)reader[6]; + d.StandardWeightLow = (decimal?)reader[7]; + } + + if (!gsID.Contains(goodsID)) + gsID.Add(goodsID); + + var gVersion = (int)reader[12]; + if (!g2.Any(x => x.Item1 == goodsID))//insert { - first = new MinClientGoodsSet(); - list.Add(first); - first.ID = id; - first.Stopped = (bool)reader[1]; - if (first.Stopped) - continue; - first.Name = (string)reader[2]; + if (!gInsert.Any(x => x.ID == goodsID)) + { + gInsert.Add(new MinGoods + { + ID = goodsID, + Name = (string)reader[8], + Code = (string)reader[9], + Spec = (string)reader[10], + MainUnit = (string)reader[11], + RowVersion = gVersion + }); + } } - var d = new ClientGoodsSet_Detail(); - first.Details.Add(d); - d.ClientGoodsSet_ID = id; - d.ID = (long)reader[3]; - d.Goods_ID = (long)reader[4]; - d.StandardWeight = (decimal?)reader[5]; - d.StandardWeightUp = (decimal?)reader[6]; - d.StandardWeightLow = (decimal?)reader[7]; - if (goodsList.Any(x => x.ID == d.Goods_ID)) - continue; - goodsList.Add(new MinGoods + else if (g2.Any(x => x.Item1 == goodsID && x.Item2 != gVersion))//upate { - ID = d.Goods_ID, - Name = (string)reader[8], - Code = (string)reader[9], - Spec = (string)reader[10], - MainUnit = (string)reader[11] - }); + if (!gUpdate.Any(x => x.ID == goodsID)) + { + gUpdate.Add(new MinGoods + { + ID = goodsID, + Name = (string)reader[8], + Code = (string)reader[9], + Spec = (string)reader[10], + MainUnit = (string)reader[11], + RowVersion = gVersion + }); + } + } } } } - var eFirst = list.FirstOrDefault(x => !x.Stopped); - if (eFirst != null) - eFirst.Goods.AddRange(goodsList); - return JsonConvert.SerializeObject(list); - } - [Rpc(RpcFlags.SkipAuth)] - public static string GetProductBatch(int range) - { - return GetProductBatchByType(range, 0); + var delete = local.Where(x => !serverIDs.Any(y => y == x.Item1)).Select(x => x.Item1); + var gDelete = g2.Where(x => !gsID.Any(y => y == x.Item1)).Select(x => x.Item1); + + var result = new Tuple, List, IEnumerable, List, List, IEnumerable>(insert, update, delete, gInsert, gUpdate, gDelete); + return JsonConvert.SerializeObject(result); } [Rpc(RpcFlags.SkipAuth)] - public static string GetProductBatchByType(int range, int type) + public static string GetProductBatchByType(int top, short type) { var query = new DQueryDom(new JoinAlias(typeof(ProductBatch))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("Name")); query.Columns.Add(DQSelectColumn.Field("Date")); - query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("BatchType", type), DQCondition.EQ("IsLocked", false), DQCondition.EQ("Stopped", false))); - query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true)); - query.Range = SelectRange.Top(range); - var list = query.EExecuteList().Select(x => new MinProductBatch { ID = x.Item1, Name = x.Item2, Date = x.Item3 }); - return JsonConvert.SerializeObject(list); - } - - - [Rpc(RpcFlags.SkipAuth)] - public static string SyncStore() - { - return GetBaseInfoJosn(); + query.Range = SelectRange.Top(top); + query.OrderBy.Expressions.Add(DQOrderByExpression.Create("Date", true)); + query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("BatchType", type), DQCondition.EQ("Stopped", false))); + var result= query.EExecuteList().Select(x => new { ID = x.Item1, Name = x.Item2, Date = x.Item3 }); + return JsonConvert.SerializeObject(result); } [Rpc(RpcFlags.SkipAuth)] @@ -167,12 +223,14 @@ namespace BWP.B3ClientService.Rpcs class MinBaseInfo { public long ID { get; set; } - public string Name { get; set; } + public int RowVersion { get; set; } } - class MinProductBatch : MinBaseInfo + class MinProductBatch { + public long ID { get; set; } + public string Name { get; set; } public DateTime? Date { get; set; } } @@ -183,25 +241,13 @@ namespace BWP.B3ClientService.Rpcs public string MainUnit { get; set; } } - class MinClientGoodsSet + class MinClientGoodsSet : MinBaseInfo { - public long ID { get; set; } - - public string Name { get; set; } - - public bool Stopped { get; set; } - List _details = new List(); public List Details { get { return _details; } } - - List _goods = new List(); - public List Goods - { - get { return _goods; } - } } } }