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 BrandInfoEdit : BasePage
{
#region 变量
BrandInfoBLL _brandInfoBLL = new BrandInfoBLL();
PaperTypeBLL _paperTypeBLL = new PaperTypeBLL();
PaperInfoBLL _paperInfoBLL = new PaperInfoBLL();
#endregion
#region 属性
#endregion
#region 方法
///
/// 初始化绑定页面
///
private void BindPage()
{
if (Request["KeyId"] != null)
{
int keyId = Convert.ToInt32(Request["KeyId"]);
if (keyId > 0)
{
SysInquiry_BrandInfo model = _brandInfoBLL.SelectModelByKey(keyId);
if (model.PaperTypeId.HasValue)
{
this.ddlPaperType.SelectedValue = model.PaperTypeId.ToString();
BindPaper(model.PaperTypeId.Value);
}
if(model.PapeId.HasValue)
{
this.ddlPaper.SelectedValue = model.PapeId.ToString();
}
this.ddlDefaultBrand.SelectedValue = model.IsDefaultBrand.ToString();
this.txtBrandName.Value = model.BrandName;
this.ddlStatus.SelectedValue = model.Status.ToString();
if (model.OrderNum.HasValue)
this.txtOrerNum.Value = model.OrderNum.Value.ToString();
ViewState["model"] = model;
}
}
}
///
/// 绑定纸张类别
///
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("请选择", ""));
}
private void BindPaper(int paperTypeId)
{
IEnumerable paperList = _paperInfoBLL.GetPaperListByPaperType(paperTypeId);
this.ddlPaper.DataSource = paperList;
this.ddlPaper.DataTextField = "PaperName";
this.ddlPaper.DataValueField = "PapeId";
this.ddlPaper.DataBind();
this.ddlPaper.Items.Insert(0, new ListItem("请选择", ""));
}
#endregion
#region 事件
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindPaperType();
BindPage();
}
}
protected void ddlPaperType_SelectedIndexChanged(object sender, EventArgs e)
{
int paperPaperType = -1;
if (this.ddlPaperType.SelectedValue.ToString() == string.Empty)
return;
paperPaperType = ddlPaperType.SelectedValue.ToInt32().Value;
IEnumerable paperList = _paperInfoBLL.GetPaperListByPaperType(paperPaperType);
this.ddlPaper.DataSource = paperList;
this.ddlPaper.DataTextField = "PaperName";
this.ddlPaper.DataValueField = "PapeId";
this.ddlPaper.DataBind();
this.ddlPaper.Items.Insert(0, new ListItem("请选择", ""));
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
bool isSuccess = false;
bool isInsert = true;
SysInquiry_BrandInfo model = ViewState["model"] as SysInquiry_BrandInfo;
SysInquiry_BrandListByPaper brandPaper = new SysInquiry_BrandListByPaper();
SysInquiry_PaperInfo paperModel = null;
SysInquiry_PaperInfo orPaperModel = null;
//新增
if (model == null)
{
if (_brandInfoBLL.IsExistsBrandName(this.txtBrandName.Value.Trim(), this.ddlPaper.SelectedValue.ToInt32().Value))
{
JavaScript.MessageBox("保存失败,品牌名称已存在", this);
return;
}
model = new SysInquiry_BrandInfo();
model.BrandName = this.txtBrandName.Value.Trim();
model.Status = Convert.ToInt32(this.ddlStatus.SelectedValue);
model.OrderNum = this.txtOrerNum.Value.ToInt32().Value;
brandPaper.PaperId = this.ddlPaper.SelectedValue.ToInt32().Value;
if (ddlDefaultBrand.SelectedValue == "1")
{
paperModel = new SysInquiry_PaperInfo();
paperModel.PapeId = brandPaper.PaperId;
}
isSuccess = _brandInfoBLL.InsertBrand(model, brandPaper, paperModel);
}
else
{
//修改
isInsert = false;
if (model.BrandName != this.txtBrandName.Value.Trim())
{
if (_brandInfoBLL.IsExistsBrandName(this.txtBrandName.Value.Trim(), this.ddlPaper.SelectedValue.ToInt32().Value))
{
JavaScript.MessageBox("保存失败,品牌名称已存在", this);
return;
}
}
model.BrandName = this.txtBrandName.Value.Trim();
model.Status = Convert.ToInt32(this.ddlStatus.SelectedValue);
model.OrderNum = this.txtOrerNum.Value.ToInt32().Value;
int selPaperId = ddlPaper.SelectedValue.ToInt32().Value;
brandPaper.PaperId = selPaperId;
brandPaper.BrandId = model.KeyId;
if (selPaperId != model.PapeId)
{
if (model.IsDefaultBrand == 1)
{
orPaperModel = new SysInquiry_PaperInfo();
orPaperModel.PapeId = model.PapeId.Value;
orPaperModel.DefaultBrandId = null;
}
}
paperModel = new SysInquiry_PaperInfo();
paperModel.PapeId = selPaperId;
if (ddlDefaultBrand.SelectedValue == "1")
{
paperModel.DefaultBrandId = model.KeyId;
}
else
{
paperModel.DefaultBrandId = null;
}
isSuccess = _brandInfoBLL.UpdateBrand(model, brandPaper, paperModel, orPaperModel);
}
if (isSuccess)
{
JavaScript.MessageBox("保存成功", this);
if (isInsert)
{
this.ddlStatus.SelectedIndex = 0;
this.txtBrandName.Value = string.Empty;
this.txtOrerNum.Value = (model.OrderNum + 1).ToString2();
}
else
{
JavaScript.RefreshDIVOpener(this);
}
}
else
{
JavaScript.MessageBox("保存失败", this);
}
}
protected void ddlPaper_SelectedIndexChanged(object sender, EventArgs e)
{
int paperId = -1;
if (ddlPaper.SelectedValue == string.Empty)
{
return;
}
paperId = ddlPaper.SelectedValue.ToInt32().Value;
int orderNum = _brandInfoBLL.GetOrderNumByMax(paperId);
this.txtOrerNum.Value = orderNum.ToString();
}
#endregion
}
}