123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using WMS.Info;
- using System.Data;
- using WMS.Util;
- using Microsoft.AspNetCore.Mvc;
- namespace WMS.Core
- {
- /// <summary>
- /// 描 述:附件管理
- /// </summary>
- public class AnnexesFileCore
- {
- /*缓存文件分片信息*/
- private string cacheKey = "AnnexesFile";
- /// <summary>
- /// 保存附件(支持大文件分片传输)
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="fileName">文件名称</param>
- /// <param name="chunks">文件总共分多少片</param>
- /// <param name="fileStream">文件二进制流</param>
- /// <returns></returns>
- //public static string SaveAnnexes(string fileGuid, string fileName, int chunks, UserInfo userInfo)
- //{
- // try
- // {
- // //获取文件完整文件名(包含绝对路径)
- // //文件存放路径格式:/Resource/ResourceFile/{userId}/{date}/{guid}.{后缀名}
- // string filePath = "C:/";//Config.GetValue("AnnexesFile");
- // string uploadDate = DateTime.Now.ToString("yyyyMMdd");
- // string FileEextension = Path.GetExtension(fileName);
- // string virtualPath = string.Format("{0}/{1}/{3}{4}", filePath, uploadDate, fileGuid, FileEextension);
- // // string virtualPath = string.Format("{0}/{1}/{2}/{3}{4}", filePath, userInfo.userId, uploadDate, fileGuid, FileEextension);
- // //创建文件夹
- // string path = Path.GetDirectoryName(virtualPath);
- // Directory.CreateDirectory(path);
- // //AnnexesFileEntity fileAnnexesEntity = new AnnexesFileEntity();
- // if (!System.IO.File.Exists(virtualPath))
- // {
- // long filesize = SaveAnnexesToFile(fileGuid, virtualPath, chunks);
- // if (filesize == -1)// 表示保存失败
- // {
- // RemoveChunkAnnexes(fileGuid, chunks);
- // return "";
- // }
- // }
- // return virtualPath;
- // }
- // catch (Exception ex)
- // {
- // throw ex;
- // }
- //}
- /// <summary>
- /// 保存附件到文件中
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="filePath">文件路径</param>
- /// <param name="chunks">总共分片数</param>
- /// <param name="buffer">文件二进制流</param>
- /// <returns>-1:表示保存失败</returns>
- public long SaveAnnexesToFile(string fileGuid, string filePath, int chunks)
- {
- try
- {
- long filesize = 0;
- //创建一个FileInfo对象
- FileInfo file = new FileInfo(filePath);
- //创建文件
- FileStream fs = file.Create();
- for (int i = 0; i < chunks; i++)
- {
- byte[] bufferByRedis = RedisCache.Read<byte[]>(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
- if (bufferByRedis == null)
- {
- return -1;
- }
- //写入二进制流
- fs.Write(bufferByRedis, 0, bufferByRedis.Length);
- filesize += bufferByRedis.Length;
- RedisCache.Remove(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
- }
- //关闭文件流
- fs.Close();
- return filesize;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 保存附件到文件中
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="filePath">文件路径</param>
- /// <param name="chunks">总共分片数</param>
- /// <param name="buffer">文件二进制流</param>
- /// <returns>-1:表示保存失败</returns>
- public DataTable SaveAnnexesToDataTable(string fileGuid, string ext, int chunks)
- {
- try
- {
- MemoryStream ms = new MemoryStream();
- for (int i = 0; i < chunks; i++)
- {
- byte[] bufferByRedis = RedisCache.Read<byte[]>(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
-
- if (bufferByRedis == null)
- {
- throw SysExCore.ThrowFailException("传输失败");
- }
- //写入二进制流
- ms.Write(bufferByRedis, 0, bufferByRedis.Length);
- RedisCache.Remove(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
- }
- return ExcelHelper.ExcelImport(ms, "." + ext);
- }
- catch (Exception ex)
- {
- throw new Exception("无法识别的文件");
- }
- }
- /// <summary>
- /// 保存错误文件保存到缓冲中
- /// </summary>
- public void SaveErrJsonExecl(string fileGuid, JsonExecl jsonExecl)
- {
- try
- {
- RedisCache.Remove(cacheKey + "Err" + fileGuid, ERedisCacheNo.System);
- string jsonstring = jsonExecl.ToJson();
- RedisCache.Write<string>(cacheKey + "Err" + fileGuid, jsonstring, DateTime.Now.AddMinutes(5), ERedisCacheNo.System);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 下载错误文件
- /// </summary>
- public void DownErrJsonExecl(string fileGuid,string FileName)
- {
- try
- {
- var jsone = RedisCache.Read<string>(cacheKey + "Err" + fileGuid, ERedisCacheNo.System);
-
- if (jsone != null)
- {
- JsonExecl je = jsone.ToObject<JsonExecl>();
- je.ExeclCfg.FileName = FileName;
- ExcelHelper.ExcelDownload(je.dtSou, je.ExeclCfg);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 保存分片附件
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="chunk">分片文件序号</param>
- /// <param name="fileStream">文件流</param>
- public void SaveChunkAnnexes(string fileGuid, int chunk, Stream fileStream)
- {
- try
- {
- byte[] bytes = new byte[fileStream.Length];
- fileStream.Read(bytes, 0, bytes.Length);
- RedisCache.Write<byte[]>(cacheKey + chunk + "_" + fileGuid, bytes, ERedisCacheNo.System);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 下载Exel模板
- /// </summary>
- public FileStreamResult DownSchemeFile(int bustype)
- {
- try
- {
- BASE_IMPORTRELATIONSHIP item = null;
- if (bustype == 0)
- throw SysExCore.ThrowFailException("单据类型异常");
- else if (bustype > 0)
- // 获取参数配置
- item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(v => v.BUSINSERTVAL == bustype).First();
- else if (bustype == -1 || bustype == -2)
- {
- bustype = 7; // PO收货单(中)
- if (bustype == -1)
- item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(v => v.F_NO == "9").First();
- else // PO收货单(英)
- item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(v => v.F_NO == "51").First(); ;
- }
- else
- {
- throw SysExCore.ThrowFailException("未识别的单据类型");
- }
- //item = SysDbCore.GetDbCtx().Queryable<BASE_IMPORTRELATIONSHIP>().Where(it => it.BUSINSERTVAL == bustype).First();
- if (item == null)
- {
- throw SysExCore.ThrowFailException("未找到Excel模板配置值。");
- }
- byte[] bytes = RedisCache.Read<byte[]>(cacheKey + "_Scheme_" + bustype, ERedisCacheNo.System);
- if (bytes == null || bytes.Length == 0)
- {
- string s = FileDownUtil.MapPathFile("\\Content\\ExeclScheme\\" + item.BUSINESSTYPE + ".xlsx");
- bytes = File.ReadAllBytes(s);
- RedisCache.Write<byte[]>(cacheKey + "_Scheme_" + bustype, bytes, ERedisCacheNo.System);
- }
- MemoryStream ms = new MemoryStream();
- ms.Write(bytes, 0, bytes.Length);
- return ExcelHelper.ExcelDownload(ms, item.BUSINESSTYPE + ".xlsx");
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 移除文件分片数据
- /// </summary>
- /// <param name="fileGuid">文件主键</param>
- /// <param name="chunks">文件分片数</param>
- public void RemoveChunkAnnexes(string fileGuid, int chunks)
- {
- try
- {
- for (int i = 0; i < chunks; i++)
- {
- RedisCache.Remove(cacheKey + i + "_" + fileGuid, ERedisCacheNo.System);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
|