public ActionResult UpdateRequest(int requestId, int prioryty, int status, string description) { try { var result = _requestDomain.UpdateRequest(requestId, prioryty, status, description); return(Json(new { result }, JsonRequestBehavior.AllowGet)); } finally { if (status == (int)RequestStatusEnum.Pending) { var result = _agencyDomain.FindITSupporterByRequestId(requestId); if (!result.IsError && result.ObjReturn > 0) { FirebaseService firebaseService = new FirebaseService(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterReceive(result.ObjReturn, requestId); } } } }
public ActionResult FindITSupporterByRequestId(int requestId) { var result = _agencyDomain.FindITSupporterByRequestId(requestId); if (!result.IsError && result.ObjReturn > 0) { FirebaseService firebaseService = new FirebaseService(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterReceive(result.ObjReturn, requestId); //int counter = 60; //while (counter > 0) //{ // counter--; // Thread.Sleep(1000); //} //_requestDomain.AcceptRequestFromITSupporter(result.ObjReturn, requestId, false); return(Json(new { result }, JsonRequestBehavior.AllowGet)); } return(Json(new { result }, JsonRequestBehavior.AllowGet)); }
public static ResponseObject <bool> AcceptRequestFromITSupporter(int itSupporterId, int requestId, bool isAccept) { //MemoryCacher memoryCacher = new MemoryCacher(); RedisTools redisTools = new RedisTools(); try { var requestService = new RequestService(); var itSupporterService = new ITSupporterService(); var requestHistoryService = new RequestHistoryService(); var ticketService = new TicketService(); if (isAccept) { var request = requestService.GetAll().SingleOrDefault(p => p.RequestId == requestId); var itSupporter = itSupporterService.GetAll().SingleOrDefault(p => p.ITSupporterId == itSupporterId); if (request != null && itSupporter != null) { request.RequestStatus = (int)RequestStatusEnum.Processing; request.CurrentITSupporter_Id = itSupporter.ITSupporterId; request.StartTime = DateTime.UtcNow.AddHours(7); requestService.Update(request); itSupporterService.Update(itSupporter); itSupporter.IsBusy = true; var ticketsOfRequest = ticketService.GetAll().Where(p => p.RequestId == requestId).ToList(); foreach (var ticket in ticketsOfRequest) { ticket.UpdateDate = DateTime.UtcNow; ticketService.Update(ticket); } //memoryCacher.Delete("ITSupporterListWithWeights"); redisTools.Clear("ITSupporterListWithWeights"); return(new ResponseObject <bool> { IsError = false, SuccessMessage = "Nhận thành công", ObjReturn = true }); } } else { //var itSupporterFound = memoryCacher.GetValue("ITSupporterListWithWeights"); var itSupporterFound = redisTools.Get("ITSupporterListWithWeights"); Queue <RenderITSupporterListWithWeight> idSupporterListWithWeights; if (itSupporterFound != null) { idSupporterListWithWeights = JsonConvert.DeserializeObject <Queue <RenderITSupporterListWithWeight> >(itSupporterFound); if (idSupporterListWithWeights.Count > 1) { var rejected = idSupporterListWithWeights.Dequeue(); rejected.TimesReject++; var requestHistory = new RequestHistory() { IsITSupportAccept = false, IsDelete = false, Pre_It_SupporterId = rejected.ITSupporterId, RequestId = requestId, CreateDate = DateTime.UtcNow.AddHours(7) }; requestHistoryService.Add(requestHistory); var idSupporterListWithWeightNext = idSupporterListWithWeights.FirstOrDefault(); Console.WriteLine($"REjecct --- {rejected.TimesReject}"); if (rejected.TimesReject < 3) { Console.WriteLine($"Ho tro vien {rejected.ITSupporterName} da tu choi {rejected.TimesReject} lan"); idSupporterListWithWeights.Enqueue(rejected); } else { Console.WriteLine($"Reject ho tro vien {rejected.ITSupporterName}"); var itSupporter = itSupporterService.GetAll().SingleOrDefault(p => p.ITSupporterId == rejected.ITSupporterId); itSupporter.IsOnline = false; itSupporterService.Update(itSupporter); FirebaseService firebaseService = new FirebaseService(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterOffline(rejected.ITSupporterId); Console.WriteLine($"ho tro vien {rejected.ITSupporterName} da offline"); } //memoryCacher.Add("ITSupporterListWithWeights", idSupporterListWithWeights, DateTimeOffset.UtcNow.AddHours(1)); redisTools.Save("ITSupporterListWithWeights", idSupporterListWithWeights); if (idSupporterListWithWeightNext != null) { FirebaseService firebaseService = new FirebaseService(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterReceive(idSupporterListWithWeightNext.ITSupporterId, requestId); int counter = 60; while (counter > 0) { Console.WriteLine($"Gui lai sau khi tu choi trong {counter} giây"); counter--; Thread.Sleep(1000); } AcceptRequestFromITSupporter(idSupporterListWithWeightNext.ITSupporterId, requestId, false); return(new ResponseObject <bool> { IsError = false, WarningMessage = "Nhận duoc thong tin ho tro vien", ObjReturn = true }); } else { //memoryCacher.Delete("ITSupporterListWithWeights"); redisTools.Clear("ITSupporterListWithWeights"); var agencyService = new AgencyService(); var result = FindITSupporterByRequestId(requestId); if (!result.IsError && result.ObjReturn > 0) { FirebaseService firebaseService = new FirebaseService(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterReceive(result.ObjReturn, requestId); int counter = 60; while (counter > 0) { Console.WriteLine($"gui lai yeu cau trong {counter} giay"); counter--; Thread.Sleep(1000); } AcceptRequestFromITSupporter(result.ObjReturn, requestId, false); } } } else { var rejected = idSupporterListWithWeights.Dequeue(); rejected.TimesReject++; var requestHistory = new RequestHistory() { IsITSupportAccept = false, IsDelete = false, Pre_It_SupporterId = rejected.ITSupporterId, RequestId = requestId, CreateDate = DateTime.UtcNow.AddHours(7) }; requestHistoryService.Add(requestHistory); var idSupporterListWithWeightNext = idSupporterListWithWeights.FirstOrDefault(); Console.WriteLine($"REjecct --- {rejected.TimesReject}"); if (rejected.TimesReject < 3) { Console.WriteLine($"Ho tro vien {rejected.ITSupporterName} da tu choi {rejected.TimesReject} lan!"); idSupporterListWithWeights.Enqueue(rejected); } else { Console.WriteLine($"Chuyen trang thai ho tro vien {rejected.ITSupporterName} thanh offline!"); var itSupporter = itSupporterService.GetAll().SingleOrDefault(p => p.ITSupporterId == rejected.ITSupporterId); itSupporter.IsOnline = false; itSupporterService.Update(itSupporter); FirebaseService firebaseService2 = new FirebaseService(); firebaseService2.SendNotificationFromFirebaseCloudForITSupporterOffline(rejected.ITSupporterId); Console.WriteLine($"ho tro vien {rejected.ITSupporterName} da offline"); } //memoryCacher.Add("ITSupporterListWithWeights", idSupporterListWithWeights, DateTimeOffset.UtcNow.AddHours(1)); redisTools.Save("ITSupporterListWithWeights", idSupporterListWithWeights); FirebaseService firebaseService = new FirebaseService(); idSupporterListWithWeightNext = idSupporterListWithWeights.FirstOrDefault(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterReceive(idSupporterListWithWeightNext.ITSupporterId, requestId); int counter = 60; while (counter > 0) { Console.WriteLine($"Gui lại sau khi tu choi trong {counter} giây"); counter--; Thread.Sleep(1000); } AcceptRequestFromITSupporter(idSupporterListWithWeightNext.ITSupporterId, requestId, false); } } } return(new ResponseObject <bool> { IsError = true, WarningMessage = "Nhận thất bại", ObjReturn = false }); } catch (Exception e) { return(new ResponseObject <bool> { IsError = true, WarningMessage = "Nhận thất bại", ObjReturn = false, ErrorMessage = e.ToString() }); } }
static void Main(string[] args) { Start: Thread thread = new Thread(UpdateSeniority); thread.Start(); RequestService requestService = new RequestService(); var requests = requestService.GetAll().Where(p => p.RequestStatus == 1 && p.CurrentITSupporter_Id == null).OrderBy(p => p.CreateDate).OrderBy(p => p.Priority).ToList(); var input = 0; var itsupporterService = new ITSupporterService(); var itName = ""; bool acceptRequest = false; if (requests != null) { foreach (var requestItem in requests) { Console.WriteLine($"Yeu cau {requestItem.RequestId} chua duoc xu li"); } Console.WriteLine("==============================="); foreach (var item in requests) { if (itsupporterService.GetAll().Any(a => a.IsBusy == false && a.IsOnline == true)) { input = item.RequestId; var result = FindITSupporterByRequestId(input); Console.WriteLine($"Dang tim ho tro vien cho yeu cau {input}"); if (result.ObjReturn == 0) { itName = "Chua co ho tro vien nao"; } else { itName = itsupporterService.GetAll().SingleOrDefault(i => i.ITSupporterId == result.ObjReturn).ITSupporterName; } if (!result.IsError && result.ObjReturn > 0) { FirebaseService firebaseService = new FirebaseService(); firebaseService.SendNotificationFromFirebaseCloudForITSupporterReceive(result.ObjReturn, input); Console.WriteLine($"Dang doi ho tro vien {itName} phan hoi:"); int counter = 60; while (counter > 0) { var itsupporterIsOnline = itsupporterService.GetAll().SingleOrDefault(i => i.ITSupporterId == result.ObjReturn).IsOnline; var itsupporterIsBusy = itsupporterService.GetAll().SingleOrDefault(i => i.ITSupporterId == result.ObjReturn).IsBusy; if (itsupporterIsOnline == false) { goto Stop; } else if (itsupporterIsBusy == true) { goto Accept; } Console.WriteLine($"Đang chờ phản hồi trong {counter} giây"); counter--; Thread.Sleep(1000); } Accept: var accept = AcceptRequestFromITSupporter(result.ObjReturn, input, false); acceptRequest = accept.ObjReturn; } Stop: if (acceptRequest == false) { Console.WriteLine("Ho tro vien " + itName + " da khong xac nhan yeu cau " + input); } else { Console.WriteLine("Ho tro vien " + itName + " da xac nhan yeu cau " + input); } } } Thread threadSleep = new Thread(CounterSleep); threadSleep.Start(); Thread.Sleep(60000); Console.WriteLine("==============================="); Console.WriteLine("Moi ngu day sau 60s"); } goto Start; }