Пример #1
0
        public void SynchronizeCardWithBPM()
        {
            var pack = new Dictionary <Guid, string>();

            do
            {
                pack.Clear();
                using (var conn = new NpgsqlConnection(GetConnectionString()))
                {
                    conn.Open();

                    using (var cmd = new NpgsqlCommand(@"SELECT ""CardId"", ""Number""
															FROM ""public"".""CardTemp"" Where ""IsSendedToBPM"" = 0 and ""AttemptCount"" < 5 limit 100"                                                            , conn))
                    {
                        using (var reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                pack.Add(reader.GetGuid(0), reader.GetString(1));
                            }
                        }
                    }
                }
                var cards = pack.Select(c => new Card()
                {
                    Id = c.Key, Number = c.Value, CardStatus = "1", IsMain = false
                });
                var body = JsonConvert.SerializeObject(cards);

                var provider = new CRMIntegrationProvider();
                body = String.Format(@"{{""request"": {0}}}", new IntegrationObjectRequest()
                {
                    Objects = body, TableName = "CardGenerate"
                }.ToJson());
                var result = provider.MakeRequest("GateIntegrationService/IntegratePack", body);

                if (result.IsSuccess)
                {
                    //var results = JsonConvert.DeserializeObject<PackResults>(result.ResponseStr);
                    //results.IntegratePackResult.ForEach(r => { if (r.IsSuccess) SetCardSended(new Guid(r.Id)); else SetCardError(new Guid(r.Id), r.ErrorMessage); });
                    cards.ToList().ForEach(c => SetCardSended(c.Id, "IsSendedToBPM"));
                }
                else
                {
                    cards.ToList().ForEach(c => SetCardError(c.Id, result.ResponseStr, "IsSendedToBPM"));
                }
            } while (pack.Count > 0);
        }
Пример #2
0
        public virtual void Execute()
        {
            Logger.LogInfo("Начался импорт", _tableName);

            var tasks = new List <Task>();

            for (var j = 0; j < threadCount; j++)
            {
                var task = new Task(() =>
                {
                    var pack = new List <BaseIntegrationObject>();
                    try
                    {
                        pack = ReadPack();
                        Logger.LogInfo(string.Format("Прочитано данных из {0}: {1}", _tableName, pack.Count), "");
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(string.Format("Ошибка чтения данных из {0}", _tableName), e);
                    }

                    var crm = new CRMIntegrationProvider(true);

                    while (pack.Count > 0)
                    {
                        var now = DateTime.Now;

                        var res = crm.MakeRequest("GateIntegrationService/IntegratePack", GetBody(pack));

                        Logger.LogInfo(string.Format("Запрос {0} к CRM выполнен за {1}с", _tableName, (DateTime.Now - now).TotalSeconds.ToString("F1")), "");

                        if (!res.IsSuccess)
                        {
                            ProceedResult(new PackResult()
                            {
                                IsSuccess = false, ErrorMessage = res.ResponseStr, IsTimeout = res.IsTimeout
                            }, pack);
                        }
                        else
                        {
                            var results = JsonConvert.DeserializeObject <PackResults>(res.ResponseStr);

                            var successResults   = results.IntegratePackResult.Where(r => r.IsSuccess);
                            var unsuccessResults = results.IntegratePackResult.Where(r => !r.IsSuccess);
                            ProceedResults(new PackResults()
                            {
                                IntegratePackResult = unsuccessResults.ToList()
                            });

                            foreach (var r in successResults)
                            {
                                var p = pack.FirstOrDefault(x => x.ERPId == r.Id);
                                if (p != null && r.CustomFields != null)
                                {
                                    p.CustomFields = r.CustomFields;
                                }
                            }

                            if (_isNeedSendToProcessing)
                            {
                                var isProcessingSuccess = false;
                                try
                                {
                                    isProcessingSuccess = SendToProcessing(pack, out now, out results, successResults, false);
                                }
                                catch (Exception e)
                                {
                                    Logger.LogError("SendToProcessing error ", e);
                                }

                                successResults   = results.IntegratePackResult.Where(r => r.IsSuccess);
                                unsuccessResults = results.IntegratePackResult.Where(r => !r.IsSuccess);

                                ProceedResults(new PackResults()
                                {
                                    IntegratePackResult = unsuccessResults.ToList()
                                });

                                if (_isNeedSendToPersonalArea)
                                {
                                    var isPersonalAreaSuccess = SendToProcessing(pack, out now, out results, successResults, true);

                                    if (isPersonalAreaSuccess)
                                    {
                                        ProceedResults(results);
                                    }
                                }
                                else
                                {
                                    ProceedResults(results);
                                }
                            }
                            else
                            {
                                ProceedResults(results);
                            }
                        }

                        Logger.LogInfo(_tableName, "pack finished");
                        pack = new List <BaseIntegrationObject>();
                        try
                        {
                            pack = ReadPack();
                            Logger.LogInfo(string.Format("Прочитано данных из {0}: {1}", _tableName, pack.Count), "");
                        }
                        catch (Exception e)
                        {
                            Logger.LogError(string.Format("Ошибка чтения данных из {0}", _tableName), e);
                        }
                    }
                });

                task.Start();
                tasks.Add(task);
            }

            Task.WaitAll(tasks.ToArray());

            Logger.LogInfo("Finished", _tableName);
        }
Пример #3
0
        public void Execute()
        {
            try
            {
                lock (_lock)
                {
                    if (_shuttingDown)
                    {
                        return;
                    }

                    GlobalCacheReader.GetValue(GlobalCacheReader.CacheKeys.PromocodeAttemptsTransferPackSize, out int packSize);

                    var crmProvider = new CRMIntegrationProvider(true);
                    while (_dBContext.PromocodeUseAttempts.Where(p => !p.IsError).Count() > 0)
                    {
                        var pack = _dBContext.PromocodeUseAttempts.Where(p => !p.IsError).Take(packSize).ToList();
                        var microsoftDateFormatSettings = new JsonSerializerSettings
                        {
                            DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                        };
                        var result = crmProvider.MakeRequest("PromocodeService\\AddPromocodeUseAttempt", $"{{\"promocodeUseAttempts\": {JsonConvert.SerializeObject(pack, microsoftDateFormatSettings)}}}");
                        if (result == null || !result.IsSuccess)
                        {
                            Logger.LogError($"Ошибка вызова сервиса PromocodeService\\AddPromocodeUseAttempt: {result?.ResponseStr}");
                        }
                        else
                        {
                            var response = JsonConvert.DeserializeObject <PromocodeUseAttemptServiceResponse>(result.ResponseStr).AddPromocodeUseAttemptResult;

                            if (!response.IsSuccess)
                            {
                                Logger.LogError($"Ошибка вызова сервиса PromocodeService\\AddPromocodeUseAttempt: {response.ErrorMessage}");
                            }
                            else
                            {
                                var errorPromocodes         = response.ErrorPromocodes.ToDictionary(pair => pair.Key, pair => pair.Value);
                                var successWritedPromocodes = pack.Where(p => !errorPromocodes.ContainsKey(p.Id));
                                if (successWritedPromocodes.Count() > 0)
                                {
                                    _dBContext.RemoveRange(successWritedPromocodes);
                                }

                                foreach (var errorPromocode in errorPromocodes)
                                {
                                    var promocode = pack.FirstOrDefault(p => p.Id == errorPromocode.Key);
                                    if (promocode != null)
                                    {
                                        promocode.Error   = errorPromocode.Value;
                                        promocode.IsError = true;
                                        _dBContext.Update(promocode);
                                    }
                                }
                                _dBContext.SaveChanges();
                            }
                        }
                    }
                }
            }
            finally
            {
            }
        }