lixiaojun
2024-10-25 a27817f01817b22fff839a2d0b98cbb6e45a22f7
BLL/HStation.BLL.TransferFile.Core/03-localclient/02-revit/TransferRevitFile.cs
@@ -1,12 +1,205 @@
namespace HStation.CAL.LocalClient
using HStation.Dto.TransferFile;
using Microsoft.AspNetCore.Http;
namespace HStation.CAL.LocalClient
{
    /// <summary>
    ///
    ///
    /// </summary>
    public class TransferRevitFile : ITransferRevitFile
    {
        private readonly HStation.Service.TransferRevitFile _service = new();
        public TransferRevitFile(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        private readonly IHttpContextAccessor _httpContextAccessor;
        #region Query
        /// <summary>
        /// 获取所有
        /// </summary>
        public async Task<List<TransferRevitFileDto>> GetAll()
        {
            return await Task.Factory.StartNew(() =>
            {
                var list = _service.GetAll();
                var vmList = list?.Select(x => new TransferRevitFileDto(x)).ToList();
                return vmList;
            });
        }
        /// <summary>
        /// 通过 ID 获取
        /// </summary>
        public async Task<TransferRevitFileDto> GetByID(long input)
        {
            return await Task.Factory.StartNew(() =>
            {
                var model = _service.GetByID(input);
                return model == null ? null : new TransferRevitFileDto(model);
            });
        }
        /// <summary>
        /// 通过 Ids 获取
        /// </summary>
        public async Task<List<TransferRevitFileDto>> GetByIds(List<long> ids)
        {
            return await Task.Factory.StartNew(() =>
            {
                var list = _service.GetByIds(ids);
                var vmList = list?.Select(x => new TransferRevitFileDto(x)).ToList();
                return vmList;
            });
        }
        /// <summary>
        /// 获取模糊列表
        /// </summary>
        public async Task<List<TransferRevitFileDto>> 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;
            });
        }
        /// <summary>
        /// 获取模糊分页列表
        /// </summary>
        public async Task<PageListOutput<TransferRevitFileDto>> 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<TransferRevitFileDto>() { Total = total, List = vmList };
            });
        }
        #endregion Query
        #region Insert
        /// <summary>
        /// 插入一条 (同时上传文件)
        /// </summary>
        public async Task<long> 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
        /// <summary>
        /// 判断 FileCode 是否存在
        /// </summary>
        public async Task<bool> IsExistFileCode(string FileCode)
        {
            return await Task.Factory.StartNew(() =>
            {
                var bol = _service.IsExistFileCode(FileCode);
                return bol;
            });
        }
        /// <summary>
        ///  判断 FileCode 是否存在 ExceptID
        /// </summary>
        public async Task<bool> IsExistFileCodeExceptID(FileCodeExceptIDInput input)
        {
            return await Task.Factory.StartNew(() =>
            {
                var bol = _service.IsExistFileCodeExceptID(input.FileCode, input.ExceptID);
                return bol;
            });
        }
        #endregion Exist
        #region Delete
        /// <summary>
        /// 通过 ID 删除
        /// </summary>
        public async Task<bool> 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
    }
}