例如我們在設置審批流程時,往往需要設置限制時限,例如2天內審核好等等,這時候會遇到休息日,需要把休息日去掉,當然有各種各樣復雜的情況,我們先把問題想得簡單一些,就按普通的休息一整天,全公司都統一休息的方式。
//--------------------------------------------------------------------
// 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());
}
// 今年為默認選中
this.cmbYear.SelectedIndex = this.cmbYear.Items.Count - 2;
}
private void FrmHolidays_Load(object sender, EventArgs e)
{
this.GetYearList();
}
/// <summary>
/// 獲取節假日
/// </summary>
/// <param name="holiday">當前點中了某個日期了</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;
// 把這一年的變粗的都加上來,這個是后臺處理程序,所以沒考慮數據庫性能的問題,每次都讀取了一下,有需要時可以改進一下
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)
{
// 從數據庫里讀取出來節假日的設置
GetYearDas();
}
private void btnInitializeYear_Click(object sender, EventArgs e)
{
BaseHolidaysManager manager = new BaseHolidaysManager(this.UserInfo);
// 這里把周日,知道的5.1. 10.1 等節日進行休息日設置
int year = int.Parse(this.cmbYear.SelectedItem.ToString());
DateTime startDate = new DateTime(year, 1, 1);
while (startDate.Year == year)
{
// 周六周日是默認休息日
if (startDate.DayOfWeek == DayOfWeek.Saturday || startDate.DayOfWeek == DayOfWeek.Sunday)
{
manager.AddHoliday(startDate.ToString(BaseSystemInfo.DateFormat));
}
// 5.1 是休息日
// 10.1 是休息日
startDate = startDate.AddDays(1);
}
// 從數據庫里讀取出來節假日的設置
GetYearDas();
}
private void mc_DateSelected(object sender, DateRangeEventArgs e)
{
GetYearDas(this.mc.SelectionStart.ToString(BaseSystemInfo.DateFormat));
}
}
}
//-----------------------------------------------------------------
// 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
/// 節假日表
///
/// 修改記錄
///
/// 2012.12.24 版本:1.0 JiRiGaLa 創建主鍵。
///
///
/// 版本:1.0
/// </summary>
/// <author>
/// <name>JiRiGaLa</name>
/// <date>2012.12.24</date>
/// </author>
/// </summary>
public partial class BaseHolidaysManager : BaseManager //, IBaseRoleManager
{
/// <summary>
/// 計算截至日期為幾號
/// </summary>
/// <param name="currentDay">當前日期</param>
/// <param name="days">幾個工作日</param>
/// <returns>應該在幾號完成 yyyy-MM-dd</returns>
public static string CalculateDays(DateTime currentDate, int days)
{
// 計算有幾個節假日
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;
// 若這個日期是節假日,需要繼續加一天
find = listEntity.Count(entity => !string.IsNullOrEmpty(entity.Holiday) && entity.Holiday.Equals(endDay.ToString(BaseSystemInfo.DateFormat), StringComparison.OrdinalIgnoreCase)) > 0;
while (find)
{
// 若這個日期是節假日,需要繼續加一天
endDay = endDay.AddDays(1);
find = listEntity.Count(entity => !string.IsNullOrEmpty(entity.Holiday) && entity.Holiday.Equals(endDay.ToString(BaseSystemInfo.DateFormat), StringComparison.OrdinalIgnoreCase)) > 0;
}
}
// 計算
return endDay.ToString(BaseSystemInfo.DateFormat);
}
/// <summary>
/// 計算截至日期為幾號
/// </summary>
/// <param name="currentDay">當前日期 yyyy-MM-dd</param>
/// <param name="days">幾個工作日</param>
/// <returns>應該在幾號完成</returns>
public static string CalculateDays(string currentDate, int days)
{
DateTime dateTime = DateTime.Parse(currentDate);
return CalculateDays(dateTime, days);
}
/// <summary>
/// 前日期與指定一個日期之間的, 工作日天數對吧?
/// </summary>
/// <param name="currentDate">開始日期 yyyy-MM-dd</param>
/// <param name="endDate">結束日期 yyyy-MM-dd</param>
/// <returns>工作日天數</returns>
public static int CalculateWorkDays(string currentDate, string endDate)
{
int returnValue = 0;
// 計算這2個日期相差幾天
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;
// 計算有幾個節假日
string where = BaseHolidaysEntity.FieldHoliday + " >= '" + currentDate + "'" +
" AND " + BaseHolidaysEntity.FieldHoliday + " <= '" + endDate + "'";
BaseHolidaysManager manager = new DotNet.Business.BaseHolidaysManager();
List<BaseHolidaysEntity> listEntity = manager.GetList<BaseHolidaysEntity>(where);
// 在數據庫里找還有幾個工作日
returnValue = returnValue - listEntity.Count;
return returnValue;
}
}
}