Ejemplo n.º 1
0
        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()
                });
            }
        }