context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Data;
  using System.Dynamic;
  using System.Linq;
  using MixERP.Net.DbFactory;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using MixERP.Net.Framework.Extensions;
  using Npgsql;
  using PetaPoco;
  using Serilog;
  
  namespace MixERP.Net.Core.Modules.HRM.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "hrm.leave_types".
      /// </summary>
      public class LeaveType : DbAccess, ILeaveTypeRepository
      {
          /// <summary>
          /// The schema of this table. Returns literal "hrm".
          /// </summary>
          public override string _ObjectNamespace => "hrm";
  
          /// <summary>
          /// The schema unqualified name of this table. Returns literal "leave_types".
          /// </summary>
          public override string _ObjectName => "leave_types";
  
          /// <summary>
          /// Login id of application user accessing this table.
          /// </summary>
          public long _LoginId { get; set; }
  
          /// <summary>
          /// User id of application user accessing this table.
          /// </summary>
          public int _UserId { get; set; }
  
          /// <summary>
          /// The name of the database on which queries are being executed to.
          /// </summary>
          public string _Catalog { get; set; }
  
          /// <summary>
          /// Performs SQL count on the table "hrm.leave_types".
          /// </summary>
          /// <returns>Returns the number of rows of the table "hrm.leave_types".</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public long Count()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return 0;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"LeaveType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM hrm.leave_types;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.leave_types" to return all instances of the "LeaveType" class. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "LeaveType" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<MixERP.Net.Entities.HRM.LeaveType> GetAll()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"LeaveType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types ORDER BY leave_type_id;";
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.leave_types" to return all instances of the "LeaveType" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "LeaveType" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<dynamic> Export()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"LeaveType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types ORDER BY leave_type_id;";
              return Factory.Get<dynamic>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.leave_types" with a where filter on the column "leave_type_id" to return a single instance of the "LeaveType" class. 
          /// </summary>
          /// <param name="leaveTypeId">The column "leave_type_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "LeaveType" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.HRM.LeaveType Get(int leaveTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get entity \"LeaveType\" filtered by \"LeaveTypeId\" with value {LeaveTypeId} was denied to the user with Login ID {_LoginId}", leaveTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types WHERE leave_type_id=@0;";
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql, leaveTypeId).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the first record of the table "hrm.leave_types". 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instance of "LeaveType" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.HRM.LeaveType GetFirst()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the first record of entity \"LeaveType\" was denied to the user with Login ID {_LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types ORDER BY leave_type_id LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the previous record of the table "hrm.leave_types" sorted by leaveTypeId.
          /// </summary>
          /// <param name="leaveTypeId">The column "leave_type_id" parameter used to find the next record.</param>
          /// <returns>Returns a non-live, non-mapped instance of "LeaveType" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.HRM.LeaveType GetPrevious(int leaveTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the previous entity of \"LeaveType\" by \"LeaveTypeId\" with value {LeaveTypeId} was denied to the user with Login ID {_LoginId}", leaveTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types WHERE leave_type_id < @0 ORDER BY leave_type_id DESC LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql, leaveTypeId).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the next record of the table "hrm.leave_types" sorted by leaveTypeId.
          /// </summary>
          /// <param name="leaveTypeId">The column "leave_type_id" parameter used to find the next record.</param>
          /// <returns>Returns a non-live, non-mapped instance of "LeaveType" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.HRM.LeaveType GetNext(int leaveTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the next entity of \"LeaveType\" by \"LeaveTypeId\" with value {LeaveTypeId} was denied to the user with Login ID {_LoginId}", leaveTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types WHERE leave_type_id > @0 ORDER BY leave_type_id LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql, leaveTypeId).FirstOrDefault();
          }
  
  
          /// <summary>
          /// Gets the last record of the table "hrm.leave_types". 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instance of "LeaveType" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public MixERP.Net.Entities.HRM.LeaveType GetLast()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the last record of entity \"LeaveType\" was denied to the user with Login ID {_LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types ORDER BY leave_type_id DESC LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql).FirstOrDefault();
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.leave_types" with a where filter on the column "leave_type_id" to return a multiple instances of the "LeaveType" class. 
          /// </summary>
          /// <param name="leaveTypeIds">Array of column "leave_type_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped collection of "LeaveType" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<MixERP.Net.Entities.HRM.LeaveType> Get(int[] leaveTypeIds)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to entity \"LeaveType\" was denied to the user with Login ID {LoginId}. leaveTypeIds: {leaveTypeIds}.", this._LoginId, leaveTypeIds);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.leave_types WHERE leave_type_id IN (@0);";
  
              return Factory.Get<MixERP.Net.Entities.HRM.LeaveType>(this._Catalog, sql, leaveTypeIds);
          }
  
          /// <summary>
          /// Custom fields are user defined form elements for hrm.leave_types.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection for the table hrm.leave_types</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<PetaPoco.CustomField> GetCustomFields(string resourceId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get custom fields for entity \"LeaveType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              string sql;
              if (string.IsNullOrWhiteSpace(resourceId))
              {
                  sql = "SELECT * FROM core.custom_field_definition_view WHERE table_name='hrm.leave_types' ORDER BY field_order;";
                  return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql);
              }
  
              sql = "SELECT * from core.get_custom_field_definition('hrm.leave_types'::text, @0::text) ORDER BY field_order;";
              return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql, resourceId);
          }
  
          /// <summary>
          /// Displayfields provide a minimal name/value context for data binding the row collection of hrm.leave_types.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table hrm.leave_types</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<DisplayField> GetDisplayFields()
          {
              List<DisplayField> displayFields = new List<DisplayField>();
  
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return displayFields;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get display field for entity \"LeaveType\" was denied to the user with Login ID {LoginId}", this._LoginId);
  
gt:                     throw new UnauthorizedException("Access is denied.");