FIC_Solar/SolarPower/Quartz/Jobs/OperationScheduleJob.cs
Kai 18e4fa4b57 1. 加入Quartz
2. 地圖總覽
2021-07-01 09:51:33 +08:00

149 lines
5.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using Quartz;
using SolarPower.Models;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SolarPower.Quartz.Jobs
{
[DisallowConcurrentExecution]
public class OperationScheduleJob : IJob
{
private readonly ILogger<OperationScheduleJob> logger;
private IOperationRepository operationRepository;
public OperationScheduleJob(ILogger<OperationScheduleJob> logger, IOperationRepository operationRepository)
{
this.logger = logger;
this.operationRepository = operationRepository;
}
public async Task Execute(IJobExecutionContext context)
{
try
{
var getTime = await operationRepository.GetOperationSchedules();
foreach (var a in getTime)
{
DateTime Updatedtime;
if (a.ScheduleType == 0)//日
{
Updatedtime = Convert.ToDateTime(a.StartTime).AddDays(a.ScheduleNum);
}
else if (a.ScheduleType == 1)//周
{
Updatedtime = Convert.ToDateTime(a.StartTime).AddDays(a.ScheduleNum * 7);
}
else if (a.ScheduleType == 2)//月
{
Updatedtime = Convert.ToDateTime(a.StartTime).AddMonths(a.ScheduleNum);
}
else if (a.ScheduleType == 3)//季
{
Updatedtime = Convert.ToDateTime(a.StartTime).AddMonths(a.ScheduleNum * 3);
}
else // 年
{
Updatedtime = Convert.ToDateTime(a.StartTime).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()
{
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"),
PowerStationId = a.PowerStationId,
Type = a.Type,
PlanId = DateTime.Now.ToString("yyyyMMdd") + newSerialNumber,
CreatedBy = a.CreatedBy
};
List<string> properties = new List<string>()
{
"EmailType",
"ScheduleNum",
"Description",
"WorkDay",
"ScheduleType",
"SerialNumber",
"StartTime",
"PowerStationId",
"Type",
"PlanId",
"CreatedBy"
};
await operationRepository.AddOperationPlan(OperationPlan, properties);
var record = new PlanToRecord()
{
WorkType = a.Type,
PowerStationId = a.PowerStationId,
StartTime = Updatedtime.ToString("yyyy-MM-dd hh:mm:ss"),
CreatedBy = a.CreatedBy,
EndTime = Updatedtime.AddDays(a.WorkDay).ToString("yyyy-MM-dd hh:mm:ss")
};
List<string> properties2 = new List<string>()
{
"WorkType",
"PowerStationId",
"StartTime",
"CreatedBy",
"EndTime"
};
await operationRepository.AddToRecord(record, properties2);
var operation = await operationRepository.GetOneOperation(a.Id);
await operationRepository.DeleteOneByIdWithCustomTable(a.Id, "operation_plan_create");
}
}
}
catch (Exception exception)
{
logger.LogError("【{0}】{1}", nameof(logger), exception.Message);
}
}
/// <summary>
/// 取得最新的流水號
/// </summary>
/// <param name="current">當前的</param>
/// <param name="pad"></param>
/// <param name="direction">0: PadLeft1: PadRight</param>
/// <returns></returns>
public string GetLastSerialNumber(string current = "", int pad = 4, byte direction = 0)
{
var tempSerialNumber = 0;
if (!string.IsNullOrEmpty(current))
{
tempSerialNumber = Convert.ToInt32(current) + 1;
}
else
{
tempSerialNumber = 1;
}
if (direction == 0)
{
return tempSerialNumber.ToString().Trim().PadLeft(pad, '0');
}
else
{
return tempSerialNumber.ToString().Trim().PadRight(pad, '0');
}
}
}
}