99 lines
4.1 KiB
C#
99 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using Ease.Core.DataAccess;
|
|
|
|
namespace HRM.DA
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
internal static class EmployeeBulkUploadDA
|
|
{
|
|
/// <summary>How a value is written into SQL.</summary>
|
|
internal enum SqlKind
|
|
{
|
|
Text, // %s
|
|
Unicode, // %u — the Bangla columns
|
|
Number, // %n — IDs, enums, numbers
|
|
Date // %d
|
|
}
|
|
|
|
#region Get
|
|
|
|
/// <summary>
|
|
/// EmployeeID, EmployeeNo, PayrollTypeID, GradeID, TaxCircle and Status of every employee — any payroll type, any
|
|
/// status — whose Employee No is one of <paramref name="employeeNos"/>.
|
|
/// </summary>
|
|
internal static IDataReader GetEmployeesByNo(TransactionContext tc, IEnumerable<string> 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);
|
|
}
|
|
|
|
/// <summary>The employee's contact row, or 0 when it has none.</summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>The ID (as RecordID) and the given key columns of an employee's records in one table.</summary>
|
|
internal static IDataReader GetRecords(TransactionContext tc, string table, string idColumn,
|
|
IEnumerable<string> keyColumns, int employeeID)
|
|
{
|
|
return tc.ExecuteReader("SELECT %q AS RecordID, %q FROM %q WHERE EmployeeID=%n",
|
|
idColumn, string.Join(", ", keyColumns), table, employeeID);
|
|
}
|
|
|
|
/// <summary>The nomination purposes whose shares add up to more than 100% for the employee.</summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
internal static void UpdateColumns(TransactionContext tc, string table, string idColumn, int id,
|
|
IEnumerable<KeyValuePair<string, KeyValuePair<SqlKind, object>>> columns)
|
|
{
|
|
List<string> 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
|
|
}
|
|
}
|