เพิ่มส่วนการแจ้งเตือนผ่าน notification

This commit is contained in:
Nakorn Rientrakrunchai
2020-08-30 17:45:48 +07:00
parent b5d58656bb
commit 2532a9e067

View File

@@ -14,6 +14,11 @@ using System.Net;
using TTSW.Configure;
using Microsoft.Extensions.Options;
using System.Data;
using Npgsql;
using NpgsqlTypes;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System.Text;
namespace TodoAPI2.Models
{
@@ -22,12 +27,17 @@ namespace TodoAPI2.Models
private IBaseRepository2<eva_create_evaluation_detailEntity, int> _repository;
private IMyDatabase db;
private Iexternal_linkageService ext;
private Iexternal_employeeService emp;
private IConfiguration Configuration { get; set; }
public eva_create_evaluation_detail_statusService(IBaseRepository2<eva_create_evaluation_detailEntity, int> repository, IMyDatabase mydb, Iexternal_linkageService inext)
public eva_create_evaluation_detail_statusService(IBaseRepository2<eva_create_evaluation_detailEntity, int> repository,
IMyDatabase mydb, Iexternal_linkageService inext, Iexternal_employeeService inemp, IConfiguration configuration)
{
_repository = repository;
db = mydb;
ext = inext;
emp = inemp;
Configuration = configuration;
}
#region Private Functions
@@ -160,8 +170,6 @@ namespace TodoAPI2.Models
where k.id == existingEntity.create_evaluation_id
select k).FirstOrDefault();
// Do notification here
if (existingEntity != null)
{
existingEntity.create_evaluation_id = model.create_evaluation_id;
@@ -173,39 +181,66 @@ namespace TodoAPI2.Models
if (current_eva.employee_id == existingEntity.chief) // หัวหน้าและผู้ประเมินสูงสุด เป็นคนคนเดียวกัน
{
if(existingEntity.status_chief == "Y") existingEntity.status_supervisor = existingEntity.status_chief;
if (existingEntity.status_chief == "Y") existingEntity.status_supervisor = existingEntity.status_chief;
}
int? noti_to_employee_id = null;
int? owner_eva_employee_id = existingEntity.employee_id;
string noti_message = "กรุณาตรวจสอบแบบประเมินของ {0}";
string noti_url = "/eva/eva_create_evaluation_detail_processView/eva_create_evaluation_detail_process_d2?id=" + existingEntity.id.ToString();
if (model.status_mode == "next0")
{
existingEntity.status_self_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
noti_to_employee_id = existingEntity.chief;
noti_message = "กรุณาตรวจสอบแบบประเมินของ {0}";
noti_url = "/eva/eva_create_evaluation_detail_processView/eva_create_evaluation_detail_process_d?id=" + existingEntity.id.ToString();
}
else if (model.status_mode == "next1")
{
existingEntity.status_chief_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
if (current_eva.employee_id == existingEntity.chief) // หัวหน้าและผู้ประเมินสูงสุด เป็นคนคนเดียวกัน
{
noti_to_employee_id = current_eva.supervisor1_id;
}
else
{
noti_to_employee_id = current_eva.employee_id;
}
}
else if (model.status_mode == "back1")
{
existingEntity.status_chief_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
existingEntity.status_self_click_date = null;
noti_to_employee_id = existingEntity.employee_id;
noti_message = "ข้อตกลงการประเมินของคุณ ({0}) ถูกตีกลับ";
noti_url = "/eva/eva_create_evaluation_detail_agreementView/eva_create_evaluation_detail_agreement_d?id=" + existingEntity.id.ToString();
}
else if (model.status_mode == "next2")
{
existingEntity.status_supervisor_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
noti_to_employee_id = current_eva.supervisor1_id;
}
else if (model.status_mode == "back2")
{
existingEntity.status_supervisor_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
existingEntity.status_chief_click_date = null;
noti_to_employee_id = existingEntity.chief;
noti_message = "แบบประเมินของ {0} ถูกตีกลับ";
noti_url = "/eva/eva_create_evaluation_detail_agreementView/eva_create_evaluation_detail_agreement_d?id=" + existingEntity.id.ToString();
}
else if (model.status_mode == "next3")
{
existingEntity.status_supervisor1A_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
noti_to_employee_id = current_eva.supervisor2_id;
}
else if (model.status_mode == "back3")
{
existingEntity.status_supervisor1A_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
existingEntity.status_supervisor_click_date = null;
noti_to_employee_id = current_eva.employee_id;
noti_message = "แบบประเมินของ {0} ถูกตีกลับ";
noti_url = "/eva/eva_create_evaluation_detail_agreementView/eva_create_evaluation_detail_agreement_d2?id=" + existingEntity.id.ToString();
}
else if (model.status_mode == "next4")
{
@@ -215,8 +250,13 @@ namespace TodoAPI2.Models
{
existingEntity.status_supervisor2A_click_date = MyHelper.RemoveTimeFromDate(DateTime.Now);
existingEntity.status_supervisor1A_click_date = null;
noti_to_employee_id = current_eva.supervisor1_id;
noti_message = "แบบประเมินของ {0} ถูกตีกลับ";
noti_url = "/eva/eva_create_evaluation_detail_agreementView/eva_create_evaluation_detail_agreement_d2?id=" + existingEntity.id.ToString();
}
SendNotification(noti_to_employee_id, owner_eva_employee_id, existingEntity.id, noti_message, noti_url);
var updated = _repository.Update(id, existingEntity);
return Get(updated.id);
}
@@ -224,6 +264,38 @@ namespace TodoAPI2.Models
throw new NotificationException("No data to update");
}
private void SendNotification(int? noti_to_employee_id, int? owner_eva_employee_id, int? detail_id, string noti_message, string noti_url)
{
if (!noti_to_employee_id.HasValue || !owner_eva_employee_id.HasValue || !detail_id.HasValue) return;
var all_emp = emp.GetAllEmployee();
int? noti_to_user_id = (from i in all_emp
where i.id == noti_to_employee_id
select i.user_id).FirstOrDefault();
var owner_info = (from i in all_emp
where i.id == owner_eva_employee_id
select i).FirstOrDefault();
string sql = $@"
insert into notifications(id,type,notifiable_type,notifiable_id,data)
values('{Guid.NewGuid().ToString()}','App\Notifications\WorkflowNotification','App\Models\User',{noti_to_user_id.ToString()},@data)
";
DataOfNotification d = new DataOfNotification();
d.title = noti_message.Replace("{0}", owner_info.fullname);
d.body = noti_message.Replace("{0}", owner_info.fullname);
d.url = noti_url;
NpgsqlParameter data = new NpgsqlParameter();
data.DbType = DbType.String;
data.Direction = ParameterDirection.Input;
data.ParameterName = "data";
data.Value = JsonConvert.SerializeObject(d);
List<NpgsqlParameter> para = new List<NpgsqlParameter>();
para.Add(data);
db.ExecuteNonQueryNpgsql(sql, para);
}
public string UpdateMultiple(List<eva_create_evaluation_detail_statusInputModel> model)
{
foreach (var i in model)
@@ -240,7 +312,6 @@ namespace TodoAPI2.Models
existingEntity.status_supervisor1A = i.status_supervisor1A;
existingEntity.status_supervisor2A = i.status_supervisor2A;
_repository.UpdateWithoutCommit(i.id.Value, existingEntity);
}
}
@@ -290,4 +361,11 @@ namespace TodoAPI2.Models
#endregion
}
public class DataOfNotification
{
public string title { get; set; }
public string body { get; set; }
public string url { get; set; }
}
}