值班排班功能:按周排班/按日排班

 public  class MaintenanceDutyPsersonDto
    {
        /// <summary>
        /// 值班人员(组织兵力唯一标识)
        /// </summary>
        [StringLength(50)]
        public string MilitaryRowGuid { get; set; }

        /// <summary>
        /// 是否值班领导
        /// </summary>
        [StringLength(50)]
        public string IsLeader { get; set; }
        /// <summary>
        /// 值班电话
        /// </summary>
        [StringLength(50)]
        public string PhoneNumber { get; set; }
        /// <summary>
        /// 值班房间唯一标识
        /// </summary>
        [StringLength(50)]
        public string RoomRowGuid { get; set; }

        /// <summary>
        /// 值班日期
        /// </summary>
     //[StringLength(50)]
        public DateTime DutyDate { get; set; }

        /// <summary>
        /// 值班时段(数据字典)
        /// </summary>
        public string[] DutyPeriods { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [StringLength(100)]
        public string Remark { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        [StringLength(50)]
        public string Creater { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }
        /// <summary>
        /// 排班模式
        /// </summary>
        [StringLength(50)]
        public string Dutymodel { get; set; }
        /// <summary>
        ////// </summary>

        public string[] weeks { get; set; }
        /// <summary>
        ////// </summary>
        public string[] days { get; set; }
      
        /// <summary>
        /// 租户
        /// </summary>
        public int? TenantId { get; set; }
    }
#region 新增排班
/// <summary>
/// 新增排班
/// </summary>
/// <param name="maintenanceDuty"></param>
/// <returns></returns>
[UnitOfWork(isTransactional: false)]
public Task<bool> CreateMaintenanceDuty(List<MaintenanceDutyPsersonDto> maintenanceDuty)
{
    try
    {
     foreach (MaintenanceDutyPsersonDto item in maintenanceDuty)
        {
            string yearandmonth = item.DutyDate.ToString("yyyy-MM");
            int year = item.DutyDate.Year;
            int month = item.DutyDate.Month;
            int d = DateTime.DaysInMonth(year, month);   //获取指定年月的天数
            if (item.Dutymodel == "")//按周排班
            {
                string[] weeks = item.weeks;
                List<string> datelist1 = new List<string>();
                for (int i = 0; i < weeks.Length; i++)
                {
                    switch (weeks[i])
                    {
                        case "周一":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Monday)
                                {
                                  
                                  datelist1.Add(dt.ToShortDateString());
                                   
                                }
                            }
                            break;
                        case "周二":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Tuesday)
                                {
 
                                    datelist1.Add(dt.ToShortDateString());
 
                                }                                   }
                            break;
                        case "周三":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Wednesday)
                                {
 
                                    datelist1.Add(dt.ToShortDateString());
 
                                }
                            }
                            break;
                        case "周四":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Thursday)
                                {
 
                                    datelist1.Add(dt.ToShortDateString());
 
                                }
                            }
                            break;
                        case "周五":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Friday)
                                {
 
                                    datelist1.Add(dt.ToShortDateString());
 
                                }
                            }
                            break;
                        case "周六":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Saturday)
                                {
 
                                    datelist1.Add(dt.ToShortDateString());
 
                                }
                            }
                            break;
                        case "周日":
                            for (int a = 1; a <= d; a++)
                            {
                                DateTime dt = new DateTime(year, month, a);
                                if (dt.DayOfWeek == DayOfWeek.Sunday)
                                {
 
                                    datelist1.Add(dt.ToShortDateString());
                                }
                            }
                            break;
                        default:
 
                            break;
 
                    }
                }
                string[] DutyPeriods = item.DutyPeriods;
                for (int c = 0; c < DutyPeriods.Length; c++)
                {
                    for (int s = 0; s < datelist1.Count; s++)
                    {
                        string id = Guid.NewGuid().ToString();
                        _repository.Insert(new Model.Maintenance_Duty
                        {
                            Id = id,
                            MilitaryRowGuid = item.MilitaryRowGuid,
                            IsLeader = item.IsLeader,
                            RoomRowGuid = item.RoomRowGuid,
                            DutyDate =Convert.ToDateTime(datelist1[s]),
                            DutyPeriod = DutyPeriods[c],
                            TenantId = item.TenantId,
                            Creater = "",
                            CreateTime = DateTime.Now,
                            Remark = item.Remark
                        });
                    }
                }
            }
            else//按日排班
            {
                string[] days = item.days;
                List<DateTime> datelist2 = new List<DateTime>();
                for (int i = 0; i < days.Length; i++)
                {
                    DateTime time = Convert.ToDateTime(yearandmonth + "-" + days[i]);
                    datelist2.Add(time);
                }
                string[] DutyPeriods = item.DutyPeriods;
                for (int k = 0; k < DutyPeriods.Length; k++)
                {                     
                    for (int j = 0; j < datelist2.Count; j++) {
                        string id = Guid.NewGuid().ToString();
                        _repository.Insert(new Model.Maintenance_Duty
                    {
                        Id =id,
                        MilitaryRowGuid = item.MilitaryRowGuid,
                        IsLeader = item.IsLeader,
                        RoomRowGuid = item.RoomRowGuid,
                        DutyDate = datelist2[j],
                        DutyPeriod = DutyPeriods[k],
                        TenantId = item.TenantId,
                        Creater = "",
                        CreateTime = DateTime.Now,
                        Remark=item.Remark
                    });              
                }
                }
            }
        }
        return Task.FromResult(true);
    }
    catch (Exception e)
    {
        throw new UserFriendlyException(e.Message);
    }
}
#endregion
原文地址:https://www.cnblogs.com/cyqdeshenluo/p/12166757.html