コード例 #1
0
        public async Task <IActionResult> Index()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user != null)
            {
                DateTime localTimeUTC7 = SystemUtilites.ConvertToTimeZone(DateTime.UtcNow, "SE Asia Standard Time");
                var      dayJob        = _dayJobServices.GetDayJob(user.Id, SystemUtilites.ConvertToTimeStamp(localTimeUTC7.Date));
                if (dayJob == null)
                {
                    dayJob = new DayJob()
                    {
                        DayJobId   = Guid.NewGuid().ToString(),
                        Date       = SystemUtilites.ConvertToTimeStamp(localTimeUTC7.Date),
                        DriverId   = user.Id,
                        Transports = new List <TransportInformation>()
                    };
                    await _dayJobServices.Create(dayJob);
                }
                dayJob.Transports = dayJob.Transports.OrderByDescending(t => t.DateStartLocal).ToList();
                return(View(dayJob));
            }
            return(RedirectToAction(actionName: "Login", controllerName: "Account", new { area = "" }));
        }
コード例 #2
0
        public async Task <IActionResult> Create(CreateTransInfoViewModel model)
        {
            //get local time at Timezone UTC 7
            DateTime localTimeUTC7 = SystemUtilites.ConvertToTimeZone(DateTime.UtcNow, "SE Asia Standard Time");
            //get timestamp of day at 0 AM
            double TStodayUTC7At0Am = SystemUtilites.ConvertToTimeStamp(localTimeUTC7.Date);
            //get timestamp now at utc
            double TSUTCNow = SystemUtilites.ConvertToTimeStamp(DateTime.UtcNow);

            //get data for select elements
            model.Drivers  = _userServices.GetDriverUsers().ToList();
            model.Routes   = _routeServices.GetAllRoutes().ToList();
            model.Vehicles = (await _vehicleServices.GetAllNotDeletedAndAvailableVehicles()).ToList();
            string message = String.Empty;

            if (ModelState.IsValid)
            {
                //check the vehicle is used
                string driverIdUseVehicle = await _vehicleServices.IsVehicleInUsedByAnotherDriver(model.DriverId, model.VehicleId);

                if (!String.IsNullOrEmpty(driverIdUseVehicle))
                {
                    var driverUseVehicle = _userServices.GetUser(driverIdUseVehicle);
                    message = $"Xe đang được sử dụng bởi {driverUseVehicle.FullName}";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                    return(View(model));
                }

                //create new TransportInformation
                var user = await _userManager.GetUserAsync(User);

                TransportInformation newTrans = new TransportInformation()
                {
                    TransportId    = Guid.NewGuid().ToString(),
                    AdvanceMoney   = model.AdvanceMoney,
                    DateStartUTC   = TSUTCNow,
                    DateStartLocal = SystemUtilites.ConvertToTimeStamp(localTimeUTC7),
                    TimeZone       = "SE Asia Standard Time",
                    CargoTypes     = model.CargoTypes,
                    Note           = model.Note,
                    VehicleId      = model.VehicleId,
                    RouteId        = model.RouteId,
                    UserCreateId   = user.Id
                };
                //get or create if not dayjob has date match today timeStamp
                DayJob driverDayJob = _dayJobServices.GetDayJob(model.DriverId, TStodayUTC7At0Am);
                if (driverDayJob == null)
                {
                    driverDayJob = new DayJob()
                    {
                        DayJobId = Guid.NewGuid().ToString(),
                        DriverId = model.DriverId,
                        Date     = TStodayUTC7At0Am
                    };
                    if (!(await _dayJobServices.Create(driverDayJob)))
                    {
                        message = "Lỗi không xác định, xin mời thao tác lại";
                        TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
                        return(View(model));
                    }
                }
                newTrans.DayJobId = driverDayJob.DayJobId;
                //create new TransInfo in SQL
                if (await _transInfoServices.CreateNewTransInfo(newTrans))
                {
                    var vehicle = await _vehicleServices.GetVehicle(newTrans.VehicleId);

                    if (vehicle != null)
                    {
                        if (!vehicle.IsInUse)
                        {
                            await _vehicleServices.MakeVehicleInUsed(vehicle);
                        }
                    }
                    var driver = await _userManager.FindByIdAsync(model.DriverId);

                    if (driver != null)
                    {
                        if (driver.IsAvailable)
                        {
                            await _userServices.MakeDriverIsBusy(driver);
                        }
                    }
                    message = "Chuyến vận chuyển đã được tạo";
                    TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Success, message);
                    return(RedirectToAction(actionName: "Manage"));
                }
            }
            message = "Lỗi không xác định, xin mời thao tác lại";
            TempData["UserMessage"] = SystemUtilites.SendSystemNotification(NotificationType.Error, message);
            return(View(model));
        }