using BWP.B3ClientService.BO; using Forks.EnterpriseServices.BusinessInterfaces; 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.Linq; using System.Text; using System.Threading; using TSingSoft.WebPluginFramework.TimerTasks; namespace BWP.B3ClientService.Tasks { public class CreateB3OutputTask : ITimerTask { private static volatile object _lockObj = new object(); public void Execute() { if (!Monitor.TryEnter(_lockObj)) { throw new SameTaskNotFinishException(this); } var serverUri = ServerHost.GetServerUrl(); if (string.IsNullOrEmpty(serverUri)) throw new Exception("请配置服务器地址"); try { RpcFacade.Init(serverUri, "B3ClientServer"); } catch (Exception ex) { if (ex.Message != "Can only start once") throw; } try { DoExecute(); } catch (Exception e) { throw new ApplicationException(e.ToString()); } finally { Monitor.Exit(_lockObj); } } string method = "/MainSystem/B3ButcherManage/Rpcs/OutputRpc/CreateOutput"; private void DoExecute() { using (var context = new TransactionContext()) { var list = GetUnSyncdList(context.Session); if (list.Count == 0) return; TransferToB3(list); SetAsSyncd(context.Session, list.Select(x => x.ID)); context.Commit(); } } void TransferToB3(List list) { var arr = list.Where(x => x.NeedUpload).GroupBy(x => new { x.Date, x.UserName, x.ProductBatch, x.Goods_Code }).Select(x => new { Date = x.Key.Date, UserName = x.Key.UserName, ProductBatch = x.Key.ProductBatch, Goods_Code = x.Key.Goods_Code, Weight = x.Sum(y => y.Weight), Number = x.Sum(y => y.Number) }); RpcFacade.Call(method, JsonConvert.SerializeObject(arr)); } private void SetAsSyncd(IDmoSession session, IEnumerable ids) { var update = new DQUpdateDom(typeof(SegmentProductionInfo)); update.Where.Conditions.Add(DQCondition.InList(DQExpression.Field("ID"), ids.Select(x => DQExpression.Value(x)).ToArray())); update.Columns.Add(new DQUpdateColumn("CreateOutput", true)); update.Columns.Add(new DQUpdateColumn("Added", DQExpression.LogicCase(DQCondition.EQ("Delete", true), DQExpression.Value(true), DQExpression.Value(false)))); session.ExecuteNonQuery(update); } private List GetUnSyncdList(IDmoSession session) { var query = new DQueryDom(new JoinAlias(typeof(SegmentProductionInfo))); query.Columns.Add(DQSelectColumn.Field("ID")); query.Columns.Add(DQSelectColumn.Field("ProductTime")); query.Columns.Add(DQSelectColumn.Field("Goods_Code")); query.Columns.Add(DQSelectColumn.Field("Worker_Name")); query.Columns.Add(DQSelectColumn.Field("ProductBatch_Name")); query.Columns.Add(DQSelectColumn.Field("Weight")); query.Columns.Add(DQSelectColumn.Field("Delete")); query.Columns.Add(DQSelectColumn.Field("Added")); query.Columns.Add(DQSelectColumn.Field("CreateOutput")); query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("Worker_ID")), DQCondition.IsNotNull(DQExpression.Field("ProductTime")))); query.Where.Conditions.Add(DQCondition.Or(DQCondition.EQ("CreateOutput", false), DQCondition.And(DQCondition.EQ("Delete", true), DQCondition.EQ("Added", false)))); query.Range = SelectRange.Top(100); var list = new List(); using (var reader = session.ExecuteReader(query)) { while (reader.Read()) { var entity = new Temp(); entity.Number = 1; entity.ID = (long)reader[0]; entity.Date = ((DateTime)reader[1]).Date; entity.Goods_Code = (string)reader[2]; entity.UserName = (string)reader[3]; entity.ProductBatch = (string)reader[4]; entity.Weight = (decimal)reader[5]; entity.Delete = (bool)reader[6]; entity.Added = (bool)reader[7]; entity.NeedUpload = true; if ((bool)reader[8]) { if (entity.Delete && !entity.Added) { entity.Weight = -1 * entity.Weight; entity.Number = -1 * entity.Number; } } else if (entity.Delete) entity.NeedUpload = false; list.Add(entity); } } return list; } public string Name { get { return "分割生产生成屠宰产出单"; } } } class Temp { public long ID { get; set; } public DateTime Date { get; set; } public string Goods_Code { get; set; } public string UserName { get; set; } public string ProductBatch { get; set; } public decimal Weight { get; set; } public int Number { get; set; } public bool Delete { get; set; } public bool Added { get; set; } public bool NeedUpload { get; set; } } }