340 lines
13 KiB
C#
340 lines
13 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 System.Data.Odbc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace TodoAPI2.Models
|
|
{
|
|
public class upload_result
|
|
{
|
|
public string path { get; set; }
|
|
}
|
|
|
|
public class input_finger
|
|
{
|
|
public string data { get; set; }
|
|
public string base64_img { get; set; }
|
|
public string image_id { get; set; }
|
|
}
|
|
|
|
public class eva_temp_fingerscanService : Ieva_temp_fingerscanService
|
|
{
|
|
private IBaseRepository2<eva_temp_fingerscanEntity, string> _repository;
|
|
private IMyDatabase db;
|
|
private Iexternal_linkageService ext;
|
|
private Iexternal_employeeService emp;
|
|
private IConfiguration Configuration { get; set; }
|
|
public eva_temp_fingerscanService(IBaseRepository2<eva_temp_fingerscanEntity, string> repository, IMyDatabase mydb,
|
|
Iexternal_linkageService inext, Iexternal_employeeService inemp, IConfiguration configuration)
|
|
{
|
|
_repository = repository;
|
|
db = mydb;
|
|
ext = inext;
|
|
emp = inemp;
|
|
Configuration = configuration;
|
|
}
|
|
|
|
public eva_temp_fingerscanWithSelectionViewModel GetBlankItem()
|
|
{
|
|
var i = new eva_temp_fingerscanWithSelectionViewModel();
|
|
i.checktime = DateTime.Now;
|
|
|
|
return i;
|
|
}
|
|
|
|
public List<eva_temp_fingerscanViewModel> GetListBybadgenumber(string badgenumber)
|
|
{
|
|
var model = new eva_temp_fingerscanSearchModel();
|
|
model.badgenumber = badgenumber;
|
|
return GetListBySearch(model);
|
|
}
|
|
|
|
private string GetDateStringForDB(DateTime d)
|
|
{
|
|
string year = d.Year.ToString();
|
|
string month = d.Month.ToString();
|
|
if (d.Month < 10)
|
|
{
|
|
month = "0" + month;
|
|
}
|
|
string day = d.Day.ToString();
|
|
if (d.Day < 10)
|
|
{
|
|
day = "0" + day;
|
|
}
|
|
|
|
return year + "-" + month + "-" + day;
|
|
}
|
|
|
|
private string GetTimeStringForDB(DateTime d)
|
|
{
|
|
string hour = d.Hour.ToString();
|
|
if (d.Hour < 10)
|
|
{
|
|
hour = "0" + hour;
|
|
}
|
|
string minute = d.Minute.ToString();
|
|
if (d.Minute < 10)
|
|
{
|
|
minute = "0" + minute;
|
|
}
|
|
|
|
return hour + ":" + minute;
|
|
}
|
|
|
|
public List<eva_temp_fingerscanViewModel> GetListBySearch(eva_temp_fingerscanSearchModel model)
|
|
{
|
|
//var all_emp = emp.GetListByemployee_type(null, null);
|
|
|
|
//if (!model.checktime.HasValue) return null;
|
|
|
|
//string connectionString = Configuration.GetConnectionString("fingerScanConnectionString");
|
|
//List<eva_temp_fingerscanViewModel> data = null;
|
|
|
|
//List<userinfo> userinfo_data = new List<userinfo>();
|
|
//List<checkinout> checkinout_data = new List<checkinout>();
|
|
|
|
//using (OdbcConnection odbcConnection = new OdbcConnection(connectionString))
|
|
//{
|
|
// odbcConnection.Open();
|
|
|
|
// using (var command = odbcConnection.CreateCommand())
|
|
// {
|
|
// command.CommandType = System.Data.CommandType.Text;
|
|
// command.CommandText = "select Badgenumber,userid,name,lastname from USERINFO";
|
|
// OdbcDataReader reader = command.ExecuteReader();
|
|
|
|
// while (reader.Read())
|
|
// {
|
|
// userinfo info = new userinfo();
|
|
// info.badgenumber = Convert.ToString(reader[0]);
|
|
// info.userid = Convert.ToString(reader["userid"]);
|
|
// info.name = Convert.ToString(reader["name"]);
|
|
// info.lastname = Convert.ToString(reader["lastname"]);
|
|
// userinfo_data.Add(info);
|
|
// }
|
|
// }
|
|
|
|
// odbcConnection.Close();
|
|
//}
|
|
|
|
//using (OdbcConnection odbcConnection2 = new OdbcConnection(connectionString))
|
|
//{
|
|
// odbcConnection2.Open();
|
|
|
|
// using (var command2 = odbcConnection2.CreateCommand())
|
|
// {
|
|
// command2.CommandType = System.Data.CommandType.Text;
|
|
// command2.CommandText = "select CHECKTIME,userid from checkinout";
|
|
// OdbcDataReader reader2 = command2.ExecuteReader();
|
|
// while (reader2.Read())
|
|
// {
|
|
// checkinout inout = new checkinout();
|
|
// inout.checktime = Convert.ToDateTime(reader2["CHECKTIME"]);
|
|
// inout.userid = Convert.ToString(reader2["userid"]);
|
|
// checkinout_data.Add(inout);
|
|
// }
|
|
// }
|
|
// odbcConnection2.Close();
|
|
//}
|
|
|
|
//data = (from i in checkinout_data
|
|
// join j in userinfo_data on i.userid equals j.userid
|
|
// join e in all_emp on j.badgenumber equals e.position_number
|
|
// where GetDateStringForDB(i.checktime) == GetDateStringForDB(model.checktime.Value)
|
|
// orderby i.checktime descending
|
|
// select new eva_temp_fingerscanViewModel()
|
|
// {
|
|
// badgenumber = j.badgenumber,
|
|
// checktime = i.checktime,
|
|
// fullname_in_finger_scan = j.name + " " + j.lastname,
|
|
// fullname_in_system = e.fullname,
|
|
// txt_checktime = GetDateStringForDB(i.checktime) + " " + GetTimeStringForDB(i.checktime),
|
|
// id = e.id.ToString() + "#" + GetDateStringForDB(i.checktime) + "#" + GetTimeStringForDB(i.checktime)
|
|
// }).ToList();
|
|
|
|
//return data;
|
|
|
|
return null;
|
|
}
|
|
|
|
private string ClearName(string fullname)
|
|
{
|
|
if (!string.IsNullOrEmpty(fullname))
|
|
{
|
|
return fullname.Replace(" ", "")
|
|
.Replace("นาย", "")
|
|
.Replace("นางสาว", "")
|
|
.Replace("นาง", "")
|
|
.Replace("น.ส.", "")
|
|
.Replace("ผศ.ดร.", "");
|
|
}
|
|
else
|
|
{
|
|
return fullname;
|
|
}
|
|
}
|
|
|
|
private string PostAttachment(byte[] data, string filename)
|
|
{
|
|
HttpContent content = new ByteArrayContent(data);
|
|
|
|
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
|
|
|
|
using (var form = new MultipartFormDataContent())
|
|
{
|
|
form.Add(content, "file", filename);
|
|
|
|
using (var client = new HttpClient())
|
|
{
|
|
var url = MyHelper.GetConfig(Configuration, "SiteInformation:hr_upload_api");
|
|
Console.WriteLine("SiteInformation:hr_upload_api = "+MyHelper.GetConfig(Configuration, "SiteInformation:hr_upload_api"));
|
|
var response = client.PostAsync(url, form).Result;
|
|
response.EnsureSuccessStatusCode();
|
|
var json = response.Content.ReadAsStringAsync().Result;
|
|
upload_result u = JsonConvert.DeserializeObject<upload_result>(json);
|
|
return u.path;
|
|
}
|
|
}
|
|
}
|
|
|
|
public string RecieveFingerScanFromLocal(List<fingerscanDataModel> model)
|
|
{
|
|
var all_emp = emp.GetListByemployee_type(null, null);
|
|
|
|
//var result = (from i in model
|
|
// join e in all_emp on ClearName(i.fullname) equals ClearName(e.halfname)
|
|
// select e.id.ToString() + "#" + GetDateStringForDB(i.checktime) + "#" + GetTimeStringForDB(i.checktime)).ToList();
|
|
|
|
var result = new List<input_finger>();
|
|
|
|
foreach (var i in model)
|
|
{
|
|
var theemp = (from e in all_emp
|
|
where ClearName(i.fullname) == ClearName(e.halfname)
|
|
select e).FirstOrDefault();
|
|
if (theemp != null)
|
|
{
|
|
var id = theemp.id.ToString();
|
|
var r2 = id.ToString() + "#" + GetDateStringForDB(i.checktime) + "#" + GetTimeStringForDB(i.checktime);
|
|
var a = new input_finger();
|
|
a.data = r2;
|
|
a.base64_img = i.base64img;
|
|
a.image_id = i.image_id;
|
|
result.Add(a);
|
|
}
|
|
}
|
|
|
|
return InsertFingerScanToHR(result);
|
|
}
|
|
|
|
public string InsertFingerScanToHR(List<input_finger> model)
|
|
{
|
|
int count_insert = 0;
|
|
foreach (var i in model)
|
|
{
|
|
var data = i.data.Split('#');
|
|
string employee_id = data[0];
|
|
string date = data[1];
|
|
string time = data[2];
|
|
|
|
string sql_checking = $"select employee_id,date,time from tad_time_attendances where employee_id={employee_id} and date='{date}' and time='{time}' ";
|
|
|
|
DataTable dt = db.ExecuteDataTableNpgsql(sql_checking, null);
|
|
if (dt.Rows.Count == 0)
|
|
{
|
|
byte[] imageBytes = Convert.FromBase64String(i.base64_img);
|
|
var picture = PostAttachment(imageBytes, "jk" + i.image_id + ".jpg");
|
|
|
|
string sql_insert = $"insert into tad_time_attendances(employee_id,date,time,picture) values({employee_id},'{date}','{time}','{picture}')";
|
|
db.ExecuteNonQueryNpgsql(sql_insert, null);
|
|
count_insert++;
|
|
}
|
|
}
|
|
|
|
return count_insert.ToString();
|
|
}
|
|
}
|
|
|
|
public class userinfo
|
|
{
|
|
public string badgenumber { get; set; }
|
|
public string userid { get; set; }
|
|
public string name { get; set; }
|
|
public string lastname { get; set; }
|
|
}
|
|
|
|
public class checkinout
|
|
{
|
|
public DateTime checktime { get; set; }
|
|
public string userid { get; set; }
|
|
}
|
|
|
|
public class fingerscanDataModel
|
|
{
|
|
public string badgenumber { get; set; }
|
|
|
|
public DateTime checktime { get; set; }
|
|
|
|
public string fullname { get; set; }
|
|
|
|
public string device_id { get; set; }
|
|
public string image_id { get; set; }
|
|
public string base64img { get; set; }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//using (var command = odbcConnection.CreateCommand())
|
|
// {
|
|
// command.CommandType = System.Data.CommandType.Text;
|
|
// string checktime = GetDateStringForDB(model.checktime.Value);
|
|
// command.CommandText = $@"
|
|
//SELECT USERINFO.[name], USERINFO.[lastname], USERINFO.Badgenumber, CHECKINOUT.CHECKTIME
|
|
//FROM CHECKINOUT INNER JOIN USERINFO ON CHECKINOUT.USERID = USERINFO.USERID
|
|
//where CHECKTIME is not null
|
|
//and DateValue(CHECKINOUT.CHECKTIME) = #{checktime}#
|
|
//order by CHECKTIME desc; ";
|
|
// OdbcDataReader reader = command.ExecuteReader();
|
|
|
|
// while (reader.Read())
|
|
// {
|
|
// var n = new eva_temp_fingerscanViewModel();
|
|
// //n.id = Convert.ToDateTime(reader["CHECKTIME"]);
|
|
// if(reader["checktime"] != DBNull.Value)
|
|
// {
|
|
// n.badgenumber = Convert.ToString(reader["Badgenumber"]);
|
|
// n.checktime = Convert.ToDateTime(reader["CHECKTIME"]);
|
|
// n.txt_checktime = GetDateStringForDB(n.checktime.Value) + " " + GetTimeStringForDB(n.checktime.Value);
|
|
// n.fullname_in_finger_scan = Convert.ToString(reader["name"]) + " " + Convert.ToString(reader["lastname"]);
|
|
|
|
// var x = (from i in all_emp
|
|
// where i.position_number == n.badgenumber
|
|
// select i).FirstOrDefault();
|
|
// if (x != null)
|
|
// {
|
|
// n.id = x.id.ToString()+"#"+ GetDateStringForDB(n.checktime.Value)+"#"+ GetTimeStringForDB(n.checktime.Value);
|
|
// n.fullname_in_system = x.fullname;
|
|
// };
|
|
// data.Add(n);
|
|
// }
|
|
// }
|
|
//} |