context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using MixERP.Net.DbFactory;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using Npgsql;
  using PetaPoco;
  using Serilog;
  
  namespace MixERP.Net.Schemas.Core.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database view "core.shipping_address_scrud_view".
      /// </summary>
      public class ShippingAddressScrudView : DbAccess, IShippingAddressScrudViewRepository
      {
          /// <summary>
          /// The schema of this view. Returns literal "core".
          /// </summary>
          public override string _ObjectNamespace => "core";
  
          /// <summary>
          /// The schema unqualified name of this view. Returns literal "shipping_address_scrud_view".
          /// </summary>
          public override string _ObjectName => "shipping_address_scrud_view";
  
          /// <summary>
          /// Login id of application user accessing this view.
          /// </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 view "core.shipping_address_scrud_view".
          /// </summary>
          /// <returns>Returns the number of rows of the view "core.shipping_address_scrud_view".</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 \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM core.shipping_address_scrud_view;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the view "core.shipping_address_scrud_view" to return all instances of the "ShippingAddressScrudView" class. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "ShippingAddressScrudView" 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.Core.ShippingAddressScrudView> Get()
          {
              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 \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.shipping_address_scrud_view ORDER BY 1;";
              return Factory.Get<MixERP.Net.Entities.Core.ShippingAddressScrudView>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Displayfields provide a minimal name/value context for data binding the row collection of core.shipping_address_scrud_view.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the view core.shipping_address_scrud_view</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 \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT shipping_address_id AS key, shipping_address_code as value FROM core.shipping_address_scrud_view;";
              using (NpgsqlCommand command = new NpgsqlCommand(sql))
              {
                  using (DataTable table = DbOperation.GetDataTable(this._Catalog, command))
                  {
                      if (table?.Rows == null || table.Rows.Count == 0)
                      {
                          return displayFields;
                      }
  
                      foreach (DataRow row in table.Rows)
                      {
                          if (row != null)
                          {
                              DisplayField displayField = new DisplayField
                              {
                                  Key = row["key"].ToString(),
                                  Value = row["value"].ToString()
                              };
  
                              displayFields.Add(displayField);
                          }
                      }
                  }
              }
  
              return displayFields;
          }
  
  
  
          /// <summary>
          /// Performs a select statement on the view "core.shipping_address_scrud_view" producing a paginated result of 10.
          /// </summary>
          /// <returns>Returns the first page of collection of "ShippingAddressScrudView" 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.Core.ShippingAddressScrudView> GetPaginatedResult()
          {
              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 first page of the entity \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}.", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.shipping_address_scrud_view ORDER BY 1 LIMIT 10 OFFSET 0;";
              return Factory.Get<MixERP.Net.Entities.Core.ShippingAddressScrudView>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a select statement on the view "core.shipping_address_scrud_view" producing a paginated result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paginated result.</param>
          /// <returns>Returns collection of "ShippingAddressScrudView" 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.Core.ShippingAddressScrudView> GetPaginatedResult(long pageNumber)
          {
              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 Page #{Page} of the entity \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}.", pageNumber, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              long offset = (pageNumber - 1) * 10;
              const string sql = "SELECT * FROM core.shipping_address_scrud_view ORDER BY 1 LIMIT 10 OFFSET @0;";
  
              return Factory.Get<MixERP.Net.Entities.Core.ShippingAddressScrudView>(this._Catalog, sql, offset);
          }
  
          public List<EntityParser.Filter> GetFilters(string catalog, string filterName)
          {
              const string sql = "SELECT * FROM core.filters WHERE object_name='core.shipping_address_scrud_view' AND lower(filter_name)=lower(@0);";
              return Factory.Get<EntityParser.Filter>(catalog, sql, filterName).ToList();
          }
  
          /// <summary>
          /// Performs a filtered count on view "core.shipping_address_scrud_view".
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns number of rows of "ShippingAddressScrudView" class using the filter.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public long CountWhere(List<EntityParser.Filter> filters)
          {
              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 \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}. Filters: {Filters}.", this._LoginId, filters);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              Sql sql = Sql.Builder.Append("SELECT COUNT(*) FROM core.shipping_address_scrud_view WHERE 1 = 1");
              MixERP.Net.EntityParser.Data.Service.AddFilters(ref sql, new MixERP.Net.Entities.Core.ShippingAddressScrudView(), filters);
  
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a filtered select statement on view "core.shipping_address_scrud_view" producing a paginated result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paginated result. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns collection of "ShippingAddressScrudView" 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.Core.ShippingAddressScrudView> GetWhere(long pageNumber, List<EntityParser.Filter> filters)
          {
              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 Page #{Page} of the filtered entity \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}. Filters: {Filters}.", pageNumber, this._LoginId, filters);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              long offset = (pageNumber - 1) * 10;
              Sql sql = Sql.Builder.Append("SELECT * FROM core.shipping_address_scrud_view WHERE 1 = 1");
  
              MixERP.Net.EntityParser.Data.Service.AddFilters(ref sql, new MixERP.Net.Entities.Core.ShippingAddressScrudView(), filters);
  
              sql.OrderBy("1");
  
              if (pageNumber > 0)
              {
                  sql.Append("LIMIT @0", 10);
                  sql.Append("OFFSET @0", offset);
              }
  
              return Factory.Get<MixERP.Net.Entities.Core.ShippingAddressScrudView>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a filtered count on view "core.shipping_address_scrud_view".
          /// </summary>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns number of rows of "ShippingAddressScrudView" class using the filter.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public long CountFiltered(string filterName)
          {
              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 \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}. Filter: {Filter}.", this._LoginId, filterName);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              List<EntityParser.Filter> filters = this.GetFilters(this._Catalog, filterName);
              Sql sql = Sql.Builder.Append("SELECT COUNT(*) FROM core.shipping_address_scrud_view WHERE 1 = 1");
              MixERP.Net.EntityParser.Data.Service.AddFilters(ref sql, new MixERP.Net.Entities.Core.ShippingAddressScrudView(), filters);
  
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a filtered select statement on view "core.shipping_address_scrud_view" producing a paginated result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paginated result. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns collection of "ShippingAddressScrudView" 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.Core.ShippingAddressScrudView> GetFiltered(long pageNumber, string filterName)
          {
              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 Page #{Page} of the filtered entity \"ShippingAddressScrudView\" was denied to the user with Login ID {LoginId}. Filter: {Filter}.", pageNumber, this._LoginId, filterName);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              List<EntityParser.Filter> filters = this.GetFilters(this._Catalog, filterName);
  
  
gt:             long offset = (pageNumber - 1) * 10;