using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using BLL;
using Newtonsoft.Json.Linq;
namespace FineUIPro.Web.HJGL.Match
{
public partial class InStorageEdit : PageBase
{
///
/// 主键
///
public string InStorageId
{
get
{
return (string)ViewState["InStorageId"];
}
set
{
ViewState["InStorageId"] = value;
}
}
private bool AppendToEnd = true;
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.btnClose.OnClientClick = ActiveWindow.GetHideReference();
this.InStorageId = Request.Params["InStorageId"];
///单位
this.drpUnitId.DataTextField = "UnitName";
this.drpUnitId.DataValueField = "UnitId";
this.drpUnitId.DataSource = BLL.Base_UnitService.GetUnitCodeByProjectIdList(this.CurrUser.LoginProjectId);
this.drpUnitId.DataBind();
var inStorage = BLL.HJGL_Match_InStorageService.GetHJGL_Match_InStorageByInStorageId(this.InStorageId);
if (inStorage != null)
{
this.txtInStorageCode.Text = inStorage.InStorageCode;
this.drpUnitId.SelectedValue = inStorage.UnitId;
this.txtInTime.Text = string.Format("{0:yyyy-MM-dd}", inStorage.InTime);
}
else
{
this.drpUnitId.SelectedValue = this.CurrUser.UnitId;
this.txtInTime.Text = string.Format("{0:yyyy-MM-dd}", System.DateTime.Now);
}
// 删除选中单元格的客户端脚本
string deleteScript = GetDeleteScript();
// 新增数据初始值
JObject defaultObj = new JObject();
defaultObj.Add("Code", "");
defaultObj.Add("Name", "");
defaultObj.Add("Standard", "");
defaultObj.Add("UNIT", "");
defaultObj.Add("SteelName", "");
defaultObj.Add("InCount", "1");
defaultObj.Add("Delete", String.Format("
", deleteScript, IconHelper.GetResolvedIconUrl(Icon.Delete)));
// 在第一行新增一条数据
btnNew.OnClientClick = Grid1.GetAddNewRecordReference(defaultObj, AppendToEnd);
// 删除选中行按钮
btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("请至少选择一项!") + deleteScript;
// 绑定表格
BindGrid();
}
}
///
/// 绑定GV
///
private void BindGrid()
{
string strSql = @"SELECT InStorageItemId,InStorageId,Item.MaterialId,InCount,Material.Code,Material.Name,Material.Standard,Material.SteelName,Material.UNIT "
+ @" FROM HJGL_Match_InStorageItem AS Item"
+ @" LEFT JOIN HJGL_Match_Material AS Material ON Item.MaterialId=Material.MaterialId "
+ @" WHERE InStorageId='" + this.InStorageId + "'";
List listStr = new List();
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
// 2.获取当前分页数据
//var table = this.GetPagedDataTable(Grid1, tb1);
Grid1.RecordCount = tb.Rows.Count;
tb = GetFilteredTable(Grid1.FilteredData, tb);
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
string strSql2 = @"SELECT MaterialId,ProjectId,Code,Name,Standard,UNIT,SteelName "
+ @" FROM dbo.HJGL_Match_Material ";
List listStr2 = new List();
SqlParameter[] parameter2 = listStr2.ToArray();
DataTable tb2 = SQLHelper.GetDataTableRunText(strSql2, parameter2);
// 2.获取当前分页数据
Grid2.RecordCount = tb2.Rows.Count;
tb2 = GetFilteredTable(Grid2.FilteredData, tb2);
var table2 = this.GetPagedDataTable(Grid2, tb2);
Grid2.DataSource = table2;
Grid2.DataBind();
}
protected void Grid1_PreDataBound(object sender, EventArgs e)
{
// 设置LinkButtonField的点击客户端事件
LinkButtonField deleteField = Grid1.FindColumn("Delete") as LinkButtonField;
deleteField.OnClientClick = GetDeleteScript();
}
// 删除选中行的脚本
private string GetDeleteScript()
{
return Confirm.GetShowReference("删除选中行?", String.Empty, MessageBoxIcon.Question, Grid1.GetDeleteSelectedRowsReference(), String.Empty);
}
private DataRow CreateNewData(DataTable table, Dictionary newAddedData)
{
DataRow rowData = table.NewRow();
// 设置行ID(模拟数据库的自增长列)
rowData["InStorageItemId"] = BLL.SQLHelper.GetNewID(typeof(Model.HJGL_Match_InStorageItem));
UpdateDataRow(newAddedData, rowData);
return rowData;
}
///
///
///
///
///
private void UpdateDataRow(Dictionary rowDict, DataRow rowData)
{
UpdateDataRow("Code", rowDict, rowData);
UpdateDataRow("Name", rowDict, rowData);
UpdateDataRow("Standard", rowDict, rowData);
UpdateDataRow("UNIT", rowDict, rowData);
UpdateDataRow("SteelName", rowDict, rowData);
}
///
///
///
///
///
///
private void UpdateDataRow(string columnName, Dictionary rowDict, DataRow rowData)
{
if (rowDict.ContainsKey(columnName))
{
rowData[columnName] = rowDict[columnName];
}
}
#region 提交按钮事件
///
/// 提交按钮
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
this.SaveData(BLL.Const.BtnSave);
PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
}
///
/// 提交按钮
///
///
///
protected void btnSubmit_Click(object sender, EventArgs e)
{
this.SaveData(BLL.Const.BtnSubmit);
PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
}
///
/// 提交数据方法
///
private void SaveData(string type)
{
Model.HJGL_Match_InStorage newInStorage = new Model.HJGL_Match_InStorage();
newInStorage.InStorageCode = this.txtInStorageCode.Text.Trim();
newInStorage.ProjectId = this.CurrUser.LoginProjectId;
if (!string.IsNullOrEmpty(this.drpUnitId.SelectedValue))
{
newInStorage.UnitId = this.drpUnitId.SelectedValue;
}
newInStorage.InTime = Funs.GetNewDateTime(this.txtInTime.Text.Trim());
newInStorage.CompileMan = this.CurrUser.UserId;
newInStorage.State = "0";
if (type == BLL.Const.BtnSubmit)
{
newInStorage.State = "1";
}
if (string.IsNullOrEmpty(this.InStorageId))
{
this.InStorageId = newInStorage.InStorageId = SQLHelper.GetNewID(typeof(Model.HJGL_Match_InStorage));
BLL.HJGL_Match_InStorageService.AddHJGL_Match_InStorage(newInStorage);
BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "添加管道组成件信息");
}
else
{
newInStorage.InStorageId = this.InStorageId;
BLL.HJGL_Match_InStorageService.UpdateHJGL_Match_InStorage(newInStorage);
BLL.HJGL_Match_InStorageItemService.DelInStorageItemByInStorageId(this.InStorageId);
BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "添加管道组成件信息");
}
foreach (JObject mergedRow in Grid1.GetMergedData())
{
JObject values = mergedRow.Value("values");
if (values["Code"].ToString() != "")
{
string code = values.Value("Code");
var material = BLL.HJGL_Match_MaterialService.GetMaterialByMaterialCode(code);
if (material != null)
{
Model.HJGL_Match_InStorageItem newItem = new Model.HJGL_Match_InStorageItem();
newItem.InStorageItemId = BLL.SQLHelper.GetNewID(typeof(Model.HJGL_Match_InStorageItem));
newItem.InStorageId = this.InStorageId;
newItem.MaterialId = material.MaterialId;
if (values["InCount"].ToString() != "")
{
newItem.InCount = Funs.GetNewDecimalOrZero(values.Value("InCount"));
}
var updateItem = BLL.HJGL_Match_InStorageItemService.GetInHJGL_Match_InStorageItemByInStorageIdMaterialId(this.InStorageId, newItem.MaterialId);
if (updateItem != null)
{
updateItem.InCount += (newItem.InCount.HasValue ? newItem.InCount : 0);
BLL.HJGL_Match_InStorageItemService.UpdateInStorageItem(updateItem);
}
else
{
BLL.HJGL_Match_InStorageItemService.AddInStorageItem(newItem);
}
if (type == BLL.Const.BtnSubmit)
{
Model.HJGL_Match_Storage storage = BLL.HJGL_Match_StorageService.GetHJGL_Match_StorageByMaterialId(material.MaterialId);
if (storage == null)
{
Model.HJGL_Match_Storage newStorage = new Model.HJGL_Match_Storage();
newStorage.StorageId = SQLHelper.GetNewID(typeof(Model.HJGL_Match_Storage));
newStorage.ProjectId = this.CurrUser.LoginProjectId;
newStorage.MaterialId = material.MaterialId;
newStorage.InCount = (newItem.InCount.HasValue ? newItem.InCount : 0);
BLL.HJGL_Match_StorageService.AddHJGL_Match_Storage(newStorage);
}
else
{
storage.InCount += (newItem.InCount.HasValue ? newItem.InCount : 0); ;
BLL.HJGL_Match_StorageService.UpdateHJGL_Match_Storage(storage);
}
}
}
}
}
}
#endregion
}
}