SGGL_JT/SUBQHSE/FineUIPro.Web/Customization/CNCCG/HSSE/Rewards/PoleUnitManager.aspx.cs

472 lines
19 KiB
C#

using BLL;
using BLL.Common;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using NPOI.XWPF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.Customization.CNCCG.HSSE.Rewards
{
public partial class PoleUnitManager : PageBase
{
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
////权限按钮方法
this.GetButtonPower();
this.btnNew.OnClientClick = Window1.GetShowReference("PoleUnitEdit.aspx") + "return false;";
if (this.CurrUser != null && this.CurrUser.PageSize.HasValue)
{
Grid1.PageSize = this.CurrUser.PageSize.Value;
}
this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
// 绑定表格
this.BindGrid();
}
}
/// <summary>
/// 获取按钮权限
/// </summary>
/// <param name="button"></param>
/// <returns></returns>
private void GetButtonPower()
{
if (Request.Params["value"] == "0")
{
return;
}
var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.PoleUnitMenuId);
if (buttonList.Count() > 0)
{
if (buttonList.Contains(BLL.Const.BtnAdd))
{
this.btnNew.Hidden = false;
}
if (buttonList.Contains(BLL.Const.BtnModify))
{
this.btnMenuEdit.Hidden = false;
}
if (buttonList.Contains(BLL.Const.BtnDelete))
{
this.btnMenuDelete.Hidden = false;
}
}
}
/// <summary>
/// 绑定数据
/// </summary>
private void BindGrid()
{
string strSql = @"SELECT f.*,b.UnitName from HSSE_PoleUnit f
left join Base_Unit b on f.Unitid=b.UnitId where 1=1 ";
List<SqlParameter> listStr = new List<SqlParameter>();
if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim()))
{
strSql += " AND f.CreateMonth = @CreateMonth";
listStr.Add(new SqlParameter("@CreateMonth", Convert.ToDateTime(this.txtStartDate.Text.Trim()).ToString()));
}
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
Grid1.RecordCount = tb.Rows.Count;
//tb = GetFilteredTable(Grid1.FilteredData, tb);
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
/// <summary>
/// 改变索引事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
Grid1.PageIndex = e.NewPageIndex;
BindGrid();
}
/// <summary>
/// 分页下拉选择事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
BindGrid();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
{
Grid1.SortDirection = e.SortDirection;
Grid1.SortField = e.SortField;
BindGrid();
}
/// <summary>
/// 关闭弹出窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Window1_Close(object sender, EventArgs e)
{
BindGrid();
}
#endregion
#region
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TextBox_TextChanged(object sender, EventArgs e)
{
this.BindGrid();
}
#endregion
#region
/// <summary>
/// 双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
{
this.EditData();
}
/// <summary>
/// 右键编辑事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuEdit_Click(object sender, EventArgs e)
{
this.EditData();
}
/// <summary>
/// 编辑数据方法
/// </summary>
private void EditData()
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning);
return;
}
string id = Grid1.SelectedRowID;
var result = PoleUnitService.Detail(id);
if (result != null)
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("PoleUnitEdit.aspx?Id={0}", id, "编辑 - ")));
}
}
#endregion
#region
/// <summary>
/// 右键删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuDelete_Click(object sender, EventArgs e)
{
if (Grid1.SelectedRowIndexArray.Length > 0)
{
foreach (int rowIndex in Grid1.SelectedRowIndexArray)
{
string rowID = Grid1.DataKeys[rowIndex][0].ToString();
var result = PoleUnitService.Detail(rowID);
if (result != null)
{
PoleUnitService.Delete(rowID);
}
}
this.BindGrid();
ShowNotify("删除数据成功!", MessageBoxIcon.Success);
}
}
#endregion
#region
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
string strSql = @"select a.*,DateAdd(day,-1,DateAdd(month,1,a.CreateMonth)) endDateTime,c.UnitName
,isnull((select count(1) from HSSE_EnvironmentalAward b where b.UnitId=a.UnitId AND b.CreateTime>=a.CreateMonth AND b.CreateTime<=DateAdd(day,-1,DateAdd(month,1,a.CreateMonth))),0) CellCount
from HSSE_PoleUnit a
left join Base_Unit c on a.Unitid=c.UnitId where 1=1 ";
List<SqlParameter> listStr = new List<SqlParameter>();
if (!string.IsNullOrEmpty(this.txtStartDate.Text.Trim()))
{
strSql += " AND a.CreateMonth = @CreateMonth";
listStr.Add(new SqlParameter("@CreateMonth", Convert.ToDateTime(this.txtStartDate.Text.Trim()).ToString()));
}
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
if (tb.Rows.Count <= 0)
{
ShowNotify("没有要导出的数据,请检查!", MessageBoxIcon.Warning);
return;
}
//某单位最多奖项个数
int CellCountMax = tb.AsEnumerable().Select(t => t.Field<int>("CellCount")).Max();
int CellCount = CellCountMax;
NPOIExcel excel = new NPOIExcel();
int indexRow = 0;
excel.SetColumnWidth(0, 10);
excel.SetColumnWidth(1, 40);
excel.SetColumnWidth(2, 70);
excel.SetColumnWidth(3, 20);
excel.SetColumnWidth(4, 20);
for (int i = 0; i < CellCount; i++)
{
excel.SetColumnWidth(5 + i, 20);
}
//一个奖项或者零个奖项
if (CellCount <= 1)
{
excel.SetColumnWidth(5, 40);
}
excel.SetColumnWidth(5 + CellCount, 40);
excel.SetColumnWidth(6 + CellCount, 40);
excel.SetColumnWidth(7 + CellCount, 40);
//样式
var styleHead = excel.CreateCellStyle();
styleHead.VerticalAlignment = VerticalAlignment.Center;
styleHead.Alignment = HorizontalAlignment.Center;
IFont font = excel.CreateFont();
font.IsBold = true;
styleHead.SetFont(font);
styleHead.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleHead.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
styleHead.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleHead.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
//样式-序号
var styleXh = excel.CreateCellStyle();
styleXh.VerticalAlignment = VerticalAlignment.Center;
styleXh.Alignment = HorizontalAlignment.Center;
styleXh.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleXh.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
styleXh.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleXh.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
//样式
var styleData = excel.CreateCellStyle();
styleData.VerticalAlignment = VerticalAlignment.Center;
styleData.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleData.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
styleData.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleData.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
#region
excel.SetValue(indexRow, 0, "序号");
excel.SetStyle(indexRow, 0, styleHead);
excel.SetValue(indexRow, 1, "单位名称");
excel.SetStyle(indexRow, 1, styleHead);
excel.SetValue(indexRow, 2, "(各子(分)公司机关安全检查平均得分*50%+所属项目安全检查平均得分*50%)*70%");
excel.SetStyle(indexRow, 2, styleHead);
excel.SetValue(indexRow, 3, "日常管理得分*20%");
excel.SetStyle(indexRow, 3, styleHead);
excel.SetValue(indexRow, 4, "先进事迹材料得分*10%");
excel.SetStyle(indexRow, 4, styleHead);
for (int i = 0; i < CellCount; i++)
excel.SetStyle(indexRow, 5 + i, styleHead);
//合并单元格
if (CellCount > 1)
excel.MergeCells(indexRow, 5, indexRow, 5 + CellCountMax - 1);
excel.SetValue(indexRow, 5, "安全生产、生态环保、质量获奖处罚加减分");
excel.SetStyle(indexRow, 5, styleHead);
//这里加一是为了后面列可以往后移动一位,不然就把奖项列覆盖了
if (CellCount == 0)
CellCount = 1;
excel.SetValue(indexRow, 5 + CellCount, "合计得分");
excel.SetStyle(indexRow, 5 + CellCount, styleHead);
excel.SetValue(indexRow, 6 + CellCount, "编制月份");
excel.SetStyle(indexRow, 6 + CellCount, styleHead);
excel.SetRowHeight(indexRow, 15);
#endregion
#region
int rowCount = 1;
int rowIndex = 1;
foreach (DataRow dr in tb.Rows)
{
//序号
excel.SetStyle(indexRow + rowCount, 0, styleXh);
excel.SetStyle(indexRow + rowCount + 1, 0, styleXh);
excel.MergeCells(indexRow + rowCount, 0, indexRow + rowCount + 1, 0);
excel.SetValue(indexRow + rowCount, 0, rowIndex);
//单位名称
excel.SetStyle(indexRow + rowCount, 1, styleData);
excel.SetStyle(indexRow + rowCount + 1, 1, styleData);
excel.MergeCells(indexRow + rowCount, 1, indexRow + rowCount + 1, 1);
excel.SetValue(indexRow + rowCount, 1, dr["UnitName"].ToString());
//(各子(分)公司机关安全检查平均得分*50%+所属项目安全检查平均得分*50%)*70%
excel.SetStyle(indexRow + rowCount, 2, styleData);
excel.SetStyle(indexRow + rowCount + 1, 2, styleData);
excel.MergeCells(indexRow + rowCount, 2, indexRow + rowCount + 1, 2);
excel.SetValue(indexRow + rowCount, 2, dr["Score1"].ToString());
//日常管理得分*20%
excel.SetStyle(indexRow + rowCount, 3, styleData);
excel.SetStyle(indexRow + rowCount + 1, 3, styleData);
excel.MergeCells(indexRow + rowCount, 3, indexRow + rowCount + 1, 3);
excel.SetValue(indexRow + rowCount, 3, dr["Score2"].ToString());
excel.SetStyle(indexRow + rowCount, 3, styleData);
//先进事迹材料得分*10%
excel.SetStyle(indexRow + rowCount, 4, styleData);
excel.SetStyle(indexRow + rowCount + 1, 4, styleData);
excel.MergeCells(indexRow + rowCount, 4, indexRow + rowCount + 1, 4);
excel.SetValue(indexRow + rowCount, 4, dr["Score3"].ToString());
excel.SetStyle(indexRow + rowCount, 4, styleData);
//奖项
DateTime sTime = Convert.ToDateTime(dr["CreateMonth"].ToString());
DateTime eTime = Convert.ToDateTime(dr["endDateTime"].ToString());
var environmentalAwards = Funs.DB.HSSE_EnvironmentalAward.Where(x => x.UnitId == dr["UnitId"].ToString() && x.CreateTime >= sTime && x.CreateTime <= eTime).ToList();
for (int i = 0; i < CellCountMax; i++)
{
if (i >= environmentalAwards.Count)
{
excel.SetStyle(indexRow + rowCount, 5 + i, styleData);
excel.SetStyle(indexRow + rowCount + 1, 5 + i, styleData);
continue;
}
excel.SetStyle(indexRow + rowCount, 5 + i, styleData);
excel.SetStyle(indexRow + rowCount + 1, 5 + i, styleData);
excel.SetValue(indexRow + rowCount, 5 + i, environmentalAwards[i].AwardsName);
excel.SetValue(indexRow + rowCount + 1, 5 + i, environmentalAwards[i].Scores);
}
//无奖项 处理
if (environmentalAwards.Count == 0)
{
if(CellCountMax>1)
excel.MergeCells(indexRow + rowCount, 5, indexRow + rowCount + 1, 5 + CellCountMax - 1);
else
excel.MergeCells(indexRow + rowCount, 5, indexRow + rowCount + 1, 5);
excel.SetValue(indexRow + rowCount, 5, "无奖项");
excel.SetStyle(indexRow + rowCount, 5, styleData);
}
//合计得分
excel.SetStyle(indexRow + rowCount, 5 + CellCount, styleData);
excel.SetStyle(indexRow + rowCount + 1, 5 + CellCount, styleData);
excel.MergeCells(indexRow + rowCount, 5 + CellCount, indexRow + rowCount + 1, 5 + CellCount);
excel.SetValue(indexRow + rowCount, 5 + CellCount, dr["AllScore"].ToString());
//编制月份
excel.SetStyle(indexRow + rowCount, 6 + CellCount, styleData);
excel.SetStyle(indexRow + rowCount + 1, 6 + CellCount, styleData);
excel.MergeCells(indexRow + rowCount, 6 + CellCount, indexRow + rowCount + 1, 6 + CellCount);
excel.SetValue(indexRow + rowCount, 6 + CellCount, sTime.ToString("yyyy-MM-dd"));
rowCount = rowCount + 2;
rowIndex++;
}
#endregion
string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
//导出文件
string filePath = rootPath + "\\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string initTemplatePath = Const.ExcelUrl + Funs.GetNewFileName() + ".xls";
string uploadfilepath = Server.MapPath("~/") + initTemplatePath;
string fileName = Path.GetFileName(initTemplatePath);
excel.Save(uploadfilepath);
FileInfo filet = new FileInfo(uploadfilepath);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode($"安全质量环保管理标杆企业评选推荐表{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls"));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", filet.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(filet.FullName);
// 停止页面的执行
Response.End();
}
#endregion
protected string getScore(object unitId, object CreateMonth)
{
var msg = "";
if (unitId != null && CreateMonth != null)
{
var sTime = Convert.ToDateTime(CreateMonth);
var eTime = sTime.AddMonths(1);
var uid = unitId.ToString();
var list = Funs.DB.HSSE_EnvironmentalAward.Where(x => x.UnitId == uid && x.CreateTime >= sTime && x.CreateTime <= eTime).ToList();
if (list.Count > 0)
{
foreach (var item in list)
{
msg += "奖项名称:" + item.AwardsName + ",加分值:" + item.Scores.ToString() + ";";
}
if (!string.IsNullOrEmpty(msg))
{
msg = msg.Substring(0, msg.Length - 1) + "。";
}
}
else
{
msg = "无奖项";
}
}
return msg;
}
}
}