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/B3Sale/Rpcs/SaleOutStoreRpc/GetSaleOutStoreNumber"; 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.Columns.Add(DQSelectColumn.Field("StandardPic")); query.Columns.Add(DQSelectColumn.Field("Goods_ID")); 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.StandardPic = (bool)reader[8]; entity.Goods_ID = (long)reader[9]; entity.Submited = submited; list.Add(entity); } } } return list; } public static short? GetGoodsType(long goodsID) { var query = new DQueryDom(new JoinAlias(typeof(Goods))); query.Where.Conditions.Add(DQCondition.EQ("ID", goodsID)); query.Columns.Add(DQSelectColumn.Field("GoodsType")); return query.EExecuteScalar(); } 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); FillMsgID(session, entity); session.Commit(); return entity; } } static void FillMsgID(IDmoSession session, SegmentProduction item) { var update = new DQUpdateDom(typeof(SegmentProduction)); update.Where.Conditions.Add(DQCondition.EQ("ID", item.ID)); update.Columns.Add(new DQUpdateColumn("MsgID", string.Format("{0}_{1}", AppContext.ConnectInfo.ClientCode, item.ID))); session.ExecuteNonQuery(update); } public static SegmentProduction InsertAndSetGroupID(SegmentProduction entity, DateTime batchDate) { using (var session = DmoSession.New()) { entity.UserID = AppContext.Worker.ID; entity.RowIndex = GenerateRowIndex(entity.ProductBatch_ID, session); entity.BarCode = string.Format("260912011{0:yyyyMMdd}{1}{2:00000}", batchDate, AppContext.ConnectInfo.ClientCode, entity.RowIndex); entity.Submited = true; 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("MsgID", string.Format("{0}_{1}", AppContext.ConnectInfo.ClientCode, 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 List GetInStoreState(List codeArr) { try { var json = RpcFacade.Call(RpcPath + "CheckInStored", JsonConvert.SerializeObject(codeArr)); return JsonConvert.DeserializeObject>(json); } catch { #if DEBUG throw; #endif return new List(); } } 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("Delete", false), 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.Columns.Add(DQSelectColumn.Field("Delete")); query.Columns.Add(DQSelectColumn.Field("StandardPic")); query.Columns.Add(DQSelectColumn.Field("MsgID")); query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Submited", true), DQCondition.EQ("Sync", false), DQCondition.EQ("Delete", false))); //query.Where.Conditions.Add(DQCondition.LessThan("CreateTime", DateTime.Now.AddMinutes(-1))); 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]; obj.Delete = (bool)reader[9]; obj.StandardPic = (bool)reader[10]; obj.MsgID = (string)reader[11]; 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); } public static void SetAsDelete(long id, string barCode) { var canDelete = CheckCanDelete(barCode); if (canDelete == null) throw new Exception("网络中断,请稍后再试!"); else if (canDelete == false) throw new Exception("已入库,不允许删除!"); var update = new DQUpdateDom(typeof(SegmentProduction)); update.Where.Conditions.Add(DQCondition.EQ("ID", id)); update.Columns.Add(new DQUpdateColumn("Sync", true)); update.Columns.Add(new DQUpdateColumn("Delete", true)); update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1)))); update.EExecute(); } public static void Delete(SegmentProduction entity) { var update = new DQUpdateDom(typeof(SegmentProduction)); update.Where.Conditions.Add(DQCondition.EQ("ID", entity.ID)); update.Columns.Add(new DQUpdateColumn("Sync", true)); update.Columns.Add(new DQUpdateColumn("Delete", true)); update.Columns.Add(new DQUpdateColumn("RowVersion", DQExpression.Add(DQExpression.Field("RowVersion"), DQExpression.Value(1)))); update.EExecute(); var log = new SegmentLog(entity.BarCode, "删除"); log.Message = string.Format("存货名称:{0} 重量:{1:#0.###}", entity.Goods_Name, entity.Weight); InsertLog(log); } static bool? CheckCanDelete(string barCode) { try { var rst = RpcFacade.Call(RpcPath + "CheckInStored", JsonConvert.SerializeObject(new List { barCode })); return JsonConvert.DeserializeObject>(rst).Count == 0; } catch { return null; } } public static void DeleteBefore() { var delete = new DQDeleteDom(typeof(SegmentProduction)); delete.Where.Conditions.Add(DQCondition.LessThan("CreateTime", new DateTime(2018, 10, 15))); delete.EExecute(); var delete2 = new DQDeleteDom(typeof(SegmentLog)); delete2.Where.Conditions.Add(DQCondition.LessThan("Time", DateTime.Today.AddDays(-30))); delete2.EExecute(); } public static int GetUnSyncCount() { var query = new DQueryDom(new JoinAlias("_main", typeof(SegmentProduction))); query.Columns.Add(DQSelectColumn.Count()); query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("Submited", true), DQCondition.EQ("Sync", false))); return Convert.ToInt32(query.EExecuteScalar()); } public static void InsertLog(SegmentLog log) { using (var session = DmoSession.New()) { session.Insert(log); session.Commit(); } } public static IEnumerable GetLog(string type) { var query = new DmoQuery(typeof(SegmentLog)); query.Where.Conditions.Add(DQCondition.GreaterThan("Time", DateTime.Today.AddDays(-2))); query.OrderBy.Expressions.Add(DQOrderByExpression.Create("ID", true)); if (!string.IsNullOrEmpty(type)) query.Where.Conditions.Add(DQCondition.EQ("Action", type)); return query.EExecuteList().Cast(); } } 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; } public bool Delete { get; set; } public bool StandardPic { get; set; } public string MsgID { get; set; } } class TaskTemp { public decimal? Weight { get; set; } public decimal? Number { get; set; } } }