1. 背景執行日月報表 迴圈加 try catch

This commit is contained in:
Kai 2021-09-24 10:09:15 +08:00
parent 082850213d
commit 458dff0078

View File

@ -8,6 +8,7 @@ using SolarPower.Models.PowerStation;
using SolarPower.Repository.Interface; using SolarPower.Repository.Interface;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -870,40 +871,83 @@ namespace SolarPower.Quartz.Jobs
#region #region
var users = userRepository.GetAllAsync(); var users = userRepository.GetAllAsync();
var ttt = new List<string>() {
"s506488@gmail.com",
"cesarliuc@gmail.com"
};
foreach (var user in users.Result) foreach (var user in users.Result)
{ {
List<OperationPersonnel> powerstations = new List<OperationPersonnel>(); try
powerstations = await noticeScheduleRepository.GetPowerStationOperationPersonnel(user.Id);
if (powerstations.Count == 0)
{ {
continue; logger.LogInformation("【CalcAvgPowerStationJob】【開始產生使用者[{0}({1})]的日月報】", user.Account, user.Name);
}
List<Excelpowerstation> sentdaypowerstations = powerstations.Where(x => x.EmailDayReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
List<Excelpowerstation> sentMaxpowerstations = powerstations.Where(x => x.EmailComplexReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
Controllers.StationReportController stationReportController = new Controllers.StationReportController(powerStationRepository, stationReportRepository); List<OperationPersonnel> powerstations = new List<OperationPersonnel>();
//日報表 powerstations = await noticeScheduleRepository.GetPowerStationOperationPersonnel(user.Id);
if (sentdaypowerstations.Count != 0) if (powerstations.Count == 0)
{
Excel dayexcel = new Excel()
{ {
FormType = 0, continue;
PowerStation = sentdaypowerstations, }
SearchType = 0, List<Excelpowerstation> sentdaypowerstations = powerstations.Where(x => x.EmailDayReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"), List<Excelpowerstation> sentMaxpowerstations = powerstations.Where(x => x.EmailComplexReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList();
Userid = user.Id
}; Controllers.StationReportController stationReportController = new Controllers.StationReportController(powerStationRepository, stationReportRepository);
var stationReportName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(dayexcel, Formatting.Indented)); //日報表
if (stationReportName != "") if (sentdaypowerstations.Count != 0)
{ {
NoticeSchedule DaySchedule = new NoticeSchedule() 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));
if (stationReportName != "")
{
NoticeSchedule DaySchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 0,
RecipientEmail = user.Email,
Subject = "日報表",
Attachment = stationReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties);
}
}
//綜合報表 每日
if (sentMaxpowerstations.Count != 0)
{
Select_table2 maxdayexcel = new Select_table2()
{
FormType = 0,
PowerStation = sentMaxpowerstations,
SearchType = 0,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),
Userid = user.Id
};
var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented));
NoticeSchedule MaxSchedule = new NoticeSchedule()
{ {
UserId = user.Id, UserId = user.Id,
EmailType = 0, EmailType = 2,
RecipientEmail = user.Email, RecipientEmail = user.Email,
Subject = "日報表", Subject = "綜合報表",
Attachment = stationReportName, Attachment = stationMaxReportName,
RecipientName = user.Name, RecipientName = user.Name,
Type = 1 Type = 1
}; };
@ -917,120 +961,100 @@ namespace SolarPower.Quartz.Jobs
"RecipientName", "RecipientName",
"Type" "Type"
}; };
await noticeScheduleRepository.AddOneAsync(DaySchedule, properties); await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties);
if (DateTime.Now.ToString("dd") == "01")
{
Select_table2 maxmonthexcel = new Select_table2()
{
FormType = 0,
PowerStation = sentMaxpowerstations,
SearchType = 0,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM"),
Userid = user.Id
};
var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented));
NoticeSchedule MaxmonthSchedule = new NoticeSchedule()
{
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationReportmaxmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 2
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2);
}
} }
}
//綜合報表 每日
if (sentMaxpowerstations.Count != 0)
{
Select_table2 maxdayexcel = new Select_table2()
{
FormType = 0,
PowerStation = sentMaxpowerstations,
SearchType = 0,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"),
Userid = user.Id
};
var stationMaxReportName = stationReportController.ExportExcelmaxtableBackDownload(JsonConvert.SerializeObject(maxdayexcel, Formatting.Indented));
NoticeSchedule MaxSchedule = new NoticeSchedule()
{
UserId = user.Id,
EmailType = 2,
RecipientEmail = user.Email,
Subject = "綜合報表",
Attachment = stationMaxReportName,
RecipientName = user.Name,
Type = 1
};
List<string> properties = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MaxSchedule, properties);
if (DateTime.Now.ToString("dd") == "01") if (DateTime.Now.ToString("dd") == "01")
{ {
Select_table2 maxmonthexcel = new Select_table2() 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)
{ {
FormType = 0, break;
PowerStation = sentMaxpowerstations, }
SearchType = 0, Excel monthexcel = new Excel()
{
FormType = 1,
PowerStation = sentmonthpowerstations,
SearchType = 2,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM"), Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM"),
Userid = user.Id Userid = user.Id
}; };
var stationReportmaxmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(maxmonthexcel, Formatting.Indented)); var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented));
NoticeSchedule MaxmonthSchedule = new NoticeSchedule() if (stationReportmonthName != "")
{ {
RecipientEmail = user.Email, NoticeSchedule MonthSchedule = new NoticeSchedule()
Subject = "綜合報表", {
Attachment = stationReportmaxmonthName, RecipientEmail = user.Email,
RecipientName = user.Name, Subject = "月報表",
Type = 1, Attachment = stationReportmonthName,
UserId = user.Id, RecipientName = user.Name,
EmailType = 2 Type = 1,
}; UserId = user.Id,
List<string> properties2 = new List<string>() EmailType = 1
{ };
"UserId", List<string> properties2 = new List<string>()
"EmailType", {
"RecipientEmail", "UserId",
"Subject", "EmailType",
"Attachment", "RecipientEmail",
"RecipientName", "Subject",
"Type" "Attachment",
}; "RecipientName",
await noticeScheduleRepository.AddOneAsync(MaxmonthSchedule, properties2); "Type"
};
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
}
} }
logger.LogInformation("【CalcAvgPowerStationJob】【產生完成使用者[{0}({1})]的日月報】", user.Account, user.Name);
} }
catch (Exception exception)
if (DateTime.Now.ToString("dd") == "01")
{ {
List<Excelpowerstation> sentmonthpowerstations = powerstations.Where(x => x.EmailMonthReport == 1).Select(a => new Excelpowerstation { Name = a.Name, Value = a.PowerStationId.ToString() }).ToList(); logger.LogError("【CalcAvgPowerStationJob】【產生失敗使用者[{0}({1})]的日月報】", user.Account, user.Name);
if (sentmonthpowerstations.Count == 0) logger.LogError("【CalcAvgPowerStationJob】[Exception] - {0}", exception.Message);
if (exception.InnerException != null)
{ {
break; logger.LogError("【CalcAvgPowerStationJob】[InnerException] - {0}", exception.InnerException.Message);
}
Excel monthexcel = new Excel()
{
FormType = 1,
PowerStation = sentmonthpowerstations,
SearchType = 2,
Time = DateTime.Now.AddDays(-1).ToString("yyyy-MM"),
Userid = user.Id
};
var stationReportmonthName = stationReportController.ExportExcelBackDownload(JsonConvert.SerializeObject(monthexcel, Formatting.Indented));
if (stationReportmonthName != "")
{
NoticeSchedule MonthSchedule = new NoticeSchedule()
{
RecipientEmail = user.Email,
Subject = "月報表",
Attachment = stationReportmonthName,
RecipientName = user.Name,
Type = 1,
UserId = user.Id,
EmailType = 1
};
List<string> properties2 = new List<string>()
{
"UserId",
"EmailType",
"RecipientEmail",
"Subject",
"Attachment",
"RecipientName",
"Type"
};
await noticeScheduleRepository.AddOneAsync(MonthSchedule, properties2);
} }
var line = new StackTrace(exception, true).GetFrame(0).GetFileLineNumber();
logger.LogError("【CalcAvgPowerStationJob】[錯誤行數] - {0}", line);
} }
} }