2019-10-10 17:13:30 +00:00
using Dapper ;
using System ;
using System.Collections.Generic ;
using System.Configuration ;
using System.Data ;
using System.Data.SQLite ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
namespace QueueSys
{
2019-10-10 17:36:32 +00:00
public class CustomerView
2019-10-10 17:13:30 +00:00
{
2019-10-12 04:30:25 +00:00
public string Customer { get ; }
public string Status { get ; }
2019-10-15 15:03:15 +00:00
}
public class ReportView
{
public int id { get ; }
public string customer_name { get ; set ; }
public int num_bags { get ; set ; }
public string employee { get ; set ; }
public int status { get ; set ; }
public string start_time { get ; set ; }
public string end_time { get ; set ; }
public string time_elapsed { get ; set ; }
2019-10-10 17:13:30 +00:00
}
2019-10-10 17:36:32 +00:00
public class EmployeeView
2019-10-10 17:13:30 +00:00
{
2019-10-16 22:10:44 +00:00
public int id { get ; set ; }
2019-10-10 17:13:30 +00:00
public string customer_name { get ; set ; }
2019-10-15 20:04:22 +00:00
}
2019-10-31 16:25:43 +00:00
2019-10-15 20:04:22 +00:00
public class StatusModel
{
public string status { get ; set ; }
2019-10-10 17:13:30 +00:00
}
2019-11-07 17:48:30 +00:00
public class endTimeModel
{
public string end_time { get ; set ; }
}
2019-10-10 17:13:30 +00:00
public class SqliteDataAccess
{
2019-10-12 04:30:25 +00:00
public List < EmployeeView > LoadEmployeeView ( )
2019-10-10 17:13:30 +00:00
{
//A using statement protects us as a failsafe: it guarantees the connection to the
//database will be closed, even in event of an application or computer crash.
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
2019-10-15 20:04:22 +00:00
string strQuery = "SELECT id,customer_name FROM active_customers;" ;
2019-10-11 22:28:31 +00:00
2019-10-10 17:36:32 +00:00
var output = cnn . Query < EmployeeView > ( strQuery , new DynamicParameters ( ) ) ;
2019-10-10 17:13:30 +00:00
return output . ToList ( ) ;
}
}
2019-10-15 20:04:22 +00:00
public List < StatusModel > GetCxStatus ( int customer_id )
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
string strQuery = "SELECT status_lookup.status FROM active_customers INNER JOIN status_lookup on active_customers.status_id=status_lookup.status_id WHERE id=@id;" ;
var output = cnn . Query < StatusModel > ( strQuery , new { id = customer_id } ) ;
return output . ToList ( ) ;
}
}
2019-10-12 04:30:25 +00:00
public List < CustomerView > LoadCustomerView ( )
2019-10-10 17:13:30 +00:00
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
2019-10-15 15:03:15 +00:00
string strQuery = "SELECT customer_name as Customer,status_lookup.status as Status FROM active_customers INNER JOIN status_lookup on active_customers.status_id=status_lookup.status_id ORDER BY active_customers.status_id" ;
2019-10-10 17:36:32 +00:00
var output = cnn . Query < CustomerView > ( strQuery , new DynamicParameters ( ) ) ;
2019-10-10 17:13:30 +00:00
return output . ToList ( ) ;
}
}
2019-10-15 15:42:17 +00:00
public void SaveCustomer ( ReportView customer )
2019-10-10 17:13:30 +00:00
{
2019-10-11 22:28:31 +00:00
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
2019-10-12 06:45:10 +00:00
string strQuery = "INSERT INTO active_customers (customer_name, num_bags, employee, status_id) VALUES (@customer_name, @num_bags, @employee, 3)" ;
2019-10-11 22:28:31 +00:00
cnn . Execute ( strQuery , customer ) ;
}
}
2019-10-16 22:10:44 +00:00
public void ArchiveCustomer ( EmployeeView customer )
2019-10-11 22:28:31 +00:00
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
2019-11-07 17:48:30 +00:00
//Check to see if the customer already has an end_time
string strQuery = "SELECT end_time FROM active_customers WHERE id=@id" ;
List < endTimeModel > etm = new List < endTimeModel > ( ) ;
var output = cnn . Query < endTimeModel > ( strQuery , new { id = customer . id } ) ;
etm = output . ToList ( ) ;
if ( etm [ 0 ] . end_time ! = null )
{
strQuery = "INSERT INTO archive(id, customer_name,num_bags,employee,status_id,start_time,end_time) SELECT id,customer_name,num_bags,employee,status_id,start_time,end_time FROM active_customers WHERE id=@id" ;
}
else
{
strQuery = "INSERT INTO archive(id, customer_name,num_bags,employee,status_id,start_time) SELECT id,customer_name,num_bags,employee,status_id,start_time FROM active_customers WHERE id=@id" ;
}
2019-10-11 22:28:31 +00:00
cnn . Execute ( strQuery , customer ) ;
2019-10-16 22:10:44 +00:00
strQuery = "DELETE FROM active_customers WHERE id=@id" ;
cnn . Execute ( strQuery , customer ) ;
2019-10-11 22:28:31 +00:00
}
2019-10-10 17:13:30 +00:00
}
2019-10-15 20:04:22 +00:00
public List < StatusModel > LoadStatusStrings ( )
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
string strQuery = "SELECT status FROM status_lookup" ;
var output = cnn . Query < StatusModel > ( strQuery , new DynamicParameters ( ) ) ;
return output . ToList ( ) ;
}
}
2019-10-16 22:10:44 +00:00
public void UpdateCustomerStatus ( int customer_id_in , string status_in )
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
string strQuery = "UPDATE active_customers SET status_id = (SELECT status_lookup.status_id from status_lookup WHERE status_lookup.status = @status_str) WHERE id=@customer_id" ;
cnn . Execute ( strQuery , new { customer_id = customer_id_in , status_str = status_in } ) ;
}
}
2019-11-07 17:48:30 +00:00
public void UpdateEndTime ( int customer_id_in , string trans )
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
switch ( trans )
{
case "add" :
{
string strQuery = "UPDATE active_customers SET end_time = datetime() WHERE id=@customer_id" ;
cnn . Execute ( strQuery , new { customer_id = customer_id_in } ) ;
break ;
}
case "remove" :
{
string strQuery = "UPDATE active_customers SET end_time = NULL WHERE id=@customer_id" ;
cnn . Execute ( strQuery , new { customer_id = customer_id_in } ) ;
break ;
}
}
}
}
2019-10-25 03:58:52 +00:00
public List < ReportView > QueryReports ( )
{
using ( IDbConnection cnn = new SQLiteConnection ( LoadConnectionString ( ) ) )
{
2019-10-31 16:25:43 +00:00
//Clear old customers from the archive table.
string strQuery = "DELETE FROM archive WHERE julianday(end_time) < julianday('now','-6 months')" ;
cnn . Execute ( strQuery ) ;
strQuery = "SELECT *,Cast ((JulianDay(end_time) - JulianDay(start_time)) * 24 * 60 As Integer) as time_elapsed from archive" ;
2019-10-25 03:58:52 +00:00
var output = cnn . Query < ReportView > ( strQuery , new DynamicParameters ( ) ) ;
return output . ToList ( ) ;
}
}
2019-10-10 17:13:30 +00:00
private static string LoadConnectionString ( string id = "Default" )
{
return ConfigurationManager . ConnectionStrings [ id ] . ConnectionString ;
}
}
}