Beispiel #1
0
        Dictionary <string, Parameters.DetailInfo> IProvider.GetDetailInfo(string detailCode, bool findAnalog, List <string> detailsInBasket)
        {
            WriteLog("==>>");
            string analog = "on";
            Dictionary <string, Parameters.DetailInfo> detailInfo = new Dictionary <string, Parameters.DetailInfo>();


            //DatabaseControl dbControl = new DatabaseControl();
            //detailInfo = dbControl.GetIndexedSearch(detailCode, findAnalog);
            //if (detailInfo.Count > 0) return detailInfo;

            using (AvtoTOService autotoService = new AvtoTOService())
            {
                long idReq = Singleton <Logger> .Instance.GetId();

                string retJons = @"{""user_id"":""" + UsrId + @""",""user_login"":"""
                                 + Login + @""",""user_password"":""" + Password
                                 + @""",""search_code"":""" + detailCode.Trim().Replace("\t", "") + @""",""search_cross"":""" + "" + analog + "" + @"""}";
                WriteLog(string.Format("Request [{0}]: {1}", idReq, retJons.Replace(Password, "*****").Replace(Login, "*****")));
                string response = autotoService.SearchStart(retJons);
                WriteLog(string.Format("Response [{0}]: {1}", idReq, response.Replace(Password, "*****").Replace(Login, "*****")));
                Dictionary <string, dynamic> responseDic = GetJsondata(response);
                string requestId = responseDic["ProcessSearchId"].ToString();
                int    actCount  = 0;
                while (true)
                {
                    idReq = Singleton <Logger> .Instance.GetId();

                    if (actCount > 3)
                    {
                        break;
                    }
                    Thread.Sleep(1500);
                    retJons = @"{""ProcessSearchId"":""" + requestId + "\"}";
                    WriteLog(string.Format("Request [{0}]: {1}", idReq, retJons.Replace(Password, "*****").Replace(Login, "*****")));
                    response = autotoService.SearchGetParts(retJons);
                    WriteLog(string.Format("Response [{0}]: {1}", idReq, response.Replace(Password, "*****").Replace(Login, "*****")));
                    responseDic = GetJsondata(response);

                    string valinfo = responseDic["Info"].ToString();
                    bool   whait;
                    CheckError(valinfo, out whait);
                    if (whait)
                    {
                        continue;        // если запрос в обработке то ждем еще
                    }
                    JArray     val  = responseDic["Parts"];
                    JContainer info = responseDic["Info"];
                    string     id   = info.First.ToString().Split(':')[1].Replace("\"", "");
                    if (val != null)
                    {
                        DatabaseControl dc = new DatabaseControl();
                        Dictionary <string, Parameters.DetailInfo> dic = new Dictionary <string, Parameters.DetailInfo>();
                        foreach (JContainer container in val.Children())
                        {
                            string detailParametersString = container.ToString();
                            Parts  part = JsonConvert.DeserializeObject <Parts>(detailParametersString);
                            Parameters.DetailInfo di = new Parameters.DetailInfo();
                            di.PartId    = part.AvtotoData["PartId"];
                            di.Name      = part.Name;
                            di.RealPrice = part.Price;
                            di.Price     = Manage.GetSummWithComission(part.Price);
                            di.Code      = part.Code;
                            di.BaseCount = part.BaseCount;
                            di.Delivery  = part.Delivery;
                            di.Storage   = part.Storage;
                            di.Manuf     = part.Manuf;
                            di.SearchID  = id;
                            int    totalDays;
                            string hash = Manage.CalculateDetailHash(di, out totalDays);
                            di.Hash = hash;
                            if (detailsInBasket.Contains(hash))
                            {
                                di.InBasket = true;
                            }
                            di.AverageDelivery = totalDays;
                            if (dic.ContainsKey(hash))
                            {
                                Parameters.DetailInfo detailInfoTEmp = dic[hash];
                                if (detailInfoTEmp.RealPrice <= di.Price)// если сумма меньше
                                {
                                    if (detailInfoTEmp.AverageDelivery <= di.AverageDelivery)
                                    {
                                        continue;
                                    }
                                    if ((detailInfoTEmp.AverageDelivery - di.AverageDelivery) < 2)
                                    {
                                        continue;
                                    }
                                }
                                else
                                {
                                    dic[hash] = di;
                                }
                            }
                            else
                            {
                                dic.Add(hash, di);
                            }
                            if (!detailInfo.ContainsKey(hash))
                            {
                                detailInfo.Add(hash, di);
                            }
                        }
                        dc.SetIndexedSearch(dic, detailCode, ID, findAnalog);
                        break;
                    }
                    actCount++;
                }
            }
            return(detailInfo);
        }