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/"; public static bool DoWithPadData(long? workUnitID, long? productBatch) { bool changed = false; var json = RpcFacade.Call(RpcPath + "GetUnSyncPadData"); var data = JsonConvert.DeserializeObject>(json).OrderBy(x => x.ID); using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { foreach (var item in data) { if (UpdateIfExist(item, session)) continue; changed = true; var entity = new CarcassInStore(); entity.FromPad = true; entity.WorkUnit_ID = workUnitID; entity.ProductBatch_ID = productBatch; entity.UserID = AppContext.Worker.ID; entity.BarCode = item.BarCode; entity.Goods_ID = item.Goods_ID; session.Insert(entity); } session.Commit(); } var backInfo = data.Select(x => new IDRowVersion { ID = x.ID, RowVersion = x.RowVersion }); RpcFacade.Call(RpcPath + "SetPadDataSync", JsonConvert.SerializeObject(backInfo)); return changed; } static bool UpdateIfExist(PadCarcassInStore data, IDmoSession session) { var query = new DQueryDom(new JoinAlias(typeof(CarcassInStore))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Where.Conditions.Add(DQCondition.EQ("BarCode", data.BarCode)); var id = (long?)session.ExecuteScalar(query); if (id == null) return false; Update(id.Value, "Goods_ID", data.Goods_ID, session); return true; } public 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 long Insert(CarcassInStore entity) { using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) { session.Insert(entity); session.Commit(); return entity.ID; } } public static BindingList GetLocalDataWithState(bool submit) { 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 (submit) { 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"))); 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 (submit) { 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 }); } } }