📄 signstatistic.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Office.Model;
using Office.BLL;
using System.Collections.Generic;
public partial class ManualSign_ManualSingStatistics : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DIV1.Visible = false;
BindControl();
}
}
//绑定控件
private void BindControl()
{
this.DropDownList1.DataSource = BranchInfoManager.GetAllBranchInfos();
this.DropDownList1.DataTextField = "BranchName";
this.DropDownList1.DataValueField = "BranchId";
this.DropDownList1.DataBind();
this.DropDownList2.DataSource = DepartInfoManager.GetAllDepartInfos();
this.DropDownList2.DataTextField = "DepartName";
this.DropDownList2.DataValueField = "DepartId";
this.DropDownList2.DataBind();
this.DropDownList1.Items.Insert(0, new ListItem("===请选择==="));
this.DropDownList2.Items.Insert(0, new ListItem("===请选择==="));
}
//选择事件
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "===请选择===")
{
Int32 BranchId = Int32.Parse(DropDownList1.SelectedValue);
this.DropDownList2.DataSource = DepartInfoManager.GetDepartInfoByBranchId(BranchId);
this.DropDownList2.DataTextField = "DepartName";
this.DropDownList2.DataValueField = "DepartId";
this.DropDownList2.DataBind();
}
else
{
BindControl();
}
}
//统计
protected void btnStat_Click(object sender, EventArgs e)
{
DIV1.Visible = true;
//创建时间
lblAppearName.Text = DateTime.Now.ToString();
//创建用户
lblCreateTabelName.Text = ((UserInfo)Session["User"]).UserId;
//开始时间
String StartTime = txtStartTime.Text;
//结束时间
String EndTime = txtEndTime.Text;
if (StartTime == "" || EndTime == "")
{
//选择本日
if (radDay.Checked)
{
StartTime += DateTime.Now.ToShortDateString() + " 00:00:00";
EndTime += DateTime.Now.ToShortDateString() + " 23:59:59";
}//选择本周
else if (radWeek.Checked)
{
//取出今天是一周中的第几天
Int32 week = Int32.Parse(DateTime.Now.DayOfWeek.ToString());
//如果是星期天
if (week == 0)//从周一开始
StartTime += DateTime.Now.AddDays(-(week + 7) + 1).ToShortDateString() + " 00:00:00";
else
StartTime += DateTime.Now.AddDays(-week + 1).ToShortDateString() + " 00:00:00";
//周未时间
EndTime += DateTime.Now.AddDays(-week + 7).ToShortDateString() + " 23:59:59";
}//选择本月
else if (radMonth.Checked)
{
//得到一个月的第一天
StartTime += DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString() + " 00:00:00";
//得到一个月的最后一天
EndTime += DateTime.Now.AddDays(-DateTime.Now.Day).AddMonths(1).ToShortDateString() + " 23:59:59";
}
else
{
return;
}
}
//机构名称
String BranchName = DropDownList1.SelectedItem.Text;
//部门名称
String DepartmentName = DropDownList2.SelectedItem.Text;
if (BranchName == "===请选择===")
BranchName = "";
if (DepartmentName == "===请选择===")
DepartmentName = "";
//根据用户选择的条件得到数据库原始记录
IList<ViewInfo> list = ViewManager.GetViewInfoByCondition(StartTime, EndTime, BranchName, DepartmentName);
//根据原始数据得到计算后的结果
IList<StatInfo> Statlist = GetStatInfo(list, StartTime, EndTime);
//绑定数据
this.GridView1.DataSource = Statlist;
this.GridView1.DataBind();
}
//从数据库中得到(上班--下班)时间
WorkTime workTime = WorkTimeManager.GetWorkTimeByWorkTimeId(1);
//对集合进行计算(迟到、早退、旷工、出勤率)
private IList<StatInfo> GetStatInfo(IList<ViewInfo> ViewList, String StartTime, String EndTime)
{
//上班时间
DateTime ONDUTY = DateTime.Parse(workTime.OnDutyTime);
//下班时间
DateTime OFFDUTY = DateTime.Parse(workTime.OffDutyTime);
//得到两个日期之间的天数
Int32 days = GetDays(StartTime, EndTime);
List<StatInfo> list = new List<StatInfo>();
//将所有用户添加到数组中
ArrayList arrUser = new ArrayList();
foreach (ViewInfo view in ViewList)
{
//去掉重复项
if (!arrUser.Contains(view.UserId))
arrUser.Add(view.UserId);
}
foreach (ViewInfo V in ViewList)
{
//如果存在此用户则继续
if (arrUser.Contains(V.UserId))
{
Int32 late = 0;//迟到
Int32 leave = 0;//早退
Int32 absentWork = 0;//旷工
Double duty = 00.00;//出勤
StatInfo stat = new StatInfo();
foreach (ViewInfo view in ViewList)
{
if (arrUser.Contains(view.UserId))
{
String Time = view.SignTime;
DateTime OnDuty = DateTime.Parse(Time.Substring(Time.IndexOf(" ")));
//如果签到时间大于正常上班时间
if (view.SignTag == "1" && OnDuty > ONDUTY)
{
late++;//迟到累加
}
//如签退时间小于下班时间
if (view.SignTag == "0" && OnDuty < OFFDUTY)
{
leave++;//早退累加
}
if (view.SignTime != "")
{
//出勤旷工累加
duty++;
absentWork++;
}
}
}
if (arrUser.Contains(V.UserId))
{
stat.Name = V.UserName;
stat.Branch = V.BranchName;
stat.Department = V.DepartName;
}
//计算出勤率
String sss = (duty / 2 / days) * 100 + "";
if (sss.Length > 5)
stat.Duty = sss.Substring(0, 5) + "%";
stat.Late = late.ToString();
stat.Learve = leave.ToString();
//计算旷工天数
stat.AbsentWork = days - (absentWork / 2) + "";
//将计算结果添加到集合中
list.Add(stat);
//移除该用户
arrUser.Remove(V.UserId);
}
}
return list;
}
//计算开始时间到结束时间之间的天数
#region 方法
private Int32 GetDays(String StartTime, String EndTime)
{
Int32 result = 0;
if (StartTime != "" && StartTime != null && EndTime != "" && EndTime != null)
{
//开始年
Int32 BeginYear = Int32.Parse(StartTime.Substring(0, 4));
//开始月
Int32 BeginMonth = Int32.Parse(StartTime.Substring(5, 2));
//开始日
Int32 BeginDay = Int32.Parse(StartTime.Substring(8, 2));
//结束年
Int32 EndYear = Int32.Parse(EndTime.Substring(0, 4));
//结束月
Int32 EndMonth = Int32.Parse(EndTime.Substring(5, 2));
//结束日
Int32 EndDay = Int32.Parse(EndTime.Substring(8, 2));
//计算当前日期到年未之间的日期
Int32 Startdays = fun(BeginYear) - GetDayOfYear(BeginYear, BeginMonth, BeginDay);
//计算当前日期到年前之间的日期
Int32 Enddays = GetDayOfYear(EndYear, EndMonth, EndDay);
//计算开始时间和结束时间之间的天数
if (EndYear - BeginYear == 1)
{
result = Startdays + Enddays;
}
else if (EndYear - BeginYear == 0)
{
return Enddays - GetDayOfYear(BeginYear, BeginMonth, BeginDay);
}
else
{
for (int i = 1; i < EndYear - BeginYear; i++)
{
result += fun(BeginYear + i);
}
result += (Startdays + Enddays);
}
}
return result;
}
//是否是润年
private Int32 fun(Int32 Year)
{
if ((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0)
{
return 366;
}
else
{
return 365;
}
}
//得到一年中的某一天在全年中是第几天
private Int32 GetDayOfYear(Int32 year, Int32 month, Int32 day)
{
int result = 0;
switch (month)
{
case 12:
result += 31;
goto case 11;
case 11:
result += 30;
goto case 10;
case 10:
result += 31;
goto case 9;
case 9:
result += 30;
goto case 8;
case 8:
result += 31;
goto case 7;
case 7:
result += 31;
goto case 6;
case 6:
result += 30;
goto case 5;
case 5:
result += 31;
goto case 4;
case 4:
result += 30;
goto case 3;
case 3:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
result += 29;
else
result += 28;
goto case 2;
case 2:
result += 31;
goto case 1;
case 1:
result += day;
break;
}
return result;
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -