public void UpdateDevicePlan(DevicePlanInsertModel model, int organizationId)
        {
            string sql = "update public.\"DevicePlan\" set \"Name\" = @name , \"Description\" = @description , \"BackupStartDate\" = @backupStartDate, " +
                         " \"RetryPlan\" = @retryPlan , \"PlanId\" = @planId , \"LocalSource\" = @localSource , \"RemoteSource\" = @remoteSource " +
                         " where \"OrganizationId\" = @organizationId and \"Id\" = @id ";
            object data = new {
                name            = model.Name,
                description     = model.Description,
                backupStartDate = model.RealInsertBackupStartDate,
                retryPlan       = model.RetryPlan,
                planId          = model.PlanId,
                localSource     = model.LocalSource,
                remoteSource    = model.RemoteSource,
                organizationId  = organizationId,
                id = model.Id
            };

            _connection.Execute(sql, data);
        }
        public int InsertDevicePlan(DevicePlanInsertModel model)
        {
            string sql = "insert into " +
                         " public.\"DevicePlan\" (\"Name\",\"Description\",\"BackupStartDate\",\"RetryPlan\",\"PlanId\",\"LocalSource\",\"RemoteSource\",\"OrganizationId\",\"DeviceId\",\"ActiveStatus\") " +
                         " values(@Name, @Description, @BackupStartDate, @RetryPlan, @PlanId, @LocalSource, @RemoteSource, @OrganizationId, @DeviceId,1)   RETURNING \"Id\" ";
            object data = new
            {
                Name            = model.Name,
                Description     = model.Description,
                BackupStartDate = model.RealInsertBackupStartDate,
                RetryPlan       = model.RetryPlan,
                PlanId          = model.PlanId,
                LocalSource     = model.LocalSource,
                RemoteSource    = model.RemoteSource,
                OrganizationId  = model.OrganizationId,
                DeviceId        = model.DeviceId
            };

            return(_connection.ExecuteScalar <int>(sql, data));
        }
        private BackupJobModelGoogle ReturnGoogleJobModel(PlatformInsertModel plan, DevicePlanInsertModel model, DpOperations db, string googleApiCode)
        {
            dynamic  platformDetail = JObject.Parse(plan.JsonData);
            DateTime?expireToken    = null;

            if (!String.IsNullOrEmpty(platformDetail.tokenExpire.ToString()))
            {
                expireToken = Convert.ToDateTime(platformDetail.tokenExpire.ToString());
            }

            BackupJobModelGoogle returnModel = new BackupJobModelGoogle();

            if (expireToken.HasValue && expireToken.Value > DateTime.Now)

            {
                returnModel.PlanType           = plan.Type;
                returnModel.LocalDirectory     = "'" + model.LocalSource + "'";
                returnModel.RemoteDirectory    = "'" + model.RemoteSource + "'";
                returnModel.DevicePlanId       = "'" + model.Id.ToString() + "'";
                returnModel.GoogleAccessToken  = "'" + platformDetail.accessToken + "'";
                returnModel.GoogleApiCode      = "'" + googleApiCode + "'";
                returnModel.GoogleTokenExpired = "'" + platformDetail.tokenExpire.ToString() + "'";
            }
            else
            {
                //PlatformGoogle gg = new PlatformGoogle();
                TokenResponse tr = ResetGoogleDriveToken(plan, plan.Id, db, plan.OrganizationId);

                returnModel.PlanType           = plan.Type;
                returnModel.LocalDirectory     = "'" + model.LocalSource + "'";
                returnModel.RemoteDirectory    = "'" + model.RemoteSource + "'";
                returnModel.DevicePlanId       = "'" + model.Id.ToString() + "'";
                returnModel.GoogleAccessToken  = "'" + platformDetail.accessToken + "'";
                returnModel.GoogleApiCode      = "'" + googleApiCode + "'";
                returnModel.GoogleTokenExpired = "'" + platformDetail.tokenExpire.ToString() + "'";
            }
            return(returnModel);
        }
        public IActionResult NewJob(string id, string deviceId)
        {
            SetNewJobViewBag();

            int realId = WebUtilities.DecryptId(id, GetUserName(), GetOrganizationId());

            if (realId > 0)
            {
                DevicePlanInsertModel model = operations.DevicePlan.GetDevicePlanById(GetOrganizationId(), realId);
                model.HashedDevicePlanId = WebUtilities.EncryptId(model.Id, GetUserName(), GetOrganizationId());
                model.BackupStartDate    = model.RealInsertBackupStartDate.Value.Day + "-" + model.RealInsertBackupStartDate.Value.Month + "-" + model.RealInsertBackupStartDate.Value.Year;
                model.BackupStartTime    = model.RealInsertBackupStartDate.Value.Hour + ":" + model.RealInsertBackupStartDate.Value.Minute;
                model.HashedPlanId       = WebUtilities.EncryptId(model.PlanId, GetUserName(), GetOrganizationId());
                model.DeviceHashedId     = deviceId;
                return(View(model));
            }
            else
            {
                DevicePlanInsertModel model = new DevicePlanInsertModel();
                model.DeviceHashedId = deviceId;
                return(View(model));
            }
        }
        public IActionResult NewJob(DevicePlanInsertModel model)
        {
            SetNewJobViewBag();
            model.CloseModal = false;
            if (String.IsNullOrEmpty(model.Name))
            {
                TempData["Notification"] = WebUtilities.InsertNotification("İsim boş geçilemez");
                return(View(model));
            }
            if (String.IsNullOrEmpty(model.Description))
            {
                TempData["Notification"] = WebUtilities.InsertNotification("Açıklama boş geçilemez");
                return(View(model));
            }
            if (String.IsNullOrEmpty(model.LocalSource))
            {
                TempData["Notification"] = WebUtilities.InsertNotification("Kaynak bilgisayar boş geçilemez");
                return(View(model));
            }
            if (String.IsNullOrEmpty(model.RemoteSource))
            {
                TempData["Notification"] = WebUtilities.InsertNotification("Hedef bilgisayar boş geçilemez");
                return(View(model));
            }
            if (String.IsNullOrEmpty(model.BackupStartDate))
            {
                TempData["Notification"] = WebUtilities.InsertNotification("Başlangıç tarihi boş geçilemez");
                return(View(model));
            }
            if (String.IsNullOrEmpty(model.BackupStartTime))
            {
                TempData["Notification"] = WebUtilities.InsertNotification("Başlangıç saati boş geçilemez");
                return(View(model));
            }
            try
            {
                string[] dateArray = model.BackupStartDate.Split('-');
                string[] timeArray = model.BackupStartTime.Split(':');
                model.RealInsertBackupStartDate = new DateTime(Convert.ToInt32(dateArray[2]), Convert.ToInt32(dateArray[1]), Convert.ToInt32(dateArray[0]), Convert.ToInt32(timeArray[0]), Convert.ToInt32(timeArray[1]), 0);
            }
            catch
            {
                TempData["Notification"] = WebUtilities.InsertNotification("Lütfen tarih ve saat bilgisini kontrol ediniz.");
                return(View(model));
            }
            model.PlanId         = WebUtilities.DecryptId(WebUtility.UrlDecode(model.HashedPlanId), GetUserName(), GetOrganizationId());
            model.DeviceId       = WebUtilities.DecryptId(model.DeviceHashedId, GetUserName(), GetOrganizationId());
            model.OrganizationId = GetOrganizationId();
            if (String.IsNullOrEmpty(model.HashedDevicePlanId))
            {
                int returnValue = operations.DevicePlan.InsertDevicePlan(model);

                if (returnValue > 0)
                {
                    model.Id = returnValue;
                    TriggerHangfireJob(model);
                    model.HashedDevicePlanId = WebUtilities.EncryptId(returnValue, GetUserName(), GetOrganizationId());
                    TempData["Notification"] = WebUtilities.InsertNotification("Cihaz planı başarılı bir şekilde eklenmiştir.");
                    model.CloseModal         = true;
                    return(View(model));
                }
                else
                {
                    TempData["Notification"] = WebUtilities.InsertNotification("Ekleme sırasında bir hata oluştu lütfen sistem yöneticisine başvurunuz.");
                    model.CloseModal         = false;
                    return(View(model));
                }
            }
            else
            {
                int realId = WebUtilities.DecryptId(WebUtility.UrlDecode(model.HashedDevicePlanId), GetUserName(), GetOrganizationId());
                if (realId > 0)
                {
                    model.CloseModal = true;
                    model.Id         = realId;
                    operations.DevicePlan.UpdateDevicePlan(model, GetOrganizationId());

                    List <string> list = new List <string>();
                    list.Add(operations.DevicePlan.GetDevicePlanById(GetOrganizationId(), realId).BackgroundJobId);
                    HangfireProvider.DeleteHangFireJobs(list);
                    TriggerHangfireJob(model);
                    return(View(model));
                }
                else
                {
                    TempData["Notification"] = WebUtilities.InsertNotification("Hatalı işlem lütfen pencereyi kapatıp tekrar deneyiniz.");
                    model.CloseModal         = false;
                    return(View(model));
                }
            }
            // return View();
        }
        public void TriggerHangfireJob(DevicePlanInsertModel model)
        {
            model.RealInsertBackupStartDate = model.RealInsertBackupStartDate.Value.AddHours(-3);
            if (model.RetryPlan == Database.Enum.RetryPlan.OneTime)
            {
                HangfireProvider.InsertJobOneTimeBackup(model.RealInsertBackupStartDate.Value, model.Id, model.DeviceId, GetOrganizationId());
            }
            else
            {
                string retryPlan = "";
                if (model.RetryPlan == Database.Enum.RetryPlan.Minute30)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + "/30 * * * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Hour1)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + "/1 * * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Hour3)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + "/3 * * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Hour6)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + "/6 * * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Hour12)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + "/12 * * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day1)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 1) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day2)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 2) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day3)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 3) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day4)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 4) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day5)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 5) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day6)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 6) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day7)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 7) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day8)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 8) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day9)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 9) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Day10)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + DailyCron(model.RealInsertBackupStartDate.Value.Day, 10) + " * *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Month1)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + model.RealInsertBackupStartDate.Value.Day + " " + MonthCron(model.RealInsertBackupStartDate.Value.Month, 1) + " *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Month2)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + model.RealInsertBackupStartDate.Value.Day + " " + MonthCron(model.RealInsertBackupStartDate.Value.Month, 2) + " *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Month3)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + model.RealInsertBackupStartDate.Value.Day + " " + MonthCron(model.RealInsertBackupStartDate.Value.Month, 3) + " *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Month4)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + model.RealInsertBackupStartDate.Value.Day + " " + MonthCron(model.RealInsertBackupStartDate.Value.Month, 4) + " *";
                }
                else if (model.RetryPlan == Database.Enum.RetryPlan.Year1)
                {
                    retryPlan = "" + model.RealInsertBackupStartDate.Value.Minute + " " + model.RealInsertBackupStartDate.Value.Hour + " " + model.RealInsertBackupStartDate.Value.Day + " " + model.RealInsertBackupStartDate.Value.Month + " *";
                }


                HangfireProvider.InsertJobTimePlanBackup(model.Id, retryPlan, model.DeviceId, GetOrganizationId());
            }
        }