1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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 方法
        /// <summary>
        /// 初始化绑定页面
        /// </summary>
        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<SysInquiry_BrandInfo> 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;
                }
            }
        }
 
        ///// <summary>
        ///// 绑定默认品牌列表
        ///// </summary>
        //private void BindddlDefaultBrand(IEnumerable<SysInquiry_BrandInfo> brandList)
        //{
        //    this.ddlDefaultBrand.DataSource = brandList;
        //    this.ddlDefaultBrand.DataTextField = "BrandName";
        //    this.ddlDefaultBrand.DataValueField = "KeyId";
        //    this.ddlDefaultBrand.DataBind();
        //}
        ///// <summary>
        ///// 绑定品牌列表
        ///// </summary>
        //private void BindBrandList()
        //{
        //    IEnumerable<SysInquiry_BrandInfo> brandList = _brandInfoBLL.GetBrandList();
        //    this.cbkBrandList.DataSource = brandList;
        //    this.cbkBrandList.DataTextField = "BrandName";
        //    this.cbkBrandList.DataValueField = "KeyId";
        //    this.cbkBrandList.DataBind();
        //}
 
        /// <summary>
        /// 绑定纸张类别
        /// </summary>
        private void BindPaperType()
        {
            IEnumerable<SysInquiry_PaperType> 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<SysInquiry_BrandListByPaper> brandList = new List<SysInquiry_BrandListByPaper>();
            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<SysInquiry_PaperInfo> modelList = new List<SysInquiry_PaperInfo>();
 
                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<SysInquiry_PaperInfo> modelList = new List<SysInquiry_PaperInfo>();
                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
 
       
 
       
    }
}