namespace VECV_WebApi.Models.Vehicle
{
#region Namespaces
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using DBHelper;
using Npgsql;
using VECV_WebApi.Models.Authorization;
using LoggingHelper;
#endregion
#region Repository Class
///
/// This class contain method releted to vehicle
///
public class VehicleRepository
{
#region Global Variables
///
/// making object of LoggingUtility class available to this class
///
LoggingUtility objLog = new LoggingUtility();
///
/// making the data-log file path available to this class
///
string path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["PathLog"]);
///
/// making error log file path available to this class
///
string errorlogtf = (ConfigurationManager.AppSettings["ErrorLog"]);
///
/// making the Database connection string available to this class
///
private string _connStr;
///
/// making Customer Repository object available to this class
///
AuthenticationRepository objAuthorization;
#endregion
#region Contructors
///
/// Default constructor intialize connection string of vecv database
///
public VehicleRepository(string connString)
{
this._connStr = connString;
}
#endregion
#region API Methods
///
/// Tho get vehicle detail
///
/// vehicle info
/// status and vehicle details
public VehicleModel GetVehicleDetail(VehicleModel model)
{
VehicleModel objModel = new VehicleModel();
try
{
DataSet ds = new DataSet();
objAuthorization = new AuthenticationRepository();
NpgsqlParameter[] nSqlParam = new NpgsqlParameter[2];
nSqlParam[0] = new NpgsqlParameter("inid", model.Id);
nSqlParam[1] = new NpgsqlParameter("inregistration_number", model.RegistrationNo);
ds = NpgSqlHelper.ExecuteDataset(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_get_vehicle_details"], nSqlParam);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0]["id"].ToString().Trim() != null)
{
objModel.Id = ds.Tables[0].AsEnumerable().Select(s => s.Field("id")).FirstOrDefault();
objModel.RegistrationNo = ds.Tables[0].AsEnumerable().Select(s => s.Field("registration_number")).FirstOrDefault();
objModel.VehicleNumberPlate = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_number_plate")).FirstOrDefault();
objModel.ModelNumber = ds.Tables[0].AsEnumerable().Select(s => s.Field("model_number")).FirstOrDefault();
objModel.Isdeleted = ds.Tables[0].AsEnumerable().Select(s => s.Field("isdeleted")).FirstOrDefault();
objModel.VehicleType = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_type")).FirstOrDefault();
objModel.VehicleInstallationDate = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_installation_date")).FirstOrDefault() != null ? Convert.ToDateTime(ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_installation_date")).FirstOrDefault()).AddMinutes(Convert.ToInt16(model.UtcMinute)).ToString(ConfigurationManager.AppSettings["DateFormat"]) : null;
objModel.ChassisNumber = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_number_plate")).FirstOrDefault();
}
objModel.Status = "1";
}
else
{
objModel.Status = "0";
}
return objModel;
}
catch (Exception Ex)
{
objModel.Status = "0";
objModel.Message = Ex.Message;
objLog.ErrorLogFile("GetVehicleDetail", Ex.Message, path, errorlogtf);
return objModel;
}
}
///
/// To insert the detail of vehicle
///
/// vehicle details
/// contain the status 0 or 1
public VehicleModel InsertVehicleDetail(VehicleModel model)
{
VehicleModel objModel = new VehicleModel();
try
{
DataSet ds = new DataSet();
objAuthorization = new AuthenticationRepository();
if (objAuthorization.AuthenticateDevice(model.Token))
{
NpgsqlParameter[] nSqlParam = new NpgsqlParameter[6];
nSqlParam[0] = new NpgsqlParameter("inregistration_number", model.RegistrationNo);
nSqlParam[1] = new NpgsqlParameter("invehicle_number_plate", model.VehicleNumberPlate);
nSqlParam[2] = new NpgsqlParameter("inmodel_number", model.ModelNumber);
nSqlParam[3] = new NpgsqlParameter("invehicle_type", model.VehicleType);
nSqlParam[4] = new NpgsqlParameter("ininstallation_date", model.InstallationDate != null ? Convert.ToDateTime(model.InstallationDate).AddMinutes(-model.UtcMinute) : model.InstallationDate);
nSqlParam[5] = new NpgsqlParameter("inkamuser",Convert.ToBoolean(model.KamUser=="Yes")?true:false);
NpgSqlHelper.ExecuteNonQuery(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_insert_vehicle"], nSqlParam);
// For Telematics Use
//NpgSqlHelper.ExecuteNonQuery(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_sync_vehicle_details"]);
objModel.Status = "1";
}
else
{
objModel.Status = "0";
objModel.Message = ConfigurationManager.AppSettings["DeviceConfigurationTokenMessage"].ToString();
}
return objModel;
}
catch (Exception Ex)
{
objModel.Status = "0";
objModel.Message = Ex.Message;
objLog.ErrorLogFile("InsertVehicleDetail", Ex.Message, path, errorlogtf);
return objModel;
}
}
///
/// To update the detail of vehicle
///
/// vehicle details
/// contain the status 0 or 1
public VehicleModel UpdateVehicleDetail(VehicleModel model)
{
VehicleModel objModel = new VehicleModel();
try
{
DataSet ds = new DataSet();
objAuthorization = new AuthenticationRepository();
if (objAuthorization.AuthenticateDevice(model.Token))
{
NpgsqlParameter[] nSqlParam = new NpgsqlParameter[2];
nSqlParam[0] = new NpgsqlParameter("inregistration_number", model.RegistrationNo);
nSqlParam[1] = new NpgsqlParameter("invehicle_number_plate", model.VehicleNumberPlate);
NpgSqlHelper.ExecuteNonQuery(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_update_vehicle_detail"], nSqlParam);
// For Telematics Use
//NpgSqlHelper.ExecuteNonQuery(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_sync_vehicle_details"]);
objModel.Status = "1";
}
else
{
objModel.Status = "0";
objModel.Message = ConfigurationManager.AppSettings["DeviceConfigurationTokenMessage"].ToString();
}
return objModel;
}
catch (Exception Ex)
{
objModel.Status = "0";
objModel.Message = Ex.Message;
objLog.ErrorLogFile("UpdateVehicleDetail", Ex.Message, path, errorlogtf);
return objModel;
}
}
//Mobile OTP CR
///
/// To update the detail of vehicle
///
/// vehicle details
/// contain the status 0 or 1
public CheckMobileModel CheckOTPMobile(CheckMobileModel model)
{
CheckMobileModel response_model = new CheckMobileModel();
try
{
DataSet ds = new DataSet();
objAuthorization = new AuthenticationRepository();
if (objAuthorization.AuthenticateDevice(model.Token))
{
NpgsqlParameter[] nSqlParam = new NpgsqlParameter[3];
nSqlParam[0] = new NpgsqlParameter("in_ticket_id", model.ticketID);
nSqlParam[1] = new NpgsqlParameter("in_van_id", model.vanID);
nSqlParam[2] = new NpgsqlParameter("in_otp", model.OTP);
ds = NpgSqlHelper.ExecuteDataset(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_check_ticket_otp"], nSqlParam);
response_model.response = Convert.ToBoolean(ds.Tables[0].Rows[0][0]);
}
return response_model;
}
catch (Exception Ex)
{
response_model.response = false;
objLog.ErrorLogFile("CheckOTPsenttoUser", Ex.Message, path, errorlogtf);
return response_model;
}
}
//priya
public VehicleModel GetchessisDetail(VehicleModel model)
{
VehicleModel objModel = new VehicleModel();
try
{
DataSet ds = new DataSet();
objAuthorization = new AuthenticationRepository();
NpgsqlParameter[] nSqlParam = new NpgsqlParameter[2];
nSqlParam[0] = new NpgsqlParameter("inid", model.Id);
nSqlParam[1] = new NpgsqlParameter("inregistration_number", model.RegistrationNo);
ds = NpgSqlHelper.ExecuteDataset(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_get_vehicle_details"], nSqlParam);
if (ds.Tables[0].Rows.Count > 0)
{
if (ds.Tables[0].Rows[0]["id"].ToString().Trim() != null)
{
objModel.Id = ds.Tables[0].AsEnumerable().Select(s => s.Field("id")).FirstOrDefault();
objModel.RegistrationNo = ds.Tables[0].AsEnumerable().Select(s => s.Field("registration_number")).FirstOrDefault();
objModel.VehicleNumberPlate = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_number_plate")).FirstOrDefault();
objModel.ModelNumber = ds.Tables[0].AsEnumerable().Select(s => s.Field("model_number")).FirstOrDefault();
objModel.Isdeleted = ds.Tables[0].AsEnumerable().Select(s => s.Field("isdeleted")).FirstOrDefault();
objModel.VehicleType = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_type")).FirstOrDefault();
objModel.VehicleInstallationDate = ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_installation_date")).FirstOrDefault() != null ? Convert.ToDateTime(ds.Tables[0].AsEnumerable().Select(s => s.Field("vehicle_installation_date")).FirstOrDefault()).AddMinutes(Convert.ToInt16(model.UtcMinute)).ToString(ConfigurationManager.AppSettings["DateFormat"]) : null;
objModel.ChassisNumber = ds.Tables[0].AsEnumerable().Select(s => s.Field("_chassis_number")).FirstOrDefault();
}
objModel.Status = "1";
}
else
{
objModel.Status = "0";
}
return objModel;
}
catch (Exception Ex)
{
objModel.Status = "0";
objModel.Message = Ex.Message;
objLog.ErrorLogFile("GetVehicleDetail", Ex.Message, path, errorlogtf);
return objModel;
}
}
public string updateVehicleConnectedFlag(string ticketid,string vehiclestatus)
{
VehicleModel objModel = new VehicleModel();
string strMsg = "";
try
{
DataSet ds = new DataSet();
objAuthorization = new AuthenticationRepository();
NpgsqlParameter[] nSqlParam = new NpgsqlParameter[2];
nSqlParam[0] = new NpgsqlParameter("inticketId", ticketid);
nSqlParam[1] = new NpgsqlParameter("invehiclestatus", vehiclestatus);
ds = NpgSqlHelper.ExecuteDataset(_connStr, CommandType.StoredProcedure, ConfigurationManager.AppSettings["usp_update_vehicle_connected_flag"], nSqlParam);
if (ds.Tables[0].Rows.Count > 0)
{
strMsg = "success";
objModel.Status = "1";
}
else
{
strMsg = "un successful";
objModel.Status = "0";
}
return strMsg;
}
catch (Exception Ex)
{
objModel.Status = "0";
objModel.Message = Ex.Message;
objLog.ErrorLogFile("GetVehicleDetail", Ex.Message, path, errorlogtf);
return strMsg;
}
}
#endregion
}
#endregion
}