节假日批量设置的C#.NET程序代码参考
2012-12-31 18:08 通用C#系统架构 阅读(3906) 评论(4) 编辑 收藏 举报例如我们在设置审批流程时,往往需要设置限制时限,例如2天内审核好等等,这时候会遇到休息日,需要把休息日去掉,当然有各种各样复杂的情况,我们先把问题想得简单一些,就按普通的休息一整天,全公司都统一休息的方式。
下面是程序的运行效果,主要是把12个月展示在上面,有时候费力一些,黑色部分是表示休息,本想用红色显示,但是微软默认是黑色的,没能搞定
实现代码可以参考
//-------------------------------------------------------------------- // 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; } } }
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。