| @ -0,0 +1,150 @@ | |||
| using BWP.B3ClientService.BO; | |||
| using Forks.EnterpriseServices.DomainObjects2; | |||
| using Forks.EnterpriseServices.DomainObjects2.DQuery; | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using TSingSoft.WebPluginFramework.TimerTasks; | |||
| using TSingSoft.WebPluginFramework; | |||
| using Forks.EnterpriseServices.BusinessInterfaces; | |||
| using Forks.EnterpriseServices.SqlDoms; | |||
| using Newtonsoft.Json; | |||
| using Forks.JsonRpc.Client; | |||
| namespace BWP.B3ClientService.Tasks | |||
| { | |||
| public class UploadProductInStore : ITimerTask | |||
| { | |||
| void InitRpc(string uri) | |||
| { | |||
| try | |||
| { | |||
| RpcFacade.Init(uri, "B3ClientServer"); | |||
| } | |||
| catch (Exception ex) | |||
| { | |||
| if (ex.Message != "Can only start once") | |||
| throw; | |||
| } | |||
| } | |||
| public void Execute() | |||
| { | |||
| var serverUri = ServerHost.GetServerUrl(); | |||
| if (string.IsNullOrEmpty(serverUri)) | |||
| throw new Exception("请配置服务器地址"); | |||
| var need = GetNeedUploadBatchStore(); | |||
| if (need.Count == 0) | |||
| return; | |||
| InitRpc(serverUri); | |||
| var method = "/MainSystem/B3ButcherManage/Rpcs/ProductInStoreRpc/InsertInstore"; | |||
| foreach (var item in need) | |||
| { | |||
| using (var context = new TransactionContext()) | |||
| { | |||
| var arr = GetArrList(item.Item1, item.Item2); | |||
| var summary = new List<ProductInStoreJson>(); | |||
| foreach (var g in arr.GroupBy(x => new { x.ProductBatch, x.StoreCode, x.Goods_Code })) | |||
| { | |||
| var detail = new ProductInStoreJson(); | |||
| detail.ProductBatch = g.Key.ProductBatch; | |||
| detail.StoreCode = g.Key.StoreCode; | |||
| detail.Goods_Code = g.Key.Goods_Code; | |||
| detail.Number = g.Sum(x => x.Number ?? 0); | |||
| detail.SecondNumber = g.Sum(x => x.SecondNumber ?? 0); | |||
| summary.Add(detail); | |||
| } | |||
| RpcFacade.Call<int>(method, JsonConvert.SerializeObject(summary)); | |||
| foreach (var sy in arr) | |||
| UpdateAsSync(context.Session, sy); | |||
| context.Commit(); | |||
| } | |||
| } | |||
| } | |||
| List<Tuple<long, long>> GetNeedUploadBatchStore() | |||
| { | |||
| var query = new DQueryDom(new JoinAlias(typeof(SegmentProductionInfo))); | |||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.IsNotNull(DQExpression.Field("ProductBatch_ID")), DQCondition.IsNotNull(DQExpression.Field("Store_ID")), DQCondition.IsNotNull(DQExpression.Field("InStoreTime")), DQCondition.EQ("IsSync", false))); | |||
| query.Columns.Add(DQSelectColumn.Field("ProductBatch_ID")); | |||
| query.Columns.Add(DQSelectColumn.Field("Store_ID")); | |||
| query.GroupBy.Expressions.Add(DQExpression.Field("ProductBatch_ID")); | |||
| query.GroupBy.Expressions.Add(DQExpression.Field("Store_ID")); | |||
| return query.EExecuteList<long, long>(); | |||
| } | |||
| List<ProductInStoreJson> GetArrList(long batchID, long storeID) | |||
| { | |||
| var main = new JoinAlias(typeof(SegmentProductionInfo)); | |||
| var batch = new JoinAlias(typeof(ProductBatch)); | |||
| var store = new JoinAlias(typeof(Store)); | |||
| var goods = new JoinAlias(typeof(Goods)); | |||
| var query = new DQueryDom(main); | |||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(batch), DQCondition.EQ(main, "ProductBatch_ID", batch, "ID")); | |||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(store), DQCondition.EQ(main, "Store_ID", store, "ID")); | |||
| query.From.AddJoin(JoinType.Left, new DQDmoSource(goods), DQCondition.EQ(main, "Goods_ID", goods, "ID")); | |||
| query.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ProductBatch_ID", batchID), DQCondition.EQ("Store_ID", storeID), DQCondition.IsNotNull(DQExpression.Field("InStoreTime")))); | |||
| query.Columns.Add(DQSelectColumn.Field("Name", batch)); | |||
| query.Columns.Add(DQSelectColumn.Field("Code", store)); | |||
| query.Columns.Add(DQSelectColumn.Field("Code", goods)); | |||
| query.Columns.Add(DQSelectColumn.Field("Weight")); | |||
| query.Columns.Add(DQSelectColumn.Field("RowVersion")); | |||
| query.Columns.Add(DQSelectColumn.Field("ID")); | |||
| var list = new List<ProductInStoreJson>(); | |||
| using (var session = Dmo.NewSession()) | |||
| { | |||
| using (var reader = session.ExecuteReader(query)) | |||
| { | |||
| while (reader.Read()) | |||
| { | |||
| var detail = new ProductInStoreJson(); | |||
| detail.ProductBatch = (string)reader[0]; | |||
| detail.StoreCode = (string)reader[1]; | |||
| detail.Goods_Code = (string)reader[2]; | |||
| detail.Number = (decimal?)reader[3]; | |||
| detail.SecondNumber = 1; | |||
| detail.RowVersion = (int)reader[4]; | |||
| detail.ID = (long)reader[5]; | |||
| list.Add(detail); | |||
| } | |||
| } | |||
| } | |||
| return list; | |||
| } | |||
| void UpdateAsSync(IDmoSession session, ProductInStoreJson item) | |||
| { | |||
| var update = new DQUpdateDom(typeof(ProductInStoreJson)); | |||
| update.Where.Conditions.Add(DQCondition.And(DQCondition.EQ("ID", item.ID), DQCondition.EQ("RowVersion", item.RowVersion))); | |||
| update.Columns.Add(new DQUpdateColumn("IsSync", true)); | |||
| session.ExecuteNonQuery(update); | |||
| } | |||
| public string Name | |||
| { | |||
| get { return "上传成品入库"; } | |||
| } | |||
| class ProductInStoreJson | |||
| { | |||
| public string ProductBatch { get; set; } | |||
| public string StoreCode { get; set; } | |||
| public string Goods_Code { get; set; } | |||
| public decimal? Number { get; set; } | |||
| public decimal? SecondNumber { get; set; } | |||
| [JsonIgnore] | |||
| public int RowVersion { get; set; } | |||
| [JsonIgnore] | |||
| public long ID { get; set; } | |||
| } | |||
| } | |||
| } | |||