定時任務(修改)

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>();
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<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);
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.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<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);
apiResult.Code = "0000";
apiResult.Msg = "編輯成功";

View File

@ -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) */;

View File

@ -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

View File

@ -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<string> properties = new List<string>()
{
"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<string> properties2 = new List<string>()
{
@ -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");
}

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())
{
try
{
var sql = @$"SELECT * FROM operation_plan_create opc
WHERE opc.Deleted = 0";
result = (await conn.QueryAsync<OperationCreatePlanModal>(sql)).ToList();
result = (await conn.QueryAsync<OperationCreatePlan>(sql)).ToList();
}
catch (Exception exception)
{

View File

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

View File

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

View File

@ -181,7 +181,7 @@
<div class="col-5">
<input type="date" id="operation_startTime_modal" class="form-control" placeholder="2000-01-01">
</div>
<div class="col">~<span id="operation_endTime_modal"></span></div>
@*<div class="col">~<span id="operation_endTime_modal"></span></div>*@
</div>
</div>
@ -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');

View File

@ -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",