WorkFlowPDFHelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. using Sugar.Enties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ServiceReference1;
  9. using System.Data;
  10. using System.IO;
  11. using iTextSharp.text.pdf;
  12. using WebAPIBase.Utils;
  13. using iTextSharp.text;
  14. using Sugar;
  15. using SqlSugar;
  16. /// <summary>
  17. /// 流程审核生成pdf帮助类
  18. /// </summary>
  19. public class WorkFlowPDFHelper
  20. {
  21. private static string webserviceUrl = "http://127.0.0.1/WorkFlowWebService60NodeJS/query.asmx";
  22. private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  23. /// <summary>
  24. /// pdf文件存放路径
  25. /// </summary>
  26. public static string pdfDirectory
  27. {
  28. get
  29. {
  30. return RequestHelper.IsRemote ? AppSettingsHelper.Configuration["ServiceVirtualPath:RealPath"] : AppSettingsHelper.Configuration["LocalVirtualPath:RealPath"];
  31. }
  32. }
  33. /// <summary>
  34. /// 创建webservice实例
  35. /// </summary>
  36. /// <returns></returns>
  37. public static QuerySoap Services()
  38. {
  39. //创建 HTTP 绑定对象
  40. var binding = new BasicHttpBinding();
  41. binding.SendTimeout = new TimeSpan(0, 5, 0);
  42. binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
  43. //根据 WebService 的 URL 构建终端点对象,参数是提供的WebService地址
  44. var endpoint = new EndpointAddress(webserviceUrl);
  45. //创建调用接口的工厂,注意这里泛型只能传入接口 泛型接口里面的参数是WebService里面定义的类名+Soap
  46. var factory = new ChannelFactory<QuerySoap>(binding, endpoint);
  47. //从工厂获取具体的调用实例
  48. var callClient = factory.CreateChannel();
  49. return callClient;
  50. }
  51. /// <summary>
  52. /// 判断pdf文件夹是否存在
  53. /// </summary>
  54. /// <returns></returns>
  55. public static string CheckServerPdfFolder()
  56. {
  57. string WebServiceUrl = new ConfigItemValueManager().GetConfigItemValue("基础模块-流程管理-流程审批WebService地址");
  58. if (WebServiceUrl != "")
  59. {
  60. if (Services().PdfFileFolderCheck())
  61. {
  62. return WebServiceUrl;
  63. }
  64. else
  65. return "";
  66. }
  67. return "";
  68. }
  69. /// <summary>
  70. /// 检查pdf文件是否能存在
  71. /// </summary>
  72. /// <param name="filename"></param>
  73. /// <returns></returns>
  74. public static bool CheckServerPdfFileExists(string filename)
  75. {
  76. string WebServiceUrl = CheckServerPdfFolder();
  77. if (WebServiceUrl != "")
  78. {
  79. return Services().PdfFileExistsCheck(filename);
  80. }
  81. return false;
  82. }
  83. /// <summary>
  84. /// 建立pdf文件
  85. /// </summary>
  86. /// <param name="ds"></param>
  87. /// <param name="fileName"></param>
  88. public static void CreatePDFForm(DataSet ds, string fileName)
  89. {
  90. FileStream fs = null;
  91. try
  92. {
  93. #region --生成pdf文件--
  94. if (ds == null)
  95. {
  96. return;
  97. }
  98. //新建文件,大小尺寸
  99. //iTextSharp.text.Rectangle singleFormSize = new iTextSharp.text.Rectangle(166.32F, 350.64F);
  100. int maxColumnNum = 0;
  101. for (int i = 1; i < ds.Tables.Count; i++)
  102. {
  103. if (maxColumnNum < ds.Tables[i].Columns.Count)
  104. maxColumnNum = ds.Tables[i].Columns.Count;
  105. }
  106. iTextSharp.text.Document doc = new iTextSharp.text.Document();
  107. doc.SetMargins(5f, 5f, 5f, 5f);
  108. if (maxColumnNum > 5)
  109. {
  110. doc.SetPageSize(iTextSharp.text.PageSize.A4);
  111. }
  112. else
  113. {
  114. doc.SetPageSize(iTextSharp.text.PageSize.A6);
  115. }
  116. #region --字体--
  117. iTextSharp.text.pdf.BaseFont bfChinese = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
  118. //iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
  119. //主表表单字体
  120. iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.Black);
  121. //明细表字体
  122. iTextSharp.text.Font fontDetail = new iTextSharp.text.Font(bfChinese, 10, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.Black);
  123. #endregion
  124. //保存文件
  125. if (!Directory.Exists(pdfDirectory))
  126. {
  127. Directory.CreateDirectory(pdfDirectory);
  128. }
  129. string filePath = $"{pdfDirectory}\\{fileName}.pdf";
  130. logger.Info($"【CreatePDFForm】filePath:{filePath}");
  131. fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
  132. fs.SetLength(0);
  133. /*
  134. if (System.IO.File.Exists(filePath))
  135. {
  136. System.IO.File.Delete(filePath);
  137. }
  138. fs = new FileStream(filePath, FileMode.Create);
  139. * */
  140. PdfWriter writer = PdfWriter.GetInstance(doc, fs);
  141. //打开文件
  142. doc.Open();
  143. #region --画表格--
  144. if (ds.Tables.Count > 0)
  145. {
  146. iTextSharp.text.Paragraph pTitle = new iTextSharp.text.Paragraph(new iTextSharp.text.Chunk(ds.DataSetName, fontChinese));
  147. pTitle.SetAlignment("center");
  148. doc.Add(pTitle);
  149. #region --主表--
  150. DataTable dtMain = ds.Tables[0];
  151. //主表样式
  152. iTextSharp.text.Table tblMain = new iTextSharp.text.Table(2);
  153. tblMain.BorderWidth = 1;
  154. tblMain.BorderColor = new iTextSharp.text.BaseColor(0, 0, 0);
  155. tblMain.Cellpadding = 1;
  156. tblMain.Cellspacing = 1;
  157. tblMain.Width = 100;
  158. tblMain.SetWidths(new int[] { 35, 65 });
  159. //主表内容
  160. foreach (DataColumn cc in dtMain.Columns)
  161. {
  162. string[] cellTitle = cc.ColumnName.Split('$');
  163. tblMain.AddCell(new iTextSharp.text.Cell(new iTextSharp.text.Phrase(cellTitle[0], fontChinese)));
  164. iTextSharp.text.Cell cellValue = new iTextSharp.text.Cell(new iTextSharp.text.Phrase(dtMain.Rows[0][cc.ColumnName].SConvertString(), fontChinese));
  165. if (cellTitle.Length > 1)
  166. {
  167. cellValue.Colspan = Convert.ToInt16(cellTitle[1]);
  168. }
  169. tblMain.AddCell(cellValue);
  170. }
  171. //写入文件
  172. doc.Add(tblMain);
  173. #endregion
  174. #region --明细表--
  175. for (int i = 1; i < ds.Tables.Count; i++)
  176. {
  177. DataTable dtDetail = ds.Tables[i];
  178. //明细表名称
  179. doc.Add(new iTextSharp.text.Paragraph(dtDetail.TableName, fontChinese));
  180. //明细表样式
  181. iTextSharp.text.Table tblDetail = new iTextSharp.text.Table(dtDetail.Columns.Count);
  182. tblDetail.BorderWidth = 1;
  183. tblDetail.BorderColor = new iTextSharp.text.BaseColor(0, 0, 0);
  184. tblDetail.Cellpadding = 1;
  185. tblDetail.Cellspacing = 1;
  186. tblDetail.Width = 100;
  187. //表头
  188. foreach (DataColumn cc in dtDetail.Columns)
  189. {
  190. tblDetail.AddCell(new iTextSharp.text.Cell(new iTextSharp.text.Phrase(cc.ColumnName, fontDetail)));
  191. }
  192. //数据行
  193. foreach (DataRow dr in dtDetail.Rows)
  194. {
  195. foreach (DataColumn cc in dtDetail.Columns)
  196. {
  197. iTextSharp.text.Cell cellValue = new iTextSharp.text.Cell(new iTextSharp.text.Phrase(dr[cc.ColumnName].SConvertString(), fontDetail));
  198. cellValue.SetHorizontalAlignment("right");
  199. cellValue.SetVerticalAlignment("bottom");
  200. tblDetail.AddCell(cellValue);
  201. }
  202. }
  203. //写入文件
  204. doc.Add(tblDetail);
  205. }
  206. #endregion
  207. }
  208. else
  209. {
  210. doc.Add(new iTextSharp.text.Paragraph("对不起,没有取到数据", fontChinese));
  211. }
  212. #endregion
  213. //关闭文件
  214. doc.Close();
  215. writer.Close();
  216. fs.Close();
  217. fs.Dispose();
  218. #endregion
  219. string WebServiceUrl = CheckServerPdfFolder();
  220. if (WebServiceUrl != "")
  221. {
  222. //将pdf 文件写入database
  223. WorkFlowPDFFileService pdffileService = new WorkFlowPDFFileService();
  224. pdffileService.Save("WorkFlowCase", fileName, "");
  225. //将database中的pdf文件写入webservice服务器 异步调用接口
  226. logger.Info($"【复制文件到服务器 PdfFileCopy】fileName:{fileName}");
  227. Services().PdfFileCopyAsync(fileName, "WorkFlowCase");
  228. //logger.Info($"【复制文件到服务器 PdfFileCopy】copy:{copy}");
  229. //处理附件
  230. new WorkFlowAttachRule().ConvertFileToAttachment(fileName, WebServiceUrl);
  231. }
  232. }
  233. catch (Exception ex)
  234. {
  235. //logger.Error(ex);
  236. throw new ApplicationException("CreatePDFForm错误:" + ex.Message);
  237. }
  238. }
  239. /// <summary>
  240. /// 删除pdf文件
  241. /// </summary>
  242. /// <param name="fileName"></param>
  243. public static void DeleteFile(string fileName)
  244. {
  245. try
  246. {
  247. string filePath = $"{pdfDirectory}\\{fileName}.pdf";
  248. if (System.IO.File.Exists(filePath))
  249. {
  250. System.IO.File.Delete(filePath);
  251. }
  252. //在database中标记要删除的pdf文件
  253. WorkFlowPDFFileService pdffileService = new WorkFlowPDFFileService();
  254. pdffileService.MarkDelete("WorkFlowCase", fileName);
  255. //删除服务器上的pdf文件,删除database中的pdf文件记录
  256. string WebServiceUrl = CheckServerPdfFolder();
  257. if (WebServiceUrl != "")
  258. {
  259. Services().PdfFileRemoveAsync(fileName, "WorkFlowCase");
  260. Services().PdfFileRemoveAsync(fileName, "CaseCode");
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. throw new ApplicationException("WorkFlowPDFHelper-DeleteFile错误:" + ex.Message);
  266. }
  267. }
  268. /// <summary>
  269. /// 填充dt主表单
  270. /// </summary>
  271. /// <param name="dt"></param>
  272. /// <param name="columnCaption"></param>
  273. /// <param name="rowValue"></param>
  274. public static void FillMainFormData(DataTable dt, string columnCaption, string rowValue)
  275. {
  276. dt.Columns.Add(columnCaption);
  277. if (dt.Rows.Count == 0)
  278. {
  279. dt.Rows.Add(dt.NewRow());
  280. }
  281. dt.Rows[0][columnCaption] = rowValue;
  282. }
  283. }
  284. public class WorkFlowAttachRule : WorkFlowProcedureManager
  285. {
  286. NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  287. public WorkFlowAttachRule()
  288. {
  289. //
  290. // TODO: 在此处添加构造函数逻辑
  291. //
  292. }
  293. public static bool CheckServerPdfFolder(string casecode)
  294. {
  295. string WebServiceUrl = WorkFlowPDFHelper.CheckServerPdfFolder();
  296. if (WebServiceUrl != "")
  297. {
  298. if (WorkFlowPDFHelper.Services().AttachFileExistsCheck(casecode))
  299. return true;
  300. }
  301. return false;
  302. }
  303. /// <summary>
  304. /// 把附件存在数据库中
  305. /// </summary>
  306. /// <param name="attachmenttype"></param>
  307. /// <param name="mastercode"></param>
  308. public void ConvertFileToAttachment(string casecode, string WebServiceUrl)
  309. {
  310. try
  311. {
  312. //todo:删除服务器上的附件文件
  313. // ../workflow/@casecode/
  314. WorkFlowPDFHelper.Services().PdfFileRemoveAsync(casecode, "CaseCode");
  315. //清空workflowpdffile
  316. string sqlDelete = $"delete from workflowpdffile where attachmenttype='WorkFlowActOpinion'and CaseCode='{casecode}'";
  317. logger.Info($"【ConvertFileToAttachment】sqlDelete:{sqlDelete}");
  318. Db.Ado.ExecuteCommand(sqlDelete);
  319. //从附件表attachment中写入附件到workflowpdffile
  320. string sqlAllAttachment = $@"select * from attachment where attachmenttype='WorkFlowActOpinion' and MasterCode in
  321. (select ApplicationCode from WorkFlowOpinion where CaseCode='{casecode}')
  322. and (right(filename,4) in('.doc','.xls','.ppt','.pdf','.log','.txt','.htm','.mp3','.mp4','.jpg','.gif','.png','.bmp')
  323. or right(filename,5) in('.docx','.xlsx','.pptx','.html','.jgep','.jpeg'))";
  324. logger.Info($"【ConvertFileToAttachment】sqlAllAttachment:{sqlAllAttachment}");
  325. DataTable dtAttachment = Db.Ado.GetDataTable(sqlAllAttachment);
  326. logger.Info($"【ConvertFileToAttachment】dtAttachment.count:{dtAttachment.Rows.Count}");
  327. if (dtAttachment.Rows.Count > 0)
  328. {
  329. WorkFlowPDFFileService pdfService = new WorkFlowPDFFileService();
  330. foreach (DataRow dr in dtAttachment.Rows)
  331. {
  332. //写入文件
  333. pdfService.SaveAttachment(dr, casecode);
  334. }
  335. }
  336. //业务单据的附件
  337. string sqlFormAttachment = $@"select at.* from attachment at inner join(
  338. select b.AttachmentType,a.ApplicationCode,a.casecode from WorkFlowCase a
  339. inner join WorkFlowAttachmentType b on a.ApplicationPath=b.ApplicationPath where a.CaseCode='{casecode}'
  340. ) as wk on at.AttachmentType=wk.AttachmentType and at.MasterCode=wk.ApplicationCode
  341. where (right(filename,4) in('.doc','.xls','.ppt','.pdf','.log','.txt','.htm','.mp3','.mp4','.jpg','.gif','.png','.bmp')
  342. or right(filename,5) in('.docx','.xlsx','.pptx','.html','.jgep','.jpeg'))";
  343. logger.Info($"【ConvertFileToAttachment】sqlFormAttachment:{sqlFormAttachment}");
  344. DataTable dtFormAttachment = Db.Ado.GetDataTable(sqlFormAttachment);
  345. if (dtFormAttachment.Rows.Count > 0)
  346. {
  347. WorkFlowPDFFileService pdfService = new WorkFlowPDFFileService();
  348. foreach (DataRow dr in dtFormAttachment.Rows)
  349. {
  350. //写入文件
  351. pdfService.SaveAttachment(dr, casecode);
  352. }
  353. }
  354. //复制文件
  355. WorkFlowPDFHelper.Services().PdfFileCopyAsync(casecode, "CaseCode");
  356. }
  357. catch (Exception ex)
  358. {
  359. throw new ApplicationException("WorkFlowPDFHelper-ConvertFileToAttachment错误:" + ex.Message);
  360. }
  361. }
  362. }
  363. #region --WorkFlowPDFFileService
  364. /// <summary>
  365. ///
  366. /// </summary>
  367. public class WorkFlowPDFFileService : WorkFlowProcedureManager
  368. {
  369. NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  370. public WorkFlowPDFFileService()
  371. {
  372. //
  373. // TODO: 在此处添加构造函数逻辑
  374. //
  375. }
  376. public DataTable GetByMasterCodeAttachmentType(string attachmenttype, string mastercode)
  377. {
  378. try
  379. {
  380. string sql = $"select * from WorkFlowPDFFile where attachmenttype='{attachmenttype}' and mastercode='{mastercode}'";
  381. ;
  382. DataTable dt = Db.Ado.GetDataTable(sql);
  383. return dt;
  384. }
  385. catch (Exception ex)
  386. {
  387. throw new ApplicationException("WorkFlowPDFHelper-GetByMasterCodeAttachmentType错误:" + ex.Message);
  388. }
  389. }
  390. /// <summary>
  391. /// 保存流程表单pdf文件
  392. /// </summary>
  393. /// <param name="attachmenttype"></param>
  394. /// <param name="mastercode"></param>
  395. /// <param name="usercode"></param>
  396. public void Save(string attachmenttype, string mastercode, string usercode)
  397. {
  398. try
  399. {
  400. string filename = string.Format("{0}.pdf", mastercode);
  401. string fullname = $"{WorkFlowPDFHelper.pdfDirectory}\\{filename}";
  402. FileInfo fi = new FileInfo(fullname);
  403. if (fi.Exists)
  404. {
  405. byte[] content = new byte[fi.Length];
  406. FileStream fs = fi.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  407. fs.Read(content, 0, (int)fi.Length);
  408. fs.Close();
  409. fs.Dispose();
  410. string sql = $"select 1 from WorkFlowPDFFile where attachmenttype='{attachmenttype}' and mastercode='{mastercode}'";
  411. if (Db.Ado.GetString(sql).IsNotNullAndEmpty())
  412. {
  413. //删除旧pdf文件记录
  414. Db.Ado.ExecuteCommand($"delete from WorkFlowPDFFile where attachmenttype='{attachmenttype}' and mastercode='{mastercode}'");
  415. }
  416. var pdfFile = new WorkFlowPDFFile();
  417. pdfFile.AttachMentType = attachmenttype;
  418. pdfFile.MasterCode = mastercode;
  419. pdfFile.Content_Type = "application/pdf";
  420. pdfFile.Length = (int)fi.Length;
  421. pdfFile.FileName = filename;
  422. pdfFile.CreatePerson = "";
  423. pdfFile.CreateDate = DateTime.Now;
  424. pdfFile.Content = content;
  425. logger.Info($"【保存pdf附件】attachmenttype:{attachmenttype},mastercode:{mastercode}");
  426. var row = Db.Insertable(pdfFile).ExecuteCommand();
  427. logger.Info($"【保存pdf附件】执行成功row:{row}");
  428. }
  429. }
  430. catch (Exception ex)
  431. {
  432. throw new ApplicationException("WorkFlowPDFHelper-Save错误:" + ex.Message);
  433. }
  434. }
  435. public void MarkDelete(string attachmenttype, string mastercode)
  436. {
  437. try
  438. {
  439. string sql = @"update WorkFlowPDFFile
  440. set attachmenttype='---'+attachmenttype
  441. where (attachmenttype='{attachmenttype}' and mastercode='{mastercode}') or (attachmenttype='WorkFlowActOpinion'and CaseCode='{mastercode}')";
  442. Db.Ado.ExecuteCommand(sql);
  443. }
  444. catch (Exception ex)
  445. {
  446. throw new ApplicationException("WorkFlowPDFHelper-MarkDelete错误:" + ex.Message);
  447. }
  448. }
  449. /// <summary>
  450. /// 保存流程附件文件
  451. /// </summary>
  452. /// <param name="dr"></param>
  453. /// <param name="casecode"></param>
  454. public void SaveAttachment(DataRow dr, string casecode)
  455. {
  456. try
  457. {
  458. var entity = new WorkFlowPDFFile();
  459. entity.AttachMentType = dr["attachmenttype"].SConvertString();
  460. entity.MasterCode = dr["mastercode"].SConvertString();
  461. entity.FileName = dr["filename"].SConvertString();
  462. entity.Content_Type = dr["content_type"].SConvertString();
  463. entity.CreatePerson = dr["createperson"].SConvertString();
  464. entity.CaseCode = casecode;
  465. bool isAdd = true;
  466. if (dr["Content"] != DBNull.Value)
  467. {
  468. entity.Content = (byte[])dr["Content"];
  469. entity.Length = dr["length"].ToInteger();
  470. }
  471. else
  472. {
  473. //存文件的
  474. string guidname = ConvertRule.ToString(dr["GuidName"]);
  475. string fullname = GetFileName(guidname, ConvertRule.ToDateString(dr["CreateDate"]));
  476. FileInfo fi = new FileInfo(fullname);
  477. if (fi.Exists)
  478. {
  479. byte[] content = new byte[fi.Length];
  480. FileStream fs = fi.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  481. fs.Read(content, 0, (int)fi.Length);
  482. entity.Content = content;
  483. entity.Length = (int)fi.Length;
  484. fs.Close();
  485. fs.Dispose();
  486. }
  487. else
  488. {
  489. isAdd = !isAdd;
  490. }
  491. }
  492. if (isAdd)
  493. {
  494. Db.Insertable(entity).ExecuteCommand();
  495. }
  496. }
  497. catch (Exception ex)
  498. {
  499. throw new ApplicationException("WorkFlowPDFHelper-SaveAttachment错误:" + ex.Message);
  500. }
  501. }
  502. #region--private方法--
  503. /// <summary>
  504. /// 替换文件名中的非法字符为同样式的其他字符
  505. /// </summary>
  506. /// <param name="name"></param>
  507. /// <returns></returns>
  508. private static string ReplaceFileName(string name)
  509. {
  510. name = name.Replace("/", "/");
  511. name = name.Replace("\\", "\");
  512. name = name.Replace(":", ":");
  513. name = name.Replace("*", "※");
  514. name = name.Replace("?", "?");
  515. name = name.Replace("\"", "“");
  516. name = name.Replace("<", "<");
  517. name = name.Replace(">", ">");
  518. name = name.Replace("|", "│");
  519. return name;
  520. }
  521. private string GetPath(string date)
  522. {
  523. new DocumentRule().Init();
  524. string fullname;
  525. string _Path = new ConfigItemValueManager().GetConfigItemValue("基础模块-附件存储设置-文件存储目录").ToLower();
  526. switch (DocumentRule._SavePathMode)
  527. {
  528. case AttachmentSavePathMode.ROOT:
  529. fullname = _Path;
  530. break;
  531. case AttachmentSavePathMode.YYYY:
  532. fullname = Path.Combine(_Path, GetYearString(date));
  533. break;
  534. case AttachmentSavePathMode.YYYYMM:
  535. fullname = Path.Combine(_Path, GetYearMonthString(date));
  536. break;
  537. case AttachmentSavePathMode.YYYYMMDD:
  538. fullname = Path.Combine(_Path, GetYearMonthDateString(date));
  539. break;
  540. default:
  541. fullname = _Path;
  542. break;
  543. }
  544. return fullname;
  545. }
  546. private string GetFileName(string filename, string date)
  547. {
  548. return Path.Combine(GetPath(date), filename);
  549. }
  550. private static string GetYearString(string date)
  551. {
  552. DateTime dt = new DateTime();
  553. if (date == string.Empty) return string.Empty;
  554. if (DateTime.TryParse(date, out dt))
  555. {
  556. return dt.Year.ToString();
  557. }
  558. else
  559. {
  560. return string.Empty;
  561. }
  562. }
  563. private static string GetYearMonthString(string date)
  564. {
  565. DateTime dt = new DateTime();
  566. if (date == string.Empty) return string.Empty;
  567. if (DateTime.TryParse(date, out dt))
  568. {
  569. return dt.Year.ToString() + dt.Month.ToString().PadLeft(2, '0');
  570. }
  571. else
  572. {
  573. return string.Empty;
  574. }
  575. }
  576. private static string GetYearMonthDateString(string date)
  577. {
  578. DateTime dt = new DateTime();
  579. if (date == string.Empty) return string.Empty;
  580. if (DateTime.TryParse(date, out dt))
  581. {
  582. return dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString().PadLeft(2, '0');
  583. }
  584. else
  585. {
  586. return string.Empty;
  587. }
  588. }
  589. #endregion
  590. }
  591. #endregion