SafeDispatch/SafeMobileLIB_DLL/DBmanagers/DBdailyreportManager.cs

228 lines
7.6 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
namespace SafeMobileLib.DBmanagers
{
public class DBdailyreportManager : DBmanager
{
public DBdailyreportManager(string p_server, string p_dbname, string p_user, string p_password, string p_port)
: base(p_server, p_dbname, p_user, p_password, p_port)
{
}
public List<dailyReport> getAlldailyReports()
{
List<dailyReport> dailyReports = new List<dailyReport>();
try
{
String query = "SELECT startdate, stopdate, geofence_id, idx, report_id, unit_ids, email, login FROM reports r JOIN users u ON u.userid = r.userid";
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(query, connection))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
dailyReport reg = new dailyReport();
reg.intdatafrom = (Int32)reader.GetValue(0);
reg.intdatato = (Int32)reader.GetValue(1);
reg.zonename = reader.GetValue(2).ToString();
reg.idx = (Int32)reader.GetValue(3);
reg.reptype = reader.GetValue(4).ToString();
reg.unitname = (String)reader.GetValue(5);
reg.email = reader.GetValue(6).ToString();
reg.UserName = reader.GetString(7);
dailyReports.Add(reg);
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine($" getAllSafeDispatches: {e.Message}" );
}
return dailyReports;
}
public String get_Vehname_from_sc_id(String sc_id)
{
string vehname = "";
try
{
string command = " SELECT v.name FROM subscriber_history as sh " +
$" INNER JOIN vehicle as v on (v.id = sh.veh_id) WHERE sh.sc_id = {sc_id} ";
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
object result = null;
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
result = cmd.ExecuteScalar();
if (result != null)
vehname = cmd.ExecuteScalar().ToString();
}
}
}
catch (Exception e)
{
Console.WriteLine($" getAllSafeDispatches: {e.Message}", ConsoleColor.Red);
}
return vehname;
}
public String get_Vehname_from_imei(String imei)
{
string vehname = "";
try
{
string command = "SELECT v.name FROM subscriber as s" +
" INNER JOIN subscriber_history as sh on (s.sc_id = sh.sc_id) " +
" INNER JOIN vehicle as v on (v.id = sh.veh_id) where s.imei ='" + imei + "'";
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
vehname = cmd.ExecuteScalar().ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine($" getAllSafeDispatches: {ex.Message}", ConsoleColor.Red);
}
return vehname;
}
public String get_Zonename_from_ID(String ID)
{
string zonename = "";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT name FROM zonename where idx=" + ID, connection))
{
zonename = cmd.ExecuteScalar().ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine($" get_Zonename_from_ID: {ex.Message}", ConsoleColor.Red);
}
return zonename;
}
public String get_Placename_from_ID(String ID)
{
String zonename = "";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand("SELECT name FROM place where idx=" + ID, connection))
{
zonename = cmd.ExecuteScalar().ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine($" get_Placename_from_ID: {ex.Message}", ConsoleColor.Red);
}
return zonename;
}
public String delete_reports(String ID)
{
String zonename = "";
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand("DELETE FROM reports WHERE idx=" + ID.ToString(), connection))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Console.WriteLine($" get_Placename_from_ID: {ex.Message}", ConsoleColor.Red);
}
return zonename;
}
public void update_report_user(String ID, String value)
{
try
{
using (NpgsqlConnection connection = new NpgsqlConnection())
{
connection.ConnectionString = getConnectionString();
connection.Open();
string command = "UPDATE reports SET userid = (select userid FROM users WHERE login ='" + value + "') WHERE idx=" + ID.ToString();
using (NpgsqlCommand cmd = new NpgsqlCommand(command, connection))
{
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Console.WriteLine($" get_Placename_from_ID: {ex.Message}", ConsoleColor.Red);
}
}
}
}