Files
hrm_eva/Models/external_employee/external_employeeService.cs
2020-05-24 00:33:22 +07:00

433 lines
20 KiB
C#

using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TTSW.EF;
using TTSW.Utils;
using TTSW.Constant;
using TTSW.Common;
using TodoAPI2.Models;
using System.IO;
using System.Web;
using System.Net;
using TTSW.Configure;
using Microsoft.Extensions.Options;
using System.Data;
using Npgsql;
namespace TodoAPI2.Models
{
public class external_employeeService : Iexternal_employeeService
{
private IMyDatabase db;
public external_employeeService(IMyDatabase mydb)
{
db = mydb;
}
public int? GetLeader(int? emp_id)
{
var sql = @"
SELECT ha.employee_id as emp_id,
CONCAT(hta.title_name,ha.firstname,' ',ha.lastname) as emp_name,
ha.chief_id,
CONCAT(htb.title_name,hb.firstname,' ',hb.lastname) as chief_name
FROM public.hrm_employees as ha
left join public.hrm_employees as hb on hb.employee_id = ha.chief_id
left join public.hrm_title_masters as hta on hta.id = ha.prefix_card_name
left join public.hrm_title_masters as htb on hb.prefix_card_name = htb.id
where ha.employee_id = @emp_id and ha.workingstatus = 'สถานะปฏิบัติงาน';
";
var para = db.GetParameterListNpgsql();
var ix = new NpgsqlParameter("emp_id", DbType.Int32);
ix.Value = emp_id;
para.Add(ix);
DataTable dt = db.ExecuteDataTableNpgsql(sql, para);
if (dt.Rows.Count == 1)
{
if(dt.Rows[0]["chief_id"] == DBNull.Value)
{
return null;
}
else
{
return Convert.ToInt32(dt.Rows[0]["chief_id"]);
}
}
return null;
}
public List<external_employeeViewModel> GetListByemployee_type(int? employee_type, int? position_type)
{
var sql = string.Format(@"
select he.employee_id as id, mpn.position_number,he.employee_no,he.position_no,
CONCAT(htm.title_name,' ',he.firstname,' ',he.lastname) as fullname,opd.position_name,
CONCAT(he.firstname,he.lastname) as halfname,
orgdata.id as department_id,orgdata.department_name,orgdata.department_code,he.salary,
he.employee_type_id, het.employee_type_name,opd.position_id as position_id,
u.email as user_email, u.id as user_id,hpl.position_level_name,he.position_level_id,
he.position_type_id,hpt.position_type_name,he.packing_date,
(CASE WHEN (SELECT hrm_work_rec.id FROM hrm_working_records as hrm_work_rec
WHERE hrm_work_rec.employee_id = he.employee_id
AND hrm_work_rec.start_date <= now()
AND hrm_work_rec.end_date >= now()
AND hrm_work_rec.deleted_at IS null
LIMIT 1) IS NOT null THEN 'ช่วยปฎิบัติหน้าที่'
ELSE null END) as remark_hrm_work_record,
org_type_depart.department_degree_id
from public.hrm_employees as he
left join public.hrm_position_types as hpt on he.position_type_id=hpt.id
left join public.hrm_position_levels as hpl on he.position_level_id = hpl.id
left join public.mpp_position_numbers as mpn on he.position_no = mpn.id
left join public.org_position_datas as opd on opd.position_id = mpn.position_id
left join public.hrm_title_masters as htm on htm.id = he.prefix_card_name
left join public.{0}DepartmentData{0} as orgdata on orgdata.id = he.department_id
left join public.hrm_employee_types as het on het.id = he.employee_type_id
left join public.users as u on u.employee_id = he.employee_id
left join public.org_type_departments as org_type_depart on orgdata.department_type_id = org_type_depart.id
left join public.org_type_department_colors as org_depart_color on org_type_depart.department_degree_id = org_depart_color.id
where he.deleted_at is null
and hpt.deleted_at is null
and hpl.deleted_at is null
and mpn.deleted_at is null
and opd.deleted_at is null
and htm.deleted_at is null
and orgdata.deleted_at is null
and het.deleted_at is null
and u.deleted_at is null
and org_type_depart.deleted_at is null
and org_depart_color.deleted_at is null
and he.workingstatus = 'สถานะปฏิบัติงาน'
order by he.firstname, he.lastname;
", '"'.ToString());
var para = db.GetParameterListNpgsql();
DataTable dt = db.ExecuteDataTableNpgsql(sql, para);
var result = new List<external_employeeViewModel>();
foreach (DataRow dr in dt.Rows)
{
var i = new external_employeeViewModel();
i.id = Convert.ToInt32(dr["id"]);
i.position_number = dr["position_number"].ToString();
i.position_name = dr["position_name"].ToString();
i.fullname= dr["fullname"].ToString();
i.halfname = dr["halfname"].ToString();
if (dr["employee_type_id"] != DBNull.Value)
{
i.employee_type_id = Convert.ToInt32(dr["employee_type_id"]);
i.employee_type_name = dr["employee_type_name"].ToString();
}
if (dr["position_type_id"] != DBNull.Value)
{
i.position_type_id = Convert.ToInt32(dr["position_type_id"]);
i.position_type_name = dr["position_type_name"].ToString();
}
if (dr["position_id"] != DBNull.Value)
{
i.position_id = Convert.ToInt32(dr["position_id"]);
}
if (dr["user_id"] != DBNull.Value)
{
i.user_email = dr["user_email"].ToString();
i.user_id = Convert.ToInt32(dr["user_id"]);
}
if (dr["department_id"] != DBNull.Value)
{
i.department_id = Convert.ToInt32(dr["department_id"]);
i.department_name = dr["department_name"].ToString();
i.department_code = dr["department_code"].ToString();
}
if(dr["employee_no"] != DBNull.Value)
{
i.employee_no = dr["employee_no"].ToString();
}
if (dr["position_level_id"] != DBNull.Value)
{
i.position_level_id = Convert.ToInt32(dr["position_level_id"]);
i.position_level_text = dr["position_level_name"].ToString();
}
i.salary = 0;
if (dr["salary"] != DBNull.Value)
{
i.salary = Convert.ToDecimal(dr["salary"]);
}
if (dr["packing_date"] != DBNull.Value)
{
i.packing_date = Convert.ToDateTime(dr["packing_date"]);
}
if (dr["department_degree_id"] != DBNull.Value)
{
i.department_degree_id = Convert.ToInt32(dr["department_degree_id"]);
}
if(dr["remark_hrm_work_record"] != DBNull.Value)
{
i.remark_hrm_work_record = dr["remark_hrm_work_record"].ToString();
}
result.Add(i);
}
return result;
}
public external_employeeViewModel GetEmployeeForLogin(int? user_id)
{
var sql = @"
select he.employee_id as id, mpn.position_number, opd.position_name,
CONCAT(htm.title_name,' ',he.firstname,' ',he.lastname) as fullname,
u.email as user_email, u.id as user_id,u.name as fullname2
from public.hrm_employees as he
left join public.mpp_position_numbers as mpn on he.position_no = mpn.id
left join public.org_position_datas as opd on opd.position_id = mpn.position_id
left join public.hrm_title_masters as htm on htm.id = he.prefix_card_name
left join public.users as u on u.employee_id = he.employee_id
where he.workingstatus = 'สถานะปฏิบัติงาน' and he.deleted_at is null and mpn.deleted_at is null
and opd.deleted_at is null --and htm.deleted_at is null
and u.id=@user_id;
";
var para = db.GetParameterListNpgsql();
var ix = new NpgsqlParameter("user_id", DbType.Int32);
ix.Value = user_id;
para.Add(ix);
DataTable dt = db.ExecuteDataTableNpgsql(sql, para);
var result = new List<external_employeeViewModel>();
foreach (DataRow dr in dt.Rows)
{
var i = new external_employeeViewModel();
i.id = Convert.ToInt32(dr["id"]);
i.position_number = dr["position_number"].ToString();
i.position_name = dr["position_name"].ToString();
i.fullname = dr["fullname2"].ToString();
i.employee_type_id = null;
i.position_type_id = null;
if (dr["user_id"] != DBNull.Value)
{
i.user_email = dr["user_email"].ToString();
i.user_id = Convert.ToInt32(dr["user_id"]);
}
result.Add(i);
}
if (result.Count == 1)
{
return result[0];
}
else
{
return null;
}
}
public employee_leaveViewModel GetLeaveOfEmployee(int employee_id, DateTime? start_date, DateTime? end_date)
{
var sql = string.Format(@"
select {0}b{0}.{0}employee_id{0}, (
SELECT SUM(tad_general.total_dayoff) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id = 2
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as sum_day_sick_leave, (
SELECT COUNT(tad_general.*) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id = 2
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as count_sick_leave, (
SELECT SUM(tad_general.total_dayoff) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id = 4
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as sum_day_personal_leave, (
SELECT COUNT(tad_general.*) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id = 4
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as count_personal_leave, (
SELECT SUM(tad_general.total_dayoff) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id = 1
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as sum_day_vacation_leave, (
SELECT COUNT(tad_general.*) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id = 1
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as count_vacation_leave, (
SELECT COUNT(DISTINCT tad_stop.date) FROM tad_stop_workings as tad_stop
WHERE tad_stop.deleted_at is null
AND tad_stop.employee_id = b.employee_id
AND tad_stop.date BETWEEN @start_date AND @end_date
) as count_stop_working, (
SELECT COUNT(prs_time_result.id) FROM tad_processing_time_results as prs_time_result
LEFT JOIN tad_processing_times as prs_time ON prs_time_result.processing_time_id = prs_time.id
WHERE prs_time_result.deleted_at is null
AND prs_time.deleted_at is null
AND prs_time_result.employee_id = b.employee_id
AND prs_time_result.time_in is not null
AND prs_time_result.time_out is not null
AND prs_time_result.late is not null
AND prs_time_result.date BETWEEN @start_date AND @end_date
) as count_late_tad_processing_time_results, (
SELECT COUNT(prs_time_result.id) FROM tad_processing_time_results as prs_time_result
LEFT JOIN tad_processing_times as prs_time ON prs_time_result.processing_time_id = prs_time.id
WHERE prs_time_result.deleted_at is null
AND prs_time.deleted_at is null
AND prs_time_result.employee_id = b.employee_id
AND prs_time_result.time_in is null
AND prs_time_result.time_out is null
AND prs_time_result.record_type = 'Absence'
AND prs_time_result.date BETWEEN @start_date AND @end_date
) as count_absence_tad_processing_time_results, (
SELECT SUM(tad_general.total_dayoff) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id in (2,4)
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as sum_day_sick_personal_leave, (
SELECT COUNT(tad_general.*) FROM tad_general_leaves as tad_general
LEFT JOIN tad_leave_types as leave_type ON tad_general.leave_type_id = CAST(leave_type.id as varchar)
WHERE tad_general.deleted_at is null
AND leave_type.deleted_at is null
AND leave_type.id in (2,4)
AND tad_general.employee_id = b.employee_id
AND tad_general.status = '3'
AND tad_general.start_date >= @start_date
AND tad_general.end_date <= @end_date
) as count_sick_personal_leave from {0}tad_table_workings{0} as {0}a{0} left join {0}hrm_employees{0} as {0}b{0} on {0}a{0}.{0}employee_id{0} = {0}b{0}.{0}employee_id{0} left join {0}tad_workshifts{0} as {0}c{0} on {0}a{0}.{0}workshift_id{0} = {0}c{0}.{0}id{0} where {0}a{0}.{0}deleted_at{0} is null and {0}b{0}.{0}deleted_at{0} is null and {0}c{0}.{0}deleted_at{0} is null and {0}b{0}.{0}employee_id{0} = @employee_id
", '"'.ToString());
var para = db.GetParameterListNpgsql();
var p_employee_id = new NpgsqlParameter("employee_id", DbType.Int32);
p_employee_id.Value = employee_id;
para.Add(p_employee_id);
var p_start_date = new NpgsqlParameter("start_date", DbType.Date);
p_start_date.Value = start_date;
para.Add(p_start_date);
var p_end_date = new NpgsqlParameter("end_date", DbType.Date);
p_end_date.Value = end_date;
para.Add(p_end_date);
DataTable dt = db.ExecuteDataTableNpgsql(sql, para);
var dr = dt.Rows[0];
var i = new employee_leaveViewModel();
i.sum_day_sick_leave = 0;
i.count_sick_leave = 0;
i.sum_day_personal_leave = 0;
i.count_personal_leave = 0;
i.sum_day_vacation_leave = 0;
i.count_vacation_leave = 0;
i.count_stop_working = 0;
i.count_late_tad_processing_time_results = 0;
i.count_absence_tad_processing_time_results = 0;
i.sum_day_sick_personal_leave = 0;
i.count_sick_personal_leave = 0;
if (dr["sum_day_sick_leave"] != DBNull.Value)
{
i.sum_day_sick_leave = Convert.ToDecimal(dr["sum_day_sick_leave"]);
}
if (dr["count_sick_leave"] != DBNull.Value)
{
i.count_sick_leave = Convert.ToDecimal(dr["count_sick_leave"]);
}
if (dr["sum_day_personal_leave"] != DBNull.Value)
{
i.sum_day_personal_leave = Convert.ToDecimal(dr["sum_day_personal_leave"]);
}
if (dr["count_personal_leave"] != DBNull.Value)
{
i.count_personal_leave = Convert.ToDecimal(dr["count_personal_leave"]);
}
if (dr["sum_day_vacation_leave"] != DBNull.Value)
{
i.sum_day_vacation_leave = Convert.ToDecimal(dr["sum_day_vacation_leave"]);
}
if (dr["count_vacation_leave"] != DBNull.Value)
{
i.count_vacation_leave = Convert.ToDecimal(dr["count_vacation_leave"]);
}
if (dr["count_stop_working"] != DBNull.Value)
{
i.count_stop_working = Convert.ToDecimal(dr["count_stop_working"]);
}
if (dr["count_late_tad_processing_time_results"] != DBNull.Value)
{
i.count_late_tad_processing_time_results = Convert.ToDecimal(dr["count_late_tad_processing_time_results"]);
}
if (dr["count_absence_tad_processing_time_results"] != DBNull.Value)
{
i.count_absence_tad_processing_time_results = Convert.ToDecimal(dr["count_absence_tad_processing_time_results"]);
}
if (dr["sum_day_sick_personal_leave"] != DBNull.Value)
{
i.sum_day_sick_personal_leave = Convert.ToDecimal(dr["sum_day_sick_personal_leave"]);
}
if (dr["count_sick_personal_leave"] != DBNull.Value)
{
i.count_sick_personal_leave = Convert.ToDecimal(dr["count_sick_personal_leave"]);
}
return i;
}
public external_employeeWithSelectionViewModel GetBlankItem()
{
var i = new external_employeeWithSelectionViewModel();
//var j = Mapper.Map<List<eva_performance_criteriaViewModel>>((from x in epc.GetListALLeva_performance_criteria() select x));
//i.eva_performance_criteria_id = j;
return i;
}
}
}