背景送Email
This commit is contained in:
parent
d0073e159b
commit
bfc4b2ca30
@ -1170,21 +1170,21 @@ namespace SolarPower.Controllers
|
||||
}
|
||||
var Datename = postObject.Time.Replace("-", "");
|
||||
|
||||
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "report")))
|
||||
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report")))
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "report"));
|
||||
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report"));
|
||||
}
|
||||
|
||||
|
||||
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "report", Datename)))
|
||||
if (!Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report", Datename)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "report", Datename));
|
||||
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report", Datename));
|
||||
}
|
||||
var n = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "report", Datename, Datename + "_" + postObject.Userid + "_" + name + "報表.xlsx");
|
||||
var n = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "report", Datename, Datename + "_" + postObject.Userid + "_" + name + "報表.xlsx");
|
||||
FileStream FS = new FileStream(n, FileMode.Create, FileAccess.Write);
|
||||
workbook.Write(FS);
|
||||
FS.Close();
|
||||
return Path.Combine("report", Datename, Datename + "_" + postObject.Userid + "_" + name + "報表.xlsx");
|
||||
return Path.Combine("\\" + "upload" ,"report", Datename, Datename + "_" + postObject.Userid + "_" + name + "報表.xlsx");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,4 +17,14 @@ namespace SolarPower.Models
|
||||
public byte IsDelivery { get; set; }
|
||||
public string DeliveryAt { get; set; }
|
||||
}
|
||||
|
||||
public class OperationPersonnel
|
||||
{
|
||||
public int PowerStationId { get; set; }
|
||||
public int EmailDayReport { get; set; }
|
||||
public int EmailMonthReport { get; set; }
|
||||
public int EmailComplexReport { get; set; }
|
||||
public int EmailException { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,4 +70,6 @@ namespace SolarPower.Models
|
||||
public string LeaseRate { get; set; }
|
||||
public string Landowner { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Quartz;
|
||||
using SolarPower.Models;
|
||||
using SolarPower.Models.PowerStation;
|
||||
using SolarPower.Repository.Interface;
|
||||
using System;
|
||||
@ -16,17 +17,108 @@ namespace SolarPower.Quartz.Jobs
|
||||
{
|
||||
private readonly ILogger<CalcAvgPowerStationJob> logger;
|
||||
private readonly IPowerStationRepository powerStationRepository;
|
||||
private readonly IUserRepository userRepository;
|
||||
private readonly INoticeScheduleRepository noticeScheduleRepository;
|
||||
private readonly IStationReportRepository stationReportRepository;
|
||||
|
||||
public CalcAvgPowerStationJob(ILogger<CalcAvgPowerStationJob> logger, IPowerStationRepository powerStationRepository)
|
||||
public CalcAvgPowerStationJob(ILogger<CalcAvgPowerStationJob> logger, IPowerStationRepository powerStationRepository,IUserRepository userRepository, INoticeScheduleRepository noticeScheduleRepository,IStationReportRepository stationReportRepository)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.powerStationRepository = powerStationRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.noticeScheduleRepository = noticeScheduleRepository;
|
||||
this.stationReportRepository = stationReportRepository;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
#region 寄送日月報
|
||||
var users = userRepository.GetAllAsync();
|
||||
|
||||
foreach (var user in users.Result)
|
||||
{
|
||||
List<OperationPersonnel> powerstations = new List<OperationPersonnel>();
|
||||
powerstations = await noticeScheduleRepository.GetPowerStationOperationPersonnel(user.Id);
|
||||
if(powerstations.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
List<Excelpowerstation> sentdaypowerstations = powerstations.Where(x => x.EmailDayReport == 1).Select(a => new Excelpowerstation {Name = a.Name,Value = a.PowerStationId.ToString()}).ToList();
|
||||
Controllers.StationReportController stationReportController = new Controllers.StationReportController(powerStationRepository, stationReportRepository);
|
||||
if (sentdaypowerstations.Count != 0)
|
||||
{
|
||||
Excel dayexcel = new Excel()
|
||||
{
|
||||
FormType = 0,
|
||||
PowerStation = sentdaypowerstations,
|
||||
SearchType = 0,
|
||||
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),
|
||||
Userid = user.Id
|
||||
};
|
||||
|
||||
var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented));
|
||||
NoticeSchedule DaySchedule = new NoticeSchedule()
|
||||
{
|
||||
RecipientEmail = user.Email,
|
||||
Subject = "日報表",
|
||||
Attachment = stationReportName,
|
||||
RecipientName = user.Name,
|
||||
Type = 1
|
||||
};
|
||||
List<string> properties = new List<string>()
|
||||
{
|
||||
"RecipientEmail",
|
||||
"Subject",
|
||||
"Attachment",
|
||||
"RecipientName",
|
||||
"Type"
|
||||
};
|
||||
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
|
||||
}
|
||||
if(DateTime.Now.ToString("dd")=="02")
|
||||
{
|
||||
List<Excelpowerstation> sentmonthpowerstations = powerstations.Where(x => x.EmailMonthReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
|
||||
if(sentmonthpowerstations.Count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Excel monthexcel = new Excel()
|
||||
{
|
||||
FormType = 1,
|
||||
PowerStation = sentmonthpowerstations,
|
||||
SearchType = 2,
|
||||
Time = DateTime.Now.AddDays(-2).ToString("yyyy-MM"),
|
||||
Userid = user.Id
|
||||
};
|
||||
var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented));
|
||||
NoticeSchedule MonthSchedule = new NoticeSchedule()
|
||||
{
|
||||
RecipientEmail = user.Email,
|
||||
Subject = "月報表",
|
||||
Attachment = stationReportmonthName,
|
||||
RecipientName = user.Name,
|
||||
Type = 1
|
||||
};
|
||||
List<string> properties2 = new List<string>()
|
||||
{
|
||||
"RecipientEmail",
|
||||
"Subject",
|
||||
"Attachment",
|
||||
"RecipientName",
|
||||
"Type"
|
||||
};
|
||||
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region step1. 找出所有電站
|
||||
logger.LogInformation("【CalcAvgPowerStationJob】【開始取得電站資料】");
|
||||
var powerStations = await powerStationRepository.GetAllAsync();
|
||||
@ -628,6 +720,10 @@ namespace SolarPower.Quartz.Jobs
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
|
||||
@ -64,5 +64,35 @@ namespace SolarPower.Repository.Implement
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<OperationPersonnel>> GetPowerStationOperationPersonnel(int Userid)
|
||||
{
|
||||
using (IDbConnection conn = this._databaseHelper.GetConnection())
|
||||
{
|
||||
conn.Open();
|
||||
var result = new List<OperationPersonnel>();
|
||||
using (var trans = conn.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var sql = $@"SELECT p.*,ps.Name FROM power_station_operation_personnel p
|
||||
LEFT JOIN power_station ps ON p.PowerStationId = ps.Id
|
||||
WHERE p.UserId = {Userid} AND p.Deleted = 0 ";
|
||||
|
||||
result = (await conn.QueryAsync<OperationPersonnel>(sql)).ToList();
|
||||
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
trans.Rollback();
|
||||
throw exception;
|
||||
}
|
||||
finally
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,5 +10,6 @@ namespace SolarPower.Repository.Interface
|
||||
{
|
||||
Task<List<NoticeSchedule>> GetNotYetDelivery();
|
||||
Task UpdateList(List<NoticeSchedule> noticeSchedules, List<string> properties);
|
||||
Task<List<OperationPersonnel>> GetPowerStationOperationPersonnel(int Userid);
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,7 +26,7 @@
|
||||
"CalcAvgPowerStationJob": "0 0 2 * * ?",
|
||||
"OperationScheduleJob": "0 0 2 * * ?",
|
||||
"CalcInverter15minJob": "0 2/15 * * * ?",
|
||||
"SendEmailJob": "0/10 * * * * ?"
|
||||
"SendEmailJob": "0 0 3 * * ?"
|
||||
},
|
||||
"SMTPConfig": {
|
||||
"Host": "smtp.gmail.com",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user