diff --git a/SolarPower/Controllers/MyBaseController.cs b/SolarPower/Controllers/MyBaseController.cs index ad923fe..6671cd2 100644 --- a/SolarPower/Controllers/MyBaseController.cs +++ b/SolarPower/Controllers/MyBaseController.cs @@ -21,6 +21,7 @@ using SolarPower.Models.OperatorLogModel; using Newtonsoft.Json; using SolarPower.Models.Company; using SolarPower.Models.Role; +using Microsoft.AspNetCore.Routing; namespace SolarPower.Controllers { @@ -55,9 +56,14 @@ namespace SolarPower.Controllers controllerName = ControllerContext.RouteData.Values["controller"].ToString(); //controller名稱 actionName = ControllerContext.RouteData.Values["action"].ToString(); //action名稱 - if (string.IsNullOrEmpty(myAccount) && myAccount.CompareTo(HttpContext.Session.GetString("MyAccount")) == 0) + if (string.IsNullOrEmpty(myAccount)) { - //session 找不到account或者無法成功解密 + filterContext.Result = new RedirectToRouteResult( + new RouteValueDictionary + { + {"controller", "Login"}, + {"action", "Index"} + }); return; } diff --git a/SolarPower/Services/Implement/OperationScheduleBackgroundService.cs b/SolarPower/Services/Implement/OperationScheduleBackgroundService.cs new file mode 100644 index 0000000..fc685ca --- /dev/null +++ b/SolarPower/Services/Implement/OperationScheduleBackgroundService.cs @@ -0,0 +1,179 @@ +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +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.Services.Implement +{ + public class OperationScheduleBackgroundService : IHostedService, IDisposable + { + static Timer _timer; + private int time_interval = 30; //查詢間隔(秒) + private readonly ILogger _log; + private int execCount = 0; + private readonly IOperationRepository operationRepository; + + public OperationScheduleBackgroundService(ILogger log, + IOperationRepository operationRepository + ) + { + this.operationRepository = operationRepository; + this._log = log; + } + + public void Dispose() + { + _timer?.Dispose(); + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _timer = new Timer(DoWork, null, + TimeSpan.Zero, + TimeSpan.FromSeconds(time_interval)); + + return Task.CompletedTask; + } + + public async void DoWork(object state) + { + //利用 Interlocked 計數防止重複執行 + if (execCount <= 0) + { + Interlocked.Increment(ref execCount); + } + + if (execCount == 1) + { + try + { + var getTime = await operationRepository.GetOperationSchedules(); + foreach (OperationCreatePlanModal 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 properties = new List() + { + "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 properties2 = new List() + { + "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 ex) + { + _log.LogError("【OperationScheduleService】 - " + ex.Message); + } + finally + { + Interlocked.Decrement(ref execCount); + } + } + } + + 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'); + } + } + + public Task StopAsync(CancellationToken cancellationToken) + { + //調整Timer為永不觸發,停用定期排程 + _timer?.Change(Timeout.Infinite, 0); + return Task.CompletedTask; + } + } +} diff --git a/SolarPower/Services/Implement/SendEmailBackgroundService.cs b/SolarPower/Services/Implement/SendEmailBackgroundService.cs new file mode 100644 index 0000000..ba429c3 --- /dev/null +++ b/SolarPower/Services/Implement/SendEmailBackgroundService.cs @@ -0,0 +1,62 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace SolarPower.Services.Implement +{ + public class SendEmailBackgroundService : IHostedService, IDisposable + { + static Timer _timer; + private int time_interval = 30; //查詢間隔(秒) + private readonly ILogger _log; + private int execCount = 0; + + public SendEmailBackgroundService(ILogger log) + { + this._log = log; + } + + public Task StartAsync(CancellationToken cancellationToken) + { + _timer = new Timer(DoWork, null, + TimeSpan.Zero, + TimeSpan.FromSeconds(time_interval)); + + return Task.CompletedTask; + } + + public void DoWork(object state) + { + //利用 Interlocked 計數防止重複執行 + if (execCount <= 0) + { + Interlocked.Increment(ref execCount); + } + + if (execCount == 1 && send_complete) + { + try + { + + } + catch (Exception ex) + { + } + finally + { + Interlocked.Decrement(ref execCount); + } + } + } + + public Task StopAsync(CancellationToken cancellationToken) + { + //調整Timer為永不觸發,停用定期排程 + _timer?.Change(Timeout.Infinite, 0); + return Task.CompletedTask; + } + } +} diff --git a/SolarPower/Startup.cs b/SolarPower/Startup.cs index f00a281..d45e776 100644 --- a/SolarPower/Startup.cs +++ b/SolarPower/Startup.cs @@ -14,6 +14,7 @@ using SolarPower.Models; using SolarPower.Repository.Implement; using SolarPower.Repository.Interface; using SolarPower.Services; +using SolarPower.Services.Implement; using SolarPower.Services.Interface; using System; using System.Collections.Generic; @@ -43,6 +44,8 @@ namespace SolarPower // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddControllersWithViews(); + services.AddLogging( builder => { @@ -58,16 +61,16 @@ namespace SolarPower #endregion #region DBHelper `J - services.AddScoped(x => new DatabaseHelper(dBConfig)); + services.AddTransient(x => new DatabaseHelper(dBConfig)); #endregion #region Repository `J - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); #endregion double loginExpireMinute = this.Configuration.GetValue("LoginExpireMinute"); @@ -76,7 +79,9 @@ namespace SolarPower options.IdleTimeout = TimeSpan.FromMinutes(loginExpireMinute); }); - services.AddControllersWithViews(); + #region [JI + //services.AddHostedService(); + #endregion } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.