using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Util;
namespace WMS.BZServices
{
public class AnnexesFileService
{
private string cacheKey = "AnnexesFile";
private readonly RedisService _redisService;
public AnnexesFileService(RedisService redisService)
{
_redisService = redisService;
}
///
/// 保存分片附件
///
/// 文件主键
/// 分片文件序号
/// 文件流
public void SaveChunkAnnexes(string fileGuid, int chunk, byte[] bytes)
{
try
{
_redisService.Set(cacheKey + chunk + "_" + fileGuid, bytes,TimeSpan.FromDays(1));
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 保存附件到文件中
///
/// 文件主键
/// 文件路径
/// 总共分片数
/// 文件二进制流
/// -1:表示保存失败
public DataTable SaveAnnexesToDataTable(string fileGuid, string ext, int chunks)
{
try
{
MemoryStream ms = new MemoryStream();
for (int i = 0; i < chunks; i++)
{
byte[] bufferByRedis = _redisService.Get(cacheKey + i + "_" + fileGuid);
if (bufferByRedis == null)
{
throw BZSysExCore.ThrowFailException("传输失败");
}
//写入二进制流
ms.Write(bufferByRedis, 0, bufferByRedis.Length);
_redisService.Del(cacheKey + i + "_" + fileGuid);
}
return ExcelHelper.ExcelImport(ms, "." + ext);
}
catch (Exception ex)
{
throw new Exception("无法识别的文件");
}
}
}
}