AnnexesFileCore.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using WMS.Info;
  5. using System.Data;
  6. using WMS.Util;
  7. using Microsoft.AspNetCore.Mvc;
  8. namespace WMS.Core
  9. {
  10. /// <summary>
  11. /// 描 述:附件管理
  12. /// </summary>
  13. public class AnnexesFileCore
  14. {
  15. /*缓存文件分片信息*/
  16. private string cacheKey = "AnnexesFile";
  17. /// <summary>
  18. /// 保存附件(支持大文件分片传输)
  19. /// </summary>
  20. /// <param name="fileGuid">文件主键</param>
  21. /// <param name="fileName">文件名称</param>
  22. /// <param name="chunks">文件总共分多少片</param>
  23. /// <param name="fileStream">文件二进制流</param>
  24. /// <returns></returns>
  25. //public static string SaveAnnexes(string fileGuid, string fileName, int chunks, UserInfo userInfo)
  26. //{
  27. // try
  28. // {
  29. // //获取文件完整文件名(包含绝对路径)
  30. // //文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
  31. // string filePath = "C:/";//Config.GetValue("AnnexesFile");
  32. // string uploadDate = DateTime.Now.ToString("yyyyMMdd");
  33. // string FileEextension = Path.GetExtension(fileName);
  34. // string virtualPath = string.Format("{0}/{1}/{3}{4}", filePath, uploadDate, fileGuid, FileEextension);
  35. // // string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, FileEextension);
  36. // //创建文件夹
  37. // string path = Path.GetDirectoryName(virtualPath);
  38. // Directory.CreateDirectory(path);
  39. // //AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
  40. // if (!System.IO.File.Exists(virtualPath))
  41. // {
  42. // long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks);
  43. // if (filesize == -1)// 表示保存失败
  44. // {
  45. // RemoveChunkAnnexes(fileGuid, chunks);
  46. // return "";
  47. // }
  48. // }
  49. // return virtualPath;
  50. // }
  51. // catch (Exception ex)
  52. // {
  53. // throw ex;
  54. // }
  55. //}
  56. /// <summary>
  57. /// 保存附件到文件中
  58. /// </summary>
  59. /// <param name="fileGuid">文件主键</param>
  60. /// <param name="filePath">文件路径</param>
  61. /// <param name="chunks">总共分片数</param>
  62. /// <param name="buffer">文件二进制流</param>
  63. /// <returns>-1:表示保存失败</returns>
  64. public long SaveAnnexesToFile(string fileGuid, string filePath, int chunks)
  65. {
  66. try
  67. {
  68. long filesize = 0;
  69. //创建一个FileInfo对象
  70. FileInfo file = new FileInfo(filePath);
  71. //创建文件
  72. FileStream fs = file.Create();
  73. for (int i = 0; i < chunks; i++)
  74. {
  75. byte[] bufferByRedis = RedisCache.Read<byte[]>(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
  76. if (bufferByRedis == null)
  77. {
  78. return -1;
  79. }
  80. //写入二进制流
  81. fs.Write(bufferByRedis, 0, bufferByRedis.Length);
  82. filesize += bufferByRedis.Length;
  83. RedisCache.Remove(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
  84. }
  85. //关闭文件流
  86. fs.Close();
  87. return filesize;
  88. }
  89. catch (Exception ex)
  90. {
  91. throw ex;
  92. }
  93. }
  94. /// <summary>
  95. /// 保存附件到文件中
  96. /// </summary>
  97. /// <param name="fileGuid">文件主键</param>
  98. /// <param name="filePath">文件路径</param>
  99. /// <param name="chunks">总共分片数</param>
  100. /// <param name="buffer">文件二进制流</param>
  101. /// <returns>-1:表示保存失败</returns>
  102. public DataTable SaveAnnexesToDataTable(string fileGuid, string ext, int chunks)
  103. {
  104. try
  105. {
  106. MemoryStream ms = new MemoryStream();
  107. for (int i = 0; i < chunks; i++)
  108. {
  109. byte[] bufferByRedis = RedisCache.Read<byte[]>(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
  110. if (bufferByRedis == null)
  111. {
  112. throw SysExCore.ThrowFailException("传输失败");
  113. }
  114. //写入二进制流
  115. ms.Write(bufferByRedis, 0, bufferByRedis.Length);
  116. RedisCache.Remove(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
  117. }
  118. return ExcelHelper.ExcelImport(ms, "." + ext);
  119. }
  120. catch (Exception ex)
  121. {
  122. throw new Exception("无法识别的文件");
  123. }
  124. }
  125. /// <summary>
  126. /// 保存错误文件保存到缓冲中
  127. /// </summary>
  128. public void SaveErrJsonExecl(string fileGuid, JsonExecl jsonExecl)
  129. {
  130. try
  131. {
  132. RedisCache.Remove(cacheKey + "Err" + fileGuid, ERedisCacheNo.System);
  133. string jsonstring = jsonExecl.ToJson();
  134. RedisCache.Write<string>(cacheKey + "Err" + fileGuid, jsonstring, DateTime.Now.AddMinutes(5), ERedisCacheNo.System);
  135. }
  136. catch (Exception ex)
  137. {
  138. throw ex;
  139. }
  140. }
  141. /// <summary>
  142. /// 下载错误文件
  143. /// </summary>
  144. public void DownErrJsonExecl(string fileGuid,string FileName)
  145. {
  146. try
  147. {
  148. var jsone = RedisCache.Read<string>(cacheKey + "Err" + fileGuid, ERedisCacheNo.System);
  149. if (jsone != null)
  150. {
  151. JsonExecl je = jsone.ToObject<JsonExecl>();
  152. je.ExeclCfg.FileName = FileName;
  153. ExcelHelper.ExcelDownload(je.dtSou, je.ExeclCfg);
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. throw ex;
  159. }
  160. }
  161. /// <summary>
  162. /// 保存分片附件
  163. /// </summary>
  164. /// <param name="fileGuid">文件主键</param>
  165. /// <param name="chunk">分片文件序号</param>
  166. /// <param name="fileStream">文件流</param>
  167. public void SaveChunkAnnexes(string fileGuid, int chunk, Stream fileStream)
  168. {
  169. try
  170. {
  171. byte[] bytes = new byte[fileStream.Length];
  172. fileStream.Read(bytes, 0, bytes.Length);
  173. RedisCache.Write<byte[]>(cacheKey + chunk + "_" + fileGuid, bytes, ERedisCacheNo.System);
  174. }
  175. catch (Exception ex)
  176. {
  177. throw ex;
  178. }
  179. }
  180. /// <summary>
  181. /// 下载Exel模板
  182. /// </summary>
  183. public FileStreamResult DownSchemeFile(int bustype)
  184. {
  185. try
  186. {
  187. BASE_IMPORTRELATIONSHIP item = null;
  188. if (bustype == 0)
  189. throw SysExCore.ThrowFailException("单据类型异常");
  190. else if (bustype > 0)
  191. // 获取参数配置
  192. item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(v => v.BUSINSERTVAL == bustype).First();
  193. else if (bustype == -1 || bustype == -2)
  194. {
  195. bustype = 7; // PO收货单(中)
  196. if (bustype == -1)
  197. item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(v => v.F_NO == "9").First();
  198. else // PO收货单(英)
  199. item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(v => v.F_NO == "51").First(); ;
  200. }
  201. else
  202. {
  203. throw SysExCore.ThrowFailException("未识别的单据类型");
  204. }
  205. //item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(it => it.BUSINSERTVAL == bustype).First();
  206. if (item == null)
  207. {
  208. throw SysExCore.ThrowFailException("未找到Excel模板配置值。");
  209. }
  210. byte[] bytes = RedisCache.Read<byte[]>(cacheKey + "_Scheme_" + bustype, ERedisCacheNo.System);
  211. if (bytes == null || bytes.Length == 0)
  212. {
  213. string s = FileDownUtil.MapPathFile("\\Content\\ExeclScheme\\" + item.BUSINESSTYPE + ".xlsx");
  214. bytes = File.ReadAllBytes(s);
  215. RedisCache.Write<byte[]>(cacheKey + "_Scheme_" + bustype, bytes, ERedisCacheNo.System);
  216. }
  217. MemoryStream ms = new MemoryStream();
  218. ms.Write(bytes, 0, bytes.Length);
  219. return ExcelHelper.ExcelDownload(ms, item.BUSINESSTYPE + ".xlsx");
  220. }
  221. catch (Exception ex)
  222. {
  223. throw ex;
  224. }
  225. }
  226. /// <summary>
  227. /// 移除文件分片数据
  228. /// </summary>
  229. /// <param name="fileGuid">文件主键</param>
  230. /// <param name="chunks">文件分片数</param>
  231. public void RemoveChunkAnnexes(string fileGuid, int chunks)
  232. {
  233. try
  234. {
  235. for (int i = 0; i < chunks; i++)
  236. {
  237. RedisCache.Remove(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. throw ex;
  243. }
  244. }
  245. }
  246. }