public static List <StudentSentMessage> GetStudentSentMessage(this List <StudentSentMessage> models, TimeSpan timeStart, TimeSpan timeEnd) { var results = new List <StudentSentMessage>(); foreach (var item in models) { var chkTime = item.ChkTime; var time = new TimeSpan(chkTime.Hour, chkTime.Minute, chkTime.Millisecond); if (time.IsBetween(timeStart, timeEnd)) { var model = new StudentSentMessage() { EmpId = item.EmpId, Status = item.Status, SentType = item.SentType, SentTime = item.SentTime, ChkTime = item.ChkTime }; results.Add(model); } } return(results); }
public void SaveStudentSentMessage(StudentSentMessage model) { try { using (var connection = new MySqlConnection(connString)) { connection.Execute(MySqlDbQuery.SaveStudentSentMessages(), model); } } catch (Exception ex) { SaveExceptionLog(ex); } }
public bool SentSuccess(string empId, SentType sentType) { bool result = false; try { using (var connection = new MySqlConnection(connString)) { var item = connection.Query <StudentSentMessage>(MySqlDbQuery.SentSuccess(), new { emp_id = empId }).ToList(); var model = new StudentSentMessage(); if (sentType == SentType.Entry) { var entryStartTime = GlobalConfig.AppSettings("entryStartTime").Split(':'); var entryEndTime = GlobalConfig.AppSettings("entryEndTime").Split(':'); var timeStart = new TimeSpan(int.Parse(entryStartTime[0]), int.Parse(entryStartTime[1]), int.Parse(entryStartTime[2])); var timeEnd = new TimeSpan(int.Parse(entryEndTime[0]), int.Parse(entryEndTime[1]), int.Parse(entryEndTime[2])); model = item.GetStudentSentMessage(timeStart, timeEnd).FirstOrDefault(); } else if (sentType == SentType.Exit) { var exitStartTime = GlobalConfig.AppSettings("exitStartTime").Split(':'); var exitEndTime = GlobalConfig.AppSettings("exitEndTime").Split(':'); var timeStart = new TimeSpan(int.Parse(exitStartTime[0]), int.Parse(exitStartTime[1]), int.Parse(exitStartTime[2])); var timeEnd = new TimeSpan(int.Parse(exitEndTime[0]), int.Parse(exitEndTime[1]), int.Parse(exitEndTime[2])); model = item.GetStudentSentMessage(timeStart, timeEnd).FirstOrDefault(); } if (model != null) { result = true; } } } catch (Exception ex) { SaveExceptionLog(ex); result = false; } return(result); }
public IEnumerable <StudentSentMessage> GetStudentSentMessage(IEnumerable <StudentSentMessage> models, TimeSpan timeStart, TimeSpan timeEnd) { var result = new List <StudentSentMessage>(); foreach (var item in models) { var sentTime = item.SentTime; var time = new TimeSpan(sentTime.Hour, sentTime.Minute, sentTime.Millisecond); if (time.IsBetween(timeStart, timeEnd)) { var model = new StudentSentMessage() { EmpId = item.EmpId, Status = item.Status, SentType = item.SentType, SentTime = sentTime }; result.Add(model); } } return(result); }
public void StudentSentMessage(IEnumerable <StudentCheckTime> models, SentType sentType) { try { var url = GlobalConfig.AppSettings("sentMessageService") .Replace("{schoolCode}", GlobalConfig.AppSettings("schoolCode")) .Replace("{roleCode}", GlobalConfig.AppSettings("roleCode")); var studentSentMessageDb = new List <StudentSentMessage>(); if (sentType == SentType.Entry) { studentSentMessageDb = studentService.GetStudentSentMessageEntry(Helper.GetDate("current")).ToList(); } else if (sentType == SentType.Exit) { studentSentMessageDb = studentService.GetStudentSentMessageExit(Helper.GetDate("current")).ToList(); } var studentForSentMessage = models.Where(s => !studentSentMessageDb.Any(s2 => s.EmpId == s2.EmpId)); foreach (var item in studentForSentMessage) { var results = new List <StudentSentMessage>(); var client = new RestClient(url); var request = new RestRequest(Method.POST); request.AddBody("content-type", "application/form-data"); request.AddParameter("students", "999902"); request.AddParameter("message", $"ID: {item.EmpId} Name: {item.EmpName} {sentType.ToString()}: {item.ChkTime}"); request.AddParameter("rooms", ""); request.AddParameter("username", "0411"); // delay x sec Thread.Sleep(TimeSpan.FromSeconds(int.Parse(GlobalConfig.AppSettings("ThreadSleepSentMessageSec")))); client.ExecuteAsync(request, response => { var data = response.Content; StudentSentMessage model = new StudentSentMessage(); var json = response.Content; ResponseMessage res = JsonConvert.DeserializeObject <ResponseMessage>(json); if (res != null) { if (res.success == "1") { model = new StudentSentMessage() { EmpId = item.EmpId, Status = $"{SentStatus.Success}", SentType = sentType.ToString(), SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")) }; results.Add(model); } //await mySqlDataConnection.SaveStudentSentMessage(results); } }); } } catch (Exception ex) { studentService.SaveExceptionLog(ex); } }
public ResponseMessage SentOnceNotifyMessage(StudentCheckTime model, SentType sentType) { var result = new ResponseMessage(); if (MySqlDataConnection.SentSuccess(model.EmpId, sentType)) { result = new ResponseMessage() { success = "1" }; return(result); } var studentsAddPara = GlobalConfig.AppSettings("students"); //var roomsAddPara = GlobalConfig.AppSettings("rooms"); //var messageAddPara = GlobalConfig.AppSettings("message"); //var usernameAddPara = GlobalConfig.AppSettings("username"); //var appName = GlobalConfig.AppSettings("appName"); #region for test var students = studentsAddPara.Split('|'); if (GlobalConfig.Index > students.Length - 1) { GlobalConfig.Index = 0; } var sentStd = students[GlobalConfig.Index]; GlobalConfig.Index += 1; #endregion var studentsReq = studentsAddPara == "" ? model.EmpId : sentStd; var url = GlobalConfig.AppSettings("sentMessageService") .Replace("{schoolCode}", GlobalConfig.AppSettings("schoolCode")) .Replace("{studentCode}", studentsReq); var client = new RestClient(url); var request = new RestRequest(Method.POST); request.AddBody("content-type", "application/form-data"); #region message //entry //ทดสอบ //รหัสนักเรียน //ชื่อ //เวลาเข้าเรียน //exit //ทดสอบ //รหัสนักเรียน //ชื่อ //เวลาเลิกเรียน #endregion request.AddParameter("students", studentsReq); if (sentType == SentType.Entry) { request.AddParameter("message", $"ส่งจากระบบ" + System.Environment.NewLine + $"รหัสนักเรียน: {model.EmpId}" + System.Environment.NewLine + $"ชื่อ: {model.EmpName}" + System.Environment.NewLine + $"เวลาเข้าเรียน: {model.ChkTime}"); } else if (sentType == SentType.Exit) { request.AddParameter("message", $"ส่งจากระบบ" + System.Environment.NewLine + $"รหัสนักเรียน: {model.EmpId}" + System.Environment.NewLine + $"ชื่อ: {model.EmpName}" + System.Environment.NewLine + $"เวลาเลิกเรียน: {model.ChkTime}"); } //request.AddParameter("rooms", roomsAddPara); //request.AddParameter("username", usernameAddPara); StudentSentMessage sentMessage = new StudentSentMessage(); var json = client.Execute <ResponseMessage>(request).Content; result = JsonConvert.DeserializeObject <ResponseMessage>(json); //if (res != null) //{ if (result.success == "1") { GlobalConfig.StudentCount += 1; sentMessage = new StudentSentMessage() { EmpId = model.EmpId, Status = $"{SentStatus.Success}", SentType = sentType.ToString(), SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")), ChkTime = model.ChkTime }; MySqlDataConnection.SaveStudentSentMessage(sentMessage); } else { sentMessage = new StudentSentMessage() { EmpId = model.EmpId, Status = $"{SentStatus.Error} : {result.error}", SentType = sentType.ToString(), SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")), ChkTime = model.ChkTime }; MySqlDataConnection.SaveStudentSentMessage(sentMessage); } //} #region sent Async //client.ExecuteAsync(request, response => //{ // StudentSentMessage sentMessage = new StudentSentMessage(); // var json = response.Content; // ResponseMessage resAsync = JsonConvert.DeserializeObject<ResponseMessage>(json); // result = resAsync; // if (resAsync != null) // { // if (resAsync.success == "1") // { // sentMessage = new StudentSentMessage() // { // EmpId = model.EmpId, // Status = $"{SentStatus.Success}", // SentType = sentType.ToString(), // SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")), // ChkTime = model.ChkTime // }; // MySqlDataConnection.SaveStudentSentMessage(sentMessage); // } // else // { // sentMessage = new StudentSentMessage() // { // EmpId = model.EmpId, // Status = $"{SentStatus.Error} : {resAsync.error}", // SentType = sentType.ToString(), // SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")), // ChkTime = model.ChkTime // }; // MySqlDataConnection.SaveStudentSentMessage(sentMessage); // } // } //}); #endregion return(result); }
public void SentStudentNotifyMessage(IEnumerable <StudentCheckTime> models, SentType sentType) { try { var url = GlobalConfig.AppSettings("sentMessageService") .Replace("{schoolCode}", GlobalConfig.AppSettings("schoolCode")) .Replace("{roleCode}", GlobalConfig.AppSettings("roleCode")); var studentForSentMessage = new List <StudentCheckTime>(); if (models.ToList().Count > 0) { studentForSentMessage = models.ToList(); } else { if (sentType == SentType.Entry) { // get students checktime from databas GlobalConfig.StudentCheckTimes = GetStudentsEntryMySql(GlobalConfig.Date); // get students sent message from database GlobalConfig.StudentSentMessages = GetStudentsSentMessagesEntryFromMySql(GlobalConfig.CurrentDate); var diff = GlobalConfig.StudentCheckTimes.ToList().Count - GlobalConfig.StudentSentMessages.ToList().Count; if (diff != 0) { // get students sent message error var StudentSentMessagesError = GlobalConfig.StudentCheckTimes.Where(s => !GlobalConfig.StudentSentMessages.Any(s2 => s.EmpId == s2.EmpId)); if (StudentSentMessagesError.ToList().Count == 0) { return; } SentStudentNotifyMessage(StudentSentMessagesError.Take(30), sentType); } } else if (sentType == SentType.Exit) { // get students checktime from databas GlobalConfig.StudentCheckTimes = GetStudentsExitMySql(GlobalConfig.Date); // get students sent message from database GlobalConfig.StudentSentMessages = GetStudentsSentMessagesExitFromMySql(GlobalConfig.CurrentDate); var diff = GlobalConfig.StudentCheckTimes.ToList().Count - GlobalConfig.StudentSentMessages.ToList().Count; if (diff != 0) { // get students sent message error var StudentSentMessagesError = GlobalConfig.StudentCheckTimes.Where(s => !GlobalConfig.StudentSentMessages.Any(s2 => s.EmpId == s2.EmpId)); if (StudentSentMessagesError.ToList().Count == 0) { return; } SentStudentNotifyMessage(StudentSentMessagesError.Take(30), sentType); } } return; } foreach (var item in studentForSentMessage) { var results = new List <StudentSentMessage>(); var client = new RestClient(url); var request = new RestRequest(Method.POST); request.AddBody("content-type", "application/form-data"); request.AddParameter("students", "999902"); request.AddParameter("message", $"ID: {item.EmpId} Name: {item.EmpName} {sentType.ToString()}: {item.ChkTime}"); request.AddParameter("rooms", ""); request.AddParameter("username", "0411"); // delay x sec //Thread.Sleep(TimeSpan.FromSeconds(int.Parse(GlobalConfig.AppSettings("ThreadSleepSentMessageSec")))); Thread.Sleep(100); client.ExecuteAsync(request, response => { var data = response.Content; StudentSentMessage model = new StudentSentMessage(); var json = response.Content; ResponseMessage res = JsonConvert.DeserializeObject <ResponseMessage>(json); if (res != null) { if (res.success == "1") { model = new StudentSentMessage() { EmpId = item.EmpId, Status = $"{SentStatus.Success}", SentType = sentType.ToString(), SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")) }; results.Add(model); //GlobalConfig.StudentSentMessages.Add(model); MySqlDataConnection.SaveStudentSentMessage(results); } else { model = new StudentSentMessage() { EmpId = item.EmpId, Status = $"{SentStatus.Error} : {res.error}", SentType = sentType.ToString(), SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")) }; //GlobalConfig.StudentSentMessages.Add(model); } } else { model = new StudentSentMessage() { EmpId = item.EmpId, Status = $"{SentStatus.Error}", SentType = sentType.ToString(), SentTime = DateTime.Parse(Helper.GetDateNowStringUs("yyyy-MM-dd HH:mm:ss")) }; //GlobalConfig.StudentSentMessages.Add(model); } }); } } catch (Exception ex) { SaveExceptionLog(ex); } }