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

}

}

相关推荐
_MyFavorite_8 小时前
JAVA重点基础、进阶知识及易错点总结(28)接口默认方法与静态方法
java·开发语言·windows
chushiyunen10 小时前
audacity软件实现音频处理
windows
FL162386312910 小时前
如何在windows上的卸载Photoshop的Imagecreator插件
windows
_MyFavorite_13 小时前
JAVA重点基础、进阶知识及易错点总结(30)JDK9-11 常用新特性
java·windows·python
大强同学14 小时前
Obsidian的插件Claudian报错
windows
DY009J15 小时前
从 MSYS2 环境中提取独立 MinGW-w64 工具链的技术方案
c++·windows
J2虾虾15 小时前
Java使用jcifs读取Windows的共享文件
java·开发语言·windows
angushine15 小时前
Windows下循环复制一个文件
windows
liulilittle16 小时前
OPENPPP2 1.0.0.26145 正式版发布:内核态 SYSNAT 性能飞跃 + Windows 平台避坑指南
开发语言·网络·c++·windows·通信·vrrp
分布式存储与RustFS16 小时前
Windows原生版RustFS:无需Docker,1分钟本地对象存储环境搭建
windows·docker·容器·对象存储·minio·企业存储·rustfs