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
{
//定义变量
///
/// 上传预设的虚拟路径
///
private string initPath = Const.ExcelUrl;
///
/// 错误集合
///
public static string errorInfos = string.Empty;
#region 加载
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
// 绑定表格
this.BindGrid();
}
}
#endregion
#region 绑定数据
///
/// 绑定数据
///
private void BindGrid()
{
string strSql = @"SELECT * FROM dbo.Base_MaterialCoode WHERE 1=1 ";
List listStr = new List();
if (!string.IsNullOrEmpty(this.txtCoode.Text.Trim()))
{
strSql += " AND Coode LIKE @Coode";
listStr.Add(new SqlParameter("@Coode", "%" + this.txtCoode.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();
}
///
/// 改变索引事件
///
///
///
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
BindGrid();
}
///
/// 分页下拉选择事件
///
///
///
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
BindGrid();
}
///
/// 排序
///
///
///
protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
{
BindGrid();
}
///
/// 关闭弹出窗口
///
///
///
protected void Window1_Close(object sender, EventArgs e)
{
BindGrid();
}
#endregion
#region 增加按钮事件
///
/// 增加按钮事件
///
///
///
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 编辑
///
/// 双击事件
///
///
///
protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
{
this.EditData();
}
///
/// 右键编辑事件
///
///
///
protected void btnMenuEdit_Click(object sender, EventArgs e)
{
this.EditData();
}
///
/// 编辑数据方法
///
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 删除
///
/// 右键删除事件
///
///
///
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 判断是否可删除
///
/// 判断是否可以删除
///
///
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 查询
///
/// 查询
///
///
///
protected void btnQuery_Click(object sender, EventArgs e)
{
this.BindGrid();
}
#endregion
#region 查看按钮
///
/// 查看按钮
///
///
///
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 coodeList = new List();
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对应的炉批号已存在!";
}
}
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) + "行,数量格式不正确!";
}
}
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) + "行,是否启用只能录入是或否!";
}
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 模板下载
///
/// 模板下载
///
///
///
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")));
}
///
/// 下载导入模板
///
///
///
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 获取按钮权限
///
/// 获取按钮权限
///
///
///
private bool GetButtonPower(string button)
{
return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.MaterialCoodeMenuId, button);
}
#endregion
}
}