xinjiang/SGGL/FineUIPro.Web/HSSE/SitePerson/WorkPostStatistic.aspx.cs

278 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BLL;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace FineUIPro.Web.HSSE.SitePerson
{
public partial class WorkPostStatistic : PageBase
{
/// <summary>
/// 项目id
/// </summary>
public string ProjectId
{
get
{
return (string)ViewState["ProjectId"];
}
set
{
ViewState["ProjectId"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Funs.DropDownPageSize(this.ddlPageSize);
this.ProjectId = this.CurrUser.LoginProjectId;
if (!string.IsNullOrEmpty(Request.Params["projectId"]) && Request.Params["projectId"] != this.ProjectId)
{
this.ProjectId = Request.Params["projectId"];
}
UnitService.InitUnitDropDownList(this.drpUnit, this.ProjectId, true);
this.drpUnit.SelectedValue = string.IsNullOrEmpty(this.CurrUser.UnitId) ? Const.UnitId_TCC : this.CurrUser.UnitId;
if (ProjectUnitService.GetProjectUnitTypeByProjectIdUnitId(this.ProjectId, this.CurrUser.UnitId))
{
this.drpUnit.Enabled = false;
}
WorkPostService.InitWorkPostDropDownList(this.drpWorkPost, true);
this.txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now.AddMonths(-1));
this.txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
this.setData();
}
}
public static List<Model.SitePerson_PersonInOut> getAllPersonInOutList;
/// <summary>
///
/// </summary>
private void setData()
{
getAllPersonInOutList = null;
if (!string.IsNullOrEmpty(this.ProjectId))
{
string strSql = @" select UnitId+'|'+WorkPostId as ID, UnitId,UnitName,WorkPostId,WorkPostName,count(distinct PersonId) PersonCount
from dbo.SitePerson_PersonInOut where ProjectId=@ProjectId ";
List<SqlParameter> listStr = new List<SqlParameter>();
listStr.Add(new SqlParameter("@ProjectId", CurrUser.LoginProjectId));
if (!string.IsNullOrEmpty(this.txtStartDate.Text))
{
strSql += " and ChangeTime >= @startTime";
listStr.Add(new SqlParameter("@startTime", this.txtStartDate.Text));
}
if (!string.IsNullOrEmpty(this.txtEndDate.Text))
{
strSql += " and ChangeTime <= @endTime";
listStr.Add(new SqlParameter("@endTime", this.txtEndDate.Text));
}
if (this.drpUnit.SelectedValue != Const._Null)
{
strSql += " and UnitId = @UnitId";
listStr.Add(new SqlParameter("@UnitId", this.drpUnit.SelectedValue));
}
if (this.drpWorkPost.SelectedValue != Const._Null)
{
strSql += " and WorkPostId = @WorkPostId";
listStr.Add(new SqlParameter("@WorkPostId", this.drpWorkPost.SelectedValue));
}
strSql += " group by UnitId,UnitName,WorkPostId,WorkPostName";
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
if (tb != null)
{
tb.Columns.Add("WorkTime");
string strSql1 = @" select UnitId,UnitName,WorkPostId,WorkPostName,PersonId,IsIn,ChangeTime from SitePerson_PersonInOut where ProjectId=@ProjectId ";
List<SqlParameter> listStr1 = new List<SqlParameter>();
listStr1.Add(new SqlParameter("@ProjectId", CurrUser.LoginProjectId));
if (!string.IsNullOrEmpty(this.txtStartDate.Text))
{
strSql1 += " and ChangeTime >= @startTime";
listStr1.Add(new SqlParameter("@startTime", this.txtStartDate.Text));
}
if (!string.IsNullOrEmpty(this.txtEndDate.Text))
{
strSql1 += " and ChangeTime <= @endTime";
listStr1.Add(new SqlParameter("@endTime", this.txtEndDate.Text));
}
if (this.drpUnit.SelectedValue != Const._Null)
{
strSql1 += " and UnitId = @UnitId";
listStr1.Add(new SqlParameter("@UnitId", this.drpUnit.SelectedValue));
}
if (this.drpWorkPost.SelectedValue != Const._Null)
{
strSql1 += " and WorkPostId = @WorkPostId";
listStr1.Add(new SqlParameter("@WorkPostId", this.drpWorkPost.SelectedValue));
}
strSql1 += " order by UnitId,UnitName,WorkPostId,WorkPostName,PersonId,ChangeTime desc";
SqlParameter[] parameter1 = listStr1.ToArray();
DataTable tb1 = SQLHelper.GetDataTableRunText(strSql1, parameter1);
if (tb1 != null)
{
Dictionary<string, double> dic = new Dictionary<string, double>();
HashSet<string > dicAttenDate = new HashSet<string>();
for (int i = 0; i < tb1.Rows.Count; i++)
{
DataRow outRow = null;
DateTime? outTime = null;
DataRow inRow = null;
DateTime? inTime = null;
if (tb1.Rows[i]["IsIn"].ToString() == "False")
{
outRow = tb1.Rows[i];
outTime = Funs.GetNewDateTime(outRow["ChangeTime"].ToString());
if (i < tb1.Rows.Count - 1)
{
var tempSpan = outTime- Funs.GetNewDateTime(tb1.Rows[i + 1]["ChangeTime"].ToString());
if (tb1.Rows[i + 1]["IsIn"].ToString() == "False"&& outRow["PersonId"].ToString() == tb1.Rows[i + 1]["PersonId"].ToString() && tempSpan.HasValue&& tempSpan.Value.TotalMinutes<5)
{
continue;
}
}
}
if (outRow != null)
for (int j = i + 1; j < tb1.Rows.Count; j++)//循环寻找本人24小时内的入场记录
{
DataRow temp = tb1.Rows[j];
if (outRow["PersonId"].ToString() != temp["PersonId"].ToString())//不是同一个人结束
{
break;
}
DateTime? tempTime = Funs.GetNewDateTime(temp["ChangeTime"].ToString());
var span = outTime - tempTime;
if (span.Value.TotalHours > 24)//超过24小时没有找到入场结束
{
break;
}
//24小时内的入场记录结束循环
if (outRow["PersonId"].ToString() == temp["PersonId"].ToString() && temp["IsIn"].ToString() == "True" && span.HasValue && span.Value.TotalHours <= 24)
{
inRow = temp;
inTime = Funs.GetNewDateTime(temp["ChangeTime"].ToString());
break;
}
}
if (outRow != null)
{
string personDay = outRow["PersonId"].ToString() + "$" + Funs.GetNewDateTime(outRow["ChangeTime"].ToString()).Value.ToString("yyyy-MM-dd");
if (inRow != null && outRow != null) //如果本次出场24小时内有入场计算实际出场时间
{
if (!dic.ContainsKey(outRow["UnitId"].ToString() + "$" + outRow["WorkPostId"].ToString()))
{
dic.Add(outRow["UnitId"].ToString() + "$" + outRow["WorkPostId"].ToString(), 0);
}
var span = outTime - inTime;
dic[outRow["UnitId"].ToString() + "$" + outRow["WorkPostId"].ToString()] += span.Value.TotalMinutes;
dicAttenDate.Add(personDay);
}
else if (!dicAttenDate.Contains(personDay))//找不到该入出场时间按8小时计算
{
if (!dic.ContainsKey(outRow["UnitId"].ToString() + "$" + outRow["WorkPostId"].ToString()))
{
dic.Add(outRow["UnitId"].ToString() + "$" + outRow["WorkPostId"].ToString(), 0);
}
dic[outRow["UnitId"].ToString() + "$" + outRow["WorkPostId"].ToString()] += 8 * 60;
dicAttenDate.Add(personDay);
}
}
}
foreach (DataRow dataRow in tb.Rows)
{
if (dic.ContainsKey(dataRow["UnitId"].ToString() + "$" + dataRow["WorkPostId"].ToString()))
{
dataRow["WorkTime"] = Convert.ToInt32(dic[dataRow["UnitId"].ToString() + "$" + dataRow["WorkPostId"].ToString()] / 60);
}
}
}
}
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
this.setData();
}
#region
/// <summary>
/// 分页选择下拉改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
setData();
}
#endregion
#region
/// <summary>
/// 页索引改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
Grid1.PageIndex = e.NewPageIndex;
setData();
}
#endregion
#region
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
Response.ClearContent();
string filename = Funs.GetNewFileName();
Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("现场岗位人工时统计" + filename, System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/excel";
Response.ContentEncoding = System.Text.Encoding.UTF8;
this.Grid1.PageSize = this.Grid1.RecordCount;
setData();
Response.Write(GetGridTableHtml(Grid1));
Response.End();
}
#endregion
}
}