Exemplo n.º 1
0
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "开始上传压缩包,本地压缩包地址:" + context.UploadResponse.FormatPolicyZipFilePath
                });
                Task.Factory.StartNew(() =>//开始上传
                {
                    try
                    {
                        QunarUploadPolicyRequest qunarRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);

                        //context.UploadResponse.FormatPolicyZipFilePath // 读取压缩包并上传
                        HttpClient client = new HttpClient();

                        //client.DefaultRequestHeaders.enctype

                        byte[] ct               = File.ReadAllBytes(context.UploadResponse.FormatPolicyZipFilePath); ////@"D:\ND.Application\File\Qunar\ZipFile\2015\12\2\15\20151202030916.zip"
                        HttpContent con         = new ByteArrayContent(ct, 0, ct.Length);                            //, Encoding.UTF8, "multipart/form-data"
                        con.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");

                        var res = client.PostAsync(System.Configuration.ConfigurationManager.AppSettings["QunarUpLoadUrl"].ToString(), con).Result;
                        res.EnsureSuccessStatusCode();
                        string backContent = res.Content.ReadAsStringAsync().Result;
                        // OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg ="发送完成,"+ backContent, PurchaserType = PurchaserType.Qunar });
                    }
                    catch (Exception ex)
                    {
                        OnMiddlewareWorking(new EventMsg {
                            Status = RunStatus.Exception, Msg = "QunarUploadMiddleware:去哪儿上传政策失败," + JsonConvert.SerializeObject(ex), Exception = ex, PurchaserType = PurchaserType.Qunar
                        });
                    }
                });

                // qunarRequest.QunarUpLoadUrl
                //OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "上传成功!保存最后更新记录..." });
                //string timeAndId = context.UploadResponse.PolicyRec[context.Request.UploadType].LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + "|" + context.UploadResponse.PolicyRec[context.Request.UploadType].LastPolicyId.ToString();
                //string name = context.Request.UploadType == UploadType.FullUpload ? "Qunar\\QunarFullPolicyRecLog" : "Qunar\\QunarIncrementPolicyRecLog";
                //CoreHelper.SaveLastUpTimeAndId(timeAndId, name);
                //OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "保存最后更新记录成功!执行完毕!" });
                Next.Invoke(context);
            }
            catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Exception, Msg = "QunarUploadMiddleware:去哪儿上传政策失败", Exception = ex, PurchaserType = PurchaserType.Qunar
                });
                context.UploadResponse = new UploadPolicyResponse()
                {
                    ErrCode = ResultType.Failed, ErrMsg = "QunarUploadMiddleware:" + ex.Message, Excption = ex
                };
                return;
            }
        }
        /// <summary>
        /// 开始任务
        /// </summary>
        /// <param name="timeSpan"></param>
        /// <returns></returns>
        public EmptyResponse StartWork(object request)
        {
            QunarUploadPolicyRequest qunarRequest = CoreHelper.ChangeToChild <object, QunarUploadPolicyRequest>(request);
            var task = Task <UploadPolicyResponse> .Factory.StartNew(() => {
                IUploadPolicy upload = new QunarUpLoadPolicy();
                upload.OnWoking     += upload_OnWoking;
                return(upload.UpLoadIncrementPolicy(qunarRequest));
            }, ct.Token);

            Task.WaitAll(task);
            if (task.Result.ErrCode == PolicyService.Enums.ResultType.Failed)
            {
                return(new EmptyResponse {
                    ErrCode = PolicyService.Enums.ResultType.Failed, ErrMsg = task.Result.ErrMsg, Excption = task.Result.Excption
                });
            }
            return(new EmptyResponse {
                ErrCode = PolicyService.Enums.ResultType.Sucess, ErrMsg = ""
            });
        }
        public UploadPolicyResponse UpLoadIncrementPolicy(UpLoadPolicyRequest request, bool isTaskPolicy = true)
        {
            OnWoking(this, new EventMsg {
                Status = RunStatus.Normal, PurchaserType = PolicyService.Enums.PurchaserType.Qunar, Msg = "------------------------------Start------------------------------------"
            });
            QunarUploadPolicyRequest qunarIncrementRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(request);
            IHandlerBuilder          builder = new HandlerBuilder();

            if (isTaskPolicy == true)
            {
                builder.Use <QunarLoadIncrementalPolicyMiddleware>() //载入增量政策
                .Use <QunarFilterSplitMiddleware>()                  //过滤并拆分中间件
                .Use <QunarIncrementalDispatcherMiddleware>()        //增量分发政策中间件
                .Use <QunarFormatMiddleware>()                       //先格式化成去哪儿格式并保存xml文件
                .Use <PackageZipFileMiddleware>()                    //压缩成zip文件
                .Use <QunarUploadMiddleware>();                      //上传到去哪儿服务器
            }
            else//有现成的数据
            {
                builder.Use <QunarFilterRepeatUploadMiddleware>() //过滤不在上传列表的政策中间件
                .Use <QunarFilterSplitMiddleware>()               //过滤并拆分中间件
                .Use <QunarIncrementalDispatcherMiddleware>()     //增量分发政策中间件
                .Use <QunarFormatMiddleware>()                    //先格式化成去哪儿格式并保存xml文件
                .Use <PackageZipFileMiddleware>()                 //压缩成zip文件
                .Use <QunarUploadMiddleware>();                   //上传到去哪儿服务器
            }


            IHandlerContext context = new HandlerContext(request);
            IHandler        handler = new DefaultHandler(builder, ShowMsg);

            handler.Execute(context);
            OnWoking(this, new EventMsg {
                Status = RunStatus.Normal, PurchaserType = PolicyService.Enums.PurchaserType.Qunar, Msg = "-------------------------------End---------------------------------------"
            });
            return(context.UploadResponse);
        }
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                #region   全量政策
                QunarUploadPolicyRequest qunarRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);
                Policies     policy        = qunarRequest.PolicyDataOrgin.LastOrDefault();
                PolicyRecord LastPolicyRec = new PolicyRecord {
                    LastPolicyId = 0, LastUpdateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"))
                };
                context.UploadResponse.PolicyRec[UploadType.FullUpload] = LastPolicyRec;
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "收到全量政策" + qunarRequest.PolicyDataOrgin.Count + "条,开始分批上传"
                });
                context.SetRequest(qunarRequest);
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "当前全量政策为" + qunarRequest.PolicyDataOrgin.Count + "条,不用分批上传"
                });
                qunarRequest.PolicyData.Add(UploadTypeDetail.FullUpload, qunarRequest.PolicyDataOrgin);
                Next.Invoke(context);

                #endregion
            }
            catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg()
                {
                    Status = Enums.RunStatus.Exception, Msg = "QunarFullDispatcherMiddleware:" + ex.Message, Exception = ex, PurchaserType = Enums.PurchaserType.Qunar
                });
                context.UploadResponse = new UploadPolicyResponse()
                {
                    ErrCode = Enums.ResultType.Failed, ErrMsg = ex.Message, Excption = ex
                };
                return;
            }
        }
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "开始获取去哪儿增量更新包..."
                });
                QunarUploadPolicyRequest qunarIncrementRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);


                #region 查询增量政策
                SearchPolicyRequest request = new SearchPolicyRequest()
                {
                    CommisionMoney     = qunarIncrementRequest.CommisionMoney,
                    CommsionPoint      = qunarIncrementRequest.CommsionPoint,
                    IsUpload           = true,
                    OperName           = qunarIncrementRequest.OperName,
                    PageSize           = qunarIncrementRequest.PageSize,
                    PolicyType         = qunarIncrementRequest.PolicyType,
                    pType              = PurchaserType.Qunar,
                    SqlWhere           = qunarIncrementRequest.SqlWhere,
                    UType              = qunarIncrementRequest.UploadType,
                    IsSearchTotalCount = false,
                };
                #region 如果是实时上传则根据上传过的去哪儿记录过滤要上传的记录
                //if (qunarIncrementRequest.IsRealTimeUpload)
                //{
                //   // OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "开始获取实时更新时间..." });
                //    request.IsRealTimeUpload = true;//是否实时更新


                //    //SearchRealTimeUploadResponse realTimeRep = JsonConvert.DeserializeObject<SearchRealTimeUploadResponse>(CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SearchRealTimeUploadUrl"].ToString(), request));//获取实时上传的记录
                //    //if(realTimeRep.ErrCode == ResultType.Failed)
                //    //{
                //    //    return;
                //    //}
                //    //request.RealTimePolicyRec = realTimeRep.PolicyRec;
                //    //OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "获取到实时更新时间:" + realTimeRep.PolicyRec.LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") });
                //}
                #endregion
                string searchContent           = CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SearchPolicyUrl"].ToString(), request);
                string selectSql               = "";
                int    totalCount              = 0;
                SearchPolicyResponse rep       = JsonConvert.DeserializeObject <SearchPolicyResponse>(searchContent);//先查询一遍,获取要上传的政策
                PolicyRecord         policyRec = rep.LastPolicyRecord;
                context.UploadResponse.BeforePolicyRecord = policyRec;
                if (rep.lstPolicies.Count <= 0)
                {
                    OnMiddlewareWorking(new EventMsg {
                        Status = RunStatus.Normal, Msg = "暂时没有收到增量更新包"
                    });
                    return;
                }
                #endregion

                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "收到增量更新包:" + rep.lstPolicies.Count + "条,开始过滤..."
                });
                List <Policies> lstPoliciesOrgin = rep.lstPolicies;
                #region 如果是实时上传则根据上传过的去哪儿记录过滤要上传的记录
                //if (qunarIncrementRequest.IsRealTimeUpload)
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "获取上次更新时间:" + rep.LastPolicyRecord.LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") });
                //    //更新到的政策里面去筛选出已经上传的政策Id
                //  CompleteUploadPolicyResponse completePolicyRep = JsonConvert.DeserializeObject<CompleteUploadPolicyResponse>(CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SearchCompleteUploadUrl"].ToString(), request));
                //    //把没有上传的政策删除掉
                //  if (completePolicyRep.ErrCode == ResultType.Failed || completePolicyRep.CompleteUploadPolicyCollection.Count <= 0)
                //  {
                //      OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "该更新包不在上传去哪儿的上传列表中,不用更新" });
                //      return;
                //  }
                //  lstPoliciesOrgin = CoreHelper.ReserveHaveUploadPolicy(lstPoliciesOrgin, completePolicyRep.CompleteUploadPolicyCollection);
                //    if(lstPoliciesOrgin.Count <= 0)
                //    {
                //        OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "该更新包不在上传去哪儿的上传列表中,不用更新" });
                //        return;
                //    }
                //}
                #endregion
                selectSql = context.Request.IsPrintSql ? "\r\n去哪儿获取增量数据sql:" + selectSql + "\r\n" : "";

                if (lstPoliciesOrgin.Count <= 0)
                {
                    OnMiddlewareWorking(new EventMsg {
                        Status = RunStatus.Normal, Msg = selectSql + "暂时没有增量政策更新包!"
                    });
                    return;
                }
                qunarIncrementRequest.PolicyDataOrgin     = lstPoliciesOrgin;
                context.UploadResponse.BeforePolicyRecord = rep.LastPolicyRecord;
                context.SetRequest(qunarIncrementRequest);
                Next.Invoke(context);
                #region 旧代码
                //#region 过滤政策
                //bool isHaveAll = false;
                //List<Policies> lstFiltedPolicy = CoreHelper.FilterPolicy(qunarIncrementRequest.LstQunarCodes, lstPoliciesOrgin, ref isHaveAll);
                //if (lstFiltedPolicy.Count <= 0)
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "经过去哪儿三子码过滤后未筛选到要上传的政策" });
                //    return;
                //}
                //#endregion

                //OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "过滤完成,政策数量:" + lstFiltedPolicy.Count + "条,开始拆分..." });

                //#region 拆分政策
                //List<Policies> lstIncrementalPolicies = CoreHelper.SplitPolicy(request.pType, lstFiltedPolicy, qunarIncrementRequest.LstQunarCodes);
                //qunarIncrementRequest.UploadCount = lstIncrementalPolicies.Count;

                //selectSql = context.Request.IsPrintSql ? "\r\n去哪儿获取增量数据sql:" + selectSql + "\r\n" : "";

                //if (lstIncrementalPolicies.Count <= 0)
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = selectSql + "暂时没有增量政策更新包!" });
                //    return;
                //}
                //#endregion



                //OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = selectSql + "收到拆分政策" + lstIncrementalPolicies.Count.ToString() + "条增量政策更新包!开始上传..." });
                //Policies policy = lstIncrementalPolicies.LastOrDefault();
                //PolicyRecord lastPolicyRec = new PolicyRecord() { LastPolicyId = 0, LastUpdateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")) };
                //context.UploadResponse.PolicyRec[UploadType.Incremental] = lastPolicyRec;//保存上次更新记录

                //#region 判断是否分批并交由下个中间件处理
                //int upLoadCount = System.Configuration.ConfigurationManager.AppSettings["MaxUploadCount"] == null ? 1000 : int.Parse(System.Configuration.ConfigurationManager.AppSettings["MaxUploadCount"].ToString());
                //if (lstIncrementalPolicies.Count < upLoadCount)//小于10000,自动上传
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "当前增量政策为" + lstIncrementalPolicies.Count + "条,小于最高限制条数:" + upLoadCount + "条,不用分批上传" });
                //    List<Policies> lstAddPolicies = lstIncrementalPolicies.Where(x => x.DelDegree == 1).ToList();
                //    List<Policies> lstDelPolicies = lstIncrementalPolicies.Where(x => x.DelDegree == 0).ToList();
                //    qunarIncrementRequest.PolicyData.Add(UploadTypeDetail.IncrementalAdd, lstAddPolicies);
                //    qunarIncrementRequest.PolicyData.Add(UploadTypeDetail.IncrementalDelete, lstDelPolicies);
                //    context.SetRequest(qunarIncrementRequest);
                //    Next.Invoke(context);

                //}
                //else
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "当前增量政策为" + lstIncrementalPolicies.Count + "条,大于最高限制条数:" + upLoadCount + "条,开始分批上传.." });
                //    int index = 1;
                //    while (lstIncrementalPolicies.Count > 0)
                //    {
                //        List<Policies> lstPolicies = new List<Policies>();
                //        lstPolicies = lstIncrementalPolicies.Take(upLoadCount).ToList();//取一万条先上传
                //        qunarIncrementRequest.UploadCount = lstPolicies.Count;
                //        if (lstPolicies.Count > 0)
                //        {
                //            lstIncrementalPolicies.RemoveRange(0, lstPolicies.Count);
                //        }
                //        OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "收到增量政策数量:" + lstPolicies.Count + "条,开始第" + index + "次分批上传" });
                //        List<Policies> lstAddPolicies = lstIncrementalPolicies.Where(x => x.DelDegree == 1).ToList();
                //        List<Policies> lstDelPolicies = lstIncrementalPolicies.Where(x => x.DelDegree == 0).ToList();
                //        PolicyRecord rec = new PolicyRecord() { LastPolicyId = lstPolicies.LastOrDefault().Id, LastUpdateTime = lstPolicies.LastOrDefault().UpdateTime };
                //        context.UploadResponse.BeforePolicyRecord = rec;//每次都保留上回更新的记录
                //        qunarIncrementRequest.PolicyData.Remove(UploadTypeDetail.IncrementalAdd);//先移除后添加,防止key冲突
                //        qunarIncrementRequest.PolicyData.Remove(UploadTypeDetail.IncrementalDelete);
                //        qunarIncrementRequest.PolicyData.Add(UploadTypeDetail.IncrementalAdd, lstAddPolicies);
                //        qunarIncrementRequest.PolicyData.Add(UploadTypeDetail.IncrementalDelete, lstDelPolicies);
                //        context.SetRequest(qunarIncrementRequest);
                //        Next.Invoke(context);
                //        index++;
                //    }
                //}
                //#endregion
                #endregion
            }
            catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg()
                {
                    Status = ND.PolicyService.Enums.RunStatus.Exception, Msg = "QunarLoadIncrementalPolicyMiddleware:" + ex.Message, Exception = ex, PurchaserType = ND.PolicyService.Enums.PurchaserType.Qunar
                });
                context.UploadResponse = new UploadPolicyResponse()
                {
                    ErrCode = ND.PolicyService.Enums.ResultType.Failed, ErrMsg = ex.Message, Excption = ex
                };
                return;
            }
        }
Exemplo n.º 6
0
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                #region 过滤政策
                QunarUploadPolicyRequest qunarIncrementRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);

                bool            isHaveAll       = false;
                List <Policies> lstFiltedPolicy = CoreHelper.FilterPolicy(qunarIncrementRequest.LstQunarCodes, qunarIncrementRequest.PolicyDataOrgin, ref isHaveAll);
                if (lstFiltedPolicy.Count <= 0)
                {
                    OnMiddlewareWorking(new EventMsg {
                        Status = RunStatus.Normal, Msg = "经过去哪儿三子码过滤后未筛选到要上传的政策"
                    });
                    context.UploadResponse = new UploadPolicyResponse()
                    {
                        ErrCode = Enums.ResultType.Sucess
                    };
                    return;
                }
                #endregion

                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "过滤完成,政策数量:" + lstFiltedPolicy.Count + "条,开始拆分..."
                });

                #region 拆分政策
                PoliciesType    policyType             = (PoliciesType)Enum.Parse(typeof(PoliciesType), qunarIncrementRequest.PolicyType.ToString());
                List <Policies> lstIncrementalPolicies = new List <Policies>();
                List <Policies> lstValidPolicies       = lstFiltedPolicy.Where(x => x.DelDegree == 1).ToList();
                List <Policies> lstNoValidPolicies     = lstFiltedPolicy.Where(x => x.DelDegree == 0).ToList();
                lstIncrementalPolicies.AddRange(lstNoValidPolicies);
                if (lstValidPolicies.Count > 0)
                {
                    lstValidPolicies = CoreHelper.SplitPolicy(PurchaserType.Qunar, lstValidPolicies, qunarIncrementRequest.LstQunarCodes, policyType);
                    lstIncrementalPolicies.AddRange(lstValidPolicies);
                }
                qunarIncrementRequest.UploadCount = lstIncrementalPolicies.Count;
                #endregion

                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "收到拆分政策" + lstIncrementalPolicies.Count.ToString() + "条政策包!开始上传..."
                });
                Policies policy = lstIncrementalPolicies.LastOrDefault();
                qunarIncrementRequest.PolicyDataOrgin = lstIncrementalPolicies;
                context.SetRequest(qunarIncrementRequest);
                Next.Invoke(context);
            }
            catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg()
                {
                    Status = Enums.RunStatus.Exception, Msg = "QunarFilterSplitMiddleware:" + ex.Message, Exception = ex, PurchaserType = Enums.PurchaserType.Qunar
                });
                context.UploadResponse = new UploadPolicyResponse()
                {
                    ErrCode = ResultType.Failed, ErrMsg = "QunarFilterSplitMiddleware:" + ex.Message, Excption = ex
                };
                return;
            }
        }
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                QunarUploadPolicyRequest qunarRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);

                Dictionary <UploadTypeDetail, PolicyRecord> dicRec = new Dictionary <UploadTypeDetail, PolicyRecord>();

                List <SeatDiscountDto> seatDiscountList = new List <SeatDiscountDto>();
                if (qunarRequest.PolicyType != QunarPolicyType.COMMON)
                {
                    SeatDiscountListResponse seatRep = JsonConvert.DeserializeObject <SeatDiscountListResponse>(CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SeatDiscountListUrl"], ""));//获取舱位折扣列表
                    seatDiscountList = seatRep.SeatDiscountList;
                }
                Dictionary <string, string> lstUploadedId = new Dictionary <string, string>();
                #region  择要封装哪个节点
                foreach (KeyValuePair <UploadTypeDetail, List <Policies> > item in qunarRequest.PolicyData)
                {
                    if (item.Value.Count > 0)
                    {
                        Policies ps = item.Value.LastOrDefault();
                        //if (item.Key != UploadTypeDetail.IncrementalDelete)
                        //{

                        item.Value.ForEach(x =>
                        {
                            if (!lstUploadedId.ContainsKey(x.Id.ToString()))
                            {
                                lstUploadedId.Add(x.Id.ToString(), x.PartnerPolicyId);
                            }
                        });
                        // }
                        dicRec.Add(item.Key, new PolicyRecord {
                            LastPolicyId = ps.Id, LastUpdateTime = ps.UpdateTime
                        });
                        DivideTask(item.Key, item.Value, qunarRequest, seatDiscountList, ChangeToQunarAddPolicy, ChangeToQunarDelPolicy);//开始多任务处理
                    }
                }
                #endregion
                string logPath = qunarRequest.FormatFilePath + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" + DateTime.Now.Hour + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xml";
                string zipPath = qunarRequest.FormatZipFilePath + "\\" + DateTime.Now.Year + "\\" + DateTime.Now.Month + "\\" + DateTime.Now.Day + "\\" + DateTime.Now.Hour + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip";

                if (qunarAddPolicy.ToList().Count <= 0 && qunarDelPolicy.ToList().Count <= 0)
                {
                    OnMiddlewareWorking(new EventMsg {
                        Status = RunStatus.Normal, Msg = "经过去哪儿格式化组件筛选完后没有要上传的政策"
                    });
                    return;
                }

                #region 保存上传记录
                PolicyNotifyRequest notifyRequest = new PolicyNotifyRequest();
                SaveNotifyResponse  notifyRes     = new SaveNotifyResponse();

                notifyRequest = new PolicyNotifyRequest()
                {
                    // DicRec = dicRec,
                    UploadType      = qunarRequest.UploadType,
                    PolicyRec       = context.UploadResponse.PolicyRec[qunarRequest.UploadType],//最后一条政策记录
                    Purchaser       = PurchaserType.Qunar,
                    NotifyResult    = 0,
                    FileNamePath    = zipPath,
                    ResponseParams  = "",
                    RequestParams   = qunarRequest.SqlWhere,
                    Remark          = IPAddressHelper.GetInternalIP(),
                    OperName        = qunarRequest.OperName,
                    UploadCount     = qunarRequest.UploadCount,
                    BeforePolicyRec = context.UploadResponse.BeforePolicyRecord == null ? new PolicyRecord() : context.UploadResponse.BeforePolicyRecord,
                    CommisionMoney  = qunarRequest.CommisionMoney,
                    CommisionPoint  = qunarRequest.CommsionPoint,
                    UploadPolicyIds = lstUploadedId,
                    PolicyType      = (PoliciesType)Enum.Parse(typeof(PoliciesType), qunarRequest.PolicyType.ToString())
                };
                string notifyContent = CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SavePolicyNotifyUrl"].ToString(), notifyRequest);
                notifyRes = JsonConvert.DeserializeObject <SaveNotifyResponse>(notifyContent);//先查询一遍,获取要上传的政策
                if (notifyRes.ErrCode == ResultType.Failed)
                {
                    context.UploadResponse = new UploadPolicyResponse {
                        ErrCode = PolicyService.Enums.ResultType.Failed, ErrMsg = "保存上传记录失败"
                    };
                    return;
                }
                #endregion

                #region 封装去哪儿政策类
                QunarPolicy policyBase = new QunarPolicy()
                {
                    username     = ConfigurationManager.AppSettings["QunarUsername"].ToString(),
                    password     = ConfigurationManager.AppSettings["QunarPassword"].ToString(),
                    type         = qunarRequest.PolicyType,
                    execType     = qunarRequest.UploadType == UploadType.FullUpload ? ExecType.FULL : ExecType.ADD,
                    addPolicy    = qunarAddPolicy.ToList(),                              //添加政策节点
                    deletePolicy = qunarDelPolicy.ToList(),                              //删除政策节点
                    ext          = JsonConvert.SerializeObject(notifyRes.UploadStatusId) //保存xml文件路径
                };
                #endregion

                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "封装去哪儿政策类成功,扩展信息:" + JsonConvert.SerializeObject(notifyRes.UploadStatusId) + ",开始序列化成功xml..."
                });
                string xmlContent = XmlHelper.Serializer(typeof(QunarPolicy), policyBase); //序列化成xml
                CoreHelper.CreateFile(logPath, xmlContent);                                //创建文件
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "序列化xml并创建文件成功,生成的xml文件路径:" + logPath + ",开始压缩并上传..."
                });
                context.UploadResponse.FormatPolicyFilePath    = logPath;//保存xml文件路径
                context.UploadResponse.FormatPolicyZipFilePath = zipPath;
                context.UploadResponse.UploadStatusId          = notifyRes.UploadStatusId;
                Next.Invoke(context);
            }catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Exception, Msg = "格式化去哪儿数据失败!" + JsonConvert.SerializeObject(ex)
                });
                context.UploadResponse = new UploadPolicyResponse {
                    ErrCode = PolicyService.Enums.ResultType.Failed, ErrMsg = "格式化去哪儿数据失败!", Excption = ex
                };
                return;
            }
        }
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                QunarUploadPolicyRequest qunarRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);

                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "开始获取去哪儿全量更新包..."
                });
                //PolicySyncRecLib syncLib = new PolicySyncRecLib();
                //PolicyRecord policyRec = CoreHelper.GetLastUpTimeAndId("Qunar\\QunarFullPolicyRecLog");//去哪儿全量选择日志
                #region 查询政策
                SearchPolicyRequest request = new SearchPolicyRequest()
                {
                    CommisionMoney     = qunarRequest.CommisionMoney,
                    CommsionPoint      = qunarRequest.CommsionPoint,
                    IsUpload           = true,
                    OperName           = qunarRequest.OperName,
                    PageSize           = qunarRequest.PageSize,
                    PolicyType         = qunarRequest.PolicyType,
                    pType              = PurchaserType.Qunar,
                    SqlWhere           = qunarRequest.SqlWhere,
                    UType              = qunarRequest.UploadType,
                    IsSearchTotalCount = false
                };
                string searchContent           = CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SearchPolicyUrl"].ToString(), request);
                SearchPolicyResponse rep       = JsonConvert.DeserializeObject <SearchPolicyResponse>(searchContent);//先查询一遍,获取要上传的政策
                PolicyRecord         policyRec = rep.LastPolicyRecord;
                context.UploadResponse.BeforePolicyRecord = policyRec;
                OnMiddlewareWorking(new EventMsg {
                    Status = RunStatus.Normal, Msg = "载入上次更新记录,更新时间:" + policyRec.LastUpdateTime.ToString("yyyy-MM-dd HH:mm:ss.fff") + ",上次最后一条更新id:" + policyRec.LastPolicyId.ToString() + ",收到全量政策包:" + rep.lstPolicies.Count + "条"
                });
                qunarRequest.PolicyDataOrgin = rep.lstPolicies;
                context.SetRequest(qunarRequest);
                Next.Invoke(context);
                #endregion
                #region 注释
                //bool isHaveAll = false;
                ////过滤政策
                //#region 过滤政策
                //int count = rep.lstPolicies == null ? 0 : rep.lstPolicies.Count;
                //OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "查询到政策数量:" + count + " 条,开始过滤..." });
                //List<Policies> lstFiltedPolicy = CoreHelper.FilterPolicy(qunarRequest.LstQunarCodes, rep.lstPolicies, ref isHaveAll);
                //if (lstFiltedPolicy.Count <= 0)
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "经过去哪儿三子码过滤后未筛选到要上传的政策" });
                //    return;
                //}
                ////if (isHaveAll)
                ////{
                ////  return;
                ////DialogResult dr = MessageBox.Show("当前上传的政策有全国对全国的政策,是否继续上传", "提示", MessageBoxButtons.OKCancel);
                ////if (dr == DialogResult.Cancel)
                ////{
                ////    return;
                ////}
                ////}
                //#endregion
                //List<Policies> lstAddPolicies = CoreHelper.SplitPolicy(request.pType, lstFiltedPolicy, qunarRequest.LstQunarCodes);//拆分政策
                //qunarRequest.UploadCount = lstAddPolicies.Count;
                //context.UploadResponse.BeforePolicyRecord = policyRec;
                //// List<Policies> lstAddPolicies = syncLib.LoadPolicy(context.Request.PageSize, policyRec, context.Request.SqlWhere, PurchaserType.Qunar, ref selectSql,ref totalCount, UploadType.FullUpload, context.Request.CommisionMoney, context.Request.CommsionPoint);//获取要上传的政策
                //// List<Policies> lstAddPolicies = syncLib.LoadPolicy(policyRec, context.Request.CommisionMoney, context.Request.CommsionPoint, context.Request.PageSize, context.Request.SqlWhere, ref selectSql, ref totalCount, UploadType.FullUpload);//获取要上传的政策


                ////if(!context.Request.IsPrintSql)
                ////{
                ////    selectSql = "";
                ////}
                ////selectSql = context.Request.IsPrintSql ? "\r\n筛选sql为:\r\n" + selectSql+"\r\n" : "";
                //if (lstAddPolicies.Count <= 0)
                //{
                //    OnMiddlewareWorking(new EventMsg { Status = RunStatus.Normal, Msg = "未筛选到要上传的政策,或者筛选的政策已经上传!" });
                //    context.UploadResponse = new UploadPolicyResponse() { ErrCode = ResultType.Failed, ErrMsg = "未筛选到要上传的政策,或者筛选的政策已经上传!" };
                //    return;
                //}
                #endregion
            }
            catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg()
                {
                    Status = ND.PolicyService.Enums.RunStatus.Exception, Msg = "QunarLoadFullPolicyMiddleware:" + ex.Message, Exception = ex, PurchaserType = ND.PolicyService.Enums.PurchaserType.Qunar
                });
                context.UploadResponse = new UploadPolicyResponse()
                {
                    ErrCode = ND.PolicyService.Enums.ResultType.Failed, ErrMsg = ex.Message, Excption = ex
                };
                return;
            }
        }
Exemplo n.º 9
0
 public override void Invoke(IHandlerContext context)
 {
     try
     {
         QunarUploadPolicyRequest qunarIncrementRequest = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);
         //获取所有在去哪儿上传过的政策
         string responseContent = CoreHelper.DoPost(System.Configuration.ConfigurationManager.AppSettings["SearchCompleteUploadPolicyUrl"].ToString(), null);
         if (string.IsNullOrEmpty(responseContent))
         {
             OnMiddlewareWorking(new EventMsg()
             {
                 PurchaserType = Enums.PurchaserType.Qunar, Status = Enums.RunStatus.Normal, Msg = "暂未上传过任何政策,不用更新!"
             });
             context.UploadResponse = new UploadPolicyResponse()
             {
                 ErrCode = Enums.ResultType.Sucess
             };
             return;
         }
         CompleteUploadPolicyResponse rep = JsonConvert.DeserializeObject <CompleteUploadPolicyResponse>(responseContent);
         if (rep.ErrCode == Enums.ResultType.Failed || rep.CompleteUploadPolicyCollection == null || rep.CompleteUploadPolicyCollection.Count <= 0)
         {
             OnMiddlewareWorking(new EventMsg()
             {
                 Status = Enums.RunStatus.Normal, Msg = "暂未上传过任何政策,不用更新!"
             });
             context.UploadResponse = new UploadPolicyResponse()
             {
                 ErrCode = Enums.ResultType.Sucess
             };
             return;
         }
         Dictionary <QunarPolicyType, List <Policies> > dic = CoreHelper.ReserveHaveUploadPolicy(qunarIncrementRequest.PolicyDataOrgin, rep.CompleteUploadPolicyCollection);
         if (dic.Count <= 0)
         {
             OnMiddlewareWorking(new EventMsg()
             {
                 Status = Enums.RunStatus.Normal, Msg = "此次更新包不在上传政策列表中,不用更新!"
             });
             context.UploadResponse = new UploadPolicyResponse()
             {
                 ErrCode = Enums.ResultType.Sucess
             };
             return;
         }
         foreach (KeyValuePair <QunarPolicyType, List <Policies> > item in dic)//分批上传不同类型的政策
         {
             qunarIncrementRequest.PolicyType      = item.Key;
             qunarIncrementRequest.PolicyDataOrgin = item.Value;
             context.SetRequest(qunarIncrementRequest);
             Next.Invoke(context);
         }
     }
     catch (Exception ex)
     {
         OnMiddlewareWorking(new EventMsg()
         {
             Status = Enums.RunStatus.Exception, Msg = "QunarFilterRepeatUploadMiddleware:" + ex.Message, Exception = ex, PurchaserType = Enums.PurchaserType.Qunar
         });
         context.UploadResponse = new UploadPolicyResponse()
         {
             ErrCode = Enums.ResultType.Failed, ErrMsg = ex.Message, Excption = ex
         };
         return;
     }
 }
        public override void Invoke(IHandlerContext context)
        {
            try
            {
                QunarUploadPolicyRequest qunarRequest  = CoreHelper.ChangeToChild <UpLoadPolicyRequest, QunarUploadPolicyRequest>(context.Request);
                PolicyRecord             lastPolicyRec = new PolicyRecord()
                {
                    LastPolicyId = 0, LastUpdateTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"))
                };
                context.UploadResponse.PolicyRec[UploadType.Incremental] = lastPolicyRec;//保存上次更新记录
                #region 判断是否分批并交由下个中间件处理
                int upLoadCount = System.Configuration.ConfigurationManager.AppSettings["MaxUploadCount"] == null ? 1000 : int.Parse(System.Configuration.ConfigurationManager.AppSettings["MaxUploadCount"].ToString());
                if (qunarRequest.PolicyDataOrgin.Count < upLoadCount)//小于10000,自动上传
                {
                    OnMiddlewareWorking(new EventMsg {
                        Status = RunStatus.Normal, Msg = "当前政策为" + qunarRequest.PolicyDataOrgin.Count + "条,小于最高限制条数:" + upLoadCount + "条,不用分批上传"
                    });
                    List <Policies> lstAddPolicies = qunarRequest.PolicyDataOrgin.Where(x => x.DelDegree == 1).ToList();
                    List <Policies> lstDelPolicies = qunarRequest.PolicyDataOrgin.Where(x => x.DelDegree == 0).ToList();
                    qunarRequest.PolicyData.Add(UploadTypeDetail.IncrementalAdd, lstAddPolicies);
                    qunarRequest.PolicyData.Add(UploadTypeDetail.IncrementalDelete, lstDelPolicies);
                    context.SetRequest(qunarRequest);
                    Next.Invoke(context);
                }
                else
                {
                    OnMiddlewareWorking(new EventMsg {
                        Status = RunStatus.Normal, Msg = "当前政策为" + qunarRequest.PolicyDataOrgin.Count + "条,大于最高限制条数:" + upLoadCount + "条,开始分批上传.."
                    });
                    int index = 1;
                    while (qunarRequest.PolicyDataOrgin.Count > 0)
                    {
                        List <Policies> lstPolicies = new List <Policies>();
                        lstPolicies = qunarRequest.PolicyDataOrgin.Take(upLoadCount).ToList();//取一万条先上传
                        qunarRequest.UploadCount = lstPolicies.Count;
                        if (lstPolicies.Count > 0)
                        {
                            qunarRequest.PolicyDataOrgin.RemoveRange(0, lstPolicies.Count);
                        }
                        OnMiddlewareWorking(new EventMsg {
                            Status = RunStatus.Normal, Msg = "收到政策数量:" + lstPolicies.Count + "条,开始第" + index + "次分批上传"
                        });
                        List <Policies> lstAddPolicies = lstPolicies.Where(x => x.DelDegree == 1).ToList();
                        List <Policies> lstDelPolicies = lstPolicies.Where(x => x.DelDegree == 0).ToList();
                        PolicyRecord    rec            = new PolicyRecord()
                        {
                            LastPolicyId = lstPolicies.LastOrDefault().Id, LastUpdateTime = lstPolicies.LastOrDefault().UpdateTime
                        };
                        context.UploadResponse.BeforePolicyRecord = rec;                 //每次都保留上回更新的记录
                        qunarRequest.PolicyData.Remove(UploadTypeDetail.IncrementalAdd); //先移除后添加,防止key冲突
                        qunarRequest.PolicyData.Remove(UploadTypeDetail.IncrementalDelete);
                        qunarRequest.PolicyData.Add(UploadTypeDetail.IncrementalAdd, lstAddPolicies);
                        qunarRequest.PolicyData.Add(UploadTypeDetail.IncrementalDelete, lstDelPolicies);
                        context.SetRequest(qunarRequest);
                        if (index <= 1)
                        {
                            Next.Invoke(context);
                        }
                        else
                        {
                            //先判断是否上传成功
                            OnMiddlewareWorking(new EventMsg {
                                Status = RunStatus.Normal, Msg = "查询是否上传成功..."
                            });
                            bool flag = SearchNotifyStatus(context.UploadResponse.UploadStatusId);
                            if (flag)
                            {
                                OnMiddlewareWorking(new EventMsg {
                                    Status = RunStatus.Normal, Msg = "上传成功,自动进入下次上传..."
                                });
                                Next.Invoke(context);
                            }
                            else
                            {
                                while (!flag)
                                {
                                    Thread.Sleep(2000);//休息俩秒继续查询
                                    OnMiddlewareWorking(new EventMsg {
                                        Status = RunStatus.Normal, Msg = "上传中,继续查询是否上传成功..."
                                    });
                                    flag = SearchNotifyStatus(context.UploadResponse.UploadStatusId);
                                    if (flag)
                                    {
                                        flag = false;
                                        OnMiddlewareWorking(new EventMsg {
                                            Status = RunStatus.Normal, Msg = "上传成功,自动进入下次上传..."
                                        });
                                    }
                                }
                                Next.Invoke(context);
                            }
                        }

                        index++;
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                OnMiddlewareWorking(new EventMsg()
                {
                    Status = Enums.RunStatus.Exception, Msg = "QunarIncrementalDispatcherMiddleware:" + ex.Message, Exception = ex, PurchaserType = Enums.PurchaserType.Qunar
                });
                context.UploadResponse = new UploadPolicyResponse()
                {
                    ErrCode = ResultType.Failed, ErrMsg = "QunarIncrementalDispatcherMiddleware:" + ex.Message, Excption = ex
                };
                return;
            }
        }