context: // ReSharper disable All
  /********************************************************************************
  Copyright (C) MixERP Inc. (http://mixof.org).
  
  This file is part of MixERP.
  
  MixERP is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, version 2 of the License.
  
  
  MixERP is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with MixERP.  If not, see <http://www.gnu.org/licenses/>.
  ***********************************************************************************/
  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.Core.Modules.HRM.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "hrm.employee_identification_details".
      /// </summary>
      public class EmployeeIdentificationDetail : DbAccess
      {
          /// <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 "employee_identification_details".
          /// </summary>
          public override string _ObjectName => "employee_identification_details";
  
          /// <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.employee_identification_details".
          /// </summary>
          /// <returns>Returns the number of rows of the table "hrm.employee_identification_details".</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, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"EmployeeIdentificationDetail\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM hrm.employee_identification_details;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.employee_identification_details" to return a all instances of the "EmployeeIdentificationDetail" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "EmployeeIdentificationDetail" 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.EmployeeIdentificationDetail> Get()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"EmployeeIdentificationDetail\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.employee_identification_details ORDER BY employee_identification_detail_id;";
              return Factory.Get<MixERP.Net.Entities.HRM.EmployeeIdentificationDetail>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.employee_identification_details" with a where filter on the column "employee_identification_detail_id" to return a single instance of the "EmployeeIdentificationDetail" class. 
          /// </summary>
          /// <param name="employeeIdentificationDetailId">The column "employee_identification_detail_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "EmployeeIdentificationDetail" 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.EmployeeIdentificationDetail Get(long employeeIdentificationDetailId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get entity \"EmployeeIdentificationDetail\" filtered by \"EmployeeIdentificationDetailId\" with value {EmployeeIdentificationDetailId} was denied to the user with Login ID {_LoginId}", employeeIdentificationDetailId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.employee_identification_details WHERE employee_identification_detail_id=@0;";
              return Factory.Get<MixERP.Net.Entities.HRM.EmployeeIdentificationDetail>(this._Catalog, sql, employeeIdentificationDetailId).FirstOrDefault();
          }
  
          /// <summary>
          /// Executes a select query on the table "hrm.employee_identification_details" with a where filter on the column "employee_identification_detail_id" to return a multiple instances of the "EmployeeIdentificationDetail" class. 
          /// </summary>
          /// <param name="employeeIdentificationDetailIds">Array of column "employee_identification_detail_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped collection of "EmployeeIdentificationDetail" 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.EmployeeIdentificationDetail> Get(long[] employeeIdentificationDetailIds)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to entity \"EmployeeIdentificationDetail\" was denied to the user with Login ID {LoginId}. employeeIdentificationDetailIds: {employeeIdentificationDetailIds}.", this._LoginId, employeeIdentificationDetailIds);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM hrm.employee_identification_details WHERE employee_identification_detail_id IN (@0);";
  
              return Factory.Get<MixERP.Net.Entities.HRM.EmployeeIdentificationDetail>(this._Catalog, sql, employeeIdentificationDetailIds);
          }
  
          /// <summary>
          /// Custom fields are user defined form elements for hrm.employee_identification_details.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection for the table hrm.employee_identification_details</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, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get custom fields for entity \"EmployeeIdentificationDetail\" 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.employee_identification_details' ORDER BY field_order;";
                  return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql);
              }
  
              sql = "SELECT * from core.get_custom_field_definition('hrm.employee_identification_details'::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.employee_identification_details.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table hrm.employee_identification_details</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, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get display field for entity \"EmployeeIdentificationDetail\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT employee_identification_detail_id AS key, identification_type_code as value FROM hrm.employee_identification_details;";
              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>
          /// Inserts or updates the instance of EmployeeIdentificationDetail class on the database table "hrm.employee_identification_details".
          /// </summary>
          /// <param name="employeeIdentificationDetail">The instance of "EmployeeIdentificationDetail" class to insert or update.</param>
          /// <param name="customFields">The custom field collection.</param>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public object AddOrEdit(MixERP.Net.Entities.HRM.EmployeeIdentificationDetail employeeIdentificationDetail, List<EntityParser.CustomField> customFields)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              object primaryKeyValue;
  
              employeeIdentificationDetail.AuditUserId = this._UserId;
              employeeIdentificationDetail.AuditTs = System.DateTime.UtcNow;
  
              if (employeeIdentificationDetail.EmployeeIdentificationDetailId > 0)
              {
                  primaryKeyValue = employeeIdentificationDetail.EmployeeIdentificationDetailId;
                  this.Update(employeeIdentificationDetail, employeeIdentificationDetail.EmployeeIdentificationDetailId);
              }
              else
              {
                  primaryKeyValue = this.Add(employeeIdentificationDetail);
              }
  
              string sql = "DELETE FROM core.custom_fields WHERE custom_field_setup_id IN(" +
                           "SELECT custom_field_setup_id " +
                           "FROM core.custom_field_setup " +
                           "WHERE form_name=core.get_custom_field_form_name('hrm.employee_identification_details')" +
                           ");";
  
              Factory.NonQuery(this._Catalog, sql);
  
  
gt:             if (customFields == null)