using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Ease.Core.DataAccess;
namespace HRM.DA
{
///
/// The SQL the Employee Bulk Upload save needs besides the app's own HREmployeeDA inserts:
/// finding employees and existing profile records, and updating ONLY the columns an upload
/// fills — so nothing the file does not mention is overwritten. Table and column names are
/// always constants from EmployeeBulkUploadService, never text from the file; values go
/// through MakeSQL's typed placeholders.
///
internal static class EmployeeBulkUploadDA
{
/// How a value is written into SQL.
internal enum SqlKind
{
Text, // %s
Unicode, // %u — the Bangla columns
Number, // %n — IDs, enums, numbers
Date // %d
}
#region Get
///
/// EmployeeID, EmployeeNo, PayrollTypeID, GradeID, TaxCircle and Status of every employee — any payroll type, any
/// status — whose Employee No is one of .
///
internal static IDataReader GetEmployeesByNo(TransactionContext tc, IEnumerable employeeNos)
{
// Each number is quoted and escaped by MakeSQL's %s before it is joined into the IN list.
string list = string.Join(",", employeeNos.Select(x => SQLParser.MakeSQL("%s", x)));
return tc.ExecuteReader(
"SELECT EmployeeID, EmployeeNo, PayrollTypeID, GradeID, TaxCircle, Status FROM Employee WHERE EmployeeNo IN (%q)", list);
}
/// The employee's contact row, or 0 when it has none.
internal static int GetContactID(TransactionContext tc, int employeeID)
{
object id = tc.ExecuteScalar("SELECT MIN(ContactID) FROM EmpContact WHERE EmployeeID=%n", employeeID);
return id == null || id == DBNull.Value ? 0 : Convert.ToInt32(id);
}
/// The ID (as RecordID) and the given key columns of an employee's records in one table.
internal static IDataReader GetRecords(TransactionContext tc, string table, string idColumn,
IEnumerable keyColumns, int employeeID)
{
return tc.ExecuteReader("SELECT %q AS RecordID, %q FROM %q WHERE EmployeeID=%n",
idColumn, string.Join(", ", keyColumns), table, employeeID);
}
/// The nomination purposes whose shares add up to more than 100% for the employee.
internal static IDataReader GetNomineeShareOverflow(TransactionContext tc, int employeeID)
{
return tc.ExecuteReader(
"SELECT NominationPurposeID, SUM(Percentage) AS TotalPercentage FROM EmpNominee "
+ "WHERE EmployeeID=%n GROUP BY NominationPurposeID HAVING SUM(Percentage) > 100", employeeID);
}
#endregion
#region Update
///
/// Sets only the given columns of one row: UPDATE table SET c1=v1, c2=v2 WHERE idColumn=id.
/// Does nothing when there are no columns.
///
internal static void UpdateColumns(TransactionContext tc, string table, string idColumn, int id,
IEnumerable>> columns)
{
List assignments = columns
.Select(x => SQLParser.MakeSQL("%q=" + Placeholder(x.Value.Key), x.Key, x.Value.Value))
.ToList();
if (assignments.Count == 0)
{
return;
}
tc.ExecuteNonQuery("UPDATE %q SET %q WHERE %q=%n", table, string.Join(", ", assignments), idColumn, id);
}
private static string Placeholder(SqlKind kind)
{
switch (kind)
{
case SqlKind.Unicode: return "%u";
case SqlKind.Number: return "%n";
case SqlKind.Date: return "%d";
default: return "%s";
}
}
#endregion
}
}