F_MST_0405.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. /*******************************************************************************
  2. * Copyright(c) 2014 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:F_MST_0405.cs
  5. * 2.功能描述:产品类别
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 冯雪 2014/09/11 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.Data;
  13. using System.Text;
  14. using System.Windows.Forms;
  15. using Dongke.IBOSS.PRD.Basics.BaseControls;
  16. using Dongke.IBOSS.PRD.Basics.BaseResources;
  17. using Dongke.IBOSS.PRD.Basics.Library;
  18. using Dongke.IBOSS.PRD.Client.CommonModule;
  19. using Dongke.IBOSS.PRD.Client.DataModels;
  20. using Dongke.IBOSS.PRD.WCF.Proxys;
  21. namespace Dongke.IBOSS.PRD.Client.SystemModule
  22. {
  23. /// <summary>
  24. /// 产品类别
  25. /// </summary>
  26. public partial class F_MST_0405 : FormBase
  27. {
  28. #region 成员变量
  29. // 窗体的单例模式
  30. private static F_MST_0405 _instance;
  31. // 产品类型Treeview改变前的状态
  32. private Hashtable _nodesStatus = new Hashtable();
  33. // 产品类型TreeView选中节点的路径
  34. private string _selectNodeFullPath;
  35. // 产品类型TreeView选中节点的名称
  36. private string _selectNode;
  37. // 产品类型TreeView选中索引
  38. //private int _index;
  39. // 产品类型TreeView选中ID
  40. private int _GoodsTypeID;
  41. // 全部产品类型的集合
  42. private DataTable _dtSourse;
  43. #endregion
  44. #region 属性
  45. /// <summary>
  46. /// 产品类型TreeView改变前的状态
  47. /// </summary>
  48. public Hashtable NodesStatus
  49. {
  50. get
  51. {
  52. return this._nodesStatus;
  53. }
  54. set
  55. {
  56. this._nodesStatus = value;
  57. }
  58. }
  59. /// <summary>
  60. /// 产品类型TreeView选中节点的路径
  61. /// </summary>
  62. public string SelectNodeFullPath
  63. {
  64. get
  65. {
  66. return this._selectNodeFullPath;
  67. }
  68. set
  69. {
  70. this._selectNodeFullPath = value;
  71. }
  72. }
  73. #endregion
  74. #region 构造函数
  75. /// <summary>
  76. /// 窗体的构造函数
  77. /// </summary>
  78. public F_MST_0405()
  79. {
  80. InitializeComponent();
  81. // 窗体标题、按钮文字
  82. this.Text = FormTitles.F_MST_0405;
  83. this.btnSave.Text = ButtonText.BTN_SAVE;
  84. this.btnCancel.Text = ButtonText.BTN_CLOSE;
  85. }
  86. #endregion
  87. #region 单例模式
  88. /// <summary>
  89. /// 单例模式,防止重复创建窗体
  90. /// </summary>
  91. public static F_MST_0405 Instance
  92. {
  93. get
  94. {
  95. if (_instance == null)
  96. {
  97. _instance = new F_MST_0405();
  98. }
  99. return _instance;
  100. }
  101. }
  102. #endregion
  103. #region 事件
  104. /// <summary>
  105. /// 窗体加载时发生
  106. /// </summary>
  107. /// <param name="sender"></param>
  108. /// <param name="e"></param>
  109. private void F_MST_0405_Load(object sender, EventArgs e)
  110. {
  111. try
  112. {
  113. // 加载权限
  114. FormPermissionManager.FormPermissionControl(this.Name, this,
  115. LogInUserInfo.CurrentUser.CurrentUserEntity.UserRightData, LogInUserInfo.CurrentUser.CurrentUserEntity.FunctionData);
  116. // 设置表格不自动创建列
  117. this.dgvGoodsType.AutoGenerateColumns = false;
  118. // 设置保存按钮初始是不可点击
  119. this.btnSave.Enabled = false;
  120. // 获取全部产品类别信息
  121. this.GetAllGoodsTypeData();
  122. // 生成树形产品类型结构
  123. this.CreateGoodsTypeTree();
  124. // 窗体显示的时候鼠标停在第一个子节点
  125. this.tvwType.SelectedNode = tvwType.Nodes[0].Nodes[0];
  126. this.dgvGoodsType.IsSetInputColumnsColor = true;
  127. }
  128. catch (Exception ex)
  129. {
  130. // 对异常进行共通处理
  131. ExceptionManager.HandleEventException(this.ToString(),
  132. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  133. }
  134. }
  135. /// <summary>
  136. /// 窗体关闭后,释放单例资源
  137. /// </summary>
  138. /// <param name="sender"></param>
  139. /// <param name="e"></param>
  140. private void F_MST_0405_FormClosed(object sender, FormClosedEventArgs e)
  141. {
  142. _instance = null;
  143. }
  144. /// <summary>
  145. /// 点击右上角关闭按钮
  146. /// </summary>
  147. /// <param name="sender"></param>
  148. /// <param name="e"></param>
  149. private void F_MST_0405_FormClosing(object sender, FormClosingEventArgs e)
  150. {
  151. try
  152. {
  153. // 关闭的时候需要判断是否有数据变化
  154. if (DataJudge.IsChange((DataTable)this.dgvGoodsType.DataSource))
  155. {
  156. DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text,
  157. MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  158. // 保存改变的数据
  159. if (dialogResult == DialogResult.Yes)
  160. {
  161. DataGridViewRow row = dgvGoodsType.CurrentRow;
  162. if (!row.IsNewRow)
  163. {
  164. if (row.Cells["GoodsTypeCode"].Value == null
  165. || string.IsNullOrEmpty(row.Cells["GoodsTypeCode"].Value + string.Empty))
  166. {
  167. MessageBox.Show(string.Format(Messages.MSG_MST_W0405),
  168. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  169. e.Cancel = true;
  170. this.btnSave.Enabled = false;
  171. return;
  172. }
  173. }
  174. // 异步处理
  175. object result = DoAsync(new BaseAsyncMethod(this.SaveGoodsTypeData));
  176. // 如果保存出错,不关闭窗体
  177. if (Convert.ToInt32(result) > Constant.INT_IS_ZERO)
  178. {
  179. e.Cancel = true;
  180. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "产品类别", "保存"),
  181. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  182. }
  183. else if (Convert.ToInt32(result) == (int)Constant.RETURN_IS_EXIST)
  184. {
  185. e.Cancel = true;
  186. MessageBox.Show(string.Format(Messages.MSG_CMN_W006, "产品类别名称"),
  187. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  188. }
  189. else
  190. {
  191. e.Cancel = true;
  192. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "产品类别", "保存"),
  193. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  194. }
  195. }
  196. else if (dialogResult == DialogResult.Cancel)
  197. {
  198. e.Cancel = true;
  199. }
  200. //选择否回滚
  201. else
  202. {
  203. ((DataTable)this.dgvGoodsType.DataSource).RejectChanges();
  204. }
  205. }
  206. }
  207. catch (Exception ex)
  208. {
  209. // 对异常进行共通处理
  210. ExceptionManager.HandleEventException(this.ToString(),
  211. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  212. }
  213. }
  214. /// <summary>
  215. /// 节点收缩之后,显示关闭图标
  216. /// </summary>
  217. /// <param name="sender"></param>
  218. /// <param name="e"></param>
  219. private void tvwType_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
  220. {
  221. e.Node.SelectedImageIndex = Constant.INT_IS_ZERO;
  222. e.Node.ImageIndex = Constant.INT_IS_ZERO;
  223. }
  224. /// <summary>
  225. /// 节点展开之后,显示打开图标
  226. /// </summary>
  227. /// <param name="sender"></param>
  228. /// <param name="e"></param>
  229. private void tvwType_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  230. {
  231. e.Node.SelectedImageIndex = Constant.INT_IS_TWO;
  232. e.Node.ImageIndex = Constant.INT_IS_TWO;
  233. }
  234. /// <summary>
  235. /// 选中新行时,设置默认值
  236. /// </summary>
  237. /// <param name="sender"></param>
  238. /// <param name="e"></param>
  239. private void dgvGoodsType_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
  240. {
  241. try
  242. {
  243. // 设置有效列为选中状态
  244. e.Row.Cells["ValueFlag"].Value = true;
  245. }
  246. catch (Exception ex)
  247. {
  248. // 对异常进行共通处理
  249. ExceptionManager.HandleEventException(this.ToString(),
  250. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  251. }
  252. }
  253. /// <summary>
  254. /// 选择产品类型TreeView中的节点前判断,如果数据有改变提示保存
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void tvwType_BeforeSelect(object sender, TreeViewCancelEventArgs e)
  259. {
  260. try
  261. {
  262. // 判断数据是否被修改过,修改过需要提示保存消息
  263. if (DataJudge.IsChange((DataTable)this.dgvGoodsType.DataSource))
  264. {
  265. DialogResult dialogResult = MessageBox.Show(Messages.MSG_CMN_Q001, this.Text,
  266. MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  267. if (dialogResult == DialogResult.Yes)
  268. {
  269. int rowCount = (int)DoAsync(new BaseAsyncMethod(SaveGoodsTypeData));
  270. // 保存数据后提示
  271. if (rowCount > Constant.INT_IS_ZERO)
  272. {
  273. // 保存数据成功
  274. GetAllGoodsTypeData();
  275. this._dtSourse.AcceptChanges();
  276. this.btnSave.Enabled = false;
  277. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "产品类别", "保存"),
  278. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  279. // 生成产品类别树形结构
  280. TreeNode selectedNode = this.tvwType.SelectedNode;
  281. // 重新加载窗体数据
  282. this.RefreshData();
  283. this.CreateGoodsTypeTree();
  284. this.tvwType.SelectedNode = selectedNode;
  285. //// 还原保存前产品类别TreeView的状态
  286. this.SetTreeNodesStatus(this.tvwType.Nodes);
  287. }
  288. else
  289. {
  290. // 保存数据失败
  291. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "产品类别", "保存"),
  292. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  293. }
  294. }
  295. else if (dialogResult == DialogResult.Cancel)
  296. {
  297. e.Cancel = true;
  298. }
  299. //选择否回滚
  300. else
  301. {
  302. ((DataTable)this.dgvGoodsType.DataSource).RejectChanges();
  303. }
  304. }
  305. }
  306. catch (Exception ex)
  307. {
  308. // 对异常进行共通处理
  309. ExceptionManager.HandleEventException(this.ToString(),
  310. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  311. }
  312. }
  313. /// <summary>
  314. /// 选择产品类别TreeView中的节点,DataGridView中显示相应的数据
  315. /// </summary>
  316. /// <param name="sender"></param>
  317. /// <param name="e"></param>
  318. private void tvwType_AfterSelect(object sender, TreeViewEventArgs e)
  319. {
  320. try
  321. {
  322. // 刷新DataGridView中的数据
  323. this.RefreshData();
  324. this.dgvGoodsType.Columns["GoodsTypeCode"].ReadOnly = true;
  325. foreach (DataGridViewRow gvrFor in this.dgvGoodsType.Rows)
  326. {
  327. if (gvrFor.IsNewRow == true)
  328. {
  329. break;
  330. }
  331. if (gvrFor.Cells["GoodsTypeCode"].Value.ToString().Length == Constant.INT_IS_THREE)
  332. {
  333. gvrFor.Cells["ValueFlag"].ReadOnly = true;
  334. break;
  335. }
  336. else
  337. {
  338. break;
  339. }
  340. }
  341. this._selectNodeFullPath = this.tvwType.SelectedNode.FullPath.Replace("\\", "→");
  342. this.dgvGoodsType.IsSetInputColumnsColor = true;
  343. }
  344. catch (Exception ex)
  345. {
  346. // 对异常进行共通处理
  347. ExceptionManager.HandleEventException(this.ToString(),
  348. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  349. }
  350. }
  351. /// <summary>
  352. /// 设置保存按钮的可用状态
  353. /// </summary>
  354. /// <param name="sender"></param>
  355. /// <param name="e"></param>
  356. private void dgvGoodsType_CellValidated(object sender, DataGridViewCellEventArgs e)
  357. {
  358. try
  359. {
  360. this.RefreshSaveBtnStatus();
  361. }
  362. catch (Exception ex)
  363. {
  364. // 对异常进行共通处理
  365. ExceptionManager.HandleEventException(this.ToString(),
  366. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  367. }
  368. }
  369. /// <summary>
  370. /// 单元格输入是否重复的校验
  371. /// </summary>
  372. /// <param name="sender"></param>
  373. /// <param name="e"></param>
  374. private void dgvGoodsType_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  375. {
  376. try
  377. {
  378. if (this.ActiveControl != null && "btnClose".Equals(this.ActiveControl.Name))
  379. {
  380. return;
  381. }
  382. DataGridViewRow rowItem = this.dgvGoodsType.Rows[e.RowIndex];
  383. DataGridViewColumn columnItem = this.dgvGoodsType.Columns[e.ColumnIndex];
  384. if (!this.dgvGoodsType.CurrentRow.IsNewRow)
  385. {
  386. object value = rowItem.Cells[columnItem.Name].EditedFormattedValue;
  387. // 产品类型名称
  388. if ("GoodsTypeName".Equals(columnItem.Name))
  389. {
  390. if (value != null || !string.IsNullOrEmpty(value.ToString().Trim()))
  391. {
  392. if (!this.IsUnique(value.ToString(), this.dgvGoodsType.CurrentRow.Index))
  393. {
  394. // 增加单元格的错误消息
  395. this.dgvGoodsType.CurrentRow.ErrorText
  396. = string.Format(Messages.MSG_CMN_W006, "产品类别名称 ");
  397. e.Cancel = true;
  398. return;
  399. }
  400. }
  401. }
  402. else if (columnItem.Name == "ValueFlag")
  403. {
  404. if ((bool)value != true)
  405. {
  406. int isStop = SystemModuleProxy.Service.CheckGoodsTypeToUpdate(rowItem.Cells["GoodsTypeCode"].Value.ToString());
  407. if (isStop > Constant.INT_IS_ZERO)
  408. {
  409. // 增加单元格的错误消息
  410. this.dgvGoodsType.CurrentRow.ErrorText
  411. = "该类别包含子类别或商品,不可进行停用操作!";
  412. e.Cancel = true;
  413. return;
  414. }
  415. }
  416. }
  417. // 清除单元格的错误消息
  418. this.dgvGoodsType.CurrentRow.ErrorText = string.Empty;
  419. }
  420. this.dgvGoodsType.IsSetInputColumnsColor = true;
  421. }
  422. catch (Exception ex)
  423. {
  424. // 对异常进行共通处理
  425. ExceptionManager.HandleEventException(this.ToString(),
  426. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  427. }
  428. }
  429. /// <summary>
  430. /// 设置保存按钮的可用状态
  431. /// </summary>
  432. /// <param name="sender"></param>
  433. /// <param name="e"></param>
  434. private void dgvGoodsType_RowValidated(object sender, DataGridViewCellEventArgs e)
  435. {
  436. try
  437. {
  438. this.RefreshSaveBtnStatus();
  439. }
  440. catch (Exception ex)
  441. {
  442. // 对异常进行共通处理
  443. ExceptionManager.HandleEventException(this.ToString(),
  444. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  445. }
  446. }
  447. /// <summary>
  448. /// 行离开时校验不能为空的项目
  449. /// </summary>
  450. /// <param name="sender"></param>
  451. /// <param name="e"></param>
  452. private void dgvGoodsType_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  453. {
  454. try
  455. {
  456. // 按Esc键时不校验
  457. if (!this.dgvGoodsType.IsCurrentRowDirty)
  458. {
  459. this.dgvGoodsType.IsSetInputColumnsColor = true;
  460. return;
  461. }
  462. DataGridViewRow row = this.dgvGoodsType.CurrentRow;
  463. if (!row.IsNewRow)
  464. {
  465. // 类别名称不能为空
  466. if (row.Cells["GoodsTypeName"].Value == null
  467. || string.IsNullOrEmpty(row.Cells["GoodsTypeName"].Value.ToString().Trim()))
  468. {
  469. // 增加单元格的错误消息
  470. this.dgvGoodsType.CurrentRow.ErrorText
  471. = string.Format(Messages.MSG_CMN_W005, "产品类别名称");
  472. e.Cancel = true;
  473. return;
  474. }
  475. // 过滤多余空格
  476. row.Cells["GoodsTypeName"].Value = row.Cells["GoodsTypeName"].Value.ToString().Trim();
  477. row.Cells["Remarks"].Value = row.Cells["Remarks"].Value.ToString().Trim();
  478. string BoolValueFlag = row.Cells["ValueFlag"].Value.ToString();
  479. if (BoolValueFlag == "True")
  480. {
  481. row.Cells["ValueFlag"].Value = '1';
  482. }
  483. else if (BoolValueFlag == "False")
  484. {
  485. row.Cells["ValueFlag"].Value = '0';
  486. }
  487. // 清除单元格的错误消息
  488. this.dgvGoodsType.CurrentRow.ErrorText = string.Empty;
  489. }
  490. }
  491. catch (Exception ex)
  492. {
  493. // 对异常进行共通处理
  494. ExceptionManager.HandleEventException(this.ToString(),
  495. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  496. }
  497. }
  498. /// <summary>
  499. /// 显示停用记录事件
  500. /// </summary>
  501. /// <param name="sender"></param>
  502. /// <param name="e"></param>
  503. private void chkDisplayDisabledData_CheckedChanged(object sender, EventArgs e)
  504. {
  505. try
  506. {
  507. this.RefreshData();
  508. this.dgvGoodsType.IsSetInputColumnsColor = true;
  509. }
  510. catch (Exception ex)
  511. {
  512. // 对异常进行共通处理
  513. ExceptionManager.HandleEventException(this.ToString(),
  514. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  515. }
  516. }
  517. /// <summary>
  518. /// 保存产品类型数据
  519. /// </summary>
  520. /// <param name="sender"></param>
  521. /// <param name="e"></param>
  522. private void btnSave_Click(object sender, EventArgs e)
  523. {
  524. try
  525. {
  526. int results = Conservation();
  527. if (results == Constant.INT_IS_ONE)
  528. {
  529. MessageBox.Show(string.Format(Messages.MSG_MST_W0405),
  530. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  531. this.btnSave.Enabled = false;
  532. return;
  533. }
  534. this.btnSave.Enabled = false;
  535. this.btnCancel.Enabled = false;
  536. int result = (int)DoAsync(new BaseAsyncMethod(this.SaveGoodsTypeData));
  537. this.btnSave.Enabled = true;
  538. this.btnCancel.Enabled = true;
  539. // 保存成功 2表示有数据被修改 3表示没有数据被修改
  540. if (result.Equals(Constant.INT_IS_TWO))
  541. {
  542. GetAllGoodsTypeData();
  543. this._dtSourse.AcceptChanges();
  544. this.btnSave.Enabled = false;
  545. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "产品类别管理", "保存"),
  546. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  547. }
  548. else if (result.Equals(Constant.INT_IS_FOUR))
  549. {
  550. MessageBox.Show(string.Format(Messages.MSG_CMN_X001),
  551. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  552. ((DataTable)this.dgvGoodsType.DataSource).RejectChanges();
  553. }
  554. else
  555. {
  556. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "产品类别管理", "保存"),
  557. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  558. return;
  559. }
  560. // 重新加载窗体数据
  561. this.RefreshData();
  562. this.CreateGoodsTypeTree();
  563. }
  564. catch (Exception ex)
  565. {
  566. this.btnSave.Enabled = true;
  567. this.btnCancel.Enabled = true;
  568. // 对异常进行共通处理
  569. ExceptionManager.HandleEventException(this.ToString(),
  570. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  571. }
  572. }
  573. /// <summary>
  574. /// 关闭窗体
  575. /// </summary>
  576. /// <param name="sender"></param>
  577. /// <param name="e"></param>
  578. private void btnCancel_Click(object sender, EventArgs e)
  579. {
  580. this.Close();
  581. }
  582. #endregion
  583. #region 私有方法
  584. /// <summary>
  585. /// 取得产品类别数据
  586. /// </summary>
  587. /// <returns></returns>
  588. private DataSet GetGoodsTypeData()
  589. {
  590. try
  591. {
  592. return SystemModuleProxy.Service.GetGoodsTypeData(0);
  593. }
  594. catch (Exception ex)
  595. {
  596. throw ex;
  597. }
  598. }
  599. /// <summary>
  600. /// 根据取得的产品类型数据,生成树形结构
  601. /// </summary>
  602. private void CreateGoodsTypeTree()
  603. {
  604. try
  605. {
  606. // 没有产品类型数据时,直接返回
  607. if (this._dtSourse == null || this._dtSourse.Rows.Count < Constant.INT_IS_ONE)
  608. {
  609. return;
  610. }
  611. // 将所有节点清除
  612. this.tvwType.Nodes.Clear();
  613. TreeNode goodsTypeNode = null;
  614. DataTable dtTree = this._dtSourse.Copy();
  615. DataRow[] goodsTypeRows = dtTree.Select(" LEN(GoodsTypeCode) = 3");
  616. // 显示子节点
  617. InitTreeView(dtTree, goodsTypeRows, goodsTypeNode);
  618. if (this.tvwType.SelectedNode == null)
  619. {
  620. this.tvwType.SelectedNode = this.tvwType.Nodes[0];
  621. }
  622. this.dgvGoodsType.IsSetInputColumnsColor = true;
  623. }
  624. catch (Exception ex)
  625. {
  626. throw ex;
  627. }
  628. }
  629. /// <summary>
  630. /// 保存产品类型TreeView的状态
  631. /// </summary>
  632. /// <param name="nodes"></param>
  633. private void SaveTreeNodesStatus(TreeNodeCollection nodes)
  634. {
  635. try
  636. {
  637. foreach (TreeNode node in nodes)
  638. {
  639. // 保存所有节点折叠,展开状态
  640. if (node.IsExpanded)
  641. {
  642. this.NodesStatus[node.FullPath] = true;
  643. }
  644. else
  645. {
  646. this.NodesStatus.Remove(node.FullPath);
  647. }
  648. // 当节点处于选中状态时获取根树节点到当前树节点的路径
  649. if (node.IsSelected)
  650. {
  651. SelectNodeFullPath = node.FullPath;
  652. }
  653. SaveTreeNodesStatus(node.Nodes);
  654. }
  655. }
  656. catch (Exception ex)
  657. {
  658. throw ex;
  659. }
  660. }
  661. /// <summary>
  662. /// 递归显示产品类型树形目录
  663. /// </summary>
  664. /// <param name="typeData">产品类数据源</param>
  665. /// <param name="rows"></param>
  666. /// <param name="node"></param>
  667. private void InitTreeView(DataTable typeData, DataRow[] rows, TreeNode node)
  668. {
  669. try
  670. {
  671. foreach (DataRow row in rows)
  672. {
  673. TreeNode sNode = null;
  674. if (node == null)
  675. {
  676. // 新建树节点并添加
  677. sNode = new TreeNode();
  678. sNode.Text = row["GoodsTypeName"].ToString();
  679. sNode.Name = row["GoodsTypeCode"].ToString();
  680. sNode.Tag = row["GoodsTypeID"].ToString();
  681. this.tvwType.Nodes.Add(sNode);
  682. }
  683. else
  684. {
  685. // 在原有树中加入节点
  686. sNode = node.Nodes.Add(row["GoodsTypeName"].ToString());
  687. sNode.Name = row["GoodsTypeCode"].ToString();
  688. sNode.Tag = row["GoodsTypeID"].ToString();
  689. }
  690. // 查找子节点
  691. string filterExpression = "GoodsTypeCode LIKE '" + row["GoodsTypeCode"].ToString()
  692. + "%' AND LEN(GoodsTypeCode) = " + (row["GoodsTypeCode"].ToString().Length + 3);
  693. DataRow[] subRows = typeData.Select(filterExpression);
  694. // 递归绑定子节点
  695. this.InitTreeView(typeData, subRows, sNode);
  696. }
  697. }
  698. catch (Exception ex)
  699. {
  700. throw ex;
  701. }
  702. }
  703. /// <summary>
  704. /// 保存产品类型数据
  705. /// </summary>
  706. /// <returns>影响行数</returns>
  707. private object SaveGoodsTypeData()
  708. {
  709. try
  710. {
  711. // 获取当前dataGoodsTypeData数据源
  712. DataTable dataGoodsTypeData = (DataTable)this.dgvGoodsType.DataSource;
  713. foreach (DataRow drFor in dataGoodsTypeData.Rows)
  714. {
  715. if (drFor["GoodsTypeFullName"] == null || drFor["GoodsTypeFullName"] == DBNull.Value)
  716. {
  717. drFor["GoodsTypeFullName"] = _selectNodeFullPath
  718. + "→" + drFor["GoodsTypeName"].ToString();
  719. }
  720. }
  721. int result = SystemModuleProxy.Service.SaveGoodsTypeData(dataGoodsTypeData);
  722. return result;
  723. }
  724. catch (Exception ex)
  725. {
  726. throw ex;
  727. }
  728. }
  729. /// <summary>
  730. /// 还原保存前产品类型TreeView的状态
  731. /// </summary>
  732. /// <param name="nodes">节点</param>
  733. private void SetTreeNodesStatus(TreeNodeCollection nodes)
  734. {
  735. try
  736. {
  737. foreach (TreeNode node in nodes)
  738. {
  739. if (this.NodesStatus[node.FullPath] != null)
  740. {
  741. // 展开树节点
  742. node.Expand();
  743. }
  744. // 节点路径和前选中的路径相同 设置节点为选中状态
  745. if (node.FullPath == SelectNodeFullPath)
  746. {
  747. this.tvwType.SelectedNode = node;
  748. }
  749. SetTreeNodesStatus(node.Nodes);
  750. }
  751. }
  752. catch (Exception ex)
  753. {
  754. throw ex;
  755. }
  756. }
  757. /// <summary>
  758. /// 刷新DataGridView中的数据
  759. /// </summary>
  760. private object RefreshData()
  761. {
  762. try
  763. {
  764. // 获取相应节点数据
  765. if (this.tvwType.SelectedNode != null
  766. && !string.IsNullOrEmpty(this.tvwType.SelectedNode.Name.Trim()))
  767. {
  768. this._selectNode = this.tvwType.SelectedNode.Name.Trim();
  769. this._GoodsTypeID = Convert.ToInt32(this.tvwType.SelectedNode.Tag);
  770. DataSet data = (DataSet)DoAsync(new BaseAsyncMethod(GetGoodsTypeIDData));
  771. if (data.Tables[0].Rows.Count > Constant.INT_IS_ZERO)
  772. {
  773. dgvGoodsType.AllowUserToAddRows = false;
  774. }
  775. else
  776. {
  777. dgvGoodsType.AllowUserToAddRows = true;
  778. }
  779. // 获取是否查询正常数据的标识
  780. byte valueFalg = Convert.ToByte(this.chkDisplayDisabledData.Checked);
  781. // 获取查询的数据集
  782. StringBuilder sbFilter = new StringBuilder();
  783. sbFilter.Append(" 1=1");
  784. if (valueFalg == 0)
  785. {
  786. sbFilter.Append(" and ValueFlag = '1'");
  787. }
  788. if (this._selectNode != "")
  789. {
  790. sbFilter.Append(" and GoodsTypeCode like '" + this._selectNode + "%'");
  791. }
  792. this._dtSourse.DefaultView.RowFilter = sbFilter.ToString();
  793. this.dgvGoodsType.AllowUserToModifyRows = true;
  794. // 选中新行时,自动计算产品种类编码
  795. this._dtSourse.TableNewRow += new DataTableNewRowEventHandler(Table_SelectNewRow);
  796. this.dgvGoodsType.DataSource = _dtSourse;
  797. this.dgvGoodsType.IsSetInputColumnsColor = true;
  798. }
  799. else
  800. {
  801. this.dgvGoodsType.AllowUserToModifyRows = false;
  802. }
  803. return Constant.INT_IS_ZERO;
  804. }
  805. catch (Exception ex)
  806. {
  807. throw ex;
  808. }
  809. }
  810. /// <summary>
  811. /// 按条件取得产品类型数据
  812. /// </summary>
  813. /// <returns></returns>
  814. private DataSet GetGoodsTypeByGTCode()
  815. {
  816. try
  817. {
  818. byte valueFlag = Convert.ToByte(this.chkDisplayDisabledData.Checked);
  819. DataSet dsTemp = SystemModuleProxy.Service.GetGoodsTyperData(valueFlag, this._selectNode);
  820. return dsTemp;
  821. }
  822. catch (Exception ex)
  823. {
  824. throw ex;
  825. }
  826. }
  827. /// <summary>
  828. /// 获取产品档案的ID
  829. /// </summary>
  830. /// <param name="GoodsTypeID">产品档案的ID</param>
  831. /// <returns></returns>
  832. /// <remarks>
  833. /// 2014.10.27 任海 新建
  834. /// </remarks>
  835. private DataSet GetGoodsTypeIDData()
  836. {
  837. try
  838. {
  839. return SystemModuleProxy.Service.GetGoodsTypeIDData(this._GoodsTypeID);
  840. }
  841. catch (Exception ex)
  842. {
  843. throw ex;
  844. }
  845. }
  846. /// <summary>
  847. /// 自动生成产品类型编码
  848. /// </summary>
  849. /// <param name="sender"></param>
  850. /// <param name="e"></param>
  851. private void Table_SelectNewRow(object sender, DataTableNewRowEventArgs e)
  852. {
  853. try
  854. {
  855. DataTable itemTable = sender as DataTable;
  856. DataTable itemTableF = itemTable.Copy();
  857. itemTableF.DefaultView.RowFilter = " GoodsTypeCode like '" + this._selectNode + "%'";
  858. itemTableF = itemTableF.DefaultView.ToTable();
  859. if (itemTableF != null && itemTableF.Rows.Count > Constant.INT_IS_ZERO)
  860. {
  861. // 获取本级商品种类中的最大编码
  862. string maxValue;
  863. if (itemTableF.Rows.Count > Constant.INT_IS_ONE)
  864. {
  865. DataTable filterDataTable = itemTableF.Clone();
  866. string selectNode = this.tvwType.SelectedNode.Name.Trim();
  867. DataRow[] dataRows = itemTableF.Select("LEN(GoodsTypeCode) = " + (selectNode + "000").Length);
  868. foreach (DataRow row in dataRows)
  869. {
  870. filterDataTable.Rows.Add(row.ItemArray);
  871. }
  872. maxValue = Utility.GetStringMaxValue(filterDataTable, "GoodsTypeCode");
  873. }
  874. else
  875. {
  876. maxValue = Utility.GetStringMaxValue(itemTableF, "GoodsTypeCode");
  877. }
  878. // 如果没有下级种类,编码长度加3
  879. if (itemTableF.Rows.Count == Constant.INT_IS_ONE)
  880. {
  881. e.Row["GoodsTypeCode"] = maxValue + "001";
  882. }
  883. else
  884. {
  885. e.Row["GoodsTypeCode"] = maxValue.Substring(0, maxValue.Length - 3)
  886. + ((Convert.ToInt32(maxValue.Substring(
  887. maxValue.Length - 3)) + 1)).ToString().PadLeft(3, '0');
  888. }
  889. }
  890. }
  891. catch (Exception ex)
  892. {
  893. // 对异常进行共通处理
  894. ExceptionManager.HandleEventException(this.ToString(),
  895. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  896. }
  897. }
  898. /// <summary>
  899. /// 校验值是否重复
  900. /// </summary>
  901. /// <param name="curCellValue"></param>
  902. /// <param name="curRow"></param>
  903. /// <returns></returns>
  904. private bool IsUnique(string curCellValue, int curRow)
  905. {
  906. //待修改
  907. foreach (DataGridViewRow rowItem in this.dgvGoodsType.Rows)
  908. {
  909. if (rowItem.Index == curRow)
  910. {
  911. continue;
  912. }
  913. // 类别编码
  914. if (rowItem.Cells["GoodsTypeCode"].Value != null)
  915. {
  916. if (rowItem.Cells["GoodsTypeCode"].Value.ToString().TrimEnd().
  917. Equals(curCellValue.TrimEnd()))
  918. {
  919. return false;
  920. }
  921. }
  922. }
  923. return true;
  924. }
  925. /// <summary>
  926. /// 设置TreeView选中节点
  927. /// </summary>
  928. /// <param name="treeView"></param>
  929. /// <param name="selectStr">选中节点文本</param>
  930. private void SelectTreeView(TreeView treeView, string selectStr)
  931. {
  932. treeView.Focus();
  933. for (int i = 0; i < treeView.Nodes.Count; i++)
  934. {
  935. for (int j = 0; j < treeView.Nodes[i].Nodes.Count; j++)
  936. {
  937. if (treeView.Nodes[i].Nodes[j].Text == selectStr)
  938. {
  939. treeView.SelectedNode = treeView.Nodes[i].Nodes[j];//选中
  940. //treeView.Nodes[i].Nodes[j].Checked = true;
  941. treeView.Nodes[i].Expand();//展开父级
  942. return;
  943. }
  944. }
  945. }
  946. }
  947. /// <summary>
  948. /// 保存时单元格必输项不能为空
  949. /// </summary>
  950. private int Conservation()
  951. {
  952. int isConservation = Constant.INT_IS_ZERO;
  953. DataTable dataGoodsData = (DataTable)dgvGoodsType.DataSource;
  954. foreach (DataRow drproductionData in dataGoodsData.Rows)
  955. {
  956. if (drproductionData["GoodsTypeCode"].ToString() == string.Empty)
  957. {
  958. isConservation = Constant.INT_IS_ONE;
  959. break;
  960. }
  961. }
  962. return isConservation;
  963. }
  964. /// <summary>
  965. /// 设置保存按钮的可用状态
  966. /// </summary>
  967. private void RefreshSaveBtnStatus()
  968. {
  969. // 数据有改变时,保存按钮可用,否则不可用
  970. if (DataJudge.IsChange((DataTable)this.dgvGoodsType.DataSource))
  971. {
  972. this.btnSave.Enabled = true;
  973. }
  974. else
  975. {
  976. this.btnSave.Enabled = false;
  977. }
  978. }
  979. /// <summary>
  980. /// 获取全部产品类别信息绑定到数据源中
  981. /// </summary>
  982. private void GetAllGoodsTypeData()
  983. {
  984. this._dtSourse = SystemModuleProxy.Service.GetAllGoodsTypeData().Tables[0];
  985. }
  986. #endregion
  987. }
  988. }