F_CMN_0101.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:F_CMN_0101.cs
  5. * 2.功能描述:附件上传
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 庄天威 2014/09/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.IO;
  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.WCF.DataModels;
  19. using Dongke.IBOSS.PRD.WCF.Proxys;
  20. using Dongke.IBOSS.PRD.WCF.Proxys.SystemModuleService;
  21. namespace Dongke.IBOSS.PRD.Client.CommonModule
  22. {
  23. /// <summary>
  24. /// 附件上传
  25. /// </summary>
  26. public partial class F_CMN_0101 : FormBase
  27. {
  28. #region 成员变量
  29. //产品ID
  30. private int _goodsId;
  31. // 添加附件的列表
  32. private DataTable _attachmenTable = new DataTable("attachmen");
  33. //添加附件的实体类集合
  34. private List<GoodsAttachmentEntity> _attList = new List<GoodsAttachmentEntity>();
  35. //修改附件的实体类集合
  36. private List<GoodsAttachmentEntity> _updateAttList = new List<GoodsAttachmentEntity>();
  37. #endregion
  38. #region 构造函数
  39. public F_CMN_0101(int GoodsId)
  40. {
  41. InitializeComponent();
  42. this._goodsId = GoodsId;
  43. this.Text = FormTitles.F_CMN_0101;
  44. this.btnUpload.Text = ButtonText.BTN_UPLOAD;
  45. this.btnSave.Text = ButtonText.BTN_SAVE;
  46. this.btnCancel.Text = ButtonText.BTN_CANCEL;
  47. }
  48. #endregion
  49. #region 事件
  50. /// <summary>
  51. /// 窗体加载事件
  52. /// </summary>
  53. /// <param name="sender"></param>
  54. /// <param name="e"></param>
  55. private void F_MST_0704_Load(object sender, EventArgs e)
  56. {
  57. try
  58. {
  59. // 设置不自动创建列
  60. this.dgvAttachmentNow.AutoGenerateColumns = false;
  61. // 加载已存在的附件
  62. DataSet dsAtt = GetOrderFixedAttachment();
  63. BindDGVSize();
  64. this.dgvAttachmentNow.DataSource = dsAtt.Tables[0];
  65. // 赋予成员变量附件列表_attachmenTable结构
  66. DataColumn fileNameColumn = new DataColumn("FileName");
  67. DataColumn localFilePathColumn = new DataColumn("LocalFilePath");
  68. this._attachmenTable.Columns.Add(fileNameColumn);
  69. this._attachmenTable.Columns.Add(localFilePathColumn);
  70. }
  71. catch (Exception ex)
  72. {
  73. // 对异常进行共通处理
  74. ExceptionManager.HandleEventException(this.ToString(),
  75. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  76. }
  77. }
  78. /// <summary>
  79. /// 上传按钮事件
  80. /// </summary>
  81. /// <param name="sender"></param>
  82. /// <param name="e"></param>
  83. private void btnUpload_Click(object sender, EventArgs e)
  84. {
  85. try
  86. {
  87. string fileName = string.Empty;
  88. // 打开选择文件对话框
  89. odlgFile.Filter = Constant.FILTER_DOC;
  90. odlgFile.FilterIndex = 0;
  91. odlgFile.RestoreDirectory = true;
  92. odlgFile.Title = "选择附件";
  93. odlgFile.FileName = null;
  94. odlgFile.RestoreDirectory = true;
  95. if (odlgFile.ShowDialog() == DialogResult.OK)
  96. {
  97. FileInfo file = new FileInfo(odlgFile.FileName);
  98. this.txtUploadFile.Text = odlgFile.FileName;
  99. // 将员工的附件添加到列表中
  100. fileName = Path.GetFileName(this.txtUploadFile.Text.Trim());
  101. this._attachmenTable.Rows.Add(fileName, this.txtUploadFile.Text);
  102. this.dgvAttachment.DataSource = this._attachmenTable;
  103. }
  104. BindDGVSize();
  105. }
  106. catch (Exception ex)
  107. {
  108. // 对异常进行共通处理
  109. ExceptionManager.HandleEventException(this.ToString(),
  110. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  111. }
  112. }
  113. #endregion
  114. #region 私有方法
  115. /// <summary>
  116. /// 根据ID获取产品附件
  117. /// </summary>
  118. /// <returns></returns>
  119. private DataSet GetOrderFixedAttachment()
  120. {
  121. DataSet dsAtt = SystemModuleProxy.Service.GetAttachmentByGoodsId(_goodsId);
  122. return dsAtt;
  123. }
  124. /// <summary>
  125. /// 将产品附件上传到服务器
  126. /// </summary>
  127. /// <param name="attachmenTable">本地附件列表</param>
  128. /// <returns>服务器附件列表</returns>
  129. private int UpLoadAttachmen(DataTable attachmenTable)
  130. {
  131. try
  132. {
  133. int MyReturn = 0;
  134. if ((attachmenTable != null && attachmenTable.Rows.Count > Constant.INT_IS_ZERO) || this._updateAttList.Count != Constant.INT_IS_ZERO)
  135. {
  136. // 循环员工的附件上传到服务器并保存
  137. foreach (DataRow dataRow in attachmenTable.Rows)
  138. {
  139. byte[] fileBinary = Utility.GetBytesByFilePath(dataRow["LocalFilePath"].ToString());
  140. String FileName = dataRow["FileName"].ToString();
  141. String FileType = FileName.Substring(FileName.LastIndexOf("."));
  142. string filePath = CommonModuleProxy.Service.UpLoadFile("Goods", DateTime.Now, FileType, fileBinary);
  143. // 保存实体
  144. GoodsAttachmentEntity attEntity = new GoodsAttachmentEntity();
  145. attEntity.FileName = FileName;
  146. attEntity.FilePath = filePath;
  147. attEntity.GoodsID = this._goodsId;
  148. attEntity.IsUpdateAdd = 1;
  149. this._attList.Add(attEntity);
  150. }
  151. if (this._updateAttList.Count != Constant.INT_IS_ZERO)
  152. {
  153. foreach (GoodsAttachmentEntity attFor in this._updateAttList)
  154. {
  155. this._attList.Add(attFor);
  156. }
  157. MyReturn = SystemModuleProxy.Service.UpdateAttachment(this._attList, this._goodsId);
  158. }
  159. else
  160. {
  161. MyReturn = SystemModuleProxy.Service.AddAttachment(this._attList, this._goodsId);
  162. }
  163. }
  164. return MyReturn;
  165. }
  166. catch (Exception ex)
  167. {
  168. throw ex;
  169. }
  170. }
  171. /// <summary>
  172. /// 保存按钮事件
  173. /// </summary>
  174. /// <param name="sender"></param>
  175. /// <param name="e"></param>
  176. private void btnSave_Click(object sender, EventArgs e)
  177. {
  178. try
  179. {
  180. int RowsCount = UpLoadAttachmen(this._attachmenTable);
  181. if (RowsCount > Constant.INT_IS_ZERO)
  182. {
  183. this._attachmenTable.Clear();
  184. this._attList.Clear();
  185. this._updateAttList.Clear();
  186. DataSet dsAtt = GetOrderFixedAttachment();
  187. BindDGVSize();
  188. this.dgvAttachmentNow.DataSource = dsAtt.Tables[0];
  189. this.txtUploadFile.Text = "";
  190. MessageBox.Show(string.Format(Messages.MSG_CMN_I001, "附件", "保存"),
  191. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
  192. }
  193. else
  194. {
  195. MessageBox.Show(string.Format(Messages.MSG_CMN_W001, "附件", "保存"),
  196. this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
  197. }
  198. }
  199. catch (Exception ex)
  200. {
  201. throw ex;
  202. }
  203. }
  204. /// <summary>
  205. /// 取消按钮事件
  206. /// </summary>
  207. /// <param name="sender"></param>
  208. /// <param name="e"></param>
  209. private void btnCancel_Click(object sender, EventArgs e)
  210. {
  211. this.Close();
  212. }
  213. /// <summary>
  214. /// 单击单元格内容事件
  215. /// </summary>
  216. /// <param name="sender"></param>
  217. /// <param name="e"></param>
  218. private void dgvAttachmentNow_CellContentClick(object sender, DataGridViewCellEventArgs e)
  219. {
  220. try
  221. {
  222. if (this.dgvAttachmentNow.CurrentRow != null)
  223. {
  224. // 点击下载
  225. if (e.ColumnIndex == this.dgvAttachmentNow.CurrentRow.Cells["fileDownLoad"].ColumnIndex)
  226. {
  227. // 获取附件的保存路径
  228. string fileSavePath = string.Empty;
  229. sdlgFile.Filter = Constant.FILTER_DOC;
  230. sdlgFile.FilterIndex = 0;
  231. sdlgFile.RestoreDirectory = true;
  232. sdlgFile.Title = "保存附件";
  233. sdlgFile.FileName = this.dgvAttachmentNow.CurrentRow.Cells["FileName"].Value + "";
  234. sdlgFile.AddExtension = true;
  235. if (sdlgFile.ShowDialog() == DialogResult.OK)
  236. {
  237. fileSavePath = sdlgFile.FileName;
  238. }
  239. else
  240. {
  241. return;
  242. }
  243. // 下载附件
  244. string downLoadPath = this.dgvAttachmentNow.CurrentRow.Cells["FilePath"].Value.ToString();
  245. byte[] downLoadFile = CommonModuleProxy.Service.DownloadFile(downLoadPath);
  246. // 若获取的文件流为null,提示文件不存在
  247. if (downLoadFile != null)
  248. {
  249. // 保存附件
  250. bool result = Utility.BinaryToFile(fileSavePath, downLoadFile);
  251. // 提示保存结果
  252. if (result == true)
  253. {
  254. MessageBox.Show("附件下载成功。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Information);
  255. }
  256. else
  257. {
  258. MessageBox.Show("附件下载失败。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning);
  259. }
  260. }
  261. else
  262. {
  263. MessageBox.Show("该附件文件不存在。",this.Text,MessageBoxButtons.OK,MessageBoxIcon.Warning);
  264. }
  265. }
  266. // 点击删除
  267. if (e.ColumnIndex == this.dgvAttachmentNow.CurrentRow.Cells["deleteFile"].ColumnIndex)
  268. {
  269. if (this.dgvAttachmentNow.CurrentRow.Cells["AttachmentID"].Value != null &&
  270. this.dgvAttachmentNow.CurrentRow.Cells["AttachmentID"].Value != DBNull.Value)
  271. {
  272. GoodsAttachmentEntity goodsAttEntity = new GoodsAttachmentEntity();
  273. goodsAttEntity.AttachmentID = Convert.ToInt32(this.dgvAttachmentNow.CurrentRow.Cells["AttachmentID"].Value);
  274. goodsAttEntity.ValueFlag = 0;
  275. goodsAttEntity.IsUpdateAdd = 0;
  276. this._updateAttList.Add(goodsAttEntity);
  277. }
  278. dgvAttachmentNow.Rows.RemoveAt(dgvAttachmentNow.CurrentRow.Index);
  279. }
  280. }
  281. }
  282. catch (Exception ex)
  283. {
  284. // 对异常进行共通处理
  285. ExceptionManager.HandleEventException(this.ToString(),
  286. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  287. }
  288. }
  289. /// <summary>
  290. /// 单击单元格内容事件
  291. /// </summary>
  292. /// <param name="sender"></param>
  293. /// <param name="e"></param>
  294. private void dgvAttachment_CellContentClick(object sender, DataGridViewCellEventArgs e)
  295. {
  296. try
  297. {
  298. if (e.ColumnIndex == this.dgvAttachment.CurrentRow.Cells["fileDeleteNew"].ColumnIndex)
  299. {
  300. dgvAttachment.Rows.RemoveAt(dgvAttachment.CurrentRow.Index);
  301. }
  302. BindDGVSize();
  303. }
  304. catch (Exception ex)
  305. {
  306. // 对异常进行共通处理
  307. ExceptionManager.HandleEventException(this.ToString(),
  308. System.Reflection.MethodBase.GetCurrentMethod().Name, this.Text, ex);
  309. }
  310. }
  311. /// <summary>
  312. /// 绑定数据表大小
  313. /// </summary>
  314. private void BindDGVSize()
  315. {
  316. if (this.dgvAttachment.Rows.Count == 0)
  317. {
  318. this.gbxNews.Visible = false;
  319. this.gbxNow.Height = 370;
  320. }
  321. else
  322. {
  323. this.gbxNews.Visible = true;
  324. this.gbxNow.Height = 185;
  325. }
  326. }
  327. #endregion
  328. }
  329. }