定時任務(修改)

This commit is contained in:
b110212000 2021-07-14 18:25:06 +08:00
parent 20570be356
commit 069869efac
9 changed files with 158 additions and 56 deletions

View File

@ -71,6 +71,13 @@ namespace SolarPower.Controllers
ApiResult<string> apiResult = new ApiResult<string>(); ApiResult<string> apiResult = new ApiResult<string>();
try 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) if (post.Id == 0)
{ {
@ -105,26 +112,47 @@ namespace SolarPower.Controllers
"PlanId", "PlanId",
"CreatedBy" "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<string> properties2 = new List<string>()
{
"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); 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<string> properties2 = new List<string>()
{
"WorkType",
"PowerStationId",
"StartTime",
"CreatedBy",
"EndTime"
};
await operationRepository.AddToRecord(record, properties2);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Msg = "新增成功"; apiResult.Msg = "新增成功";
} }
@ -156,6 +184,44 @@ namespace SolarPower.Controllers
"Type", "Type",
"UpdatedBy" "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<string> properties2 = new List<string>()
{
"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); await operationRepository.UpdateOperationPlan(OperationPlan, properties);
apiResult.Code = "0000"; apiResult.Code = "0000";
apiResult.Msg = "編輯成功"; apiResult.Msg = "編輯成功";

View File

@ -1569,6 +1569,15 @@ INSERT INTO `ref_err_inv` (`id`, `brend`, `deviceType`, `errCode`, `errMsg`, `er
(7, 'solaredge', 'inverter', 7, 'Fault', '設備異常'), (7, 'solaredge', 'inverter', 7, 'Fault', '設備異常'),
(8, 'solaredge', 'inverter', 8, 'Maintenance/setup', '維修中/設定中'); (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, '') */; /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */;

View File

@ -96,6 +96,8 @@ namespace SolarPower.Models
private string updatedAt; private string updatedAt;
public int UpdatedBy { get; set; } //修改者 public int UpdatedBy { get; set; } //修改者
public string UpdatedAt { get { return Convert.ToDateTime(updatedAt).ToString("yyyy-MM-dd HH:mm:ss"); } set { updatedAt = value; } } //修改時間 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 public class OperationPlanTable : OperationCreatePlan
{ {
@ -108,6 +110,8 @@ namespace SolarPower.Models
public string ScheduleDay { get; set; } public string ScheduleDay { get; set; }
public string StartTimeString { get; set; } public string StartTimeString { get; set; }
public string CreateTimeString { get; set; } public string CreateTimeString { get; set; }
} }
public class PlanToRecord : Created public class PlanToRecord : Created

View File

@ -30,69 +30,94 @@ namespace SolarPower.Quartz.Jobs
foreach (var a in getTime) foreach (var a in getTime)
{ {
DateTime Updatedtime; DateTime Updatedtime;
var useday = a.LastCreateTime;
if(a.LastCreateTime == "0001-01-01 00:00:00")
{
useday = a.StartTime;
}
if (a.ScheduleType == 0)//日 if (a.ScheduleType == 0)//日
{ {
Updatedtime = Convert.ToDateTime(a.StartTime).AddDays(a.ScheduleNum); Updatedtime = Convert.ToDateTime(useday).AddDays(a.ScheduleNum);
} }
else if (a.ScheduleType == 1)//周 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)//月 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)//季 else if (a.ScheduleType == 3)//季
{ {
Updatedtime = Convert.ToDateTime(a.StartTime).AddMonths(a.ScheduleNum * 3); Updatedtime = Convert.ToDateTime(useday).AddMonths(a.ScheduleNum * 3);
} }
else // 年 else // 年
{ {
Updatedtime = Convert.ToDateTime(a.StartTime).AddYears(a.ScheduleNum); Updatedtime = Convert.ToDateTime(useday).AddYears(a.ScheduleNum);
} }
if (Updatedtime < DateTime.Now) if (Updatedtime < DateTime.Now)
{ {
var now = DateTime.Now.ToString("yyyy-MM-dd"); 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() var OperationPlan = new OperationCreatePlan()
{ {
Id = a.Id,
EmailType = a.EmailType, EmailType = a.EmailType,
ScheduleNum = a.ScheduleNum, ScheduleNum = a.ScheduleNum,
Description = a.Description, Description = a.Description,
WorkDay = a.WorkDay, WorkDay = a.WorkDay,
ScheduleType = a.ScheduleType, ScheduleType = a.ScheduleType,
SerialNumber = newSerialNumber, StartTime = a.StartTime,
StartTime = Updatedtime.ToString("yyyy-MM-dd hh:mm:ss"),
PowerStationId = a.PowerStationId, PowerStationId = a.PowerStationId,
Type = a.Type, Type = a.Type,
PlanId = DateTime.Now.ToString("yyyyMMdd") + newSerialNumber, UpdatedBy = a.UpdatedBy,
CreatedBy = a.CreatedBy LastCreateTime = Updatedtime.ToString("yyyy-MM-dd")
}; };
List<string> properties = new List<string>() List<string> properties = new List<string>()
{ {
"Id",
"EmailType", "EmailType",
"ScheduleNum", "ScheduleNum",
"Description", "Description",
"WorkDay", "WorkDay",
"ScheduleType", "ScheduleType",
"SerialNumber",
"StartTime", "StartTime",
"PowerStationId", "PowerStationId",
"Type", "Type",
"PlanId", "UpdatedBy",
"CreatedBy" "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() var record = new PlanToRecord()
{ {
WorkType = a.Type, WorkType = a.Type,
PowerStationId = a.PowerStationId, PowerStationId = a.PowerStationId,
StartTime = Updatedtime.ToString("yyyy-MM-dd hh:mm:ss"), StartTime = a.StartTime,
CreatedBy = a.CreatedBy, CreatedBy = a.CreatedBy,
EndTime = Updatedtime.AddDays(a.WorkDay).ToString("yyyy-MM-dd hh:mm:ss") EndTime = endtime
}; };
List<string> properties2 = new List<string>() List<string> properties2 = new List<string>()
{ {
@ -102,9 +127,6 @@ namespace SolarPower.Quartz.Jobs
"CreatedBy", "CreatedBy",
"EndTime" "EndTime"
}; };
await operationRepository.AddToRecord(record, properties2);
var operation = await operationRepository.GetOneOperation(a.Id);
await operationRepository.DeleteOneByIdWithCustomTable(a.Id, "operation_plan_create");
} }

View File

@ -495,16 +495,16 @@ namespace SolarPower.Repository.Implement
} }
} }
public async Task<List<OperationCreatePlanModal>> GetOperationSchedules() public async Task<List<OperationCreatePlan>> GetOperationSchedules()
{ {
List<OperationCreatePlanModal> result; List<OperationCreatePlan> result;
using (IDbConnection conn = this._databaseHelper.GetConnection()) using (IDbConnection conn = this._databaseHelper.GetConnection())
{ {
try try
{ {
var sql = @$"SELECT * FROM operation_plan_create opc var sql = @$"SELECT * FROM operation_plan_create opc
WHERE opc.Deleted = 0"; WHERE opc.Deleted = 0";
result = (await conn.QueryAsync<OperationCreatePlanModal>(sql)).ToList(); result = (await conn.QueryAsync<OperationCreatePlan>(sql)).ToList();
} }
catch (Exception exception) catch (Exception exception)
{ {

View File

@ -68,7 +68,7 @@ namespace SolarPower.Repository.Interface
/// <returns></returns> /// <returns></returns>
Task DeleteOneOperationRecodeFile(int id); Task DeleteOneOperationRecodeFile(int id);
Task<List<OperationCreatePlanModal>> GetOperationSchedules(); Task<List<OperationCreatePlan>> GetOperationSchedules();
} }
} }

View File

@ -92,10 +92,10 @@ namespace SolarPower
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>(); services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
//添加Job //添加Job
#region #region
//services.AddSingleton<OperationScheduleJob>(); services.AddSingleton<OperationScheduleJob>();
//services.AddSingleton( services.AddSingleton(
// new JobSchedule(jobType: typeof(OperationScheduleJob), cronExpression: "0/5 * * * * ?") new JobSchedule(jobType: typeof(OperationScheduleJob), cronExpression: Configuration.GetValue<string>("BackgroundServiceCron:OperationScheduleJob"))
//); );
#endregion #endregion
#region (5) #region (5)

View File

@ -181,7 +181,7 @@
<div class="col-5"> <div class="col-5">
<input type="date" id="operation_startTime_modal" class="form-control" placeholder="2000-01-01"> <input type="date" id="operation_startTime_modal" class="form-control" placeholder="2000-01-01">
</div> </div>
<div class="col">~<span id="operation_endTime_modal"></span></div> @*<div class="col">~<span id="operation_endTime_modal"></span></div>*@
</div> </div>
</div> </div>
@ -534,11 +534,11 @@
//#endregion //#endregion
//#region 結束時間顯示 //#region 結束時間顯示
$('#operation_startTime_modal').change(function () { //$('#operation_startTime_modal').change(function () {
var dat = new Date($('#operation_startTime_modal').val()); // 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"); // 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]; // document.getElementById("operation_endTime_modal").innerHTML = day[0];
}); //});
//#endregion //#endregion
//#region 儲存定時計畫資料 //#region 儲存定時計畫資料
@ -553,7 +553,7 @@
ScheduleType: $("#operation_scheduleType_modal").val(), ScheduleType: $("#operation_scheduleType_modal").val(),
WorkDay: $('#operation_workDay_modal').val(), WorkDay: $('#operation_workDay_modal').val(),
StartTime: $("#operation_startTime_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(), Description: $("#operation_description_modal").val(),
EmailType: $('#operation_emailType_modal').val() EmailType: $('#operation_emailType_modal').val()
} }
@ -635,7 +635,7 @@
var dat = new Date(start); var dat = new Date(start);
var day = new Date(dat.setDate(dat.getDate() + Number(rel.data.workDay))).toISOString().split("T"); 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(); $("#Operation-modal").modal();
}, 'json'); }, 'json');

View File

@ -23,7 +23,8 @@
//}, //},
"BackgroundServiceCron": { "BackgroundServiceCron": {
"CalcPowerStationJob": "0 5 * * * ?", "CalcPowerStationJob": "0 5 * * * ?",
"CalcAvgPowerStationJob": "0 0 2 * * ?" "CalcAvgPowerStationJob": "0 0 2 * * ?",
"OperationScheduleJob": "0 0 2 * * ?"
}, },
"SMTPConfig": { "SMTPConfig": {
"Host": "smtp.gmail.com", "Host": "smtp.gmail.com",