.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; }

}

}

相关推荐
小羊没烦恼!5 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
rockmelodies5 天前
# Windows Server2008 R2 Standard(SP1镜像U盘)重置本地管理员密码【完整详细步骤】
windows·密码破解
kakakahahahaha5 天前
Windows C盘空间不足的安全清理与迁移流程
windows·电脑·笔记本电脑·软件需求·c盘清理
做咩啊~5 天前
远程连接提示:身份验证错误,要求的函数不受支持
windows
_Jonas6 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
百事牛科技6 天前
PPT只读怎么取消?四种情况分别处理
windows·powerpoint
熊明才6 天前
WSL2 网络突然瘫痪(Network is unreachable / 没有 eth0)最短修复指南(2026年9月20日亲测可用)
linux·windows·wsl2
xbzb7 天前
Windows 启动故障修复知识整理:蓝屏 / 无限重启 / 盘符 X / 无 PE 盘
windows
我命由我123457 天前
Mac 操作系统 - 一些使用记录
运维·windows·学习·系统安全·运维开发·mac·学习方法