Ejemplo n.º 1
0
        private async Task HandleCommandMessageAsync(UserMessageInfo userMessageInfo)
        {
            // Todo Проверка №3. Надо перенести в одно место.
            switch (userMessageInfo.Message.QueryData.MenuType)
            {
            case MenuType.NewReceiptSelectCustomer:
            {
                var data = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                if (data.TargetId == data.SelectedCustomerId)
                {
                    break;
                }

                data.SelectedCustomerId = data.TargetId;

                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.SelectCustomer, menu);

                break;
            }

            case MenuType.NewReceiptSelectConsumers:
            {
                var data = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                var selectedConsumers = new HashSet <long>(data.SelectedConsumerIds ?? new long[0]);
                if (data.TargetId != null)
                {
                    if (selectedConsumers.Contains(data.TargetId.Value))
                    {
                        selectedConsumers.Remove(data.TargetId.Value);
                    }
                    else
                    {
                        selectedConsumers.Add(data.TargetId.Value);
                    }
                }

                selectedConsumers        = new HashSet <long>(selectedConsumers.Distinct());
                data.SelectedConsumerIds = selectedConsumers.ToArray();

                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.SelectConsumers, menu);

                break;
            }

            case MenuType.NewReceiptAdd:
            {
                var    data         = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                string customerText = userMessageInfo.Customers
                                      .FirstOrDefault(x => data.SelectedCustomerId.Value == x.Id).Caption;
                string[] consumerTexts = userMessageInfo.Customers
                                         .Where(x => data.SelectedCustomerId.Value == x.Id || data.SelectedConsumerIds.Contains(x.Id))
                                         .Select(x => x.Caption).ToArray();
                Receipt receipt = await _receiptService.GetAsync(data.ReceiptId);

                await _messenger.EditMessageAsync(userMessageInfo, Resources.InProgress);

                if (receipt.Status == ReceiptStatus.New || receipt.Status == ReceiptStatus.NewManual)
                {
                    receipt.Status = receipt.Status == ReceiptStatus.New
                            ? ReceiptStatus.Filled
                            : ReceiptStatus.Manual;
                    receipt.CustomerId   = data.SelectedCustomerId;
                    receipt.PurchaseTime = DateTime.Now;
                    await _receiptService.SetCustomersToReceiptAsync(receipt.Id, data.SelectedConsumerIds);

                    await _receiptService.UpdateAsync(receipt);

                    var msgText = string.Format(Resources.NewReceiptAddedInfo, customerText, receipt.TotalAmount, string.Join(", ", consumerTexts));
                    await _messenger.EditMessageAsync(userMessageInfo, msgText);

                    _logger.Info(msgText);
                }

                break;
            }

            case MenuType.NewReceiptCancel:
            {
                var data = (AddReceiptQueryData)userMessageInfo.Message.QueryData;
                await _messenger.EditMessageAsync(userMessageInfo, Resources.NewReceiptCancel);

                Receipt receipt = await _receiptService.GetAsync(data.ReceiptId);

                receipt.Status = ReceiptStatus.Deleted;
                await _receiptService.UpdateAsync(receipt);

                break;
            }

            case MenuType.MoneyTransferSelectFrom:
            {
                var   data = (MoneyTransferQueryData)userMessageInfo.Message.QueryData;
                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.MoneyTransferSelectFrom, menu);

                break;
            }

            case MenuType.MoneyTransferSelectTo:
            {
                var   data = (MoneyTransferQueryData)userMessageInfo.Message.QueryData;
                IMenu menu = _menuProvider.GetMenu(userMessageInfo, data);
                await _messenger.EditMessageAsync(userMessageInfo, Resources.MoneyTransferSelectTo, menu);

                break;
            }

            case MenuType.MoneyTransferAdd:
            {
                var    data         = (MoneyTransferQueryData)userMessageInfo.Message.QueryData;
                string customerFrom = userMessageInfo.Customers.FirstOrDefault(x => data.CustomerFromId == x.Id)?.Caption ?? $"Id: {data.CustomerFromId}";
                string customerTo   = userMessageInfo.Customers.FirstOrDefault(x => data.CustomerToId == x.Id)?.Caption ?? $"Id: {data.CustomerToId}";
                await _messenger.EditMessageAsync(userMessageInfo, Resources.InProgress);

                var lastBillingPeriod = await _billingPeriodService.GetLastByGroupIdAsync(userMessageInfo.Group.Id);

                if (lastBillingPeriod == null)
                {
                    await _messenger.EditMessageAsync(userMessageInfo, Resources.BillingPeriodNotCreated);

                    break;
                }

                await _moneyOperationService.AddAsync(new MoneyOperation
                    {
                        Amount          = data.Amount,
                        Comment         = data.Caption,
                        CustomerFromId  = data.CustomerFromId.Value,
                        CustomerToId    = data.CustomerToId.Value,
                        OperationType   = MoneyOperationType.Transfer,
                        BillingPeriodId = lastBillingPeriod.Id
                    });

                var msgText = string.Format(Resources.MoneyTransferSuccess, customerFrom, customerTo, data.Amount);
                await _messenger.EditMessageAsync(userMessageInfo, msgText);

                _logger.Info(msgText.Replace("\n", ""));
                break;
            }

            case MenuType.MoneyTransferCancel:
            {
                await _messenger.EditMessageAsync(userMessageInfo, Resources.MoneyTransferCanceled);

                break;
            }
            }
        }