public string InsertTicket(TTicket ticket, string userId)
        {
            try
            {
                //------------------------------------------------------------//
                // check a valid of Ticket
                //------------------------------------------------------------//
                TicketSaleDateBusiness saleDatebusines = new TicketSaleDateBusiness();
                Ticket newTicket = ThriftUtil.ConvertToEntityObject(ticket) as Ticket;

                string errorMessage = CheckTicketInfo(newTicket);
                errorMessage += saleDatebusines.ValidateDateTime(newTicket.departure_time);

                if (string.IsNullOrEmpty(errorMessage) == false)
                {
                    return(errorMessage);
                }

                TicketBusiness business  = new TicketBusiness();
                string         resultMsg = business.Insert(newTicket);

                ////notify to the others client station
                //ticket.TicketId = ticketId;
                //BroadcastToClient(ClientAction.SellTicket,ticket);

                return(resultMsg);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[InsertTicket]", exc);
                return(exc.Message);
            }
        }
예제 #2
0
        protected void BroadcastToClient(ClientAction action, params object[] args)
        {
            for (int index = 0; index < _clientList.Count; index++)
            {
                try
                {
                    var client = _clientList[index];

                    string errorMessage = client.OpenTransaction();

                    if (string.IsNullOrEmpty(errorMessage) == false)
                    {
                        ServerLogger.logError("[BroadcastToClient]", string.Format(CLIENT_ERROR_FORMAT, errorMessage));
                        continue;
                    }

                    string result = SendNotifyToClient(client, action, args);
                    ServerLogger.logInfo("[BroadcastToClient]", string.Format(CLIENT_RESULT_FORMAT, result));

                    client.CloseTransaction();
                }
                catch (Exception exc)
                {
                    ServerLogger.logError("[BroadcastToClient]", exc);
                }
            }
        }
        public TUser Login(string userName, string password, string senderIP, string senderPort)
        {
            try
            {
                ServerLogger.logInfo("Login", string.Format("User login [{0}] - [{1}] - [{2}] - [{3}]", userName, password, senderIP, senderPort), serverView);
                using (UserBusiness business = new UserBusiness())
                {
                    //bool loginSuccess = business.Users.Any(u => u.user_name == userName && u.password == password && u.active_status && !u.deleted);
                    User user = business.ValidateUser(userName, password, Constants.UserRole.Staff.ToString());

                    if (user == null)
                    {
                        string failMessage = "Login Fail";
                        //if (user == null) failMessage = "Không tồn tại người dùng: " + userName;
                        //else if (user.password != password) failMessage = "Nhập sai mật khẩu";
                        //else if (user.active_status == false) failMessage = "Người dùng này đã bị khóa.";

                        ServerLogger.logInfo("Login", string.Format("login result: {0}", failMessage), serverView);
                        return(new TUser());
                    }

                    AddClientStation(userName, senderIP, int.Parse(senderPort));

                    return(ThriftUtil.ConvertToTUser(user));
                }
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[Login]", exc);
                return(new TUser());
            }
        }
        /// <remarks>must get config follow each role</remarks>
        public TSystemConfig LoadSystemConfig()
        {
            try
            {
                TSystemConfig systemConfig = new TSystemConfig();

                using (ThanhVanTranSysEntities context = new ThanhVanTranSysEntities())
                {
                    systemConfig.Configurations  = context.Configurations.ToList().Select(i => ThriftUtil.ConvertToTConfiguration(i)).ToList();
                    systemConfig.SaleTicketDates = context.TicketSaleDates.ToList().Select(i => ThriftUtil.ConvertToTSaleTicketDate(i)).ToList();
                    systemConfig.TicketPrices    = context.TicketPriceConfigurations.ToList().Select(i => ThriftUtil.ConvertToTTicketPrice(i)).ToList();
                    systemConfig.TransportPrices = context.TransportPriceConfigurations.ToList().Select(i => ThriftUtil.ConvertToTTransportPrice(i)).ToList();
                    systemConfig.GuaranteeFees   = context.GuaranteeFeeConfigurations.ToList().Select(i => ThriftUtil.ConvertToTGuaranteeFee(i)).ToList();
                    systemConfig.Buses           = context.Buses.ToList().Select(i => ThriftUtil.ConvertToTBus(i)).ToList();
                    systemConfig.Tours           = context.Tours.ToList().Select(i => ThriftUtil.ConvertToTTour(i)).ToList();
                }

                return(systemConfig);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[LoadSystemConfig]", exc);
                return(new TSystemConfig());
            }
        }
예제 #5
0
        protected bool AddClientStation(string userName, string senderIP, int senderPort)
        {
            try
            {
                if (_clientList.Count(c => c.UserName == userName) > 0)
                {
                    // duplicate logging in of one user
                }
                else
                {
                    ClientStation client = _clientList.SingleOrDefault(c => c.UserName == userName);

                    if (client != null && (client.IPAddress != senderIP || client.Port != senderPort))
                    {
                        client.IPAddress = senderIP;
                        client.Port      = senderPort;
                    }
                    else
                    {
                        _clientList.Add(new ClientStation(userName, senderIP, senderPort));
                    }
                }
                return(true);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[AddClientStation]", exc);
                return(false);
            }
        }
        public List <TOrderItem> SyncOrderItem()
        {
            try
            {
                IEnumerable <OrderHistory> changedOrderIds;

                using (ThanhVanTranSysEntities context = new ThanhVanTranSysEntities())
                    using (OrderItemBusiness business = new OrderItemBusiness())
                    {
                        changedOrderIds = context.OrderHistories
                                          .Where(i => i.date_time.Year == DateTime.Now.Year && i.date_time.Month == DateTime.Now.Month && i.date_time.Day == DateTime.Now.Day)
                                          .OrderBy(i => i.order_id).ThenBy(i => i.date_time);

                        var orderItems = (from oi in business.GetAll()
                                          join co in changedOrderIds on oi.order_id equals co.order_id
                                          select ThriftUtil.ConvertToTOrderItem(oi, co.action == Constants.DELETE_ACTION)).ToList();

                        return(orderItems);
                    }
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[SyncOrderItem]", exc);
                return(new List <TOrderItem>());
            }
        }
        public List <TIrregularOrder> SyncIrregularOrder()
        {
            try
            {
                IEnumerable <OrderHistory> changedOrderIds;

                using (ThanhVanTranSysEntities context = new ThanhVanTranSysEntities())
                    using (IrregularOrderBusiness business = new IrregularOrderBusiness())
                    {
                        //insert/update records
                        changedOrderIds = context.OrderHistories
                                          .Where(i => i.date_time.Year == DateTime.Now.Year && i.date_time.Month == DateTime.Now.Month && i.date_time.Day == DateTime.Now.Day)
                                          .OrderBy(i => i.order_id).ThenBy(i => i.date_time);

                        var orders = (from io in business.GetAll()
                                      join co in changedOrderIds on io.id equals co.order_id
                                      select ThriftUtil.ConvertToTIrregularOrder(io, co.action == Constants.DELETE_ACTION)).ToList();

                        //deleted records
                        orders.AddRange((from o in changedOrderIds
                                         where o.action == Constants.DELETE_ACTION
                                         select new TIrregularOrder()
                        {
                            OrderId = o.order_id,
                            IsDeleted = true,
                            CloseDate = DateTime.Now.ToString(),
                            CreateBy = string.Empty,
                            CreateDate = DateTime.Now.ToString(),
                            Destination = string.Empty,
                            OrderStaus = string.Empty,
                            PaymentStatus = string.Empty,
                            RecipientIdNo = string.Empty,
                            RecipientName = string.Empty,
                            RecipientPhone = string.Empty,
                            SenderIdNo = string.Empty,
                            SenderName = string.Empty,
                            SenderPhone = string.Empty,
                            UserReponsitory = string.Empty,
                            TotalCost = 0,
                            TotalQuantity = 0,
                            TotalValue = 0,
                        }));

                        return(orders);
                    }
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[LoadIrregularOrder]", exc);
                return(new List <TIrregularOrder>());
            }
        }
        public string CreateOrder(TOrder torder, List <TOrderItem> torderItems, string userId)
        {
            try
            {
                //Check Order
                string errorMessage = CheckOrder(torder);

                //check OrderItem
                List <OrderItem> orderItems = ThriftUtil.ConvertToOrderItemList(torderItems);
                errorMessage += CheckOrderItems(orderItems);

                if (string.IsNullOrEmpty(errorMessage) == false)
                {
                    return(errorMessage);
                }

                string orderId = string.Empty;
                //Insert Order
                if (torder.Type == Constants.VALUE_ORDER_TYPE_REGULAR)
                {
                    using (RegularOrderBusiness business = new RegularOrderBusiness())
                    {
                        business.Insert(ThriftUtil.ConvertToRegualrOrder(torder));
                    }
                }
                else
                {
                    using (IrregularOrderBusiness business = new IrregularOrderBusiness())
                    {
                        business.Insert(ThriftUtil.ConvertToIrregularOrder(torder));
                    }
                }

                using (OrderItemBusiness business = new OrderItemBusiness())
                {
                    business.Insert(orderItems);
                }

                //notify to the others client station
                //torder.OrderId = orderId;
                //BroadcastToClient(ClientAction.CreateOrder,order,orderItems);

                return("");
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[CreateOrder]", exc);
                return(exc.Message);
            }
        }
 public List <TCustomer> LoadCustomer()
 {
     try
     {
         using (CustomerBusiness business = new CustomerBusiness())
         {
             return(business.GetAll().ToList().Select(i => ThriftUtil.ConvertToTCustomer(i)).ToList());
         }
     }
     catch (Exception exc)
     {
         ServerLogger.logError("[LoadCustomer]", exc);
         return(new List <TCustomer>());
     }
 }
 public List <TOrderItem> LoadOrderItem()
 {
     try
     {
         using (OrderItemBusiness business = new OrderItemBusiness())
         {
             return(business.GetAll().ToList().Select(i => ThriftUtil.ConvertToThriftObject(i) as TOrderItem).ToList());
         }
     }
     catch (Exception exc)
     {
         ServerLogger.logError("[LoadOrderItem]", exc);
         return(new List <TOrderItem>());
     }
 }
 public List <TIrregularOrder> LoadIrregularOrder()
 {
     try
     {
         using (IrregularOrderBusiness business = new IrregularOrderBusiness())
         {
             return(business.GetAll().ToList().Select(i => ThriftUtil.ConvertToThriftObject(i) as TIrregularOrder).ToList());
         }
     }
     catch (Exception exc)
     {
         ServerLogger.logError("[LoadIrregularOrder]", exc);
         return(new List <TIrregularOrder>());
     }
 }
        public List <TTicket> SyncTicket()
        {
            try
            {
                IEnumerable <TicketHistory> changedTicketId;

                using (ThanhVanTranSysEntities context = new ThanhVanTranSysEntities())
                    using (TicketBusiness business = new TicketBusiness())
                    {
                        changedTicketId = context.TicketHistories
                                          .Where(i => i.changed_date.Year == DateTime.Now.Year && i.changed_date.Month == DateTime.Now.Month && i.changed_date.Day == DateTime.Now.Day)
                                          .OrderBy(i => i.ticket_id).ThenBy(i => i.changed_date);

                        //insert/update records
                        var tickets = (from t in business.GetAll()
                                       join th in changedTicketId on t.id equals th.ticket_id
                                       where t.departure_time >= DateTime.Now
                                       select ThriftUtil.ConvertToTTicket(t, th.action == Constants.DELETE_ACTION)).ToList();

                        //deleted records
                        tickets.AddRange((from th in changedTicketId
                                          where th.action == Constants.DELETE_ACTION
                                          select new TTicket
                        {
                            TicketId = th.ticket_id,
                            BusId = string.Empty,
                            CusIdNo = string.Empty,
                            CusName = string.Empty,
                            CusPhone = string.Empty,
                            DepartTime = DateTime.Now.ToString(),
                            IsDeleted = true,
                            SeatNo = 0,
                            SeatType = string.Empty,
                            Status = string.Empty,
                            TicketPrice = 0,
                            TourId = string.Empty,
                            UserId = string.Empty
                        }));

                        return(tickets);
                    }
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[LoadTicket]", exc);
                return(new List <TTicket>());
            }
        }
        public string UpdateOrderInfo(TOrder torder, List <TOrderItem> orderItems, string userId)
        {
            try
            {
                //Check Order
                string errorMessage = CheckOrder(torder, true);

                //check OrderItem
                errorMessage += orderItems.Count == 0 ? "Không có thông tin hàng hóa" : "";

                if (string.IsNullOrEmpty(errorMessage) == false)
                {
                    return(errorMessage);
                }

                if (torder.Type == Constants.VALUE_ORDER_TYPE_REGULAR)
                {
                    using (RegularOrderBusiness business = new RegularOrderBusiness())
                    {
                        business.Update(ThriftUtil.ConvertToEntityObject(torder) as RegularOrder);
                    }
                }
                else
                {
                    using (IrregularOrderBusiness business = new IrregularOrderBusiness())
                    {
                        business.Update(ThriftUtil.ConvertToEntityObject(torder) as IrregularOrder);
                    }
                }

                using (OrderItemBusiness business = new OrderItemBusiness())
                {
                    business.DeleteByOrderId(torder.OrderId);

                    business.Insert(ThriftUtil.ConvertToOrderItemList(orderItems).Cast <OrderItem>());
                }

                //notify to the others client station
                //BroadcastToClient(ClientAction.UpdateOrder,order,orderItems);

                return("");
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[UpdateOrderInfo]", exc);
                return(exc.Message);
            }
        }
 public List <TTicket> LoadTicket()
 {
     try
     {
         using (TicketBusiness business = new TicketBusiness())
         {
             return(business.GetAll().ToList()
                    .Where(i => i.departure_time >= DateTime.Now)
                    .Select(i => ThriftUtil.ConvertToTTicket(i)).ToList());
         }
     }
     catch (Exception exc)
     {
         ServerLogger.logError("[LoadTicket]", exc);
         return(new List <TTicket>());
     }
 }
예제 #15
0
        private void StartServer()
        {
            try
            {
                string startMessage = string.Format("server start with IP: {0} - Port: {1}", _serverIP, _serverPort);

                ServerLogger.logInfo("StartServer", startMessage);
                btnStart.Visible = false;
                btnStop.Visible  = true;

                _serverThread = new Thread(new ThreadStart(InitTransportService));
                _serverThread.Start();

                OutputAction(startMessage);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[StopServer]", exc);
            }
        }
        public string Logout(string userName, string senderIP, string senderPort)
        {
            try
            {
                ServerLogger.logInfo("Login", string.Format("User Logout [userName: {0}] - [Port: {1}]", userName, senderPort), serverView);

                ClientStation client = _clientList.FirstOrDefault(c => c.UserName == userName && c.IPAddress == senderIP && c.Port == int.Parse(senderPort));
                if (client != null)
                {
                    _clientList.Remove(client);
                    return("");
                }
                return("Người dùng này chưa được Đăng Nhập");
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[Logout]", exc);
                return(exc.Message);
            }
        }
예제 #17
0
        private void StopServer()
        {
            try
            {
                string stopMessage = "Server is Stoped";

                if (_server != null)
                {
                    _server.Stop();
                }
                if (_serverThread != null)
                {
                    _serverThread.Abort();
                }
                OutputAction(stopMessage);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[StopServer]", exc);
            }
        }
        public string UpdateTicket(TTicket tticket, string userId)
        {
            try
            {
                using (ThanhVanTranSysEntities context = new ThanhVanTranSysEntities())
                {
                    DateTime departTime    = DateTime.Parse(tticket.DepartTime);
                    var      existedTicket = context.Tickets.FirstOrDefault(t => t.bus_id == tticket.BusId &&
                                                                            t.departure_time == departTime &&
                                                                            t.seat_number == tticket.SeatNo &&
                                                                            t.seat_class == tticket.SeatType &&
                                                                            t.tour_id == tticket.TourId);

                    if (existedTicket == null || (existedTicket != null && existedTicket.status == Constants.TicketStatus.Cancel.ToString()))
                    {
                        return(Constants.SERVER_ERROR_CODE_SINGLE_DATA_NOT_SYNC + " Vé đã bị xóa!");
                    }
                }

                if (CheckUserPermission(userId, tticket.UserId) == false)
                {
                    return(Constants.Messages.MSG_TICKET_INSUFFICIENT_PERMISSION);
                }

                TicketBusiness business  = new TicketBusiness();
                Ticket         ticket    = ThriftUtil.ConvertToEntityObject(tticket) as Ticket;
                string         resultMsg = business.Update(ticket);

                //notify to the others client station
                //BroadcastToClient(ClientAction.UpdateTicket,ticket);

                return(resultMsg);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[UpdateTicket]", exc);
                return(exc.Message);
            }
        }
        /// <summary>
        /// CompleteOrder (just using for Irregular Order)
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public string DeleteOrder(TOrder order, string userId)
        {
            try
            {
                // Check Order
                string errorMessage = CheckOrder(order, true);

                if (string.IsNullOrEmpty(errorMessage) == false)
                {
                    return(errorMessage);
                }

                // Delete
                if (order.Type == Constants.VALUE_ORDER_TYPE_REGULAR)
                {
                    RegularOrderBusiness business = new RegularOrderBusiness();
                    business.Delete(order.OrderId);
                }
                else
                {
                    IrregularOrderBusiness business = new IrregularOrderBusiness();
                    business.Delete(order.OrderId);
                }

                OrderItemBusiness itemBusiness = new OrderItemBusiness();
                itemBusiness.DeleteByOrderId(order.OrderId);

                ////notify to the others client station
                ////BroadcastToClient(ClientAction.CompleteOrder,order);

                return("");
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[DeleteOrder]", exc);
                return(exc.Message);
            }
        }
        public string UpdateSingleOrderInfo(TOrder torder, string userId)
        {
            try
            {
                //Check Order
                string errorMessage = CheckOrder(torder, true);

                if (string.IsNullOrEmpty(errorMessage) == false)
                {
                    return(errorMessage);
                }

                if (torder.Type == Constants.VALUE_ORDER_TYPE_REGULAR)
                {
                    using (RegularOrderBusiness business = new RegularOrderBusiness())
                    {
                        business.Update(ThriftUtil.ConvertToEntityObject(torder) as RegularOrder);
                    }
                }
                else
                {
                    using (IrregularOrderBusiness business = new IrregularOrderBusiness())
                    {
                        business.Update(ThriftUtil.ConvertToEntityObject(torder) as IrregularOrder);
                    }
                }

                //notify to the others client station
                //BroadcastToClient(ClientAction.UpdateOrder,order,orderItems);

                return("");
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[UpdateSingleOrderInfo]", exc);
                return(exc.Message);
            }
        }
        public string CancelTicket(TTicket tticket, string userId)
        {
            try
            {
                using (TicketReturnFeeConfigurationBusiness _ticketReturnFeeBusiness = new TicketReturnFeeConfigurationBusiness())
                    using (TicketBusiness business = new TicketBusiness())
                    {
                        Ticket cancelTicket = business.Get(tticket.TicketId);

                        if (cancelTicket == null)
                        {
                            return(string.Format("{0} Vé không tồn tại, không thể hủy vé, Mã:{0}", Constants.SERVER_ERROR_CODE_SINGLE_DATA_NOT_SYNC, tticket.TicketId));
                        }

                        string  result    = string.Empty;
                        decimal returnFee = _ticketReturnFeeBusiness.GetReturnFee(cancelTicket, DateTime.Now);
                        if (returnFee > 0)
                        {
                            result = business.Cancel(cancelTicket, returnFee);
                        }
                        else
                        {
                            result = business.Delete(cancelTicket.id);
                        }
                        return(result);
                    }

                //notify to the others client station
                //BroadcastToClient(ClientAction.CancelTicket,tticket);
            }
            catch (Exception exc)
            {
                ServerLogger.logError("[CancelTicket]", exc);
                return(exc.Message);
            }
        }