Basf_TCC11/HJGL/FineUIPro.Web/WeldingProcess/WeldingManage/MaterialCoode.aspx.cs

473 lines
17 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Model;
using System.IO;
using System.Drawing;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.WeldingProcess.WeldingManage
{
public partial class MaterialCoode : PageBase
{
//定义变量
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 错误集合
/// </summary>
public static string errorInfos = string.Empty;
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
// 绑定表格
this.BindGrid();
}
}
#endregion
#region
/// <summary>
/// 绑定数据
/// </summary>
private void BindGrid()
{
string strSql = @"SELECT * FROM dbo.Base_MaterialCoode WHERE 1=1 ";
List<SqlParameter> listStr = new List<SqlParameter>();
if (!string.IsNullOrEmpty(this.txtCoode.Text.Trim()))
{
strSql += " AND Coode LIKE @Coode";
listStr.Add(new SqlParameter("@Coode", "%" + this.txtCoode.Text.Trim() + "%"));
}
if (!string.IsNullOrEmpty(this.txtHeartNo.Text.Trim()))
{
strSql += " AND HeartNo LIKE @HeartNo";
listStr.Add(new SqlParameter("@HeartNo", "%" + this.txtHeartNo.Text.Trim() + "%"));
}
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)
{
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)
{
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 btnNew_Click(object sender, EventArgs e)
{
if (GetButtonPower(Const.BtnAdd))
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("MaterialCoodeEdit.aspx", "新增 - ")));
}
else
{
Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
return;
}
}
#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(Resources.Lan.SelectLeastOneRecord, MessageBoxIcon.Warning);
return;
}
////双击事件 编辑权限有:编辑页面,无:查看页面 或者状态是完成时查看页面
if (GetButtonPower(Const.BtnModify))
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("MaterialCoodeEdit.aspx?MaterialCoodeId={0}&flag=edit", Grid1.SelectedRowID, "编辑 - ")));
}
else if (GetButtonPower(Const.BtnSee))
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("MaterialCoodeEdit.aspx?MaterialCoodeId={0}&flag=view", Grid1.SelectedRowID, "查看 - ")));
}
else
{
Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
return;
}
}
#endregion
#region
/// <summary>
/// 右键删除事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnMenuDelete_Click(object sender, EventArgs e)
{
if (GetButtonPower(Const.BtnDelete))
{
if (Grid1.SelectedRowIndexArray.Length > 0)
{
string strShowNotify = string.Empty;
foreach (int rowIndex in Grid1.SelectedRowIndexArray)
{
string rowID = Grid1.DataKeys[rowIndex][0].ToString();
var getCoode = BLL.MaterialCoodeService.GetMaterialCoode(rowID,this.CurrUser.LoginProjectId);
if (getCoode != null)
{
string cont = judgementDelete(rowID);
if (string.IsNullOrEmpty(cont))
{
BLL.MaterialCoodeService.DeleteMaterialCoode(rowID);
BLL.Sys_LogService.AddLog(Const.System_2, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.MediumMenuId, Const.BtnDelete, rowID);
}
else
{
strShowNotify += Resources.Lan.MediumDefinition + "" + getCoode.Coode + cont;
}
}
}
BindGrid();
if (!string.IsNullOrEmpty(strShowNotify))
{
Alert.ShowInTop(strShowNotify, MessageBoxIcon.Warning);
}
else
{
ShowNotify(Resources.Lan.DeletedSuccessfully, MessageBoxIcon.Success);
}
}
}
else
{
Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
return;
}
}
#region
/// <summary>
/// 判断是否可以删除
/// </summary>
/// <returns></returns>
private string judgementDelete(string id)
{
string content = string.Empty;
//if (Funs.DB.Pipeline_WeldJoint.FirstOrDefault(x => x.Coode1 == id) != null)
//{
// content += "已在【管线信息】中使用,不能删除!";
//}
return content;
}
#endregion
#endregion
#region
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnQuery_Click(object sender, EventArgs e)
{
this.BindGrid();
}
#endregion
#region
/// <summary>
/// 查看按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnView_Click(object sender, EventArgs e)
{
PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("MaterialCoodeEdit.aspx?MaterialCoodeId={0}&flag=view", Grid1.SelectedRowID, "查看 - ")));
}
#endregion
protected void btnImport_Click(object sender, EventArgs e)
{
if (this.CoodeUrl.HasFile == false)
{
ShowNotify("请选择Excel文件!", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(this.CoodeUrl.FileName).ToString().Trim().ToLower();
if (IsXls != ".xls" && IsXls != ".xlsx")
{
ShowNotify("只能选择Excel文件!", MessageBoxIcon.Warning);
return;
}
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
if (!Directory.Exists(initFullPath))
{
Directory.CreateDirectory(initFullPath);
}
//指定上传文件名称
this.hidFileName.Text = BLL.Funs.GetNewFileName() + IsXls;
//上传文件路径
string filePath = initFullPath + this.hidFileName.Text;
//文件上传服务器
this.CoodeUrl.PostedFile.SaveAs(filePath);
//文件上传服务器后的名称
string fileName = rootPath + initPath + this.hidFileName.Text;
//读取Excel
DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true);
errorInfos = string.Empty;
//验证Excel读取是否有误
if (!string.IsNullOrEmpty(errorInfos))
{
ShowNotify(errorInfos, MessageBoxIcon.Warning);
return;
}
string message = string.Empty;
List<Model.Base_MaterialCoode> coodeList = new List<Model.Base_MaterialCoode>();
var coodes = from x in Funs.DB.Base_MaterialCoode where x.ProjectId == this.CurrUser.LoginProjectId select x;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Model.Base_MaterialCoode d = new Model.Base_MaterialCoode();
d.ProjectId = this.CurrUser.LoginProjectId;
d.MaterialCoodeId = SQLHelper.GetNewID(typeof(Model.Base_MaterialCoode));
d.Coode = ds.Tables[0].Rows[i]["Coode"].ToString();
d.HeartNo = ds.Tables[0].Rows[i]["炉批号"].ToString();
if (!string.IsNullOrEmpty(d.Coode) && !string.IsNullOrEmpty(d.HeartNo))
{
var c = from x in coodes where x.Coode == d.Coode && x.HeartNo == d.HeartNo select x;
if (c.Count() > 0)
{
errorInfos += (i + 2) + "行,Coode对应的炉批号已存在!</br>";
}
}
if (ds.Tables[0].Rows[i]["数量"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["数量"].ToString()))
{
int? dd = Funs.GetNewInt(ds.Tables[0].Rows[i]["数量"].ToString());
if (dd != null)
{
d.Amount = Funs.GetNewInt(ds.Tables[0].Rows[i]["数量"].ToString());
}
else
{
errorInfos += (i + 2) + "行,数量格式不正确!</br>";
}
}
if (ds.Tables[0].Rows[i]["是否启用"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["是否启用"].ToString()))
{
string isUser = ds.Tables[0].Rows[i]["是否启用"].ToString();
if (isUser != "是" && isUser != "否")
{
errorInfos += (i + 2) + "行,是否启用只能录入是或否!</br>";
}
else
{
if (isUser == "是")
{
d.IsUse = true;
}
else
{
d.IsUse = false;
}
}
}
else
{
d.IsUse = false;
}
coodeList.Add(d);
}
var coodeGroup = coodeList.GroupBy(x => new { x.Coode, x.HeartNo }).Select(g => new { Coode = g.Key.Coode, HeartNo = g.Key.HeartNo }).Distinct();
foreach (var c in coodeGroup)
{
var coodeNum=from x in coodeList where x.Coode==c.Coode && x.HeartNo==c.HeartNo select x;
if (coodeNum.Count() > 1)
{
errorInfos += "coode码"+c.Coode+"炉批号:"+c.HeartNo+"有重复!";
}
}
//if (coodeList.Count() != coodeGroup.Count())
//{
// errorInfos += "Coode对应的炉批号有重复";
//}
// 数据验证错误,返回
if (!string.IsNullOrEmpty(errorInfos))
{
ShowNotify(errorInfos, MessageBoxIcon.Warning, 10000);
return;
}
else
{
foreach (var item in coodeList)
{
BLL.MaterialCoodeService.AddMaterialCoode(item);
}
ShowNotify("Coode导入成功!", MessageBoxIcon.Success);
this.BindGrid();
}
}
#region
/// <summary>
/// 模板下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDownLoad_Click(object sender, EventArgs e)
{
string rootPath = Server.MapPath("~/");
string uploadfilepath = rootPath + Const.CoodeInTemplateUrl;
string filePath = Const.CoodeInTemplateUrl;
string fileName = Path.GetFileName(filePath);
FileInfo info = new FileInfo(uploadfilepath);
long fileSize = info.Length;
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.ContentType = "excel/plain";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Length", fileSize.ToString().Trim());
Response.TransmitFile(uploadfilepath, 0, fileSize);
Response.End();
//PageContext.RegisterStartupScript(Confirm.GetShowReference("确定要下载焊工资质导入模板?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel")));
}
/// <summary>
/// 下载导入模板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PageManager1_CustomEvent(object sender, CustomEventArgs e)
{
if (e.EventArgument == "Confirm_OK")
{
string rootPath = Server.MapPath("~/");
string uploadfilepath = rootPath + Const.WelderQueTemplateUrl;
string filePath = Const.WelderQueTemplateUrl;
string fileName = Path.GetFileName(filePath);
FileInfo info = new FileInfo(uploadfilepath);
long fileSize = info.Length;
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.ContentType = "excel/plain";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Length", fileSize.ToString().Trim());
Response.TransmitFile(uploadfilepath, 0, fileSize);
Response.End();
}
}
#endregion
#region
/// <summary>
/// 获取按钮权限
/// </summary>
/// <param name="button"></param>
/// <returns></returns>
private bool GetButtonPower(string button)
{
return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.MaterialCoodeMenuId, button);
}
#endregion
}
}