Exemplo n.º 1
0
    public bool checkInDonation(Doacao objData)
    {
        double usu_lat  = 0.0;
        double usu_long = 0.0;
        double hem_lat  = 0.0;
        double hem_long = 0.0;

        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();
            //SqlTransaction transaction = conn.BeginTransaction("CheckInDonation");

            try
            {

                String queryUser = "******";
                SqlCommand queryUserCommand = new SqlCommand(queryUser, conn);
                queryUserCommand.Parameters.AddWithValue("@idUser", objData.usuarioDoador);
                SqlDataReader userRow = queryUserCommand.ExecuteReader();

                if (userRow.Read())
                {
                    usu_lat     = userRow.GetDouble(0);
                    usu_long    = userRow.GetDouble(1);
                }
                userRow.Close();

                String queryHemo = "SELECT HEM_Lat, HEM_Long FROM TB_Hemocentros WHERE HEM_IdHemocentro = @idHemo";
                SqlCommand queryHemoCommand = new SqlCommand(queryHemo, conn);
                queryHemoCommand.Parameters.AddWithValue("@idHemo", objData.idHemocentro);
                SqlDataReader hemoRow = queryHemoCommand.ExecuteReader();

                if (hemoRow.Read())
                {
                    hem_lat     = hemoRow.GetDouble(0);
                    hem_long    = hemoRow.GetDouble(1);
                }
                hemoRow.Close();

                calcDistance validateCheckIn = new calcDistance();
                if (validateCheckIn.calcDistances(usu_lat, usu_long, hem_lat, hem_long) < 0.5)
                {
                    String cmdUpdateSolcitacao = " update TB_Solicitacoes set SOL_QuantidadeRealizadas = ( Select SOL_QuantidadeRealizadas + 1 " +
                                   "                                                         from TB_Solicitacoes " +
                                   "                                                         where SOL_IdSolicitacao = @idSolicitacao) ";
                    SqlCommand checkInCursor = new SqlCommand(cmdUpdateSolcitacao, conn);
                    checkInCursor.Parameters.AddWithValue("@idSolicitacao", objData.idSolicitacao);
                    checkInCursor.ExecuteNonQuery();

                    String cmdUpdateDoacao = " update TB_Doacoes set DOC_StatusDoacao = 2 where DOC_IdDoacao = @idDoacao ";
                    checkInCursor.CommandText = cmdUpdateDoacao;
                    checkInCursor.Parameters.AddWithValue("@idDoacao", objData.idDoacao);
                    checkInCursor.ExecuteNonQuery();

                    String cmdUpdateUser = "******";
                    checkInCursor.CommandText = cmdUpdateUser;
                    checkInCursor.Parameters.AddWithValue("@idUser", objData.usuarioDoador);
                    checkInCursor.ExecuteNonQuery();

                    String cmdUpdateUser2 = " update TB_Usuarios set USU_DtdUltimaDoacao = @data where USU_IdUsuario = @idUser2 ";
                    checkInCursor.CommandText = cmdUpdateUser2;
                    checkInCursor.Parameters.AddWithValue("@idUser2", objData.usuarioDoador);
                    checkInCursor.Parameters.AddWithValue("@data", DateTime.Now);
                    checkInCursor.ExecuteNonQuery();

                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
               // transaction.Rollback();
                throw new Exception("Erro " + ex.ToString());
            }
            finally
            {
                conn.Close();
            }

        }
    }
Exemplo n.º 2
0
    public void sendNotPush(int userId, int idHemocentro, int idSolicitacao)
    {
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            conn.Open();

            try
            {
                String cmdInsert = "SELECT USU_IdUsuario, USU_Lat, USU_Long FROM TB_Usuarios WHERE USU_IdUsuario <> @userId and USU_StatusApto = 1 and USU_NotificacaoPush = 1 and USU_Long > 0 and USU_Lat > 0";
                String cmdHemo = "Select HEM_Lat, HEM_Long from TB_Hemocentros where HEM_IdHemocentro = @idHemo";

                SqlCommand getHemoData = new SqlCommand(cmdHemo, conn);
                getHemoData.Parameters.AddWithValue("@idHemo", idHemocentro);

                SqlDataReader hemoData = getHemoData.ExecuteReader();

                double hemoLat = 0;
                double hemoLog = 0;
                if (hemoData.Read())
                {
                    hemoLat = hemoData.GetDouble(0);
                    hemoLog = hemoData.GetDouble(1);
                }
                hemoData.Close();

                SqlCommand checkusr = new SqlCommand(cmdInsert, conn);
                checkusr.Parameters.AddWithValue("@userId", userId);

                SqlDataReader reader = checkusr.ExecuteReader();

                calcDistance calc = new calcDistance();
                NotificacaoPush push = new NotificacaoPush();

                foreach (DbDataRecord recordUser in reader)
                {
                    if (calc.calcDistances(hemoLat, hemoLog, recordUser.GetDouble(1), recordUser.GetDouble(2)) <= 10)
                    {
                        push.pushNotificacao(recordUser.GetInt32(0), "A Solicitação de número " + idSolicitacao + " foi aberta próximo a você.");
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
    }