/// <summary> /// Вызывается при построении шаблона. /// </summary> protected override void OnApplyTemplate() { base.OnApplyTemplate(); VisualStateManager.GoToState(this, SentType.ToString(), true); }
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 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 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); } }