using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Yh.Platform.Abstractions;
using YhCore.Common;
using YhCore.Common.Config;
using YhCore.Common.UserContext;
using YhCore.Entity;
using YhCore.Model.Dto;
using YhCore.Service.Interfaces;
namespace YhCore.WebApi.Controllers.Api.Common
{
///
/// 公共字典 / 文件(对应旧 Areas/Common/CommonController,路由 api/base)。
/// 字典部分 8 接口 + 文件部分 4 接口(FileDown / FilePreview / FileUpload / FileUploadImage)已落地;
/// 旧 [NoAuth] 语义:无需用户上下文,schId 由前端传入或取当前用户。
///
[ApiController]
[Route(ApiRoutes.ApiPath + "/base")]
public class CommonController : ControllerBase
{
private readonly IDictionaryService _dict;
private readonly IFileService _file;
public CommonController(IDictionaryService dict, IFileService file)
{
_dict = dict;
_file = file;
}
/// 获取指定字典数据(Type 1~10)。
[HttpGet("GetSysDic")]
public async Task> GetSysDic([FromQuery] int type = 0)
{
var typeId = DictionaryTypeHelper.FromSysDicType(type);
if (typeId == null)
{
throw new DException("参数错误");
}
return await _dict.GetDictListAsync(typeId, SchSerID());
}
/// 字典类别列表([NoAuth],排除部门)。
[HttpGet("GetDicTypeList")]
public async Task> GetDicTypeList() => await _dict.GetDicTypeListAsync();
/// 字典列表管理(Type=0/DEPT 取全校;其他仅超管 bestsch)。
[HttpGet("GetDictMangeList")]
public async Task> GetDictMangeList([FromQuery] string type)
{
var typeId = DictionaryTypeHelper.ResolveTypeId(type);
if (typeId == null)
{
throw new DException("参数错误");
}
if (DictionaryTypeHelper.IsDept(typeId))
{
return await _dict.GetDictMangeListAsync("DEPT", SchSerID());
}
//if (UserContextHolder.Current.() =1 )
//{
// throw new DException("非超管不得操作!");
//}
return await _dict.GetDictMangeListAsync(typeId, "");
}
/// 添加字典。
[HttpPost("AddDic")]
public async Task AddDic([FromBody] Common_Dictionary model)
{
if (model == null) throw new DException("参数错误!");
var info = new Common_Dictionary
{
typeID = model.typeID,
typeName = DictionaryTypeHelper.GetTypeName(model.typeID),
Name = model.Name,
Schserid = SchSerID()
};
await _dict.AddDicAsync(info);
}
/// 更新字典。
[HttpPost("UpdateDic")]
public async Task UpdateDic([FromBody] Common_Dictionary model)
{
if (model == null) throw new DException("参数有误!");
model.Schserid = SchSerID();
await _dict.UpdateDicAsync(model);
}
/// 编辑字典名称。
[HttpPost("EditDic")]
public async Task EditDic([FromBody] Common_Dictionary model)
{
if (model == null) throw new DException("参数有误!");
model.Schserid = SchSerID();
await _dict.EditDicAsync(model);
}
/// 添加发文部门。
[HttpPost("AddDeptDic")]
public async Task AddDeptDic([FromBody] Common_Dictionary model)
{
if (model == null) throw new DException("参数有误!");
var info = new Common_Dictionary
{
typeID = "DEPT",
typeName = "部门类别",
Name = model.Name,
Schserid = SchSerID()
};
await _dict.AddDicAsync(info);
}
/// 更新发文部门。
[HttpPost("UpdateDeptDic")]
public async Task UpdateDeptDic([FromBody] Common_Dictionary model)
{
if (model == null) throw new DException("参数有误!");
model.Schserid = SchSerID();
await _dict.UpdateDicAsync(model);
}
#region 文件:下载 / 预览 / 上传
///
/// 公用下载文件(旧 FileDown,[NoAuth])。yl=true 且为图片/PDF 时重定向到文件 URL(webapp 预览),
/// 否则物理文件下载。
///
[HttpGet("FileDown")]
public async Task FileDown([FromQuery] int id, [FromQuery] bool yl = false)
{
if (id == 0)
return Ok();
var file = await _file.GetEnclosureAsync(id);
string[] previewArry = { ".jpg", ".gif", ".png", ".pdf", ".jpeg", ".bmp" };
if (yl)
{
// 旧 sch_weboa_url.Replace("notice/api", AppType + "/") + fileurl → 新 FileWebPath + fileurl
string fileUrl = (Config.FileWebPath ?? "") + file.fileurl;
string suffName = Path.GetExtension(fileUrl);
if (previewArry.Any(x => x == suffName.ToLower()))
return Redirect(fileUrl);
}
// WebHelper.DownFile:物理文件下载
string filePath = _file.MapPhysicalPath(file.fileurl);
if (!System.IO.File.Exists(filePath))
return Content("请求的文件不存在!");
string contentType = GetContentType(filePath);
string outputFileName = file.filename ?? "file";
return PhysicalFile(filePath, contentType, outputFileName);
}
///
/// 公用预览 office 文件(旧 FilePreview / CommonPreview,[NoAuth])。
/// type=1 → 返回可用预览 url(本地 .pdf 存在优先);type=2 → 重定向本地 pdf 或 officeapps.live.com;
/// 其余 → 重定向替换后缀为 .pdf 的地址。跳转返回 RedirectResult(全局包装过滤器不拦截)。
///
[HttpGet("FilePreview")]
public async Task