我想问一下您如何重构此代码:
public static EmailSetup GetEmailSetup(ApprovalContainer approval, PivotalApprovalInformation info, ApprovalStep step, ReportTrigger trigger)
{
EmailSetup setup = new EmailSetup() { EmailAdresses = new List<string>(), ReportTypes = new List<ReportType>() };
List<string> emailRecipients = new List<string>();
if (trigger == ReportTrigger.Closed)
{
emailRecipients = approval.ApprovalTeam.Select(ap => ap.Employee.Email).ToList();
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CommonReport);
return setup;
}
else if (step.ApprovalType == ApprovalType.CustomerSpecial)
{
if (step.ApproverType == ApproverType.RDev1)
{
emailRecipients = new List<string>() { approval.Initiator.Employee.Email, step.EmployeeApprover.Employee.Email };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CustomerSpecialReport);
}
if (step.ApproverType == ApproverType.RDev2)
{
if (trigger == ReportTrigger.Created)
{
emailRecipients = new List<string>() { approval.Initiator.Employee.Email, step.EmployeeApprover.Employee.Email };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CustomerSpecialReport);
}
else
{
string rD1Email = approval.ApprovalSteps.Where(appStep => appStep.ApproverType == ApproverType.RDev1).FirstOrDefault().EmployeeApprover.Employee.Email;
emailRecipients = new List<string>() { approval.Initiator.Employee.Email, step.EmployeeApprover.Employee.Email, rD1Email };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CustomerSpecialReport);
}
}
}
else if (step.ApprovalType == ApprovalType.ExtraCosts)
{
if (trigger == ReportTrigger.Created)
{
string projectManagerEmail = approval.ApprovalTeam.Where(em => em.Role == ApprovalRole.ProjectManager).FirstOrDefault().Employee.Email;
string projectLeaderEmail = approval.ApprovalTeam.Where(em => em.Role == ApprovalRole.ProjectLeader).FirstOrDefault().Employee.Email;
emailRecipients = new List<string> { projectManagerEmail, projectLeaderEmail };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CommonReport);
}
else
{
//List<IConfigurationBase> deniedParts = approval.AllValidIconfigurationBases.Where(ap => ap.ApprovalStatus == BaseApprovalStatus.Rejected).ToList();
setup.EmailAdresses.Add(approval.Initiator.Employee.Email);
}
}
else if (step.ApprovalType == ApprovalType.ApproveChanges)
{
setup.EmailAdresses.Add(approval.Initiator.Employee.Email);
setup.ReportTypes.Add(ReportType.CommonReport);
}
else if (step.ApprovalType == ApprovalType.UpdateMissingCostandPrice)
{
if (trigger == ReportTrigger.Created)
{
string csasEmail = step.EmployeeApprover.Employee.Email;
emailRecipients = new List<string> { csasEmail, approval.Initiator.Employee.Email };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CommonReport);
}
else
{
setup.EmailAdresses.Add(approval.Initiator.Employee.Email);
setup.ReportTypes.Add(ReportType.CommonReport);
}
}
else if (step.ApprovalType == ApprovalType.CustomerApproval)
{
string projectManagerEmail = approval.ApprovalTeam.Where(ea => ea.Role == ApprovalRole.ProjectManager).FirstOrDefault().Employee.Email;
string accountManagerEmail = approval.AccountManager == null ? null : approval.AccountManager.Employee.Email;
emailRecipients = new List<string> { projectManagerEmail, accountManagerEmail };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
}
else if (step.ApprovalType == ApprovalType.ApproveCosts)
{
//string regionalSalesDirectorEmail = approval.ApprovalSteps.Where(appStep => appStep.ApproverType == ApproverType.COstSD).FirstOrDefault().EmployeeApprover.Employee.Email;
//string vicePresidentEmail = approval.ApprovalSteps.Where(appStep => appStep.ApproverType == ApproverType.CostVP).FirstOrDefault().EmployeeApprover.Employee.Email;
//string presidentEmail = approval.ApprovalSteps.Where(appStep => appStep.ApproverType == ApproverType.CostPre).FirstOrDefault().EmployeeApprover.Employee.Email;
string responsibleApprover = step.EmployeeApprover.Employee.Email;
emailRecipients = new List<string> { responsibleApprover, approval.Initiator.Employee.Email };
setup.EmailAdresses.AddRange(emailRecipients.Where(er => !string.IsNullOrEmpty(er)));
setup.ReportTypes.Add(ReportType.CommonReport);
}
return setup;
}
我意识到这很容易理解,但是该方法基于Approvalcontainer,Reporttrigger枚举和Approvalstep中包含的信息创建了一个不同的EmailSetup对象。
在我看来,此代码长度似乎是多余的,但我想不出更短的编写方法。可能使用元组或字典,但我对它们并不熟悉。