コード例 #1
0
        public IHttpActionResult Test()
        {
            //ServiceRequestController.AssignRequestToAgents(5);
            //NotificationHelper.GCMNotification("ejV8j1Ife04:APA91bGctCxqnIX3xJmsWrOqnO8b_H8h8L9LFpfXQ_-Eigk-SYQko2h5E6sUge0AHSzPraQzBdQIy7UyH_I90YGB0hnB2E_6h1au_bp0OIrd6fGytuXsPWTnZCjbFDc3-pio7BkpNGbn", "Test Message");

            long ServiceRequestId = 1;

            try
            {
                using (AppDBContext context = new AppDBContext())
                {
                    var companies        = new CompanyRepository(context).GetAll();
                    var request          = new ServiceRequestRepository(context).GetById(ServiceRequestId);
                    var agentServiceRepo = new AgentServiceRequestRepository(context);

                    foreach (var company in companies)
                    {
                        //Check vehicle number is recently serviced
                        var agentId          = agentServiceRepo.GetAgentIdIfServicedRecently(request.VehicleNo, company.Id);
                        var lastRequestAgent = agentServiceRepo.GetLastOpenServiceAgentIdByCompany(company.Id);

                        if (agentId == 0)
                        {
                            //get agents for each company
                            var agents = new UserRepository(context).GetAgentsByCompany(company.Id);

                            if (agents == null || agents.Count == 0)
                            {
                                continue;
                            }

                            Dictionary <long, int> counts = new Dictionary <long, int>();

                            foreach (var agent in agents)
                            {
                                //get open requests for each agent
                                var agentRequests = agentServiceRepo.GetByAgentIdForAssign(agent.Id)?.Where(
                                    r => (r.Status ?? 0) != (int)Constant.ServiceRequestStatus.Closed ||
                                    (r.Status ?? 0) != (int)Constant.ServiceRequestStatus.Expired);
                                var weight = 0;
                                if (agent.Id == lastRequestAgent)
                                {
                                    weight = 100;
                                }

                                if (agentRequests != null)
                                {
                                    counts.Add(agent.Id, agentRequests.Count() + weight);
                                }
                                else
                                {
                                    counts.Add(agent.Id, 0 + weight);
                                }
                            }

                            //find agent with min no of requests
                            //order by id asc and get top 1 agent
                            var item = counts.OrderBy(i => i.Value).ThenBy(i => i.Key).First();
                            agentId = item.Key;
                        }

                        //assign the request to agent
                        AgentServiceRequest asr = new AgentServiceRequest
                        {
                            AgentId          = agentId,
                            ServiceRequestId = ServiceRequestId,
                            Status           = (int)Constant.ServiceRequestStatus.Initial,
                            CreatedTime      = DateTime.Now.ToUniversalTime()
                        };

                        agentServiceRepo.Add(asr);

                        new NotificationRepository(context).Add(
                            asr.AgentId,
                            (int)Constant.NotificationType.Request,
                            asr.ServiceRequestId,
                            ConfigurationHelper.NOTIFICATION_TITLE,
                            Constant.Notification.NEW_REQUEST_TEXT);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(DefaultController), ex.Message, LogType.ERROR);
            }

            return(Ok());
        }
コード例 #2
0
        private void AssignRequestToAgents(long ServiceRequestId)
        {
            try
            {
                //get all companies
                using (AppDBContext context = new AppDBContext())
                {
                    var companies        = new CompanyRepository(context).GetAll();
                    var request          = new ServiceRequestRepository(context).GetById(ServiceRequestId);
                    var agentServiceRepo = new AgentServiceRequestRepository(context);

                    foreach (var company in companies)
                    {
                        //Check vehicle number is recently serviced
                        var agentId          = agentServiceRepo.GetAgentIdIfServicedRecently(request.VehicleNo, company.Id);
                        var lastRequestAgent = agentServiceRepo.GetLastOpenServiceAgentIdByCompany(company.Id);

                        if (agentId == 0)
                        {
                            //get agents for each company
                            var agents = new UserRepository(context).GetAgentsByCompany(company.Id);

                            if (agents == null || agents.Count == 0)
                            {
                                continue;
                            }

                            Dictionary <long, int> counts = new Dictionary <long, int>();

                            foreach (var agent in agents)
                            {
                                //get open requests for each agent
                                var agentRequests = agentServiceRepo.GetByAgentIdForAssign(agent.Id)?.Where(
                                    r => (r.Status ?? 0) != (int)Constant.ServiceRequestStatus.Closed ||
                                    (r.Status ?? 0) != (int)Constant.ServiceRequestStatus.Expired);
                                var weight = 0;
                                if (agent.Id == lastRequestAgent)
                                {
                                    weight = 100;
                                }

                                if (agentRequests != null)
                                {
                                    counts.Add(agent.Id, agentRequests.Count() + weight);
                                }
                                else
                                {
                                    counts.Add(agent.Id, 0 + weight);
                                }
                            }

                            //find agent with min no of requests
                            //order by id asc and get top 1 agent
                            var item = counts.OrderBy(i => i.Value).ThenBy(i => i.Key).First();
                            agentId = item.Key;
                        }

                        //assign the request to agent
                        AgentServiceRequest asr = new AgentServiceRequest
                        {
                            AgentId          = agentId,
                            ServiceRequestId = ServiceRequestId,
                            Status           = (int)Constant.ServiceRequestStatus.Initial,
                            CreatedTime      = DateTime.Now.ToUniversalTime()
                        };

                        agentServiceRepo.Add(asr);

                        new NotificationRepository(context).Add(
                            asr.AgentId,
                            (int)Constant.NotificationType.Request,
                            asr.ServiceRequestId,
                            ConfigurationHelper.NOTIFICATION_TITLE,
                            Constant.Notification.NEW_REQUEST_TEXT);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log(typeof(ServiceRequestController), ex.Message + ex.StackTrace, LogType.ERROR);
            }
        }