using ButcherFactory.BO.Utils; using Forks.EnterpriseServices.DomainObjects2; using Forks.EnterpriseServices.DomainObjects2.DQuery; using Forks.EnterpriseServices.SqlDoms; using Forks.JsonRpc.Client; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using TSingSoft.WebPluginFramework; namespace ButcherFactory.BO.LocalBL { public static class CarcassInStoreBL { const string RpcPath = @"/MainSystem/B3ClientService/Rpcs/CarcassInStoreRpc/"; static Dictionary _goodsNames = new Dictionary(); public static List DoWithPadData(long? workUnitID, long? batchID) { try { var json = RpcFacade.Call(RpcPath + "GetUnSyncPadData"); var data = JsonConvert.DeserializeObject>(json); var list = new List(); if (data.Count == 0) return list; var goodsBarCode = data.Select(x => new Tuple(x.Goods_ID, x.BarCode)); list = InsertOrUpdate(workUnitID, batchID, goodsBarCode, true); var backInfo = data.Select(x => new ExtensionObj { LongExt1 = x.ID, DecimalExt1 = x.RowVersion }); RpcFacade.Call(RpcPath + "SetPadDataSync", JsonConvert.SerializeObject(backInfo)); return list; } catch { #if DEBUG throw; #endif return new List(); } } public static List InsertOrUpdate(long? workUnitID, long? batchID, long goodsID, string barCode) { var list = new List> { new Tuple(goodsID, barCode) }; return InsertOrUpdate(workUnitID, batchID, list, false); } static List InsertOrUpdate(long? workUnitID, long? batchID, IEnumerable> data, bool fromPad) { var list = new List(); using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { foreach (var item in data) { var id = GetExistWithUpdate(item, session); if (id.HasValue) { var exist = new CarcassInStore(); list.Add(exist); exist.ID = id.Value; exist.Goods_ID = item.Item1; exist.Goods_Name = GetGoodsName(item.Item1, session); } else { var entity = CreateCarcassInStore(workUnitID, batchID, item, fromPad); entity.Goods_Name = GetGoodsName(item.Item1, session); session.Insert(entity); list.Add(entity); } } session.Commit(); } return list; } static CarcassInStore CreateCarcassInStore(long? workUnitID, long? batchID, Tuple goodsCode, bool fromPad) { var entity = new CarcassInStore(); entity.FromPad = fromPad; entity.WorkUnit_ID = workUnitID; entity.ProductBatch_ID = batchID; entity.UserID = AppContext.Worker.ID; entity.BarCode = goodsCode.Item2; entity.Goods_ID = goodsCode.Item1; return entity; } static string GetGoodsName(long id, IDmoSession session) { if (!_goodsNames.ContainsKey(id)) { var query = new DQueryDom(new JoinAlias(typeof(Goods))); query.Columns.Add(DQSelectColumn.Field("Name")); query.Where.Conditions.Add(DQCondition.EQ("ID", id)); _goodsNames.Add(id, query.EExecuteScalar(session)); } return _goodsNames[id]; } static long? GetExistWithUpdate(Tuple goodsCode, IDmoSession session) { var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Where.Conditions.Add(DQCondition.EQ("BarCode", goodsCode.Item2)); var id = (long?)session.ExecuteScalar(query); if (id == null) return null; Update(id.Value, "Goods_ID", goodsCode.Item1, session); return id.Value; } static void Update(long id, string fileName, object value, IDmoSession session) { var update = new DQUpdateDom(typeof(CarcassInStore)); update.Where.Conditions.Add(DQCondition.EQ("ID", id)); update.Columns.Add(new DQUpdateColumn(fileName, value)); update.Columns.Add(new DQUpdateColumn("Sync", false)); update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1)))); session.ExecuteNonQuery(update); } public static void Update(long id, string fileName, object value) { using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { Update(id, fileName, value, session); session.Commit(); } } public static BindingList GetLocalDataWithState(bool history) { var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("BarCode")); query.Columns.Add(DQSelectColumn.Field("Goods_Name")); if (history) { query.Columns.Add(DQSelectColumn.Field("Weight")); query.Columns.Add(DQSelectColumn.Field("BeforeWeight")); query.Where.Conditions.Add(DQCondition.IsNotNull(DQExpression.Field("Weight"))); query.Range = SelectRange.Top(30); } else query.Where.Conditions.Add(DQCondition.IsNull(DQExpression.Field("Weight"))); query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true)); var result = new BindingList(); using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { using (var reader = session.ExecuteReader(query)) { while (reader.Read()) { var entity = new CarcassInStore(); result.Add(entity); entity.ID = (long)reader[0]; entity.BarCode = (string)reader[1]; entity.Goods_Name = (string)reader[2]; if (history) { entity.Weight = (decimal)reader[3]; entity.BeforeWeight = (decimal?)reader[4]; } } } } return result; } public static IEnumerable GetGoodsList() { var main = new JoinAlias(typeof(ClientGoodsSet)); var detail = new JoinAlias(typeof(ClientGoodsSet_Detail)); var query = new DQueryDom(main); query.From.AddJoin(JoinType.Inner, new DQDmoSource(detail), DQCondition.EQ(main, "ID", detail, "ClientGoodsSet_ID")); query.Columns.Add(DQSelectColumn.Field("Goods_ID", detail)); query.Columns.Add(DQSelectColumn.Field("Goods_Name", detail)); query.OrderBy.Expressions.Add(DQOrderByExpression.Create(detail, "Goods_ID")); var list = query.EExecuteList(); return list.Select(x => new ClientGoodsSet_Detail { Goods_ID = x.Item1, Goods_Name = x.Item2 }); } public static List GetBeforeWeight(IEnumerable codeList) { try { var json = RpcFacade.Call(RpcPath + "GetWeightInfo", codeList); var list = JsonConvert.DeserializeObject>(json); if (list.Any()) { using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { foreach (var item in list) SaveWeightInDB(item, session); session.Commit(); } } return list; } catch { #if DEBUG throw; #endif return new List(); } } static void SaveWeightInDB(ExtensionObj obj, IDmoSession session) { var update = new DQUpdateDom(typeof(CarcassInStore)); update.Where.Conditions.Add(DQCondition.EQ("BarCode", obj.StringExt1)); update.Columns.Add(new DQUpdateColumn("BeforeWeight", obj.DecimalExt1)); session.ExecuteNonQuery(update); } public static void UploadCarcassInfo() { try { using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { var needUpload = GetUnSyncData(session); if (needUpload.Count == 0) return; var json = JsonConvert.SerializeObject(needUpload); RpcFacade.Call(RpcPath + "UploadCarcassInfo", json); foreach (var item in needUpload) SetLocalAsSyncd(item, session); session.Commit(); } } catch { #if DEBUG throw; #endif } } static List GetUnSyncData(IDmoSession session) { var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("RowVersion")); query.Columns.Add(DQSelectColumn.Field("BarCode")); query.Columns.Add(DQSelectColumn.Field("UserID")); query.Columns.Add(DQSelectColumn.Field("WorkUnit_ID")); query.Columns.Add(DQSelectColumn.Field("ProductBatch_ID")); query.Columns.Add(DQSelectColumn.Field("Goods_ID")); query.Columns.Add(DQSelectColumn.Field("Weight")); query.Columns.Add(DQSelectColumn.Field("CreateTime")); query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("Weight")), DQCondition.EQ("Sync", false))); query.Range = SelectRange.Top(10); query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID")); var upload = new List(); using (var reader = session.ExecuteReader(query)) { while (reader.Read()) { var obj = new CarcassInStoreObj(); obj.ID = (long)reader[0]; obj.RowVersion = (int)reader[1]; obj.BarCode = (string)reader[2]; obj.InStoreWorker_ID = (long)reader[3]; obj.WorkUnit_ID = (long?)reader[4]; obj.ProductBatch_ID = (long?)reader[5]; obj.InStoreGoods_ID = (long)reader[6]; obj.InStoreWeight = (decimal)reader[7]; obj.InStoreTime = (DateTime)reader[8]; upload.Add(obj); } } return upload; } static void SetLocalAsSyncd(CarcassInStoreObj obj, IDmoSession session) { var update = new DQUpdateDom(typeof(CarcassInStore)); update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ID", obj.ID), DQCondition.EQ("RowVersion", obj.RowVersion))); update.Columns.Add(new DQUpdateColumn("Sync", true)); update.EExecute(); } } class CarcassInStoreObj { public long ID { get; set; } public int RowVersion { get; set; } public string BarCode { get; set; } public long? InStoreWorker_ID { get; set; } public long? WorkUnit_ID { get; set; } public long? ProductBatch_ID { get; set; } public long? InStoreGoods_ID { get; set; } public decimal? InStoreWeight { get; set; } public DateTime? InStoreTime { get; set; } } }