From 38dcad5494d3b620b911302f209f32b587497636 Mon Sep 17 00:00:00 2001 From: yibo <361071264@qq.com> Date: Sat, 31 Mar 2018 17:38:39 +0800 Subject: [PATCH] someChange. --- ButcherFactory.BO/Base/SimpleBackObjs.cs | 15 + ButcherFactory.BO/Base/SyncBill.cs | 2 +- ButcherFactory.BO/BaseInfo/ClientGoodsSet.cs | 5 + ButcherFactory.BO/Bill/CarcassInStore.cs | 31 +- ButcherFactory.BO/ButcherFactory.BO.csproj | 3 + ButcherFactory.BO/LocalBL/CarcassInStoreBL.cs | 131 +++++++++ ButcherFactory.BO/Utils/Extensions.cs | 271 ++++++++++++++++++ 7 files changed, 455 insertions(+), 3 deletions(-) create mode 100644 ButcherFactory.BO/Base/SimpleBackObjs.cs create mode 100644 ButcherFactory.BO/LocalBL/CarcassInStoreBL.cs create mode 100644 ButcherFactory.BO/Utils/Extensions.cs diff --git a/ButcherFactory.BO/Base/SimpleBackObjs.cs b/ButcherFactory.BO/Base/SimpleBackObjs.cs new file mode 100644 index 0000000..84b40f4 --- /dev/null +++ b/ButcherFactory.BO/Base/SimpleBackObjs.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ButcherFactory.BO +{ + public class IDRowVersion + { + public long ID { get; set; } + + public int RowVersion { get; set; } + } +} diff --git a/ButcherFactory.BO/Base/SyncBill.cs b/ButcherFactory.BO/Base/SyncBill.cs index 71c2073..004b682 100644 --- a/ButcherFactory.BO/Base/SyncBill.cs +++ b/ButcherFactory.BO/Base/SyncBill.cs @@ -16,7 +16,7 @@ namespace ButcherFactory.BO public int RowVersion { get; set; } [DbColumn(DbType = SqlDbType.DateTime)] - public DateTime CreateTime { get; set; } + public DateTime? CreateTime { get; set; } public long UserID { get; set; } diff --git a/ButcherFactory.BO/BaseInfo/ClientGoodsSet.cs b/ButcherFactory.BO/BaseInfo/ClientGoodsSet.cs index 790eb69..357e83c 100644 --- a/ButcherFactory.BO/BaseInfo/ClientGoodsSet.cs +++ b/ButcherFactory.BO/BaseInfo/ClientGoodsSet.cs @@ -27,6 +27,11 @@ namespace ButcherFactory.BO public long Goods_ID { get; set; } + [NonDmoProperty] + [ReferenceTo(typeof(Goods), "Name")] + [Join("Goods_ID", "ID")] + public string Goods_Name { get; set; } + public decimal? StandardWeight { get; set; } public decimal? StandardWeightUp { get; set; } diff --git a/ButcherFactory.BO/Bill/CarcassInStore.cs b/ButcherFactory.BO/Bill/CarcassInStore.cs index 8dcc354..ac93939 100644 --- a/ButcherFactory.BO/Bill/CarcassInStore.cs +++ b/ButcherFactory.BO/Bill/CarcassInStore.cs @@ -10,11 +10,16 @@ namespace ButcherFactory.BO [MapToTable("Butcher_CarcassInStore")] public class CarcassInStore : SyncBill { + public CarcassInStore() + { + CreateTime = DateTime.Now; + } + public string BarCode { get; set; } - public long WorkUnit_ID { get; set; } + public long? WorkUnit_ID { get; set; } - public long ProductBatch_ID { get; set; } + public long? ProductBatch_ID { get; set; } public long Goods_ID { get; set; } @@ -24,8 +29,30 @@ namespace ButcherFactory.BO public decimal? BeforeWeight { get; set; } + [NonDmoProperty] + public decimal? Discont + { + get + { + if (Weight.HasValue && BeforeWeight.HasValue) + return BeforeWeight.Value - Weight.Value; + return null; + } + } + + public bool FromPad { get; set; } + [ReferenceTo(typeof(Goods), "Name")] [Join("Goods_ID", "ID")] public string Goods_Name { get; set; } } + + public class PadCarcassInStore + { + public long ID { get; set; } + public long Goods_ID { get; set; } + public string BarCode { get; set; } + public int RowVersion { get; set; } + + } } diff --git a/ButcherFactory.BO/ButcherFactory.BO.csproj b/ButcherFactory.BO/ButcherFactory.BO.csproj index 2bc77cb..2d34d71 100644 --- a/ButcherFactory.BO/ButcherFactory.BO.csproj +++ b/ButcherFactory.BO/ButcherFactory.BO.csproj @@ -59,6 +59,7 @@ + @@ -68,10 +69,12 @@ + + diff --git a/ButcherFactory.BO/LocalBL/CarcassInStoreBL.cs b/ButcherFactory.BO/LocalBL/CarcassInStoreBL.cs new file mode 100644 index 0000000..e973072 --- /dev/null +++ b/ButcherFactory.BO/LocalBL/CarcassInStoreBL.cs @@ -0,0 +1,131 @@ +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 }); + } + } +} diff --git a/ButcherFactory.BO/Utils/Extensions.cs b/ButcherFactory.BO/Utils/Extensions.cs new file mode 100644 index 0000000..35bafb8 --- /dev/null +++ b/ButcherFactory.BO/Utils/Extensions.cs @@ -0,0 +1,271 @@ +using ButcherFactory.BO.Utils; +using Forks.EnterpriseServices.DomainObjects2; +using Forks.EnterpriseServices.DomainObjects2.DQuery; +using Forks.Utils; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ButcherFactory.BO +{ + public static class Extensions + { + public static Tuple EExecuteScalar(this DQueryDom query, IDmoSession session) + { + var list = query.EExecuteList(session); + if (list.Count == 0) + { + return null; + } +#if DEBUG + if (list.Count > 1) + { + throw new Exception(string.Format("期望返回1条记录实际返回{0}条", list.Count)); + } +#endif + return list[0]; + } + + public static Tuple EExecuteScalar(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteScalar(session); + } + } + + public static Tuple EExecuteScalar(this DQueryDom query, IDmoSession session) + { + var list = query.EExecuteList(session); + if (list.Count == 0) + { + return null; + } +#if DEBUG + if (list.Count > 1) + { + throw new Exception(string.Format("期望返回1条记录实际返回{0}条", list.Count)); + } +#endif + return list[0]; + } + + public static Tuple EExecuteScalar(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteScalar(session); + } + } + + public static T EExecuteScalar(this DmoQuery query) where T : class + { + var result = query.EExecuteList(); + if (result.Count == 0) + { + return default(T); + } + if (result.Count > 1) + { + throw new Exception("ExecuteScalar返回多个数据"); + } + return (T)result[0]; + } + + public static List EExecuteList(this DQueryDom query, IDmoSession session) + { + var list = new List(); + using (var reader = session.ExecuteReader(query)) + { + while (reader.Read()) + { + list.Add((T)reader[0]); + } + } + return list; + } + + public static List EExecuteList(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteList(session); + } + } + + public static List EExecuteDmoList(this DQueryDom query, IDmoSession session, params string[] fields) where TDmo : new() + { + var type = typeof(TDmo); + PCheck.IsTrue(query.From.RootSource.Alias.DmoSource == DmoInfo.Get(type)); + + var dmoInfo = DmoInfo.Get(type); + if (fields.Length == 0) + { + + fields = dmoInfo.Fields + .Select((item) => item.Name).ToArray(); + } +#if DEBUG + else + { + foreach (var filed in fields) + { + if (!dmoInfo.Fields.Contains(filed)) + { + throw new Exception(string.Format("对象{0}上不存在字段{1}的定义", type.Name, filed)); + } + } + } +#endif + + if (query.Columns.Count == 0) + { + foreach (var filed in fields) + { + query.Columns.Add(DQSelectColumn.Field(filed)); + } + } + else if (query.Columns.Count != fields.Length) + { + throw new Exception("查询字段数量不匹配"); + } + + var indexSetDic = fields.ToDictionary((item) => Array.IndexOf(fields, item), (item) => type.GetProperty(item).GetSetMethod()); + + var list = new List(); + using (var reader = session.ExecuteReader(query)) + { + while (reader.Read()) + { + var dmo = new TDmo(); + list.Add(dmo); + foreach (var pair in indexSetDic) + { + pair.Value.Invoke(dmo, new object[] { reader[pair.Key] }); + } + } + } + return list; + } + + public static List EExecuteDmoList(this DQueryDom query, params string[] fields) where TDmo : new() + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteDmoList(session, fields); + } + } + + public static List> EExecuteList(this DQueryDom query, IDmoSession session) + { + var list = new List>(); + + using (var reader = session.ExecuteReader(query)) + { + while (reader.Read()) + { + var value = new Tuple( + (T1)reader[0], + (T2)reader[1]); + list.Add(value); + } + } + return list; + } + + public static List> EExecuteList(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteList(session); + } + } + + public static List> EExecuteList(this DQueryDom query, IDmoSession session) + { + var list = new List>(); + using (var reader = session.ExecuteReader(query)) + { + while (reader.Read()) + { + var value = new Tuple( + (T1)reader[0], + (T2)reader[1], + (T3)reader[2]); + list.Add(value); + } + } + return list; + } + + public static List> EExecuteList(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteList(session); + } + } + + public static List> EExecuteList(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteList(session); + } + } + + public static List> EExecuteList(this DQueryDom query, IDmoSession session) + { + var list = new List>(); + using (var reader = session.ExecuteReader(query)) + { + while (reader.Read()) + { + var value = new Tuple( + (T1)reader[0], + (T2)reader[1], + (T3)reader[2], (T4)reader[3]); + list.Add(value); + } + } + return list; + } + + + public static List> EExecuteList(this DQueryDom query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return query.EExecuteList(session); + } + } + + public static List> EExecuteList(this DQueryDom query, IDmoSession session) + { + var list = new List>(); + using (var reader = session.ExecuteReader(query)) + { + while (reader.Read()) + { + var value = new Tuple( + (T1)reader[0], + (T2)reader[1], + (T3)reader[2], (T4)reader[3], (T5)reader[4]); + list.Add(value); + } + } + return list; + } + + public static IList EExecuteList(this DmoQuery query) + { + using (var session = Dmo.NewSession(AppContext.ConnectInfo.SqlConnection)) + { + return session.ExecuteList(query); + } + } + } +}