简体   繁体   English

计算将来的工作日以发送提醒电子邮件

[英]Calculate business days in future to send reminder emails

I am working on this employee performance review application. 我正在处理此员工绩效审查申请。 I need to send out reminder emails when the user has left 3 and 1 business days before the end of the 15 business day period. 当用户在15个工作日结束之前的3个工作日和1个工作日离开时,我需要发送提醒电子邮件。

Here is how I plan on doing it: I have the Last Modified Date for each Review which I should be able to use to find out 15 business days in future(call it deadLineToSubmit for our example). 这是我打算做的事情:我对每个审阅都有一个“上次修改日期”,我应该可以使用它来找出将来的15个工作日(在我们的示例中称为deadLineToSubmit)。 Once I got deadLineToSubmit I should check to see if currentDate + 3 days(date) == deadLineToSubmit Then add that review to my list which I will be using to send email. 一旦我获得了deadLineToSubmit,我应该检查一下currentDate + 3 days(date)== deadLineToSubmit,然后将该评论添加到我将用于发送电子邮件的列表中。

I have a function called GetByDayPrior where I am sending values 3 and 1 to check if user have left 3 business days or 1. Below is the code: 我有一个名为GetByDayPrior的函数,我在其中发送值3和1来检查用户是否还剩下3个工作日或1个工作日。以下是代码:

    public List<int> GetByDaysPrior(int daysPrior)
    {
        List<int> response = new List<int>();

        using (DAL.HumanResourcesEntities context = new DAL.HumanResourcesEntities())
        {
            DateTime currentDate = DateTime.Now.Date;
            var lastModifiedDate = context.Reviews
                                    .Where(s => s.IsActive == false)
                                    .Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature)
                                    .Select(v => v.ModifiedDate)
                                    .ToList();
            AddBusinessDays(lastModifiedDate, daysPrior);

            var lastRuntime = context.ApplicationRuntimeVariables.Where(y => y.ParameterName == "RemindersSentDT").Select(x => x.ParameterValue).FirstOrDefault().Date;

            DateTime deadLineToSubmitReview = currentDate.AddDays(daysPrior).Date;
            if (lastRuntime.AddHours(24).Date <= currentDate) //should it be == current date ?
            {
                var reviewIDs = context.Reviews
                                        .Where(s => s.ModifiedDate < deadLineToSubmitReview)
                                        .Where(s => s.ReviewStatusId == (int)ReviewStatuses.EmployeeSignature) //Check to make sure reminders only go when Review Status is Employee Signature
                                        .Where(s => s.IsActive == false)
                                        .Select(v => v.ReviewId)
                                        .ToList();

                response = reviewIDs.ToList();
            }

            return response;
        }
    }

I have another function called AddBusinessDays should give me the deadLineToSubmit when I pass in LastModifiedDate and 15 days as parameter. 我有另一个名为AddBusinessDays的函数,当我传入LastModifiedDate和15天作为参数时,应该给我deadLineToSubmit。 But currently its not working because firstly I cannot find out how to pass List of DataTime as a paramater in this function. 但是当前它无法正常工作,因为首先我无法找到如何在此函数中将List的DataTime作为参数传递。 Also when I defined date parameter in AddBusinessDays as List now all the instances of date and DayOfWeek are complanining 另外,当我在AddBusinessDays中将date参数定义为List时,date和DayOfWeek的所有实例都在抱怨

"Error 32 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.DateTime' “错误32无法将类型'System.Collections.Generic.List'隐式转换为'System.DateTime'

Below is my AddBusinessDays function. 以下是我的AddBusinessDays函数。

 public static DateTime AddBusinessDays(List<DateTime> date, int days)
    {
        if (days < 0)
        {
            throw new ArgumentException("days cannot be negative", "days");
        }

        if (days == 0) return date;

        if (date.DayOfWeek == DayOfWeek.Saturday)
        {
            date = date.AddDays(2);
            days -= 1;
        }
        else if (date.DayOfWeek == DayOfWeek.Sunday)
        {
            date = date.AddDays(1);
            days -= 1;
        }

        date = date.AddDays(days / 5 * 7);
        int extraDays = days % 5;

        if ((int)date.DayOfWeek + extraDays > 5)
        {
            extraDays += 2;
        }

        return date.AddDays(extraDays);

    }

Here's what I need to know: How to send DateTime List as parameter in AddBusinessDays function. 这是我需要知道的:如何在AddBusinessDays函数中将DateTime列表作为参数发送。 How to make AddBusinessDays function work because now we are passing list instead of single dateTime. 如何使AddBusinessDays函数起作用,因为现在我们传递列表而不是单个dateTime。 Is my thinking logic correct to solve this issue ? 我的思维逻辑是否可以解决这个问题?

Thanks a Lot! 非常感谢! :) :)

date is a list and not a single date. date是一个列表,而不是单个日期。
You can make an operation on all items of the list by using Select 您可以使用Select对列表的所有项目进行操作
for example: 例如:

lastModifiedDate = AddBusinessDays(lastModifiedDate, daysPrior);

Step 1: Convert your function to receive a single date 步骤1:转换您的函数以接收单个日期

private static DateTime AddBusinessDays(DateTime date, int days)
{
    if (days < 0)
    {
        throw new ArgumentException("days cannot be negative", "days");
    }

    if (days == 0) return date;

    if (date.DayOfWeek == DayOfWeek.Saturday)
    {
        date = date.AddDays(2);
        days -= 1;
    }
    else if (date.DayOfWeek == DayOfWeek.Sunday)
    {
        date = date.AddDays(1);
        days -= 1;
    }

    date = date.AddDays(days / 5 * 7);
    int extraDays = days % 5;

    if ((int)date.DayOfWeek + extraDays > 5)
    {
        extraDays += 2;
    }

    return date.AddDays(extraDays);
}

Step 2: Use the single date function for the list of dates 步骤2:将单一日期功能用于日期列表

public static List<DateTime> AddBusinessDays(List<DateTime> date, int days)
{
    return date.Select(d => AddBusinessDays(d, days)).ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM