Files
hrm_eva/Models/external_employee/external_employeeService.cs
Nakorn Rientrakrunchai 8b98125e49 First Initial
2020-02-20 15:02:39 +07:00

187 lines
7.2 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,
orgdata.id as department_id,orgdata.department_name,orgdata.department_code,he.salary,
he.employee_type_id, het.employee_type_name,
u.email as user_email, u.id as user_id,hpl.position_level_name,hpl.position_level_id
from public.hrm_employees as he
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
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;
", '"'.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();
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();
}
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"]);
}
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["department_id"]);
i.position_level_text = dr["position_level_name"].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
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["fullname"].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 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;
}
}
}