using HStation.Dto.TransferFile;
using Microsoft.AspNetCore.Http;
namespace HStation.CAL.LocalClient
{
///
///
///
public class TransferRevitFile : ITransferRevitFile
{
private readonly HStation.Service.TransferRevitFile _service = new();
public TransferRevitFile(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
private readonly IHttpContextAccessor _httpContextAccessor;
#region Query
///
/// 获取所有
///
public async Task> GetAll()
{
return await Task.Factory.StartNew(() =>
{
var list = _service.GetAll();
var vmList = list?.Select(x => new TransferRevitFileDto(x)).ToList();
return vmList;
});
}
///
/// 通过 ID 获取
///
public async Task GetByID(long input)
{
return await Task.Factory.StartNew(() =>
{
var model = _service.GetByID(input);
return model == null ? null : new TransferRevitFileDto(model);
});
}
///
/// 通过 Ids 获取
///
public async Task> GetByIds(List ids)
{
return await Task.Factory.StartNew(() =>
{
var list = _service.GetByIds(ids);
var vmList = list?.Select(x => new TransferRevitFileDto(x)).ToList();
return vmList;
});
}
///
/// 获取模糊列表
///
public async Task> GetFluzzyList
(
string FileName,
string FileCode,
string FileSuffix,
string UploadUserName,
DateTime? StartTime,
DateTime? EndTime
)
{
return await Task.Factory.StartNew(() =>
{
var list = _service.GetFluzzyList(FileName, FileCode, FileSuffix, UploadUserName, StartTime, EndTime);
var vmList = list?.Select(x => new TransferRevitFileDto(x)).ToList();
return vmList;
});
}
///
/// 获取模糊分页列表
///
public async Task> GetFluzzyPageList(QueryTransferRevitFileFluzzyPageListInput input)
{
return await Task.Factory.StartNew(() =>
{
int total = 0;
var list = _service.GetFluzzyPageList(input.FileName, input.FileCode, input.FileSuffix, input.UploadUserName, input.StartTime, input.EndTime, input.PageIndex, input.PageSize, ref total);
var vmList = list?.Select(x => new TransferRevitFileDto(x)).ToList();
return new PageListOutput() { Total = total, List = vmList };
});
}
#endregion Query
#region Insert
///
/// 插入一条 (同时上传文件)
///
public async Task Insert(AddTransferRevitFileInput input)
{
return await Task.Factory.StartNew(() =>
{
var request = _httpContextAccessor.HttpContext.Request;
if (request.Form.Files == null || request.Form.Files.Count != 1)
{
throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, "文件验证错误");
}
var fileCode = input.FileCode;
if (string.IsNullOrEmpty(fileCode))
{
fileCode = Yw.YitIdHelper.NextId().ToString();
}
if (_service.IsExistFileCode(fileCode))
{
throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, "文件编码已存在");
}
var user = new HStation.Service.TransferFileUser().GetByName(input.UploadUserName);
if (user == null)
{
throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.V001, "用户名称不存在");
}
var uploadFile = request.Form.Files[0];
var model = new Model.TransferRevitFile();
model.FileName = uploadFile.FileName;
model.FileCode = fileCode;
model.FileSuffix = Path.GetExtension(uploadFile.FileName);
model.StorageHouse = ConfigHelper.DataFolder;
model.StorageCode = Yw.Service.FileHelper.UploadSubFile(ConfigHelper.DataFolder, uploadFile.OpenReadStream(), Path.GetExtension(uploadFile.FileName));
var filePath = Yw.Service.FileHelper.GetFilePath(model.StorageHouse, model.StorageCode);
var fileInfo = new FileInfo(filePath);
model.FileSize = fileInfo.Length;
model.UploadTime = DateTime.Now;
model.UploadUserID = user.ID;
model.UploadUserName = input.UploadUserName;
model.Description = input.Description;
var id = _service.Insert(model);
if (id < 1)
{
Yw.Service.FileHelper.Delete(model.StorageHouse, model.StorageCode);
}
return id;
});
}
#endregion Insert
#region Exist
///
/// 判断 FileCode 是否存在
///
public async Task IsExistFileCode(string FileCode)
{
return await Task.Factory.StartNew(() =>
{
var bol = _service.IsExistFileCode(FileCode);
return bol;
});
}
///
/// 判断 FileCode 是否存在 ExceptID
///
public async Task IsExistFileCodeExceptID(FileCodeExceptIDInput input)
{
return await Task.Factory.StartNew(() =>
{
var bol = _service.IsExistFileCodeExceptID(input.FileCode, input.ExceptID);
return bol;
});
}
#endregion Exist
#region Delete
///
/// 通过 ID 删除
///
public async Task DeleteByID(long input)
{
return await Task.Factory.StartNew(() =>
{
var model = _service.GetByID(input);
if (model == null)
{
throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.D001, "数据不存在");
}
var bol = _service.DeleteByID(input, out string msg);
if (bol)
{
Yw.Service.FileHelper.Delete(model.StorageHouse, model.StorageCode);
return bol;
}
throw YOops.Oh(eResultCode.Alert, InternalErrorCodes.D999, msg);
});
}
#endregion Delete
}
}