CNCEC_SUBQHSE_WUHUAN/SGGL/FineUIPro.Web/JDGL/SGManPower/ManPowerPlanChart.aspx.cs

191 lines
6.5 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 System;
using System.Collections.Generic;
using System.Linq;
using BLL;
using System.Data;
namespace FineUIPro.Web.JDGL.SGManPower
{
public partial class ManPowerPlanChart : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UnitId = Request.Params["UnitId"];
StartTime = Request.Params["StartTime"];
EndTime = Request.Params["EndTime"];
UnitWorkId = Request.Params["UnitWorkId"];
WorkPostId = Request.Params["WorkPostId"];
Version = Request.Params["Version"];
// 初始化图表数据
InitializeChartData();
}
}
public string UnitId
{
get => (string)ViewState["UnitId"];
set => ViewState["UnitId"] = value;
}
public string StartTime
{
get => (string)ViewState["StartTime"];
set => ViewState["StartTime"] = value;
}
public string EndTime
{
get => (string)ViewState["EndTime"];
set => ViewState["EndTime"] = value;
}
public string UnitWorkId
{
get => (string)ViewState["UnitWorkId"];
set => ViewState["UnitWorkId"] = value;
}
public string WorkPostId
{
get => (string)ViewState["WorkPostId"];
set => ViewState["WorkPostId"] = value;
}
public string Version
{
get => (string)ViewState["Version"];
set => ViewState["Version"] = value;
}
private void InitializeChartData()
{
// 检查必要参数
if (string.IsNullOrEmpty(StartTime) || string.IsNullOrEmpty(EndTime))
{
ShowNotify("缺少必要的参数,无法生成图表", MessageBoxIcon.Warning);
return;
}
try
{
DateTime startDate = Convert.ToDateTime(StartTime);
DateTime endDate = Convert.ToDateTime(EndTime);
// 获取人力计划数据
var manpowerQuery = from x in Funs.DB.JDGL_SGManPower
where x.ProjectId == this.CurrUser.LoginProjectId && x.PlanDate >= startDate && x.PlanDate <= endDate
select x;
if (UnitId != Const._Null)
{
manpowerQuery = manpowerQuery.Where(x => x.UnitId == UnitId);
}
// 添加装置筛选条件
if (!string.IsNullOrEmpty(UnitWorkId))
{
manpowerQuery = manpowerQuery.Where(x => x.UnitWorkId == UnitWorkId);
}
// 添加岗位筛选条件
if (!string.IsNullOrEmpty(WorkPostId))
{
manpowerQuery = manpowerQuery.Where(x => x.WorkPostId == WorkPostId);
}
// 添加版本筛选条件
if (!string.IsNullOrEmpty(Version))
{
manpowerQuery = manpowerQuery.Where(x => x.Version == Version);
}
var manpowerPlans = manpowerQuery.ToList();
// 按日期汇总人数
var chartData = manpowerPlans
.Where(x => x.PlanDate.HasValue && x.Quantity.HasValue)
.GroupBy(x => x.PlanDate.Value.Date)
.Select(g => new
{
Date = g.Key,
TotalQuantity = g.Sum(x => x.Quantity.Value)
})
.OrderBy(x => x.Date)
.ToList();
// 如果没有数据,显示提示信息
if (chartData.Count == 0)
{
ShowNotify("在指定时间范围内没有找到人力计划数据", MessageBoxIcon.Warning);
return;
}
// 构建图表数据源
DataTable dt = new DataTable();
dt.Columns.Add("日期", typeof(string));
dt.Columns.Add("人员数量", typeof(int));
foreach (var item in chartData)
{
DataRow row = dt.NewRow();
row["日期"] = item.Date.ToString("MM-dd");
row["人员数量"] = item.TotalQuantity;
dt.Rows.Add(row);
}
// 创建图表数据对象
Model.DataSourceChart dataSourceChart = new Model.DataSourceChart
{
Title = "总人力需求计划",
ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column,
Width = 1000,
Height = 500,
IsNotEnable3D = false
};
// 创建数据系列
Model.DataSourceTeam dataSourceTeam = new Model.DataSourceTeam
{
DataPointName = "人员数量",
DataSourcePoints = new List<Model.DataSourcePoint>() // 确保初始化
};
// 添加数据点
foreach (DataRow row in dt.Rows)
{
Model.DataSourcePoint point = new Model.DataSourcePoint
{
PointText = row["日期"].ToString(),
PointValue = row["人员数量"].ToString()
};
// 确保DataSourcePoints不为空后再添加
if (dataSourceTeam.DataSourcePoints != null)
{
dataSourceTeam.DataSourcePoints.Add(point);
}
}
// 确保DataSourceTeams不为空后再添加
if (dataSourceChart.DataSourceTeams != null)
{
dataSourceChart.DataSourceTeams.Add(dataSourceTeam);
}
else
{
// 如果DataSourceTeams为空创建一个新的列表
dataSourceChart.DataSourceTeams = new List<Model.DataSourceTeam> { dataSourceTeam };
}
// 生成图表
Chart1.CreateChart(dataSourceChart);
}
catch (Exception ex)
{
ShowNotify($"生成图表时发生错误: {ex.Message}", MessageBoxIcon.Error);
}
}
}
}