using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CY.BLL.Inquiry;
using CY.Model;
using CY.Infrastructure.Common;
namespace CY.WebForm.Pages.sysInquiry
{
public partial class PaperInfoEdit : BasePage
{
#region 变量
PaperInfoBLL _paperInfoBLL = new PaperInfoBLL();
//BrandInfoBLL _brandInfoBLL = new BrandInfoBLL();
PaperTypeBLL _paperTypeBLL = new PaperTypeBLL();
#endregion
#region 属性
#endregion
#region 方法
///
/// 初始化绑定页面
///
private void BindPage()
{
BindPaperType();
//BindddlDefaultBrand();
//BindBrandList();
if (Request["PapeId"] != null)
{
int papeId = Convert.ToInt32(Request["PapeId"]);
if (papeId > 0)
{
SysInquiry_PaperInfo model = _paperInfoBLL.GetPaperModel(papeId);
//IEnumerable brandList = _paperInfoBLL.GetBrandInfoListByPaper(model.PapeId);
//if (brandList != null && brandList.Count() > 0)
//{
// BindddlDefaultBrand(brandList);
// this.ddlDefaultBrand.SelectedValue = model.DefaultBrandId.ToString();
// this.hidDefaultBrandId.Value = model.DefaultBrandId.ToString();
// this.hidSelBrandId.Value = model.DefaultBrandId.ToString();
// foreach (SysInquiry_BrandInfo brand in brandList)
// {
// for (int i = 0; i < cbkBrandList.Items.Count; i++)
// {
// if (cbkBrandList.Items[i].Value == brand.KeyId.ToString())
// {
// cbkBrandList.Items[i].Selected = true;
// }
// }
// }
//}
this.txtPaperName.Value = model.PaperName;
this.txtResultGramWeights.Text = model.GramWeights;
if (model.PaperTypeId.HasValue)
{
this.ddlPaperType.SelectedValue = model.PaperTypeId.Value.ToString();
}
this.ddlStatus.SelectedValue = model.Status.ToString();
if (model.OrderNum.HasValue)
{
this.txtOrerNum.Value = model.OrderNum.Value.ToString();
}
ViewState["model"] = model;
}
}
}
/////
///// 绑定默认品牌列表
/////
//private void BindddlDefaultBrand(IEnumerable brandList)
//{
// this.ddlDefaultBrand.DataSource = brandList;
// this.ddlDefaultBrand.DataTextField = "BrandName";
// this.ddlDefaultBrand.DataValueField = "KeyId";
// this.ddlDefaultBrand.DataBind();
//}
/////
///// 绑定品牌列表
/////
//private void BindBrandList()
//{
// IEnumerable brandList = _brandInfoBLL.GetBrandList();
// this.cbkBrandList.DataSource = brandList;
// this.cbkBrandList.DataTextField = "BrandName";
// this.cbkBrandList.DataValueField = "KeyId";
// this.cbkBrandList.DataBind();
//}
///
/// 绑定纸张类别
///
private void BindPaperType()
{
IEnumerable typeList = _paperTypeBLL.GetPaperTypeList();
this.ddlPaperType.DataSource = typeList;
this.ddlPaperType.DataTextField = "PaperTypeName";
this.ddlPaperType.DataValueField = "KeyId";
this.ddlPaperType.DataBind();
this.ddlPaperType.Items.Insert(0, new ListItem("请选择", ""));
}
#endregion
#region 事件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindPage();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool isSuccess = false;
bool isInsert = true;
SysInquiry_PaperInfo model = ViewState["model"] as SysInquiry_PaperInfo;
IList brandList = new List();
SysInquiry_BrandListByPaper brand = null;
//新增
if (model == null)
{
if (_paperInfoBLL.IsExistspeperName(this.txtPaperName.Value.Trim()))
{
JavaScript.MessageBox("保存失败,纸张名称已存在", this);
return;
}
string gramWeights = this.txtResultGramWeights.Text.ToString();
if (string.IsNullOrEmpty(gramWeights))
{
JavaScript.MessageBox("保存失败,克重不能为空", this);
return;
}
IList modelList = new List();
int newPaperId = _paperInfoBLL.GetNewPaperId();
string[] gramWeightArry=gramWeights.Split('|');
foreach (string gramWeightStr in gramWeightArry)
{
if (string.IsNullOrEmpty(gramWeightStr))
{
continue;
}
model = new SysInquiry_PaperInfo();
model.PapeId = newPaperId;
model.PaperName = this.txtPaperName.Value.Trim();
try
{
model.GramWeight = int.Parse(gramWeightStr);
}
catch
{
JavaScript.MessageBox("保存失败,克重必须是数字", this);
return;
}
model.Status = Convert.ToBoolean(this.ddlStatus.SelectedValue);
//model.DefaultBrandId = Convert.ToInt32(this.hidSelBrandId.Value);
model.PaperTypeId = this.ddlPaperType.SelectedValue.ToInt32();
model.OrderNum = this.txtOrerNum.Value.ToInt32().Value;
model.Operater = this.CurrentUser.ShortName;
model.OperateTime = DateTime.Now;
model.LastUpdateTime = DateTime.Now;
modelList.Add(model);
}
//for (int i = 0; i < cbkBrandList.Items.Count; i++)
//{
// if (cbkBrandList.Items[i].Selected == true)
// {
// brand = new SysInquiry_BrandListByPaper();
// brand.PaperId = model.PapeId;
// brand.BrandId = cbkBrandList.Items[i].Value.ToInt32().Value;
// brandList.Add(brand);
// }
//}
isSuccess = _paperInfoBLL.InsertPaper(modelList, brandList);
}
else
{
//修改
isInsert = false;
if (model.PaperName != this.txtPaperName.Value.Trim())
{
if (_paperInfoBLL.IsExistspeperName(this.txtPaperName.Value.Trim()))
{
JavaScript.MessageBox("保存失败,纸张名称已存在", this);
return;
}
}
string gramWeights = this.txtResultGramWeights.Text.ToString();
if (string.IsNullOrEmpty(gramWeights))
{
JavaScript.MessageBox("保存失败,克重不能为空", this);
return;
}
IList modelList = new List();
SysInquiry_PaperInfo tempModel = null;
string[] gramWeightArry = gramWeights.Split('|');
foreach (string gramWeightStr in gramWeightArry)
{
if (string.IsNullOrEmpty(gramWeightStr))
{
continue;
}
tempModel = new SysInquiry_PaperInfo();
tempModel.PapeId = model.PapeId;
tempModel.PaperName = this.txtPaperName.Value.Trim();
try
{
tempModel.GramWeight = int.Parse(gramWeightStr);
}
catch
{
JavaScript.MessageBox("保存失败,克重必须是数字", this);
return;
}
//tempModel.DefaultBrandId = Convert.ToInt32(this.hidSelBrandId.Value);
tempModel.PaperTypeId = this.ddlPaperType.SelectedValue.ToInt32();
tempModel.OrderNum = this.txtOrerNum.Value.ToInt32().Value;
tempModel.Status = Convert.ToBoolean(this.ddlStatus.SelectedValue);
tempModel.Operater = this.CurrentUser.ShortName;
tempModel.OperateTime = DateTime.Now;
tempModel.LastUpdateTime = DateTime.Now;
modelList.Add(tempModel);
}
//for (int i = 0; i < cbkBrandList.Items.Count; i++)
//{
// if (cbkBrandList.Items[i].Selected == true)
// {
// brand = new SysInquiry_BrandListByPaper();
// brand.PaperId = model.PapeId;
// brand.BrandId = cbkBrandList.Items[i].Value.ToInt32().Value;
// brandList.Add(brand);
// }
//}
isSuccess = _paperInfoBLL.UpdatePaper(modelList,brandList);
}
if (isSuccess)
{
//JavaScript.MessageBox("保存成功", this);
if (isInsert)
{
this.txtPaperName.Value = string.Empty;
this.txtGramWeights.Text = string.Empty;
this.ddlStatus.SelectedIndex = 0;
this.txtOrerNum.Value = (model.OrderNum + 1).ToString2();
}
else
{
JavaScript.RefreshDIVOpener(this);
}
}
else
{
JavaScript.MessageBox("保存失败", this);
}
}
protected void ddlPaperType_SelectedIndexChanged(object sender, EventArgs e)
{
int paperTypeId = -1;
if (ddlPaperType.SelectedValue == string.Empty)
{
return;
}
paperTypeId=ddlPaperType.SelectedValue.ToInt32().Value;
int orderNum = _paperInfoBLL.GetOrderNumByMax(paperTypeId);
this.txtOrerNum.Value = orderNum.ToString();
}
#endregion
}
}