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 SegmentProductionBL { const string RpcPath = @"/MainSystem/B3ClientService/Rpcs/SegmentProductionRpc/"; const string ProductTaskRpc = @"/MainSystem/B3ButcherManage/Rpcs/ProductTaskRpc/GetProductTaskNumber"; public static BindingList GetListByState(bool submited) { var query = new DQueryDom(new JoinAlias(typeof(SegmentProduction))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("RowIndex")); query.Columns.Add(DQSelectColumn.Field("BarCode")); query.Columns.Add(DQSelectColumn.Field("Weight")); query.Columns.Add(DQSelectColumn.Field("Goods_Name")); query.Columns.Add(DQSelectColumn.Field("GroupID")); query.Columns.Add(DQSelectColumn.Field("TrunOutID")); query.Columns.Add(DQSelectColumn.Field("Goods_Spec")); query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("InStored", false), DQCondition.EQ("Delete", false), DQCondition.EQ("Submited", submited))); query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true)); if (submited) { query.Where.Conditions.Add(DQCondition.GreaterThanOrEqual("CreateTime", DateTime.Today)); 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 SegmentProduction(); entity.ID = (long)reader[0]; entity.RowIndex = (int?)reader[1]; entity.BarCode = (string)reader[2]; entity.Weight = (decimal)reader[3]; entity.Goods_Name = (string)reader[4]; entity.GroupID = (long?)reader[5]; entity.TrunOutID = (long?)reader[6]; entity.Goods_Spec = (string)reader[7]; entity.Submited = submited; list.Add(entity); } } } return list; } public static SegmentProduction Insert(long goodsID, decimal weight, long? workUnitID, long productBatchID, DateTime batchDate) { using (var session = DmoSession.New()) { var entity = new SegmentProduction(); entity.Goods_ID = goodsID; entity.Weight = weight; entity.UserID = AppContext.Worker.ID; entity.WorkUnit_ID = workUnitID; entity.ProductBatch_ID = productBatchID; entity.RowIndex = GenerateRowIndex(productBatchID, session); entity.BarCode = string.Format("260912011{0:yyyyMMdd}{1}{2:00000}", batchDate,AppContext.ConnectInfo.ClientCode, entity.RowIndex); session.Insert(entity); session.Commit(); return entity; } } public static SegmentProduction InsertAndSetGroupID(long goodsID, decimal weight, long? workUnitID, long productBatchID, DateTime batchDate,bool test) { using (var session = DmoSession.New()) { var entity = new SegmentProduction(); entity.Goods_ID = goodsID; entity.Weight = weight; entity.UserID = AppContext.Worker.ID; entity.WorkUnit_ID = workUnitID; entity.ProductBatch_ID = productBatchID; entity.RowIndex = GenerateRowIndex(productBatchID, session); entity.BarCode = string.Format("260912011{0:yyyyMMdd}{1}{2:00000}", batchDate, AppContext.ConnectInfo.ClientCode, entity.RowIndex); entity.Submited = true; if (!test) { session.Insert(entity); FillGroupIDAsID(session, entity.ID); session.Commit(); } return entity; } } static void FillGroupIDAsID(IDmoSession session, long id) { var update = new DQUpdateDom(typeof(SegmentProduction)); update.Where.Conditions.Add(DQCondition.EQ("ID", id)); update.Columns.Add(new DQUpdateColumn("GroupID", id)); session.ExecuteNonQuery(update); } static int GenerateRowIndex(long productBatchID, IDmoSession session) { var query = new DQueryDom(new JoinAlias("_main", typeof(SegmentProduction))); query.Columns.Add(DQSelectColumn.Max("RowIndex")); query.Where.Conditions.Add(DQCondition.EQ("ProductBatch_ID", productBatchID)); return (query.EExecuteScalar(session) ?? 0) + 1; } public static long SetListGroupID(IEnumerable ids) { using (var session = DmoSession.New()) { var groupID = ids.Max(); BatchUpdate(ids, session, new Tuple("GroupID", groupID)); session.Commit(); return groupID; } } static void BatchUpdate(IEnumerable ids, IDmoSession session, params Tuple[] keyValues) { var update = new DQUpdateDom(typeof(SegmentProduction)); update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray())); foreach (var item in keyValues) update.Columns.Add(new DQUpdateColumn(item.Item1, item.Item2)); 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 BatchUpdate(IEnumerable ids, params Tuple[] keyValues) { using (var session = DmoSession.New()) { BatchUpdate(ids, session, keyValues); session.Commit(); } } public static void TrunBack(IEnumerable ids) { using (var session = DmoSession.New()) { BatchUpdate(ids, session, new Tuple("TrunOutID", null)); session.Commit(); } } public static void Delete(long id) { var delete = new DQDeleteDom(typeof(SegmentProduction)); delete.Where.Conditions.Add(DQCondition.EQ("ID", id)); delete.EExecute(); } public static List GetInStoreState(List codeArr) { var json = RpcFacade.Call(RpcPath + "CheckInStored", JsonConvert.SerializeObject(codeArr)); return JsonConvert.DeserializeObject>(json); } public static void SetInStored(List ids) { var update = new DQUpdateDom(typeof(SegmentProduction)); update.Columns.Add(new DQUpdateColumn("InStored", true)); update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"),ids.Select(x=>DQExpression.Value(x)).ToArray())); update.EExecute(); } public static List GetProductTask(DateTime date, SegmentProduction detail) { TaskTemp task = null; try { var json = ButcherFactoryUtil.SecondUrlCall(ProductTaskRpc, date, detail.Goods_Code); task = JsonConvert.DeserializeObject(json); } catch { #if DEBUG throw; #endif task = new TaskTemp(); } var local = GetLocalProducted(detail); var list = new List(); list.Add(new ProductTask { Item = "重量", Need = task.Weight, Done = local.Weight }); list.Add(new ProductTask { Item = "数量", Need = task.Number, Done = local.Number }); return list; } private static TaskTemp GetLocalProducted(SegmentProduction detail) { var local = new TaskTemp(); var query = new DQueryDom(new JoinAlias(typeof(SegmentProduction))); query.Columns.Add(DQSelectColumn.Sum("Weight")); query.Columns.Add(DQSelectColumn.Count()); query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ProductBatch_ID", detail.ProductBatch_ID), DQCondition.EQ("Goods_ID", detail.Goods_ID))); using (var session = DmoSession.New()) { using (var reader = session.ExecuteReader(query)) { while (reader.Read()) { local.Weight = (decimal?)reader[0]; local.Number = Convert.ToDecimal(reader[1]); } } } return local; } public static void UploadSegmentInfo() { try { using (var session = DmoSession.New()) { var needUpload = GetUnSyncData(session); if (needUpload.Count == 0) return; var json = JsonConvert.SerializeObject(needUpload); RpcFacade.Call(RpcPath + "Insert", 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("_main", typeof(SegmentProduction))); 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.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 SegmentProductionMin(); obj.ID = (long)reader[0]; obj.RowVersion = (int)reader[1]; obj.BarCode = (string)reader[2]; obj.Worker_ID = (long)reader[3]; obj.WorkUnit_ID = (long?)reader[4]; obj.ProductBatch_ID = (long?)reader[5]; obj.Goods_ID = (long)reader[6]; obj.Weight = (decimal)reader[7]; obj.ProductTime = (DateTime)reader[8]; upload.Add(obj); } } return upload; } static void SetLocalAsSyncd(SegmentProductionMin obj, IDmoSession session) { var update = new DQUpdateDom(typeof(SegmentProduction)); 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 SegmentProductionMin { [JsonIgnore] public long ID { get; set; } [JsonIgnore] public long RowVersion { get; set; } public string BarCode { get; set; } public DateTime? ProductTime { get; set; } public long? Worker_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? InStoreTime { get; set; } } class TaskTemp { public decimal? Weight { get; set; } public decimal? Number { get; set; } } }