SGGL_SHJ/SGGL/FineUIPro.Web/PZHGL/InformationProject/ProjectConstructionLogWorkE...

456 lines
23 KiB
C#

using BLL;
using Newtonsoft.Json.Linq;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.PZHGL.InformationProject
{
public partial class ProjectConstructionLogWorkEfficiencySum : PageBase
{
/// <summary>
/// 加载表头
/// </summary>
protected void Page_Init(object sender, EventArgs e)
{
InitGrid();
}
#region
/// <summary>
/// 表头
/// </summary>
private void InitGrid()
{
Model.SGGLDB db = Funs.DB;
var contracts = (from x in db.PHTGL_Contract
where x.ProjectId == this.CurrUser.LoginProjectId
orderby x.ContractNum
select x).ToList();
GroupField gf = new GroupField();
gf.ColumnID = "GX";
gf.HeaderText = "工效统计";
gf.HeaderTextAlign = TextAlign.Center;
for (int i = 0; i < contracts.Count; i++)
{
RenderField rd1 = new RenderField();
rd1.ColumnID = "Contract" + i.ToString();
rd1.Width = Unit.Pixel(100);
rd1.DataField = "Contract" + i.ToString();
rd1.FieldType = FieldType.Double;
rd1.HeaderText = contracts[i].ContractNum;
rd1.HeaderTextAlign = TextAlign.Center;
gf.Columns.Add(rd1);
}
Grid1.Columns.Add(gf);
}
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
#endregion
private void GetData()
{
Model.SGGLDB db = Funs.DB;
var constructionLogWorkEfficiencyMonths = from x in db.ZHGL_ConstructionLogWorkEfficiencyMonth
join y in db.PHTGL_ContractTrack on x.ContractTrackId equals y.Id
where x.ProjectId == this.CurrUser.LoginProjectId
select new { x.TypeId, x.UnitOfMeasurement, y.ContractId, x.WorkEfficiency };
var persons = (from x in db.ZHGL_ConstructionLogWorkEfficiencyMonth
join y in db.Base_WorkPost on x.TypeId equals y.WorkPostId
where x.ProjectId == this.CurrUser.LoginProjectId && x.Type == "Person"
select new { y.WorkPostName, x.UnitOfMeasurement, x.TypeId }).Distinct().OrderBy(x => x.WorkPostName).ToList();
var machines = (from x in db.ZHGL_ConstructionLogWorkEfficiencyMonth
join y in db.Base_SpecialEquipment on x.TypeId equals y.SpecialEquipmentId
where x.ProjectId == this.CurrUser.LoginProjectId && x.Type == "Machine"
select new { y.SpecialEquipmentName, x.UnitOfMeasurement, x.TypeId }).Distinct().OrderBy(x => x.SpecialEquipmentName).ToList();
var contracts = (from x in db.PHTGL_Contract
where x.ProjectId == this.CurrUser.LoginProjectId
orderby x.ContractNum
select x).ToList();
var workEfficiencyProjects = from x in db.ZHGL_ConstructionLogWorkEfficiencyProject
where x.ProjectId == this.CurrUser.LoginProjectId
select x;
DataTable table = new DataTable();
table.Columns.Add(new DataColumn("Id", typeof(String)));
table.Columns.Add(new DataColumn("Code", typeof(String)));
table.Columns.Add(new DataColumn("Name", typeof(String)));
table.Columns.Add(new DataColumn("UnitOfMeasurement", typeof(String)));
table.Columns.Add(new DataColumn("BaseWorkEfficiency", typeof(String)));
table.Columns.Add(new DataColumn("AvgWorkEfficiency", typeof(String)));
table.Columns.Add(new DataColumn("Type", typeof(String)));
table.Columns.Add(new DataColumn("TypeId", typeof(String)));
table.Columns.Add(new DataColumn("Contract0", typeof(String)));
table.Columns.Add(new DataColumn("Contract1", typeof(String)));
table.Columns.Add(new DataColumn("Contract2", typeof(String)));
table.Columns.Add(new DataColumn("Contract3", typeof(String)));
table.Columns.Add(new DataColumn("Contract4", typeof(String)));
table.Columns.Add(new DataColumn("Contract5", typeof(String)));
table.Columns.Add(new DataColumn("Contract6", typeof(String)));
table.Columns.Add(new DataColumn("Contract7", typeof(String)));
table.Columns.Add(new DataColumn("Contract8", typeof(String)));
table.Columns.Add(new DataColumn("Contract9", typeof(String)));
table.Columns.Add(new DataColumn("Contract10", typeof(String)));
table.Columns.Add(new DataColumn("Contract11", typeof(String)));
table.Columns.Add(new DataColumn("Contract12", typeof(String)));
table.Columns.Add(new DataColumn("Contract13", typeof(String)));
table.Columns.Add(new DataColumn("Contract14", typeof(String)));
table.Columns.Add(new DataColumn("Contract15", typeof(String)));
table.Columns.Add(new DataColumn("Contract16", typeof(String)));
table.Columns.Add(new DataColumn("Contract17", typeof(String)));
table.Columns.Add(new DataColumn("Contract18", typeof(String)));
table.Columns.Add(new DataColumn("Contract19", typeof(String)));
table.Columns.Add(new DataColumn("Contract20", typeof(String)));
table.Columns.Add(new DataColumn("Contract21", typeof(String)));
table.Columns.Add(new DataColumn("Contract22", typeof(String)));
table.Columns.Add(new DataColumn("Contract23", typeof(String)));
table.Columns.Add(new DataColumn("Contract24", typeof(String)));
table.Columns.Add(new DataColumn("Contract25", typeof(String)));
table.Columns.Add(new DataColumn("Contract26", typeof(String)));
table.Columns.Add(new DataColumn("Contract27", typeof(String)));
table.Columns.Add(new DataColumn("Contract28", typeof(String)));
table.Columns.Add(new DataColumn("Contract29", typeof(String)));
table.Columns.Add(new DataColumn("Contract30", typeof(String)));
table.Columns.Add(new DataColumn("Contract31", typeof(String)));
table.Columns.Add(new DataColumn("Contract32", typeof(String)));
table.Columns.Add(new DataColumn("Contract33", typeof(String)));
table.Columns.Add(new DataColumn("Contract34", typeof(String)));
table.Columns.Add(new DataColumn("Contract35", typeof(String)));
table.Columns.Add(new DataColumn("Contract36", typeof(String)));
table.Columns.Add(new DataColumn("Contract37", typeof(String)));
table.Columns.Add(new DataColumn("Contract38", typeof(String)));
table.Columns.Add(new DataColumn("Contract39", typeof(String)));
table.Columns.Add(new DataColumn("Contract40", typeof(String)));
DataRow row;
row = table.NewRow();
row[0] = SQLHelper.GetNewID();
row[1] = "一";
row[2] = "人工";
table.Rows.Add(row);
int a = 1;
foreach (var person in persons)
{
row = table.NewRow();
row[0] = SQLHelper.GetNewID();
row[1] = a;
row[2] = person.WorkPostName;
row[3] = person.UnitOfMeasurement;
var workEfficiencyProject = workEfficiencyProjects.FirstOrDefault(x => x.TypeId == person.TypeId && x.UnitOfMeasurement == person.UnitOfMeasurement);
if (workEfficiencyProject != null)
{
row[4] = workEfficiencyProject.BaseWorkEfficiency;
}
int b = 0;
List<decimal> list = new List<decimal>();
foreach (var contract in contracts)
{
var workEfficiencys = from x in constructionLogWorkEfficiencyMonths
where x.TypeId == person.TypeId && x.UnitOfMeasurement == person.UnitOfMeasurement && x.ContractId == contract.ContractId && x.WorkEfficiency != null
select x;
if (workEfficiencys.Count() > 0)
{
decimal totalWorkEfficiencys = workEfficiencys.Sum(x => x.WorkEfficiency ?? 0);
decimal d = decimal.Round(Convert.ToDecimal(totalWorkEfficiencys / Convert.ToDecimal(workEfficiencys.Count())), 2);
row[8 + b] = d;
list.Add(d);
}
b++;
}
if (list.Count > 0)
{
row[5] = decimal.Round(Convert.ToDecimal(list.Sum() / Convert.ToDecimal(list.Count)), 2);
}
row[6] = "Person";
row[7] = person.TypeId;
table.Rows.Add(row);
a++;
}
row = table.NewRow();
row[0] = SQLHelper.GetNewID();
row[1] = "二";
row[2] = "机械";
table.Rows.Add(row);
a = 1;
foreach (var machine in machines)
{
row = table.NewRow();
row[0] = SQLHelper.GetNewID();
row[1] = a;
row[2] = machine.SpecialEquipmentName;
row[3] = machine.UnitOfMeasurement;
var workEfficiencyProject = workEfficiencyProjects.FirstOrDefault(x => x.TypeId == machine.TypeId && x.UnitOfMeasurement == machine.UnitOfMeasurement);
if (workEfficiencyProject != null)
{
row[4] = workEfficiencyProject.BaseWorkEfficiency;
}
int b = 0;
List<decimal> list = new List<decimal>();
foreach (var contract in contracts)
{
var workEfficiencys = from x in constructionLogWorkEfficiencyMonths
where x.TypeId == machine.TypeId && x.UnitOfMeasurement == machine.UnitOfMeasurement && x.ContractId == contract.ContractId && x.WorkEfficiency != null
select x;
if (workEfficiencys.Count() > 0)
{
decimal totalWorkEfficiencys = workEfficiencys.Sum(x => x.WorkEfficiency ?? 0);
decimal d = decimal.Round(Convert.ToDecimal(totalWorkEfficiencys / Convert.ToDecimal(workEfficiencys.Count())), 2);
row[8 + b] = d;
list.Add(d);
}
b++;
}
if (list.Count > 0)
{
row[5] = decimal.Round(Convert.ToDecimal(list.Sum() / Convert.ToDecimal(list.Count)), 2);
}
row[6] = "Machine";
row[7] = machine.TypeId;
table.Rows.Add(row);
a++;
}
Grid1.DataSource = table;
Grid1.DataBind();
}
#region
/// <summary>
/// 统计
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnAnalyse_Click(object sender, EventArgs e)
{
GetData();
}
#endregion
#region
/// <summary>
/// 保存按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
if (this.Grid1.Rows.Count > 0)
{
BLL.ConstructionLogWorkEfficiencyProjectService.DeleteConstructionLogWorkEfficiencyProjectsByConstructionLogId(this.CurrUser.LoginProjectId);
foreach (JObject mergedRow in Grid1.GetMergedData())
{
JObject values = mergedRow.Value<JObject>("values");
int a = mergedRow.Value<int>("index");
string type = values.Value<string>("Type");
if (!string.IsNullOrEmpty(type))
{
string unitOfMeasurement = values.Value<string>("UnitOfMeasurement");
string baseWorkEfficiency = values.Value<string>("BaseWorkEfficiency");
string avgWorkEfficiency = values.Value<string>("AvgWorkEfficiency");
string typeId = values.Value<string>("TypeId");
Model.ZHGL_ConstructionLogWorkEfficiencyProject workEfficiency = new Model.ZHGL_ConstructionLogWorkEfficiencyProject();
workEfficiency.ConstructionLogWorkEfficiencyProjectId = SQLHelper.GetNewID();
workEfficiency.ProjectId = this.CurrUser.LoginProjectId;
workEfficiency.Type = type;
workEfficiency.TypeId = typeId;
workEfficiency.UnitOfMeasurement = unitOfMeasurement;
workEfficiency.BaseWorkEfficiency = Funs.GetNewDecimal(baseWorkEfficiency);
workEfficiency.AvgWorkEfficiency = Funs.GetNewDecimal(avgWorkEfficiency);
BLL.ConstructionLogWorkEfficiencyProjectService.AddConstructionLogWorkEfficiencyProject(workEfficiency);
}
}
ShowNotify("保存成功!", MessageBoxIcon.Success);
}
else
{
Alert.ShowInTop("请先统计后再保存!", MessageBoxIcon.Warning);
}
}
#endregion
#region
/// 导出按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
if (this.Grid1.Rows.Count == 0)
{
Alert.ShowInTop("无数据可导出!", MessageBoxIcon.Warning);
return;
}
string rootPath = Server.MapPath("~/");
string initTemplatePath = string.Empty;
string uploadfilepath = string.Empty;
string newUrl = string.Empty;
string filePath = string.Empty;
initTemplatePath = Const.ProjectConstructionLogWorkEfficiencyTemplateUrl;
uploadfilepath = rootPath + initTemplatePath;
Model.SGGLDB db = Funs.DB;
var contracts = (from x in db.PHTGL_Contract
where x.ProjectId == this.CurrUser.LoginProjectId
orderby x.ContractNum
select x).ToList();
string projectName = BLL.ProjectService.GetProjectNameByProjectId(this.CurrUser.LoginProjectId);
newUrl = uploadfilepath.Replace(".xlsx", "(" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ")" + ".xlsx");
File.Copy(uploadfilepath, newUrl);
// 第一步:读取文件流
NPOI.SS.UserModel.IWorkbook workbook;
using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read))
{
workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
}
// 创建单元格样式
NPOI.SS.UserModel.ICellStyle cellStyle0 = workbook.CreateCellStyle();
cellStyle0.BorderTop = NPOI.SS.UserModel.BorderStyle.None;
cellStyle0.BorderRight = NPOI.SS.UserModel.BorderStyle.None;
cellStyle0.BorderBottom = NPOI.SS.UserModel.BorderStyle.None;
cellStyle0.BorderLeft = NPOI.SS.UserModel.BorderStyle.None;
cellStyle0.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle0.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
cellStyle0.WrapText = true;
var font = workbook.CreateFont();
font.FontHeightInPoints = 16;
font.IsBold = true;
cellStyle0.SetFont(font);
NPOI.SS.UserModel.ICellStyle cellStyle1 = workbook.CreateCellStyle();
cellStyle1.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle1.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle1.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle1.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle1.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
cellStyle1.WrapText = true;
var font1 = workbook.CreateFont();
font1.FontHeightInPoints = 11;
font1.IsBold = true;
cellStyle1.SetFont(font1);
NPOI.SS.UserModel.ICellStyle cellStyle2 = workbook.CreateCellStyle();
cellStyle2.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle2.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle2.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle2.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cellStyle2.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle2.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
cellStyle2.WrapText = true;
var font0 = workbook.CreateFont();
font0.FontHeightInPoints = 11;
font0.IsBold = false;
cellStyle2.SetFont(font0);
NPOI.SS.UserModel.ICellStyle cellStyle3 = workbook.CreateCellStyle();
cellStyle3.BorderTop = NPOI.SS.UserModel.BorderStyle.None;
cellStyle3.BorderRight = NPOI.SS.UserModel.BorderStyle.None;
cellStyle3.BorderBottom = NPOI.SS.UserModel.BorderStyle.None;
cellStyle3.BorderLeft = NPOI.SS.UserModel.BorderStyle.None;
cellStyle3.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
cellStyle3.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
//cellStyle3.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
//cellStyle3.FillPattern = NPOI.SS.UserModel.FillPattern.SolidForeground;
cellStyle3.WrapText = true;
var font3 = workbook.CreateFont();
font3.FontHeightInPoints = 11;
font3.IsBold = true;
cellStyle3.SetFont(font3);
// 第二步:创建新数据行
NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
NPOI.SS.UserModel.IRow row1 = sheet.GetRow(0);
NPOI.SS.UserModel.ICell cell3;
cell3 = row1.CreateCell(0);
cell3.CellStyle = cellStyle0;
cell3.SetCellValue("项目平均工效统计");
cell3 = row1.CreateCell(4 + contracts.Count);
cell3.CellStyle = cellStyle0;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 4 + contracts.Count));
row1 = sheet.GetRow(1);
cell3 = row1.CreateCell(1);
cell3.CellStyle = cellStyle3;
cell3.SetCellValue(projectName);
row1 = sheet.GetRow(2);
cell3 = row1.CreateCell(5);
cell3.CellStyle = cellStyle1;
cell3.SetCellValue("工效统计");
for (int i = 1; i < contracts.Count; i++)
{
cell3 = row1.CreateCell(5 + i);
cell3.CellStyle = cellStyle1;
}
sheet.AddMergedRegion(new CellRangeAddress(2, 2, 5, 4 + contracts.Count));
row1 = sheet.GetRow(3);
for (int i = 0; i < contracts.Count; i++)
{
cell3 = row1.CreateCell(5 + i);
cell3.CellStyle = cellStyle1;
cell3.SetCellValue(contracts[i].ContractNum);
}
int rowCount = 4;
foreach (JObject mergedRow in Grid1.GetMergedData())
{
JObject values = mergedRow.Value<JObject>("values");
int a = mergedRow.Value<int>("index");
string Code = values.Value<string>("Code");
string Name = values.Value<string>("Name");
string UnitOfMeasurement = values.Value<string>("UnitOfMeasurement");
string BaseWorkEfficiency = values.Value<string>("BaseWorkEfficiency");
string AvgWorkEfficiency = values.Value<string>("AvgWorkEfficiency");
row1 = sheet.GetRow(rowCount);
cell3 = row1.CreateCell(0);
cell3.CellStyle = cellStyle2;
cell3.SetCellValue(Code);
cell3 = row1.CreateCell(1);
cell3.CellStyle = cellStyle2;
cell3.SetCellValue(Name);
cell3 = row1.CreateCell(2);
cell3.CellStyle = cellStyle2;
cell3.SetCellValue(UnitOfMeasurement);
cell3 = row1.CreateCell(3);
cell3.CellStyle = cellStyle2;
cell3.SetCellValue(BaseWorkEfficiency);
cell3 = row1.CreateCell(4);
cell3.CellStyle = cellStyle2;
cell3.SetCellValue(AvgWorkEfficiency);
for (int i = 0; i < contracts.Count; i++)
{
cell3 = row1.CreateCell(5 + i);
cell3.CellStyle = cellStyle2;
cell3.SetCellValue(values.Value<string>("Contract" + i.ToString()));
}
rowCount++;
}
// 第三步:写入文件流
using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write))
{
workbook.Write(stream);
workbook.Close();
}
string fileName = Path.GetFileName(newUrl);
FileInfo info = new FileInfo(newUrl);
long fileSize = info.Length;
Response.Clear();
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", fileSize.ToString());
Response.TransmitFile(newUrl, 0, fileSize);
Response.Flush();
Response.Close();
File.Delete(newUrl);
}
#endregion
}
}