diff --git a/SolarPower/Controllers/OperationController.cs b/SolarPower/Controllers/OperationController.cs index 799c34a..bf87dc9 100644 --- a/SolarPower/Controllers/OperationController.cs +++ b/SolarPower/Controllers/OperationController.cs @@ -71,6 +71,13 @@ namespace SolarPower.Controllers ApiResult apiResult = new ApiResult(); try { + if(Convert.ToDateTime(post.StartTime) < Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"))) + { + apiResult.Msg = "開始時間不能選擇過去"; + apiResult.Code = "0099"; + return apiResult; + } + if (post.Id == 0) { @@ -105,26 +112,47 @@ namespace SolarPower.Controllers "PlanId", "CreatedBy" }; + + + + if(post.StartTime == DateTime.Now.ToString("yyyy-MM-dd")) + { + string endtime = ""; + switch (post.ScheduleType) + { + case 0: + endtime = (Convert.ToDateTime(post.StartTime).AddDays(post.ScheduleNum)).ToString("yyyy-MM-dd 00:00:00");break; + case 1: + endtime = (Convert.ToDateTime(post.StartTime).AddDays(post.ScheduleNum * 7)).ToString("yyyy-MM-dd 00:00:00"); break; + case 2: + endtime = (Convert.ToDateTime(post.StartTime).AddMonths(post.ScheduleNum)).ToString("yyyy-MM-dd 00:00:00"); break; + case 3: + endtime = (Convert.ToDateTime(post.StartTime).AddMonths(post.ScheduleNum * 3)).ToString("yyyy-MM-dd 00:00:00"); break; + case 4: + endtime = (Convert.ToDateTime(post.StartTime).AddYears(post.ScheduleNum * 3)).ToString("yyyy-MM-dd 00:00:00"); break; + } + var record = new PlanToRecord() + { + WorkType = post.Type, + PowerStationId = post.PowerStationId, + StartTime = post.StartTime, + CreatedBy = myUser.Id, + EndTime = endtime + }; + List properties2 = new List() + { + "WorkType", + "PowerStationId", + "StartTime", + "CreatedBy", + "EndTime" + }; + OperationPlan.LastCreateTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00"); + properties.Add("LastCreateTime"); + + await operationRepository.AddToRecord(record, properties2); + } await operationRepository.AddOperationPlan(OperationPlan, properties); - - var record = new PlanToRecord() - { - WorkType = post.Type, - PowerStationId = post.PowerStationId, - StartTime = post.StartTime, - CreatedBy = myUser.Id, - EndTime = post.EndTime - }; - List properties2 = new List() - { - "WorkType", - "PowerStationId", - "StartTime", - "CreatedBy", - "EndTime" - }; - await operationRepository.AddToRecord(record, properties2); - apiResult.Code = "0000"; apiResult.Msg = "新增成功"; } @@ -156,6 +184,44 @@ namespace SolarPower.Controllers "Type", "UpdatedBy" }; + if (post.StartTime == DateTime.Now.ToString("yyyy-MM-dd")) + { + string endtime = ""; + switch (post.ScheduleType) + { + case 0: + endtime = (Convert.ToDateTime(post.StartTime).AddDays(post.ScheduleNum)).ToString("yyyy-MM-dd 00:00:00"); break; + case 1: + endtime = (Convert.ToDateTime(post.StartTime).AddDays(post.ScheduleNum * 7)).ToString("yyyy-MM-dd 00:00:00"); break; + case 2: + endtime = (Convert.ToDateTime(post.StartTime).AddMonths(post.ScheduleNum)).ToString("yyyy-MM-dd 00:00:00"); break; + case 3: + endtime = (Convert.ToDateTime(post.StartTime).AddMonths(post.ScheduleNum * 3)).ToString("yyyy-MM-dd 00:00:00"); break; + case 4: + endtime = (Convert.ToDateTime(post.StartTime).AddYears(post.ScheduleNum * 3)).ToString("yyyy-MM-dd 00:00:00"); break; + } + var record = new PlanToRecord() + { + WorkType = post.Type, + PowerStationId = post.PowerStationId, + StartTime = post.StartTime, + CreatedBy = myUser.Id, + EndTime = endtime + }; + List properties2 = new List() + { + "WorkType", + "PowerStationId", + "StartTime", + "CreatedBy", + "EndTime" + }; + OperationPlan.LastCreateTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00"); + properties.Add("LastCreateTime"); + + await operationRepository.AddToRecord(record, properties2); + } + await operationRepository.UpdateOperationPlan(OperationPlan, properties); apiResult.Code = "0000"; apiResult.Msg = "編輯成功"; diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index 4fbf229..aff239e 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -1569,6 +1569,15 @@ INSERT INTO `ref_err_inv` (`id`, `brend`, `deviceType`, `errCode`, `errMsg`, `er (7, 'solaredge', 'inverter', 7, 'Fault', '設備異常'), (8, 'solaredge', 'inverter', 8, 'Maintenance/setup', '維修中/設定中'); +-- 新增定時任務紀錄時間 20210714 +ALTER TABLE `operation_plan_create` + ADD COLUMN `LastCreateTime` TIMESTAMP NULL DEFAULT NULL COMMENT '最新一次產生紀錄時間' AFTER `Description`; + +-- 修改欄位值 20210714 +ALTER TABLE `operation_plan_create` + CHANGE COLUMN `UpdatedAt` `UpdatedAt` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP() AFTER `UpdatedBy`; +ALTER TABLE `operation_record` + CHANGE COLUMN `FixDo` `FixDo` VARCHAR(50) NULL DEFAULT NULL COMMENT '維修項目' COLLATE 'utf8mb4_unicode_ci' AFTER `ErrorCode`; /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; diff --git a/SolarPower/Models/Operation.cs b/SolarPower/Models/Operation.cs index c3ed256..6d44b76 100644 --- a/SolarPower/Models/Operation.cs +++ b/SolarPower/Models/Operation.cs @@ -96,6 +96,8 @@ namespace SolarPower.Models private string updatedAt; public int UpdatedBy { get; set; } //修改者 public string UpdatedAt { get { return Convert.ToDateTime(updatedAt).ToString("yyyy-MM-dd HH:mm:ss"); } set { updatedAt = value; } } //修改時間 + private string lastCreateTime; + public string LastCreateTime { get { return Convert.ToDateTime(lastCreateTime).ToString("yyyy-MM-dd HH:mm:ss"); } set { lastCreateTime = value; } } } public class OperationPlanTable : OperationCreatePlan { @@ -108,6 +110,8 @@ namespace SolarPower.Models public string ScheduleDay { get; set; } public string StartTimeString { get; set; } public string CreateTimeString { get; set; } + + } public class PlanToRecord : Created diff --git a/SolarPower/Quartz/Jobs/OperationScheduleJob.cs b/SolarPower/Quartz/Jobs/OperationScheduleJob.cs index 5561c81..3c5e90d 100644 --- a/SolarPower/Quartz/Jobs/OperationScheduleJob.cs +++ b/SolarPower/Quartz/Jobs/OperationScheduleJob.cs @@ -30,69 +30,94 @@ namespace SolarPower.Quartz.Jobs foreach (var a in getTime) { DateTime Updatedtime; + var useday = a.LastCreateTime; + if(a.LastCreateTime == "0001-01-01 00:00:00") + { + useday = a.StartTime; + } + + if (a.ScheduleType == 0)//日 { - Updatedtime = Convert.ToDateTime(a.StartTime).AddDays(a.ScheduleNum); + Updatedtime = Convert.ToDateTime(useday).AddDays(a.ScheduleNum); } else if (a.ScheduleType == 1)//周 { - Updatedtime = Convert.ToDateTime(a.StartTime).AddDays(a.ScheduleNum * 7); + Updatedtime = Convert.ToDateTime(useday).AddDays(a.ScheduleNum * 7); } else if (a.ScheduleType == 2)//月 { - Updatedtime = Convert.ToDateTime(a.StartTime).AddMonths(a.ScheduleNum); + Updatedtime = Convert.ToDateTime(useday).AddMonths(a.ScheduleNum); } else if (a.ScheduleType == 3)//季 { - Updatedtime = Convert.ToDateTime(a.StartTime).AddMonths(a.ScheduleNum * 3); + Updatedtime = Convert.ToDateTime(useday).AddMonths(a.ScheduleNum * 3); } else // 年 { - Updatedtime = Convert.ToDateTime(a.StartTime).AddYears(a.ScheduleNum); + Updatedtime = Convert.ToDateTime(useday).AddYears(a.ScheduleNum); } + + + + if (Updatedtime < DateTime.Now) { var now = DateTime.Now.ToString("yyyy-MM-dd"); - var finalid = await operationRepository.GetCurrentSerialNumber("operation_plan_create", $"PowerStationId = {a.PowerStationId} AND CreatedAt LIKE '%{now}%'"); - var newSerialNumber = GetLastSerialNumber(finalid); var OperationPlan = new OperationCreatePlan() { + Id = a.Id, EmailType = a.EmailType, ScheduleNum = a.ScheduleNum, Description = a.Description, WorkDay = a.WorkDay, ScheduleType = a.ScheduleType, - SerialNumber = newSerialNumber, - StartTime = Updatedtime.ToString("yyyy-MM-dd hh:mm:ss"), + StartTime = a.StartTime, PowerStationId = a.PowerStationId, Type = a.Type, - PlanId = DateTime.Now.ToString("yyyyMMdd") + newSerialNumber, - CreatedBy = a.CreatedBy + UpdatedBy = a.UpdatedBy, + LastCreateTime = Updatedtime.ToString("yyyy-MM-dd") }; List properties = new List() { + "Id", "EmailType", "ScheduleNum", "Description", "WorkDay", "ScheduleType", - "SerialNumber", "StartTime", "PowerStationId", "Type", - "PlanId", - "CreatedBy" + "UpdatedBy", + "LastCreateTime" }; - await operationRepository.AddOperationPlan(OperationPlan, properties); + await operationRepository.UpdateOperationPlan(OperationPlan, properties); + + + string endtime = ""; + switch (a.ScheduleType) + { + case 0: + endtime = (Convert.ToDateTime(Updatedtime.ToString("yyyy-MM-dd")).AddDays(a.ScheduleNum)).ToString("yyyy-MM-dd 00:00:00"); break; + case 1: + endtime = (Convert.ToDateTime(Updatedtime.ToString("yyyy-MM-dd")).AddDays(a.ScheduleNum * 7)).ToString("yyyy-MM-dd 00:00:00"); break; + case 2: + endtime = (Convert.ToDateTime(Updatedtime.ToString("yyyy-MM-dd")).AddMonths(a.ScheduleNum)).ToString("yyyy-MM-dd 00:00:00"); break; + case 3: + endtime = (Convert.ToDateTime(Updatedtime.ToString("yyyy-MM-dd")).AddMonths(a.ScheduleNum * 3)).ToString("yyyy-MM-dd 00:00:00"); break; + case 4: + endtime = (Convert.ToDateTime(Updatedtime.ToString("yyyy-MM-dd")).AddYears(a.ScheduleNum * 3)).ToString("yyyy-MM-dd 00:00:00"); break; + } var record = new PlanToRecord() { WorkType = a.Type, PowerStationId = a.PowerStationId, - StartTime = Updatedtime.ToString("yyyy-MM-dd hh:mm:ss"), + StartTime = a.StartTime, CreatedBy = a.CreatedBy, - EndTime = Updatedtime.AddDays(a.WorkDay).ToString("yyyy-MM-dd hh:mm:ss") + EndTime = endtime }; List properties2 = new List() { @@ -102,9 +127,6 @@ namespace SolarPower.Quartz.Jobs "CreatedBy", "EndTime" }; - await operationRepository.AddToRecord(record, properties2); - var operation = await operationRepository.GetOneOperation(a.Id); - await operationRepository.DeleteOneByIdWithCustomTable(a.Id, "operation_plan_create"); } diff --git a/SolarPower/Repository/Implement/OperationRepository.cs b/SolarPower/Repository/Implement/OperationRepository.cs index dc0f5a2..7044ef0 100644 --- a/SolarPower/Repository/Implement/OperationRepository.cs +++ b/SolarPower/Repository/Implement/OperationRepository.cs @@ -495,16 +495,16 @@ namespace SolarPower.Repository.Implement } } - public async Task> GetOperationSchedules() + public async Task> GetOperationSchedules() { - List result; + List result; using (IDbConnection conn = this._databaseHelper.GetConnection()) { try { var sql = @$"SELECT * FROM operation_plan_create opc WHERE opc.Deleted = 0"; - result = (await conn.QueryAsync(sql)).ToList(); + result = (await conn.QueryAsync(sql)).ToList(); } catch (Exception exception) { diff --git a/SolarPower/Repository/Interface/IOperationRepository.cs b/SolarPower/Repository/Interface/IOperationRepository.cs index 668bb9e..a30eff9 100644 --- a/SolarPower/Repository/Interface/IOperationRepository.cs +++ b/SolarPower/Repository/Interface/IOperationRepository.cs @@ -68,7 +68,7 @@ namespace SolarPower.Repository.Interface /// Task DeleteOneOperationRecodeFile(int id); - Task> GetOperationSchedules(); + Task> GetOperationSchedules(); } } diff --git a/SolarPower/Startup.cs b/SolarPower/Startup.cs index d3bdb3f..9bad69c 100644 --- a/SolarPower/Startup.cs +++ b/SolarPower/Startup.cs @@ -92,10 +92,10 @@ namespace SolarPower services.AddSingleton(); //K[Job #region wpe - //services.AddSingleton(); - //services.AddSingleton( - // new JobSchedule(jobType: typeof(OperationScheduleJob), cronExpression: "0/5 * * * * ?") - //); + services.AddSingleton(); + services.AddSingleton( + new JobSchedule(jobType: typeof(OperationScheduleJob), cronExpression: Configuration.GetValue("BackgroundServiceCron:OperationScheduleJob")) + ); #endregion #region pqoqqT(CI5) diff --git a/SolarPower/Views/Operation/Index.cshtml b/SolarPower/Views/Operation/Index.cshtml index de0b7bb..ebac58e 100644 --- a/SolarPower/Views/Operation/Index.cshtml +++ b/SolarPower/Views/Operation/Index.cshtml @@ -181,7 +181,7 @@
-
~
+ @*
~
*@ @@ -534,11 +534,11 @@ //#endregion //#region 結束時間顯示 - $('#operation_startTime_modal').change(function () { - var dat = new Date($('#operation_startTime_modal').val()); - var day = new Date(dat.setDate(dat.getDate() + Number($('#operation_workDay_modal').val()) - 1)).toISOString().split("T"); - document.getElementById("operation_endTime_modal").innerHTML = day[0]; - }); + //$('#operation_startTime_modal').change(function () { + // var dat = new Date($('#operation_startTime_modal').val()); + // var day = new Date(dat.setDate(dat.getDate() + Number($('#operation_workDay_modal').val()) - 1)).toISOString().split("T"); + // document.getElementById("operation_endTime_modal").innerHTML = day[0]; + //}); //#endregion //#region 儲存定時計畫資料 @@ -553,7 +553,7 @@ ScheduleType: $("#operation_scheduleType_modal").val(), WorkDay: $('#operation_workDay_modal').val(), StartTime: $("#operation_startTime_modal").val(), - EndTime: document.getElementById("operation_endTime_modal").innerHTML, + //EndTime: document.getElementById("operation_endTime_modal").innerHTML, Description: $("#operation_description_modal").val(), EmailType: $('#operation_emailType_modal').val() } @@ -635,7 +635,7 @@ var dat = new Date(start); var day = new Date(dat.setDate(dat.getDate() + Number(rel.data.workDay))).toISOString().split("T"); - document.getElementById("operation_endTime_modal").innerHTML = day[0]; + //document.getElementById("operation_endTime_modal").innerHTML = day[0]; $("#Operation-modal").modal(); }, 'json'); diff --git a/SolarPower/appsettings.Development.json b/SolarPower/appsettings.Development.json index 29c2705..f28dd6b 100644 --- a/SolarPower/appsettings.Development.json +++ b/SolarPower/appsettings.Development.json @@ -23,7 +23,8 @@ //}, "BackgroundServiceCron": { "CalcPowerStationJob": "0 5 * * * ?", - "CalcAvgPowerStationJob": "0 0 2 * * ?" + "CalcAvgPowerStationJob": "0 0 2 * * ?", + "OperationScheduleJob": "0 0 2 * * ?" }, "SMTPConfig": { "Host": "smtp.gmail.com",