溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

節(jié)假日批量設(shè)置的C#.NET程序代碼參考

發(fā)布時(shí)間:2020-07-26 19:43:24 來(lái)源:網(wǎng)絡(luò) 閱讀:700 作者:jirigala 欄目:編程語(yǔ)言

例如我們?cè)谠O(shè)置審批流程時(shí),往往需要設(shè)置限制時(shí)限,例如2天內(nèi)審核好等等,這時(shí)候會(huì)遇到休息日,需要把休息日去掉,當(dāng)然有各種各樣復(fù)雜的情況,我們先把問(wèn)題想得簡(jiǎn)單一些,就按普通的休息一整天,全公司都統(tǒng)一休息的方式。

 節(jié)假日批量設(shè)置的C#.NET程序代碼參考

下面是程序的運(yùn)行效果,主要是把12個(gè)月展示在上面,有時(shí)候費(fèi)力一些,黑色部分是表示休息,本想用紅色顯示,但是微軟默認(rèn)是黑色的,沒(méi)能搞定

 節(jié)假日批量設(shè)置的C#.NET程序代碼參考

實(shí)現(xiàn)代碼可以參考

//--------------------------------------------------------------------
// All Rights Reserved ,Copyright (C) 2012 , Hairihan TECH, Ltd. .
//--------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DotNet.Business;
using DotNet.Utilities;
namespace DotNet.WinForm
{
    public partial class FrmHolidays : BaseForm
    {
        public FrmHolidays()
        {
            InitializeComponent();
        }
        private void GetYearList()
        {
            // 綁定最近幾年就可以了
            for (int i = DateTime.Now.Year - 3; i < DateTime.Now.Year + 2; i++)
            {
                this.cmbYear.Items.Add(i.ToString());
            }
            // 今年為默認(rèn)選中
            this.cmbYear.SelectedIndex = this.cmbYear.Items.Count - 2;
        }
        private void FrmHolidays_Load(object sender, EventArgs e)
        {
            this.GetYearList();
        }
        /// <summary>
        /// 獲取節(jié)假日
        /// </summary>
        /// <param name="holiday">當(dāng)前點(diǎn)中了某個(gè)日期了</param>
        private void GetYearDas(string holiday = null)
        {
            BaseHolidaysManager manager = new BaseHolidaysManager(this.UserInfo);
            if (!string.IsNullOrEmpty(holiday))
            {
                int returnValue = manager.Delete(new KeyValuePair<string, object>(BaseHolidaysEntity.FieldHoliday, holiday));
                if (returnValue == 0)
                {
                    manager.AddHoliday(holiday, false);
                }
            }
            int year = int.Parse(this.cmbYear.SelectedItem.ToString());
            this.mc.Enabled = false;
            if (this.mc.MinDate.Year <= year)
            {
                this.mc.MaxDate = new DateTime(year, 12, 31);
                this.mc.MinDate = new DateTime(year, 1, 1);
            }
            else
            {
                this.mc.MinDate = new DateTime(year, 1, 1);
                this.mc.MaxDate = new DateTime(year, 12, 31);
            }
            this.mc.Enabled = true;
                   
            // 把這一年的變粗的都加上來(lái),這個(gè)是后臺(tái)處理程序,所以沒(méi)考慮數(shù)據(jù)庫(kù)性能的問(wèn)題,每次都讀取了一下,有需要時(shí)可以改進(jìn)一下
            string where = BaseHolidaysEntity.FieldHoliday + " >= '" + this.mc.MinDate.ToString(BaseSystemInfo.DateFormat) + "'" +
                " AND " + BaseHolidaysEntity.FieldHoliday + " <= '" + this.mc.MaxDate.ToString(BaseSystemInfo.DateFormat) + "'";
            List<BaseHolidaysEntity> listEntity = manager.GetList<BaseHolidaysEntity>(where);
            List<DateTime> boldedDates = new List<DateTime>();
            foreach (BaseHolidaysEntity entity in listEntity)
            {
                boldedDates.Add(DateTime.Parse(entity.Holiday));
            }
            this.mc.BoldedDates = boldedDates.ToArray();
        }
        private void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            // 從數(shù)據(jù)庫(kù)里讀取出來(lái)節(jié)假日的設(shè)置
            GetYearDas();
        }
        private void btnInitializeYear_Click(object sender, EventArgs e)
        {
            BaseHolidaysManager manager = new BaseHolidaysManager(this.UserInfo);
            // 這里把周日,知道的5.1. 10.1 等節(jié)日進(jìn)行休息日設(shè)置
            int year = int.Parse(this.cmbYear.SelectedItem.ToString());
            DateTime startDate = new DateTime(year, 1, 1);
            while (startDate.Year == year)
            {
                // 周六周日是默認(rèn)休息日
                if (startDate.DayOfWeek == DayOfWeek.Saturday || startDate.DayOfWeek == DayOfWeek.Sunday)
                {
                    manager.AddHoliday(startDate.ToString(BaseSystemInfo.DateFormat));
                }
                // 5.1 是休息日
                // 10.1 是休息日
                startDate = startDate.AddDays(1);
            }
            // 從數(shù)據(jù)庫(kù)里讀取出來(lái)節(jié)假日的設(shè)置
            GetYearDas();
        }
        private void mc_DateSelected(object sender, DateRangeEventArgs e)
        {
            GetYearDas(this.mc.SelectionStart.ToString(BaseSystemInfo.DateFormat));
        }
    }
}

還有幾個(gè)函數(shù)相關(guān)函數(shù),可以參考一下

//-----------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , Hairihan TECH, Ltd.
//-----------------------------------------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
namespace DotNet.Business
{
    using DotNet.Utilities;
    /// <summary>
    /// BaseHolidaysManager
    /// 節(jié)假日表
    ///
    /// 修改記錄
    ///
    ///     2012.12.24 版本:1.0 JiRiGaLa 創(chuàng)建主鍵。
    ///    
    ///
    /// 版本:1.0
    /// </summary>
    /// <author>
    ///        <name>JiRiGaLa</name>
    ///        <date>2012.12.24</date>
    /// </author>
    /// </summary>
    public partial class BaseHolidaysManager : BaseManager //, IBaseRoleManager
    {
        /// <summary>
        /// 計(jì)算截至日期為幾號(hào)
        /// </summary>
        /// <param name="currentDay">當(dāng)前日期</param>
        /// <param name="days">幾個(gè)工作日</param>
        /// <returns>應(yīng)該在幾號(hào)完成 yyyy-MM-dd</returns>
        public static string CalculateDays(DateTime currentDate, int days)
        {
            // 計(jì)算有幾個(gè)節(jié)假日
            string where = BaseHolidaysEntity.FieldHoliday + " >= '" + currentDate.ToString(BaseSystemInfo.DateFormat) + "'";
            BaseHolidaysManager manager = new DotNet.Business.BaseHolidaysManager();
            List<BaseHolidaysEntity> listEntity = manager.GetList<BaseHolidaysEntity>(where);
            DateTime endDay = currentDate;
            bool find = false;
            for (int i = 0; i < days; i++)
            {
                find = false;
                // 若這個(gè)日期是節(jié)假日,需要繼續(xù)加一天
                find = listEntity.Count(entity => !string.IsNullOrEmpty(entity.Holiday) && entity.Holiday.Equals(endDay.ToString(BaseSystemInfo.DateFormat), StringComparison.OrdinalIgnoreCase)) > 0;
                while (find)
                {
                    // 若這個(gè)日期是節(jié)假日,需要繼續(xù)加一天
                    endDay = endDay.AddDays(1);
                    find = listEntity.Count(entity => !string.IsNullOrEmpty(entity.Holiday) && entity.Holiday.Equals(endDay.ToString(BaseSystemInfo.DateFormat), StringComparison.OrdinalIgnoreCase)) > 0;
                }
            }
            // 計(jì)算
            return endDay.ToString(BaseSystemInfo.DateFormat);
        }
        /// <summary>
        /// 計(jì)算截至日期為幾號(hào)
        /// </summary>
        /// <param name="currentDay">當(dāng)前日期 yyyy-MM-dd</param>
        /// <param name="days">幾個(gè)工作日</param>
        /// <returns>應(yīng)該在幾號(hào)完成</returns>
        public static string CalculateDays(string currentDate, int days)
        {
            DateTime dateTime = DateTime.Parse(currentDate);
            return CalculateDays(dateTime, days);
        }
        /// <summary>
        /// 前日期與指定一個(gè)日期之間的, 工作日天數(shù)對(duì)吧?
        /// </summary>
        /// <param name="currentDate">開(kāi)始日期 yyyy-MM-dd</param>
        /// <param name="endDate">結(jié)束日期 yyyy-MM-dd</param>
        /// <returns>工作日天數(shù)</returns>
        public static int CalculateWorkDays(string currentDate, string endDate)
        {
            int returnValue = 0;
            // 計(jì)算這2個(gè)日期相差幾天
            DateTime dateTime1 = DateTime.Parse(currentDate);
            DateTime dateTime2 = DateTime.Parse(endDate);
            TimeSpan timeSpan = new TimeSpan(dateTime2.Ticks).Subtract(new TimeSpan(dateTime1.Ticks)).Duration();
            returnValue = timeSpan.Days;
            // 計(jì)算有幾個(gè)節(jié)假日
            string where = BaseHolidaysEntity.FieldHoliday + " >= '" + currentDate + "'" +
                " AND " + BaseHolidaysEntity.FieldHoliday + " <= '" + endDate + "'";
            BaseHolidaysManager manager = new DotNet.Business.BaseHolidaysManager();
            List<BaseHolidaysEntity> listEntity = manager.GetList<BaseHolidaysEntity>(where);
            // 在數(shù)據(jù)庫(kù)里找還有幾個(gè)工作日
            returnValue = returnValue - listEntity.Count;
            return returnValue;
        }
    }
}


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI