예제 #1
0
        private async Task ProcessPendingConsoleAsync(Norms.Domain.Norm norm, IEnumerable <Adapters.Oal.dbo_normal_console_event_new> rows)
        {
            var pendingItems = await SearchEventPendingAsync(norm.ConsoleTag);

            var tasks = from p in pendingItems
                        select ProcessEventPendingItem(norm, p);

            await Task.WhenAll(tasks);

            var consolePendingList = new List <string> {
                norm.ConsoleTag
            };

            foreach (var item in rows)
            {
                var console = IsConsole(item.consignment_no);
                if (!console)
                {
                    continue;
                }
                if (consolePendingList.Contains(item.consignment_no))
                {
                    continue;
                }

                consolePendingList.Add(item.consignment_no);
            }
        }
예제 #2
0
        private async Task <bool> AddToConsoleDetailsAsync(Norms.Domain.Norm norm)
        {
            var adapter = new Adapters.Oal.dbo_console_detailsAdapter();
            var idPr    = await Policy.Handle <SqlException>(e => e.IsTimeout())
                          .WaitAndRetryAsync(RetryCount, WaitInterval)
                          .ExecuteAndCaptureAsync(() => adapter.ExecuteScalarAsync <string>($"SELECT [id] FROM dbo.console_details WHERE [console_no] = '{norm.ConsoleTag}'"));

            var id = idPr.Result;
            var consoleTagNotInConsoleDetails = string.IsNullOrWhiteSpace(id);

            if (consoleTagNotInConsoleDetails)
            {
                var map = new Integrations.Transforms.RtsNormToOalConsoleDetails();
                var row = await map.TransformAsync(norm);

                var dpr = await Policy.Handle <SqlException>(e => e.IsTimeout())
                          .WaitAndRetryAsync(RetryCount, WaitInterval)
                          .ExecuteAndCaptureAsync(() => adapter.ExecuteScalarAsync <int>($"SELECT COUNT(*) FROM dbo.console_details WHERE console_no ='{row.console_no}'"));

                if (dpr.Result == 0 && !string.IsNullOrWhiteSpace(row.console_no))
                {
                    var pr = Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout())
                             .WaitAndRetryAsync(RetryCount, WaitInterval)
                             .ExecuteAndCaptureAsync(() => adapter.InsertAsync(row));
                    var result = await pr;
                    if (result.FinalException != null)
                    {
                        throw result.FinalException; // Send to dead letter queue
                    }
                    System.Diagnostics.Debug.Assert(result.Result > 0, "Should be at least 1 row");
                }
                return(true);
            }

            var detailsPolly = await Policy.Handle <SqlException>(e => e.IsTimeout())
                               .WaitAndRetryAsync(RetryCount, WaitInterval)
                               .ExecuteAndCaptureAsync(async() => await adapter.LoadOneAsync(id));

            if (null != detailsPolly.FinalException)
            {
                throw new Exception("Console Details Polly Error", detailsPolly.FinalException);
            }

            var details      = detailsPolly.Result;
            var normDateTime = norm.Date.AddHours(norm.Time.Hour).AddMinutes(norm.Time.Minute).AddSeconds(norm.Time.Second);

            if (details.courier_id == norm.CourierId && details.office_no == norm.LocationId && (details.date_field ?? DateTime.MinValue).AddHours(28) >= normDateTime)
            {
                var notes = details.item_consignments.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                notes.AddRange(norm.AllConsignmentNotes.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries));
                details.item_consignments = string.Join("\t", notes.OrderBy(x => x).Where(x => x.Length > 3 /* remove anything less*/).Distinct());
                await adapter.UpdateAsync(details);

                return(true);
            }

            await InsertConsoleDuplicationErrorAndEventExceptionAsync(norm);

            return(false);
        }
예제 #3
0
        private async Task InsertConsoleDuplicationErrorAndEventExceptionAsync(Norms.Domain.Norm norm)
        {
            Console.WriteLine("insert into console_duplicate_error & event_exception");

            //
            var map     = new Integrations.Transforms.RtsNormToOalConsoleDetails();
            var normRow = await map.TransformAsync(norm);

            var error = new Adapters.Oal.dbo_console_duplicate_error
            {
                item_consignments         = normRow.item_consignments,
                date_field                = normRow.date_field,
                courier_id                = normRow.courier_id,
                batch_name                = normRow.batch_name,
                beat_no                   = normRow.beat_no,
                console_no                = normRow.console_no,
                console_type              = null,
                console_type_desc         = null,
                courier_name              = normRow.courier_name,
                dt_created_oal_date_field = normRow.dt_created_oal_date_field,
                event_comment             = normRow.event_comment,
                id                 = GenerateId(20),
                event_type         = normRow.event_type,
                office_dest        = null,
                version            = 0,
                office_dest_name   = null,
                office_name        = normRow.office_name,
                office_next_code   = normRow.office_next_code,
                office_no          = normRow.office_no,
                other_console_type = null,
                routing_code       = null
            };
            var errorAdapter = new Adapters.Oal.dbo_console_duplicate_errorAdapter();
            await Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout())
            .WaitAndRetryAsync(RetryCount, WaitInterval)
            .ExecuteAndCaptureAsync(() => errorAdapter.InsertAsync(error));


            var exc = new Adapters.Oal.dbo_event_exception
            {
                consignment_no = error.console_no,
                date_field     = error.date_field,
                batch_name     = error.batch_name,
                office_no      = error.office_no,
                version        = error.version,
                id             = GenerateId(34),
                courier_id     = error.courier_id,
                event_class    = "pos.oal.NormalConsoleEventNew",
                event_id       = error.id
            };
            var excAdapter = new Adapters.Oal.dbo_event_exceptionAdapter();
            await Policy.Handle <SqlException>(e => e.IsDeadlockOrTimeout())
            .WaitAndRetryAsync(RetryCount, WaitInterval)
            .ExecuteAndCaptureAsync(() => excAdapter.InsertAsync(exc));
        }
예제 #4
0
        public async Task RunAsync(Norms.Domain.Norm norm)
        {
            var pendingConsoles = new List <string>();
            var consoleList     = new List <string> {
                norm.ConsoleTag
            };

            //add to console_details first
            var success = await AddToConsoleDetailsAsync(norm);

            if (!success)
            {
                return;
            }

            var rows = new ConcurrentBag <Adapters.Oal.dbo_normal_console_event_new>();
            var map  = new Integrations.Transforms.RtsNormToNormalConsoleEventNew();
            var row  = await map.TransformAsync(norm);

            rows.Add(row);
            rows.AddRange(await GetEventRowsAsync(row, pendingConsoles, consoleList, norm.AllConsignmentNotes));

            //
            await InsertDeliveryConsoleEventNewAsync(rows);

            //
            if (pendingConsoles.Any())
            {
                await InsertEventPendingConsoleAsync(row, pendingConsoles.ToArray());
            }

            var consoleDetailsAdapter = new Adapters.Oal.dbo_console_detailsAdapter();
            var pendingCountPr        = await Policy.Handle <SqlException>(e => e.IsTimeout())
                                        .WaitAndRetryAsync(RetryCount, WaitInterval)
                                        .ExecuteAndCaptureAsync(
                () =>
                consoleDetailsAdapter.ExecuteScalarAsync <int>(
                    $"SELECT COUNT(*) FROM dbo.event_pending_console WHERE [console_no] = '{norm.ConsoleTag}'"));

            var tagExistInPendingConsole = pendingCountPr.Result > 0;

            if (tagExistInPendingConsole)
            {
                await ProcessPendingConsoleAsync(norm, rows);
            }
        }
예제 #5
0
        private async Task ProcessEventPendingItem(Norms.Domain.Norm norm, Adapters.Oal.dbo_event_pending_console pending)
        {
            var itemList = norm.AllConsignmentNotes.Split(new[] { ',', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            var ok       = false;

            switch (pending.event_class)
            {
            case "pos.oal.DeliveryEventNew":
                await ProcessDeliveryPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.SopEventNew":
                await ProcessSopPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.SipEventNew":
                await ProcessSipPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.HipEventNew":
                await ProcessHipPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.HopEventNew":
                await ProcessHopPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.StatusEventNew":
                await ProcessStatPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.VasnEventNew":
                await ProcessVasnPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.NormalConsoleEventNew":
                //ProcessNormPendingItem(item.console_no, item.event_id);
                break;

            case "pos.oal.MissortEventNew":
                await ProcessMissPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.WwpEventNewLog":
                await ProcessWwpPendingItem(pending.event_id, itemList);

                ok = true;
                break;

            case "pos.oal.IpsImport":
                await ProcessIpsPendingItem(pending.event_id, itemList);

                ok = true;
                break;
            }

            if (ok)
            {
                var pendingAdapter = new Adapters.Oal.dbo_event_pending_consoleAdapter();
                await pendingAdapter.DeleteAsync(pending.id);
            }
        }