// Ham Update balance Transfer
        public bool UpdateBalanceTransfer(Account account, Transaction historyTransaction)
        {
            DbConnection.Instance().OpenConnection();
            var transaction = DbConnection.Instance().Connection.BeginTransaction();

            try
            {
                // 1.thực hiện trừ số tiền người chuyển.
                //1.1 Lấy thông tin số dư người chuyển.
                var          queryBalanceUser    = "******";
                MySqlCommand queryBalanceCommand =
                    new MySqlCommand(queryBalanceUser, DbConnection.Instance().Connection);
                queryBalanceCommand.Parameters.AddWithValue("@username", account.Username);
                queryBalanceCommand.Parameters.AddWithValue("@status", account.Status);
                var balanceReader = queryBalanceCommand.ExecuteReader();

                if (!balanceReader.Read())
                {
                    throw new BankError("Invalid username");
                }

                // dam bao luon co ban ghi
                var currentBalance = balanceReader.GetDecimal("balance");
                balanceReader.Close();

                // kiểm tra số dư tài khoản có đủ thực hiện giao dịch k
                if (historyTransaction.Type == Transaction.TransactionType.TRANSFER &&
                    historyTransaction.Amount > currentBalance)
                {
                    throw new BankError("Số dư không đủ!");
                }

                // trừ tiền người gửi.
                if (historyTransaction.Type == Transaction.TransactionType.TRANSFER)
                {
                    currentBalance = currentBalance - historyTransaction.Amount;
                }

                // update so du vao vao tai khoan người chuyển.
                var updateAccountResult       = 0;
                var queryUpdateAccountBalance =
                    "update `account` set balance = @balance where username = @username and status = 1";
                var cmdUpdateAccountBalance =
                    new MySqlCommand(queryUpdateAccountBalance, DbConnection.Instance().Connection);
                cmdUpdateAccountBalance.Parameters.AddWithValue("@username", account.Username);
                cmdUpdateAccountBalance.Parameters.AddWithValue("@balance", currentBalance);
                updateAccountResult = cmdUpdateAccountBalance.ExecuteNonQuery();

                // 2. thực hiện cộng tiền cho người nhận
                // 2.1 lấy thông tin so dư mới nhất của người nhận.
                var queryBalanceAccountReceiver =
                    "select balance from `account` where username = @username and status = @status";
                MySqlCommand queryBalanceCommandAccount =
                    new MySqlCommand(queryBalanceAccountReceiver, DbConnection.Instance().Connection);
                queryBalanceCommandAccount.Parameters.AddWithValue("@username", Program.curentAccountReceiver.Username);
                queryBalanceCommandAccount.Parameters.AddWithValue("@status", Program.curentAccountReceiver.Status);
                var balanceReaderAccountReceiver = queryBalanceCommandAccount.ExecuteReader();

                if (!balanceReaderAccountReceiver.Read())
                {
                    throw new BankError("Invalid username");
                }

                // dam bao luon co ban ghi
                var currentBalanceAccountReceiver = balanceReaderAccountReceiver.GetDecimal("balance");
                balanceReaderAccountReceiver.Close();
                // Cộng tiền người nhận.
                if (historyTransaction.Type == Transaction.TransactionType.TRANSFER)
                {
                    currentBalanceAccountReceiver = currentBalanceAccountReceiver + historyTransaction.Amount;
                }

                // update so du moi nhat cua nguoi nhận vào database
                var updateAccountNumber        = 0;
                var queryUpdateBalanceReceiver =
                    "update `account` set balance = @balance where username = @username and status = 1";
                var cmdUpdateAccountBalanceReceiver =
                    new MySqlCommand(queryUpdateBalanceReceiver, DbConnection.Instance().Connection);
                cmdUpdateAccountBalanceReceiver.Parameters.AddWithValue("@username", Program.curentAccountReceiver.Username);
                cmdUpdateAccountBalanceReceiver.Parameters.AddWithValue("@balance", currentBalanceAccountReceiver);
                updateAccountNumber = cmdUpdateAccountBalanceReceiver.ExecuteNonQuery();

                // luu thong tin transaction
                var insertTransactionResult = 0;
                var queryInsertTransaction  = "insert into `transaction` " +
                                              "(id, type, amount, content, senderAccountNumber, receiverAccountNumber, status) " +
                                              "values (@id, @type, @amount, @content, @senderAccountNumber, @receiverAccountNumber, @status)";
                var cmdInsertTransaction =
                    new MySqlCommand(queryInsertTransaction, DbConnection.Instance().Connection);
                cmdInsertTransaction.Parameters.AddWithValue("@id", historyTransaction.Id);
                cmdInsertTransaction.Parameters.AddWithValue("@type", historyTransaction.Type);
                cmdInsertTransaction.Parameters.AddWithValue("@amount", historyTransaction.Amount);
                cmdInsertTransaction.Parameters.AddWithValue("@content", historyTransaction.Content);
                cmdInsertTransaction.Parameters.AddWithValue("@senderAccountNumber",
                                                             historyTransaction.SenderAccountNumber);
                cmdInsertTransaction.Parameters.AddWithValue("@receiverAccountNumber",
                                                             historyTransaction.ReceiverAccountNumber);
                cmdInsertTransaction.Parameters.AddWithValue("@status", historyTransaction.Status);
                insertTransactionResult = cmdInsertTransaction.ExecuteNonQuery();

                if (updateAccountResult == 1 && insertTransactionResult == 1 && updateAccountNumber == 1)
                {
                    transaction.Commit();
                    return(true);
                }
            }
            catch (BankError e)
            {
                transaction.Rollback();
                return(false);
            }

            DbConnection.Instance().CloseConnection();
            return(false);
        }
        public bool UpdateBalance(Account account, Transaction historyTransaction)
        {
            DbConnection.Instance().OpenConnection();
            // tao transaction
            var transaction = DbConnection.Instance().Connection.BeginTransaction();

            try
            {
                // Lay thong tin so du
                var          queryBalance        = "select balance from `account` where username = @username and status = @status";
                MySqlCommand queryBalanceCommand = new MySqlCommand(queryBalance, DbConnection.Instance().Connection);
                queryBalanceCommand.Parameters.AddWithValue("@username", account.Username);
                queryBalanceCommand.Parameters.AddWithValue("@status", account.Status);
                var balanceReader = queryBalanceCommand.ExecuteReader();
                // thow loi neu k ton tai ban ghi
                if (!balanceReader.Read())
                {
                    throw new BankError("Invalid username");
                }

                // dam bao luon co ban ghi
                var currentBalance = balanceReader.GetDecimal("balance");
                balanceReader.Close();
                //kiem tra kieu transaction chi chap nhan withdraw va deposit
                if (historyTransaction.Type != Transaction.TransactionType.DEPOSIT &&
                    historyTransaction.Type != Transaction.TransactionType.WITHDRAW)
                {
                    throw new BankError("Số dư không đủ");
                }

                if (historyTransaction.Type == Transaction.TransactionType.WITHDRAW &&
                    historyTransaction.Amount > currentBalance)
                {
                    throw new BankError("Số dư không đủ!");
                }

                // cong tien vao tai khoan
                if (historyTransaction.Type != Transaction.TransactionType.DEPOSIT)
                {
                    currentBalance -= historyTransaction.Amount;
                }
                else
                {
                    currentBalance += historyTransaction.Amount;
                }

                // update so du vao database

                var updateAccountResult       = 0;
                var queryUpdateAccountBalance =
                    "update `account` set balance = @balance where username = @username and status = 1";
                var cmdUpdateAccountBalance =
                    new MySqlCommand(queryUpdateAccountBalance, DbConnection.Instance().Connection);
                cmdUpdateAccountBalance.Parameters.AddWithValue("@username", account.Username);
                cmdUpdateAccountBalance.Parameters.AddWithValue("@balance", currentBalance);
                updateAccountResult = cmdUpdateAccountBalance.ExecuteNonQuery();
                // luu thong tin transaction
                var insertTransactionResult = 0;
                var queryInsertTransaction  = "insert into `transaction` " +
                                              "(id, type, amount, content, senderAccountNumber, receiverAccountNumber, status) " +
                                              "values (@id, @type, @amount, @content, @senderAccountNumber, @receiverAccountNumber, @status)";
                var cmdInsertTransaction =
                    new MySqlCommand(queryInsertTransaction, DbConnection.Instance().Connection);
                cmdInsertTransaction.Parameters.AddWithValue("@id", historyTransaction.Id);
                cmdInsertTransaction.Parameters.AddWithValue("@type", historyTransaction.Type);
                cmdInsertTransaction.Parameters.AddWithValue("@amount", historyTransaction.Amount);
                cmdInsertTransaction.Parameters.AddWithValue("@content", historyTransaction.Content);
                cmdInsertTransaction.Parameters.AddWithValue("@senderAccountNumber",
                                                             historyTransaction.SenderAccountNumber);
                cmdInsertTransaction.Parameters.AddWithValue("@receiverAccountNumber",
                                                             historyTransaction.ReceiverAccountNumber);
                cmdInsertTransaction.Parameters.AddWithValue("@status", historyTransaction.Status);
                insertTransactionResult = cmdInsertTransaction.ExecuteNonQuery();

                if (updateAccountResult == 1 && insertTransactionResult == 1)
                {
                    transaction.Commit();
                    return(true);
                }
            }
            catch (BankError e)
            {
                transaction.Rollback();
                return(false);
            }

            DbConnection.Instance().CloseConnection();
            return(false);
        }