183 lines
7.5 KiB
C#
183 lines
7.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
using BLL;
|
||
using System.Data;
|
||
using System.Data.SqlClient;
|
||
|
||
namespace FineUIPro.Web.JDGL.SGManPower
|
||
{
|
||
public partial class ManPowerWorkChart : PageBase
|
||
{
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack)
|
||
{
|
||
// 初始化图表数据
|
||
InitializeChartData();
|
||
}
|
||
}
|
||
|
||
private void InitializeChartData()
|
||
{
|
||
try
|
||
{
|
||
// 获取查询参数
|
||
string unitId = Request.Params["UnitId"];
|
||
string startTime = Request.Params["StartTime"];
|
||
string endTime = Request.Params["EndTime"];
|
||
string workPostId = Request.Params["WorkPostId"];
|
||
string unitWorkId = Request.Params["UnitWorkId"];
|
||
|
||
// 检查必要参数
|
||
if (string.IsNullOrEmpty(startTime) || string.IsNullOrEmpty(endTime))
|
||
{
|
||
ShowNotify("缺少必要的参数,无法生成图表", MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
DateTime startDate = Convert.ToDateTime(startTime);
|
||
DateTime endDate = Convert.ToDateTime(endTime);
|
||
|
||
// 使用原生SQL查询直接在数据库中进行聚合计算,提高性能
|
||
string strSql = @"
|
||
SELECT e.UnitId, e.PostId, e.RecordDate, p.WorkAreaId as UnitWorkId
|
||
FROM T_d_EmployInOutRecord e INNER JOIN SitePerson_Person p ON e.IDCardNo = p.IdentityCard AND e.ProjectId = p.ProjectId
|
||
WHERE e.PostId IS NOT NULL AND e.PostId != ''
|
||
AND e.ProjectId = @ProjectId
|
||
AND e.RecordDate >= @StartDate
|
||
AND e.RecordDate <= @EndDate";
|
||
|
||
var parameters = new List<SqlParameter>
|
||
{
|
||
new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId),
|
||
new SqlParameter("@StartDate", startDate),
|
||
new SqlParameter("@EndDate", endDate)
|
||
};
|
||
|
||
// 添加单位筛选条件
|
||
if (!string.IsNullOrEmpty(unitId) && unitId != Const._Null)
|
||
{
|
||
strSql += " AND e.UnitId = @UnitId";
|
||
parameters.Add(new SqlParameter("@UnitId", unitId));
|
||
}
|
||
|
||
// 添加岗位筛选条件
|
||
if (!string.IsNullOrEmpty(workPostId) && workPostId != Const._Null)
|
||
{
|
||
strSql += " AND e.PostId = @WorkPostId";
|
||
parameters.Add(new SqlParameter("@WorkPostId", workPostId));
|
||
}
|
||
|
||
// 执行查询
|
||
var dt = SQLHelper.GetDataTableRunText(strSql, parameters.ToArray());
|
||
|
||
// 创建一个新的DataTable来存储处理后的数据
|
||
DataTable processedDt = dt.Clone();
|
||
foreach (System.Data.DataRow row in dt.Rows)
|
||
{
|
||
string ids = row["UnitWorkId"] != DBNull.Value ? row["UnitWorkId"].ToString() : string.Empty;
|
||
if (!string.IsNullOrEmpty(ids))
|
||
{
|
||
string[] unitWorkIdArray = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||
foreach (string id in unitWorkIdArray)
|
||
{
|
||
// 往processedDt里面添加数据,每个UnitWorkId一行
|
||
DataRow newRow = processedDt.NewRow();
|
||
newRow["UnitId"] = row["UnitId"];
|
||
newRow["PostId"] = row["PostId"];
|
||
newRow["RecordDate"] = row["RecordDate"];
|
||
newRow["UnitWorkId"] = id.Trim();
|
||
processedDt.Rows.Add(newRow);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
DataRow newRow = processedDt.NewRow();
|
||
newRow["UnitId"] = row["UnitId"];
|
||
newRow["PostId"] = row["PostId"];
|
||
newRow["RecordDate"] = row["RecordDate"];
|
||
newRow["UnitWorkId"] = string.Empty;
|
||
processedDt.Rows.Add(newRow);
|
||
}
|
||
}
|
||
|
||
// 确定要用于后续处理的数据源
|
||
IEnumerable<DataRow> dataSource = processedDt.AsEnumerable();
|
||
|
||
// 如果选择了特定的装置进行筛选
|
||
if (!string.IsNullOrEmpty(unitWorkId) && unitWorkId != Const._Null)
|
||
{
|
||
dataSource = dataSource.Where(x => x["UnitWorkId"] != DBNull.Value && x["UnitWorkId"].ToString() == unitWorkId);
|
||
}
|
||
|
||
//针对dt中的数据进行分组
|
||
var data = dataSource
|
||
.GroupBy(row => new {
|
||
RecordDate = row["RecordDate"]
|
||
}).Select(group => new
|
||
{
|
||
RecordDate = group.Key.RecordDate,
|
||
RecordCount = group.Count()
|
||
}).ToList();
|
||
|
||
|
||
// 如果没有数据,显示提示信息
|
||
if (data.Count == 0)
|
||
{
|
||
ShowNotify("在指定时间范围内没有找到人力统计数据", MessageBoxIcon.Warning);
|
||
return;
|
||
}
|
||
|
||
// 根据数据点数量动态调整图表宽度,确保每个数据点有足够的显示空间
|
||
int chartWidth = Math.Max(1000, data.Count * 50); // 每个数据点至少50像素宽,最小1000像素
|
||
|
||
// 创建图表数据对象
|
||
Model.DataSourceChart dataSourceChart = new Model.DataSourceChart
|
||
{
|
||
Title = "现场考勤人力统计图表",
|
||
ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column,
|
||
Width = chartWidth,
|
||
Height = 500,
|
||
IsNotEnable3D = false
|
||
};
|
||
|
||
// 创建数据系列
|
||
Model.DataSourceTeam dataSourceTeam = new Model.DataSourceTeam
|
||
{
|
||
DataPointName = "人员数量",
|
||
DataSourcePoints = new List<Model.DataSourcePoint>()
|
||
};
|
||
|
||
// 添加数据点
|
||
foreach (var item in data)
|
||
{
|
||
if (item.RecordDate != null)
|
||
{
|
||
DateTime recordDate = Convert.ToDateTime(item.RecordDate);
|
||
|
||
Model.DataSourcePoint point = new Model.DataSourcePoint
|
||
{
|
||
PointText = recordDate.ToString("MM-dd"),
|
||
PointValue = item.RecordCount.ToString()
|
||
};
|
||
dataSourceTeam.DataSourcePoints.Add(point);
|
||
}
|
||
}
|
||
|
||
dataSourceChart.DataSourceTeams = new List<Model.DataSourceTeam> { dataSourceTeam };
|
||
|
||
// 生成图表
|
||
Chart1.CreateChart(dataSourceChart);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ShowNotify($"生成图表时发生错误: {ex.Message}", MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|