using ButcherFactory.BO.Bill; 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; namespace ButcherFactory.BO.LocalBL { public static class SegmentPickUpBL { const string RpcPath = @"/MainSystem/B3ClientService/Rpcs/SegmentPickUpRpc/"; public static BindingList GetSegmentPickUp(bool unSubmit) { var query = new DQueryDom(new JoinAlias(typeof(SegmentPickUp))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("RowIndex")); query.Columns.Add(DQSelectColumn.Field("BarCode")); query.Columns.Add(DQSelectColumn.Field("Goods_Name")); query.Columns.Add(DQSelectColumn.Field("Number")); query.Columns.Add(DQSelectColumn.Field("Weight")); query.Columns.Add(DQSelectColumn.Field("CreateTime")); query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID")); if (unSubmit) query.Where.Conditions.Add(DQCondition.EQ("Submited", false)); else { query.Where.Conditions.Add(DQCondition.Between("CreateTime", DateTime.Today, DateTime.Today + new TimeSpan(23, 59, 59))); query.Range = SelectRange.Top(20); } var list = new BindingList(); using (var session = DmoSession.New()) { using (var reader = session.ExecuteReader(query)) { while (reader.Read()) { var entity = new SegmentPickUp(); entity.ID = (long)reader[0]; entity.RowIndex = (int)reader[1]; entity.BarCode = (string)reader[2]; entity.Goods_Name = (string)reader[3]; entity.Number = (int)reader[4]; entity.Weight = (decimal?)reader[5]; if (unSubmit) entity.CreateTime = (DateTime)reader[6]; list.Add(entity); } } } return list; } public static void InsertEntityByCode(SegmentPickUp entity) { using (var session = DmoSession.New()) { if (CheckExist(entity.BarCode, session)) return; try { var json = RpcFacade.Call(RpcPath + "GetScanInfo", entity.BarCode); if (!string.IsNullOrEmpty(json)) { var obj = JsonConvert.DeserializeObject(json); entity.Goods_ID = obj.LongExt1.Value; entity.Goods_Name = obj.StringExt1; entity.Weight = obj.DecimalExt1; } } catch { #if DEBUG throw; #endif } entity.Number = 1; entity.UserID = AppContext.Worker.ID; session.Insert(entity); session.Commit(); } } static bool CheckExist(string barCode, IDmoSession session) { if (string.IsNullOrEmpty(barCode)) return false; var query = new DQueryDom(new JoinAlias(typeof(SegmentPickUp))); query.Columns.Add(DQSelectColumn.Create(DQExpression.Value(1), "c")); query.Where.Conditions.Add(DQCondition.EQ("BarCode", barCode)); return query.EExecuteScalar(session) != null; } public static void InsertEntityByWeight(SegmentPickUp entity) { using (var session = DmoSession.New()) { entity.Number = 1; entity.UserID = AppContext.Worker.ID; session.Insert(entity); session.Commit(); } } public static void UpdateNumber(long id, int number) { var update = new DQUpdateDom(typeof(SegmentPickUp)); update.Where.Conditions.Add(DQCondition.EQ("ID", id)); update.Columns.Add(new DQUpdateColumn("Number", number)); update.EExecute(); } public static void UpdateSubmit(IEnumerable ids) { var update = new DQUpdateDom(typeof(SegmentPickUp)); update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray())); update.Columns.Add(new DQUpdateColumn("Submited", true)); update.EExecute(); } public static List GetBeforeInfo(IEnumerable codeList) { try { var json = RpcFacade.Call(RpcPath + "GetBeforeInfo", codeList); var list = JsonConvert.DeserializeObject>(json); if (list.Any()) { using (var session = DmoSession.New()) { foreach (var item in list) SaveBeforeInfoInDB(item, session); session.Commit(); } } return list; } catch { #if DEBUG throw; #endif return new List(); } } private static void SaveBeforeInfoInDB(ExtensionObj obj, IDmoSession session) { var update = new DQUpdateDom(typeof(SegmentPickUp)); update.Where.Conditions.Add(DQCondition.EQ("BarCode", obj.StringExt1)); update.Columns.Add(new DQUpdateColumn("Goods_ID", obj.LongExt1)); update.Columns.Add(new DQUpdateColumn("Weight", obj.DecimalExt1)); session.ExecuteNonQuery(update); } public static void UploadSegmentInfo() { try { using (var session = DmoSession.New()) { var needUpload = GetUnSyncData(session); if (needUpload.Count == 0) return; var method = RpcPath + "UploadSegmentInfo"; var json = JsonConvert.SerializeObject(needUpload); RpcFacade.Call(method, 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(SegmentPickUp))); 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.Columns.Add(DQSelectColumn.Field("Number")); query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Submited", true), 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 SegmentPickUpObj(); obj.ID = (long)reader[0]; obj.RowVersion = (int)reader[1]; obj.BarCode = (string)reader[2]; obj.TakeOutWorker_ID = (long)reader[3]; obj.WorkUnit_ID = (long?)reader[4]; if (string.IsNullOrEmpty(obj.BarCode)) { obj.ProductBatch_ID = (long?)reader[5]; obj.Goods_ID = (long?)reader[6]; } obj.Weight = (decimal?)reader[7]; obj.Time = (DateTime)reader[8]; obj.Number = (int)reader[9]; upload.Add(obj); } } return upload; } static void SetLocalAsSyncd(SegmentPickUpObj obj, IDmoSession session) { var update = new DQUpdateDom(typeof(SegmentPickUp)); update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ID", obj.ID), DQCondition.EQ("RowVersion", obj.RowVersion))); update.Columns.Add(new DQUpdateColumn("Sync", true)); session.ExecuteNonQuery(update); } } class SegmentPickUpObj { [JsonIgnore] public long ID { get; set; } [JsonIgnore] public int RowVersion { get; set; } public string BarCode { get; set; } public long TakeOutWorker_ID { get; set; } public long? WorkUnit_ID { get; set; } public long? ProductBatch_ID { get; set; } public long? Goods_ID { get; set; } public decimal? Weight { get; set; } public DateTime? Time { get; set; } public int Number { get; set; } } }