| @ -0,0 +1,132 @@ | |||||
| 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<Temp> list) | |||||
| { | |||||
| var arr = list.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.Count() | |||||
| }); | |||||
| RpcFacade.Call<int>(method, JsonConvert.SerializeObject(arr)); | |||||
| } | |||||
| private void SetAsSyncd(IDmoSession session, IEnumerable<long> 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)); | |||||
| session.ExecuteNonQuery(update); | |||||
| } | |||||
| private List<Temp> 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.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("CreateOutput", false), DQCondition.IsNotNull(DQExpression.Field("Worker_ID")), DQCondition.IsNotNull(DQExpression.Field("ProductTime")))); | |||||
| query.Range = SelectRange.Top(100); | |||||
| var list = new List<Temp>(); | |||||
| using (var reader = session.ExecuteReader(query)) | |||||
| { | |||||
| while (reader.Read()) | |||||
| { | |||||
| var entity = new Temp(); | |||||
| 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]; | |||||
| list.Add(entity); | |||||
| } | |||||
| } | |||||
| return list; | |||||
| } | |||||
| } | |||||
| 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; } | |||||
| } | |||||
| } | |||||