public Guid ForgetPassword(string email)
        {
            var res = Guid.Empty;

            var details = _UserCollection.Get(new UserFilter()
            {
                Email = email
            });

            if (details.Any())
            {
                var detail      = details.FirstOrDefault();
                var transaction = new Transaction();
                transaction.ReferenceId = detail.Id;
                transaction.CreateTime  = DateTime.Now;
                transaction.ExpireTime  = transaction.CreateTime.AddMinutes(_Setting.TransferExpiration);
                transaction.Type        = 2;
                while (true)
                {
                    transaction.Otp = _Context.MakeOTP(6);
                    if (!_TransactionCollection.GetMany(new TransactionFilter()
                    {
                        ReferenceId = detail.Id, Type = 2, Otp = transaction.Otp
                    }).Any())
                    {
                        break;
                    }
                }

                _TransactionCollection.Create(transaction);
                if (transaction.Id != Guid.Empty)
                {
                    // Send mail
                    var sb = new StringBuilder();
                    sb.AppendFormat($"Dear {detail.Name},");
                    sb.AppendFormat("<br /><br /><b>Bạn đang yêu cầu quên mật khẩu, mã xác thực của bạn là:</b>");
                    sb.AppendFormat($"<br /><br /><b>{transaction.Otp}</b>");
                    sb.AppendFormat($"<br /><br /><b>Mã xác thực này sẽ hết hạn lúc {transaction.ExpireTime.ToLongTimeString()}.</b>");
                    sb.AppendFormat($"<br /><br /><b>Nếu yêu cầu không phải của bạn, vui lòng bỏ qua mail này.</b>");

                    if (_Context.SendMail("Xác thực yêu cầu quên mật khẩu", sb.ToString(), detail.Email, detail.Name))
                    {
                        res = transaction.Id;
                    }
                }
            }

            return(res);
        }
        public Transaction CheckoutDeptReminder(Guid userId, Guid deptReminderId)
        {
            Transaction res = null;

            using (var sessionTask = _MongoDBClient.StartSessionAsync())
            {
                var session = sessionTask.Result;
                session.StartTransaction();
                try
                {
                    var userDetail = _UserCollection.GetById(userId);
                    if (userDetail != null)
                    {
                        // Get chi tiết nhắc nợ
                        var dept = _DeptReminderCollection.GetById(deptReminderId);
                        if (dept != null && dept.RecipientId == userId)
                        {
                            // Create OTP
                            string otp = null;
                            while (true)
                            {
                                otp = _Context.MakeOTP(6);
                                if (!_TransactionCollection.GetMany(new TransactionFilter()
                                {
                                    Otp = otp, Type = 1
                                }).Any())
                                {
                                    break;
                                }
                            }

                            // expire
                            var transaction = new Transaction();
                            transaction.Id          = Guid.Empty;
                            transaction.ReferenceId = dept.Id;
                            transaction.Otp         = otp;
                            transaction.CreateTime  = DateTime.Now;
                            transaction.ExpireTime  = transaction.CreateTime.AddMinutes(_Setting.TransferExpiration);
                            transaction.Type        = 1;

                            _TransactionCollection.Create(transaction);

                            if (transaction.Id != Guid.Empty)
                            {
                                // Send mail
                                var sb = new StringBuilder();
                                sb.AppendFormat($"Dear {userDetail.Name},");
                                sb.AppendFormat("<br /><br /><b>Bạn đang yêu cầu thanh toán nhắc nợ, mã xác thực của bạn là:</b>");
                                sb.AppendFormat($"<br /><br /><b>{transaction.Otp}</b>");
                                sb.AppendFormat($"<br /><br /><b>Mã xác thực này sẽ hết hạn lúc {transaction.ExpireTime.ToLongTimeString()}.</b>");
                                sb.AppendFormat($"<br /><br /><b>Nếu yêu cầu không phải của bạn, vui lòng bỏ qua mail này.</b>");

                                if (_Context.SendMail("Xác thực thanh toán nhắc nợ", sb.ToString(), userDetail.Email, userDetail.Name))
                                {
                                    res = transaction;
                                }
                            }
                            else
                            {
                                _Setting.Message.SetMessage("Không thể thanh toán nhắc nợ!");
                            }
                        }
                        else
                        {
                            _Setting.Message.SetMessage("Không tìm thấy thông tin nhắc nợ!");
                        }
                    }
                    else
                    {
                        _Setting.Message.SetMessage("Không tìm thấy thông tin khách hàng!");
                    }

                    if (res != null)
                    {
                        session.CommitTransactionAsync();
                    }
                    else
                    {
                        session.AbortTransactionAsync();
                    }
                }
                catch
                {
                    session.AbortTransactionAsync();
                }
            }

            return(res);
        }
        public Guid PayInByPartner(Transfer transfer)
        {
            var res      = Guid.Empty;
            var linkBank = _LinkingBankCollection.GetById(transfer.SourceLinkingBankId);

            if (linkBank != null)
            {
                // get chi tiết người nhận
                using (var sessionTask = _MongoDBClient.StartSessionAsync())
                {
                    var session = sessionTask.Result;
                    session.StartTransaction();
                    try
                    {
                        var userDetail = _UserCollection.GetByAccountNumber(transfer.DestinationAccountNumber);
                        if (userDetail != null)
                        {
                            transfer.Id          = Guid.Empty;
                            transfer.Fee         = _Context.TransactionCost(transfer.Money);
                            transfer.IsConfirmed = true;
                            transfer.DestinationLinkingBankId = Guid.Empty;
                            transfer.SignedData = transfer.SignedData;
                            // Update số dư
                            userDetail.CheckingAccount.AccountBalance += transfer.Money;
                            if (!transfer.IsSenderPay)
                            {
                                userDetail.CheckingAccount.AccountBalance -= transfer.Fee;
                            }

                            // Create chuyển tiền
                            _TransferCollection.Create(transfer);

                            if (transfer.Id != Guid.Empty)
                            {
                                // Update người nhận
                                var updateUser = _UserCollection.UpdateCheckingAccount(new UserFilter()
                                {
                                    Id = userDetail.Id
                                }, userDetail.CheckingAccount);

                                if (updateUser > 0)
                                {
                                    // Create giao dịch
                                    var transaction = new Transaction();
                                    transaction.Id          = Guid.Empty;
                                    transaction.CreateTime  = DateTime.Now;
                                    transaction.ExpireTime  = transaction.CreateTime.AddMinutes(_Setting.TransferExpiration);
                                    transaction.ConfirmTime = transaction.CreateTime;
                                    transaction.ReferenceId = transfer.Id;
                                    transaction.Type        = 0;

                                    _TransactionCollection.Create(transaction);

                                    if (transaction.Id != Guid.Empty)
                                    {
                                        res = transaction.Id;
                                    }
                                    else
                                    {
                                        _Setting.Message.SetMessage("Không thể lưu thông tin giao dịch!");
                                    }
                                }
                                else
                                {
                                    _Setting.Message.SetMessage("Không thể lưu thông tin người nhận!");
                                }
                            }
                            else
                            {
                                _Setting.Message.SetMessage("Không thể lưu thông tin chuyển tiền!");
                            }
                        }
                        else
                        {
                            _Setting.Message.SetMessage("Không tìm thấy thông tin người nhận!");
                        }

                        if (res != Guid.Empty)
                        {
                            session.CommitTransactionAsync();
                        }
                        else
                        {
                            session.AbortTransactionAsync();
                        }
                    }
                    catch (Exception ex)
                    {
                        session.AbortTransactionAsync();
                        throw ex;
                    }
                }
            }
            return(res);
        }
        public bool PayIn(Guid userId, PayInfo payInfo)
        {
            var res = false;

            var details = _UserCollection.Get(new UserFilter()
            {
                AccountNumber = payInfo.AccountNumber, Username = payInfo.Username
            });
            var detailsEmployee = _UserCollection.GetById(userId);

            if (details.Any() && detailsEmployee != null)
            {
                var detail = details.FirstOrDefault();
                detail.CheckingAccount.AccountBalance += payInfo.Money;

                // luu giao dich
                var transfer = new Transfer();
                transfer.Description = "Nạp tiền";
                transfer.DestinationAccountNumber = detail.AccountNumber;
                transfer.DestinationLinkingBankId = Guid.Empty;
                transfer.Fee                 = 0;
                transfer.IsConfirmed         = true;
                transfer.IsSenderPay         = true;
                transfer.Money               = payInfo.Money;
                transfer.SourceAccountNumber = detailsEmployee.AccountNumber;
                transfer.SourceLinkingBankId = Guid.Empty;
                transfer.IsPayIn             = true;

                using (var sessionTask = _MongoDBClient.StartSessionAsync())
                {
                    var session = sessionTask.Result;
                    session.StartTransaction();
                    try
                    {
                        _TransferCollection.Create(transfer);

                        if (transfer.Id == Guid.Empty)
                        {
                            throw new Exception();
                        }

                        var resUp = _UserCollection.UpdateCheckingAccount(new UserFilter()
                        {
                            AccountNumber = payInfo.AccountNumber, Id = detail.Id
                        }, detail.CheckingAccount) > 0;

                        if (!resUp)
                        {
                            throw new Exception();
                        }

                        var transaction = new Transaction();
                        transaction.Id          = Guid.Empty;
                        transaction.ReferenceId = transfer.Id;
                        transaction.Otp         = "";
                        transaction.CreateTime  = DateTime.Now;
                        transaction.ExpireTime  = transaction.CreateTime.AddMinutes(_Setting.TransferExpiration);
                        transaction.Type        = 0;
                        transaction.ConfirmTime = transaction.CreateTime;

                        _TransactionCollection.Create(transaction);

                        if (transaction.Id == Guid.Empty)
                        {
                            throw new Exception();
                        }
                        res = true;
                        session.CommitTransactionAsync();
                    }
                    catch (Exception)
                    {
                        session.AbortTransactionAsync();
                    }
                }
            }
            else
            {
                _Setting.Message.SetMessage("Không tìm thấy thông tin tài khoản!");
            }

            return(res);
        }