using System.Dynamic;
|
using System.Text;
|
using Gs.Sys.Models;
|
using Gs.Toolbox;
|
using Gs.Toolbox.ApiCore.Abstract.Mvc;
|
using Gs.Toolbox.ApiCore.Common.Mvc;
|
using Gs.Toolbox.ApiCore.Group;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Mvc;
|
using Newtonsoft.Json.Linq;
|
|
namespace Gs.Sys.Services;
|
|
[ApiGroup(ApiGroupNames.Sys)]
|
public class MesFileController : Repository<MesFile>, IRomteService
|
{
|
private readonly IHttpContextAccessor _http;
|
private readonly string _userCode, _userGuid, _orgFids;
|
|
public MesFileController(IHttpContextAccessor httpContextAccessor)
|
{
|
_http = httpContextAccessor;
|
(_userCode, _userGuid, _orgFids) =
|
UtilityHelper.GetUserGuidAndOrgGuid(_http);
|
}
|
|
/// <summary>
|
/// 文件上传
|
/// </summary>
|
/// <param name="file"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public async Task<ReturnDto<ExpandoObject>> UploadFile(IFormFile file)
|
{
|
dynamic m = new ExpandoObject();
|
if (file == null)
|
return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success,
|
"上传失败,请重试!");
|
var _ary = file.FileName.Split("~");
|
var _parentGuid = _ary[0];
|
var _fileName = Guid.NewGuid() + _ary[1];
|
var savePath = AppContext.BaseDirectory +
|
AppSettingsHelper.getValueByKey("UploadPath");
|
if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);
|
|
if (file == null || file.Length == 0)
|
return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Exception,
|
"请选择文件");
|
var _fullName = Path.Combine(savePath, _fileName);
|
using (var stream = new FileStream(_fullName, FileMode.Create))
|
{
|
await file.CopyToAsync(stream);
|
m.urlPath = _fileName;
|
}
|
|
if (_ary.Length > 1)
|
{
|
var model = new MesFile();
|
model.Guid = Guid.NewGuid();
|
model.ParentGuid = Guid.Parse(_ary[0]);
|
model.FileTitle = _ary[1];
|
model.UrlPath = _fileName;
|
model.CreateBy = _userCode;
|
model.CreateDate = DateTime.Now;
|
model.FileType = _ary[1].Split('.')[1];
|
if (_ary.Length > 2)
|
model.fileGroup = _ary[2];
|
else
|
model.fileGroup = "";
|
var _bl = base.Insert(model);
|
}
|
|
return ReturnDto<dynamic>.QuickReturn(m, ReturnCode.Success, "上传成功");
|
}
|
|
/// <summary>
|
/// 查询列表,支持分页
|
/// </summary>
|
/// <param name="query"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public ReturnDto<PageList<MesFile>> GetListPage(PageQuery query)
|
{
|
var pageList = new PageList<MesFile>();
|
try
|
{
|
var _sbWhere = new StringBuilder(" 1=1" + query.keyWhere);
|
var _sbBy =
|
new StringBuilder(query.sortName + " " + query.sortOrder);
|
var totalCount = 0;
|
var itemsList = Db.Queryable<MesFile>()
|
.Where(_sbWhere.ToString())
|
.OrderBy(_sbBy.ToString())
|
.ToPageList(query.currentPage, query.everyPageSize,
|
ref totalCount);
|
|
pageList = new PageList<MesFile>(itemsList, totalCount,
|
query.everyPageSize);
|
return ReturnDto<PageList<MesFile>>.QuickReturn(pageList,
|
ReturnCode.Success, "读取成功");
|
}
|
catch (Exception ex)
|
{
|
return ReturnDto<PageList<MesFile>>.QuickReturn(pageList,
|
ReturnCode.Default, ex.Message);
|
}
|
}
|
|
|
/// <summary>
|
/// 删除实体,支持批量删除
|
/// </summary>
|
/// <param name="guid"></param>
|
/// <returns></returns>
|
[RequestMethod(RequestMethods.POST)]
|
public ReturnDto<int?> DeleteModel([FromBody] JArray guidList)
|
{
|
var intArray = guidList.ToObject<string[]>();
|
int? rtnInt = (int)ReturnCode.Default;
|
rtnInt = base.DeleteById(intArray) ? guidList.Count : 0;
|
if (rtnInt > 0)
|
return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Success,
|
"操作成功,共删除" + rtnInt + "条数据!");
|
return ReturnDto<int>.QuickReturn(rtnInt, ReturnCode.Exception,
|
"删除失败,请重试!");
|
}
|
}
|