.NET实现每天8小时的计划周期安排分割逻辑

开始时间,比如 2026-03-06 ,按8个小时 为一天,有多个事项

如事项1 , 周期 12小时,事项2,周期3小时, 事项3,周期6小时,事项4,周期5小时。

生成 2026-03-06 干 事项1 8小时

生成 2026-03-07 干 事项1 4小时

生成 2026-03-07 干 事项2 3 小时

生成 2026-03-07 干 事项3 1小时

生成 2026-03-08 干 事项3 5小时

生成 2026-03-08 干 事项4 3小时

生成 2026-03-09 干 事项4 2小时

using System.Collections.Generic;

using System.Diagnostics;

namespace ConsoleApp7

{

internal class Program

{

static void Main(string\[\] args)

{

Console.WriteLine("Hello, World!");

复制代码
        Thing thing1 = new Thing() { ID = 1, hour = 2, Name = "事件1", Order = 1 };

        Thing thing2 = new Thing() { ID = 2, hour = 2, Name = "事件2", Order = 2 };


        Thing thing3 = new Thing() { ID = 3, hour = 4, Name = "事件3", Order = 3 };


        Thing thing4 = new Thing() { ID = 4, hour = 4, Name = "事件4", Order = 4 };
        Thing thing5 = new Thing() { ID = 5, hour = 4, Name = "事件5", Order = 5 };
        Thing thing6 = new Thing() { ID = 6, hour = 13, Name = "事件6", Order = 6 };
        Thing thing7 = new Thing() { ID = 7, hour = 4, Name = "事件7", Order = 7 };
        Thing thing8 = new Thing() { ID = 7, hour = 16, Name = "事件8", Order = 8 };

        List<Thing> things = new List<Thing>();

        things.Add(thing1);
        things.Add(thing2);
        things.Add(thing3);
        things.Add(thing4);
        things.Add(thing5);
        things.Add(thing6);
        things.Add(thing7);
        things.Add(thing8);
        DateTime dateTime = DateTime.Now.Date   ;
        var list = GetManagerItems(dateTime, things, 8);
        string aa = "";
        Console.ReadKey();
    }


  static List<ManagerItem> GetManagerItems ( DateTime begin, List<Thing> itmelist, int hourday=8)
  {
        List<ManagerItem> managerItems = new List<ManagerItem>();
        DateTime CurrentDate = begin;
        int  restHour= hourday;
        int itemHour = 0;
        foreach (Thing thing in itmelist)
        {
            itemHour = thing.hour;
            while (itemHour>0)               
            {

                if (itemHour <= restHour)
                {
                    ManagerItem managerItem = new ManagerItem();
                    managerItem.useHour = itemHour;
                    managerItem.date = CurrentDate;
                    managerItem.useHour = itemHour;

                    managerItem.thing = thing;
                    managerItems.Add(managerItem);
                   
                    restHour = restHour - itemHour;
                    itemHour = 0;


                }
                else                   
                {

                    ManagerItem managerItem = new ManagerItem();
                    managerItem.useHour = itemHour;
                    managerItem.date = CurrentDate;
                    managerItem.useHour = restHour;
                    managerItem.thing = thing;
                    managerItems.Add(managerItem);                     
                    
                    itemHour = itemHour- restHour;
                    restHour = 0;

                }


                if (restHour == 0) {

                    CurrentDate= CurrentDate.AddDays(1);

                    restHour = hourday;

                }


            
            
            
            }



        }



        return managerItems;

  }
 



}


public class Thing
{


    public int  ID    { get; set; }
    public string Name { get; set; }
    //工时
    public  int  hour { get; set; }

    public int Order  { get; set; }
}


public class ManagerItem { 

  public DateTime  date { get; set; }
  public int useHour { set; get; }

    public Thing thing { set; get; }

}

}

相关推荐
CSharp精选营10 小时前
.NET 8 与 .NET 9 支持终止倒计时:开发者需要了解什么
.net·lts·.net8·.net9·码农刚子·升级迁移·sts·支持终止
hez20103 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
唐青枫9 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫10 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
Caco_D10 天前
一行代码抓遍全网 20 个热榜!Aneiang.Pa 4.0 发布 — 极简 .NET 爬虫库
爬虫·.net
咕白m62510 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
小码编匠11 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫13 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net