irc-netcore-api/IRaCIS.Core.Application/Service/Document/SystemDocumentService.cs

123 lines
5.0 KiB
C#

//--------------------------------------------------------------------
// 此代码由T4模板自动生成 byzhouhang 20210918
// 生成时间 2022-01-05 09:17:03
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------
using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Infrastructure.Extention;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using IRaCIS.Core.Application.Contracts;
using IRaCIS.Core.Infra.EFCore;
namespace IRaCIS.Core.Application.Services
{
/// <summary>
/// SystemDocumentService
/// </summary>
[ApiExplorerSettings(GroupName = "Trial")]
public class SystemDocumentService : BaseService, ISystemDocumentService
{
private readonly IWebHostEnvironment _hostEnvironment;
private readonly IRepository<SystemDocument> systemDocumentRepository;
public SystemDocumentService(IWebHostEnvironment hostEnvironment, IRepository<SystemDocument> systemDocumentRepository)
{
_hostEnvironment = hostEnvironment;
this.systemDocumentRepository = systemDocumentRepository;
}
/// <summary>
/// 管理端列表
/// </summary>
/// <param name="querySystemDocument"></param>
/// <returns></returns>
[HttpPost]
public async Task<PageOutput<SystemDocumentView>> GetSystemDocumentListAsync(SystemDocumentQuery querySystemDocument)
{
var systemDocumentQueryable = systemDocumentRepository
.WhereIf(!string.IsNullOrEmpty(querySystemDocument.Name), t => t.Name.Contains(querySystemDocument.Name))
.WhereIf(!string.IsNullOrEmpty(querySystemDocument.Type), t => t.Type.Contains(querySystemDocument.Type))
.ProjectTo<SystemDocumentView>(_mapper.ConfigurationProvider, new { token = _userInfo.UserToken, userId = _userInfo.Id });
return await systemDocumentQueryable.ToPagedListAsync(querySystemDocument.PageIndex, querySystemDocument.PageSize, querySystemDocument.SortField, querySystemDocument.Asc);
}
public async Task<IResponseOutput> AddOrUpdateSystemDocumentAsync(AddOrEditSystemDocument addOrEditSystemDocument)
{
if (addOrEditSystemDocument.Id == null)
{
var entity = _mapper.Map<SystemDocument>(addOrEditSystemDocument);
if (await systemDocumentRepository.AnyAsync(t => t.Type == addOrEditSystemDocument.Type && t.Name == addOrEditSystemDocument.Name))
{
return ResponseOutput.NotOk("同类型已存在该文件名");
}
await systemDocumentRepository.AddAsync(entity,true);
return ResponseOutput.Ok(entity.Id.ToString());
}
else
{
var document = await systemDocumentRepository.Where(t => t.Id == addOrEditSystemDocument.Id, true).Include(t => t.NeedConfirmedUserTypeList).FirstOrDefaultAsync();
if (document == null) return Null404NotFound(document);
if (await systemDocumentRepository.AnyAsync(t => t.Type == addOrEditSystemDocument.Type && t.Name == addOrEditSystemDocument.Name && t.Id != addOrEditSystemDocument.Id))
{
return ResponseOutput.NotOk("同类型已存在该文件名");
}
var dbDocumentType = document.Type;
_mapper.Map(addOrEditSystemDocument, document);
if (dbDocumentType != addOrEditSystemDocument.Type)
{
var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\')).IfNullThrowException().FullName;
var beforeFilePath = Path.Combine(rootPath, document.Path);
document.Path = document.Path.Replace(dbDocumentType, addOrEditSystemDocument.Type);
var nowPath = Path.Combine(rootPath, document.Path);
if (File.Exists(beforeFilePath))
{
File.Move(beforeFilePath, nowPath, true);
File.Delete(beforeFilePath);
}
}
var success = _repository.SaveChangesAsync();
return ResponseOutput.Ok(document.Id.ToString());
}
}
[HttpDelete("{systemDocumentId:guid}")]
public async Task<IResponseOutput> DeleteSystemDocumentAsync(Guid systemDocumentId)
{
if (await _repository.Where<SystemDocument>(t => t.Id == systemDocumentId).AnyAsync(u => u.SystemDocConfirmedUserList.Any()))
{
return ResponseOutput.NotOk("该文档下已有签名的用户");
}
var success = await _repository.DeleteFromQueryAsync<SystemDocument>(t => t.Id == systemDocumentId);
return ResponseOutput.Result(success);
}
}
}