You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

380 lines
13 KiB
C#

using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Ys.Core.Common;
using djy.Paas.IService;
using djy.Paas.Model;
using Microsoft.AspNetCore.Authorization;
using System.IO;
using System.Text;
using System.Web;
using System.Net;
namespace djyweb_djyPaasApi.Controllers
{/// <summary>
///
/// </summary>
[AllowAnonymous]
public class ToolsController : ApiBase
{
IToolsService _Itoos = IOC.AddServer<IToolsService>();
private AutoMapper.IMapper _mapp;
public ToolsController(AutoMapper.IMapper mapper)
{
_mapp = mapper;
}
/// <summary>
/// 获取大简云平台的数据字典 portlist港口岗站
/// </summary>
/// <param name="Key"></param>
/// <param name="Dto"></param>
/// <returns></returns>
[HttpGet("getdjydict/{key}")]
public object GetdjyDict(string Key,[FromQuery] ApiFromDto Dto) {
return _Itoos.GetDjyDict(Key,Dto);
}
/// <summary>
///获取加载的配置数据字典
/// </summary>
/// <param name="Key"></param>
/// <returns></returns>
[HttpGet("getdict/{Key}")]
public object Getdict(string Key) {
var rs = new ReturnResult<string>();
try
{
var getdata = _Itoos.GetConfigKey(Key);
rs.Data = getdata;
}
catch { }
rs.OK();
return rs;
}
/// <summary>
/// 获取数据字典键值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[HttpGet("getdict/{GroupName}/{key}")]
public object GetDcit(string GroupName, string Key)
{
var rs = new ReturnResult<tb_sys_Dictionary>();
var get = _Itoos.GetDictionaryList(new tb_sys_Dictionary { GroupName = GroupName,Key=Key }, new PageEntity());
if (get.Data.Count() > 0)
{
rs.Data = get.Data[0];
}
else
{
rs.Data=null;
}
rs.OK();
return rs;
}
/// <summary>
/// 获取数据字典分组
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
[HttpGet("getdictlist/{key}")]
public object Getdictlist(string key)
{
var rs = new ReturnResult<List<tb_sys_Dictionary>>();
rs.Data= _Itoos.GetDictionaryList(new tb_sys_Dictionary { GroupName =key }, new PageEntity() { Limit=9999999}).Data;
rs.OK();
return rs;
}
/// <summary>
/// 获取数据字典
/// </summary>
/// <returns></returns>
[HttpGet("getdictlist")]
public object Getdictlist()
{
var rs = new ReturnResult<List<tb_sys_DictionaryDto>>();
var data = _Itoos.GetDictionaryList(new tb_sys_Dictionary(), new PageEntity() { Limit=9999999}).Data;
rs.Data=_mapp.Map<List<tb_sys_Dictionary>,List<tb_sys_DictionaryDto>> (data);
rs.OK();
return rs;
}
/// <summary>
/// 获取数据字典key标准djydto list
/// </summary>
/// <param name="Key"></param>
/// <returns></returns>
[HttpGet("getdictkeylist/{Key}")]
public object Getdictkeylist(string Key) {
var rs = new ReturnResult<List<DjyDictDto>>();
try
{
var getdata = _Itoos.GetConfigKeyToDictList(Key);
if (Key.ToLower() == "carrierlist".ToLower())
{
var show = _Itoos.GetConfigKey("carrierlist_show");
if (show.IsNotNull()) {
var showlist = show.Split(',').ToList();
getdata = getdata.Where(w => showlist.Contains(w.Code)).ToList();
}
}
rs.Data = getdata;
if (getdata==null||getdata.Count==0)
{//尝试从Enum中获取
var list = new List<DjyDictDto>();
if (Key.ToLower() == "WorkStatus".ToLower()) {
foreach (EnumUser.WorkStatus item in Enum.GetValues(typeof(EnumUser.WorkStatus)))
{
list.Add(new DjyDictDto {Code= ((int)item).ToString() ,Name=item.GetEnumText() });
}
}
rs.Data = list;
}
}
catch { }
rs.OK();
return rs;
}
/// <summary>
/// Excel数据自动解析读取
/// </summary>
/// <returns></returns>
[HttpPost("excelread")]
public object ExcelRead()
{
var rs = new ReturnResult<object>();
try
{
var Files = Request.Form.Files;
var datalist =new List<Dictionary<string,string>>();
if (Files.Count > 0)
{
IWorkbook work = null;
var file = Files.First();
string[] suffix_Array = { "xls", "xlsx" };
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
//获取拓展名
var suf = fileName.Split('.');
string suffix = suf[suf.Length - 1];
if (!suffix_Array.Contains(suffix.ToLower()))
{
return new ReturnResult<object> { Message = "此文件格式不允许上传" };
}
var filestream = file.OpenReadStream();
if (suffix == "xls") {
work = new HSSFWorkbook(filestream);
}
else
{
work = new XSSFWorkbook(filestream);
}
var shell = work.GetSheetAt(0);
var titlerow = shell.GetRow(0).Cells;
for (int i = 1; i <=shell.LastRowNum; i++) {
var rows = shell.GetRow(i);
var val = new Dictionary<string, string>();
for (int ci= 0; ci <titlerow.Count;ci++){
var rowhead = "c" + ci.ToString();
try
{
var newhead = titlerow[ci].ToString();
if (val.Where(w => w.Key == newhead).Count() == 0) {
rowhead = newhead;
}
if (rowhead.IsNotNull()) {
try {
var cell = rows.GetCell(ci);
val.Add(rowhead, cell.ToString());
}
catch
{ val.Add(rowhead, ""); }
}
}
catch { }
}
if (val.Count > 0&&val.Where(w=>w.Value.Length>0).Count()>0)
{
datalist.Add(val);
}
}
rs.Data = datalist;
rs.OK();
work.Close();
}
else
{
rs.Not("没有获取到上传文件!");
}
}
catch(Exception ex) { rs.Not("错误的Excel文件");rs.MemoData = ex.Message; }
return rs;
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="Name">路径</param>
/// <returns></returns>
[HttpGet("download")]
public FileResult DownLoad([FromQuery] string Name) {
Name = WebUtility.UrlDecode(Name);
if (!System.IO.File.Exists(Name))
{
throw new Exception("找不到此文件");
}
if (!Name.StartsWith("userdata"))
{
throw new Exception("非法请求");
}
var contenttype = "application/octet-stream";
var Filedata = System.IO.File.ReadAllBytes(Name);
var filename = "";
if (Name.IndexOf('/') > -1)
{
var fl = Name.Split('/');
filename = fl[fl.Length - 1];
}
else
{
var fl = Name.Split('\\');
filename = fl[fl.Length - 1];
}
var ext = filename.GetExt();
if (ext.IndexOf("xls")>=0)
{
contenttype = "application/ms-excel";
}
return File(Filedata, contenttype, filename);
}
/// <summary>
/// 文件上传
/// </summary>
[HttpPost("upfile")]
public object UpFile()
{
var rs = new ReturnResult<List<FileListDto>>();
var upfilelist=new List<FileListDto>();
try
{
/// <summary>
/// 图片文件类型
/// </summary>
var suffix_Array_img = new string[] { "png", "jpg", "jpeg", "bmp", "gif", "ico" };
/// <summary>
/// 短视频文件类型
/// </summary>
var suffixArray_viedo = new string[] { "mp4", "mp3" };
/// <summary>
/// 音乐
/// </summary>
var suffixArray_music = new string[] { "mp3" };
//文件类型
var suffixArray_file = new string[] { "doc", "docx", "pdf", "xls", "xlsx" };
var files = Request.Form.Files;
var size = files.Sum(f => f.Length);
var maxsize = 8000 * 1000 * 100;//最大不能超过。。
if (files.Count == 0)
{
rs.Not("没有提交文件!");
return rs;
}
if (size > maxsize)
{
rs.Not($"文件过大,超过了{(maxsize / 1024.00 / 1024.00).ToString()}MB");
return rs;
}
List<string> filePathResultList = new List<string>();
foreach (var file in files)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
//获取拓展名
var suf = fileName.Split('.');
string suffix = suf[suf.Length - 1];
var suffixlist = new List<string>();
suffixlist.AddRange(suffix_Array_img);
suffixlist.AddRange(suffixArray_viedo);
suffixlist.AddRange(suffixArray_music);
suffixlist.AddRange(suffixArray_file);
if (!suffixlist.Contains(suffix.ToLower()))
{
rs.Not(suffix + "此文件格式不允许上传");
}
}
var filepath = "userdata/upfile/";
Directory.CreateDirectory(filepath);
//保存文件
foreach (var file in files)
{
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"').TrimAll();
//获取拓展名
var suf = fileName.Split('.');
string suffix = suf[suf.Length - 1];
var _filename = filepath + YsTools.GetNewNo() + fileName;
upfilelist.Add(new FileListDto {Name= suf[0], Url= _filename });
var f = file.OpenReadStream();
using (FileStream stream = System.IO.File.Create(_filename))
{
byte[] filebyte = new byte[f.Length];
f.Read(filebyte, 0, filebyte.Length);
stream.Write(filebyte);
stream.Close();
}
}
rs.Data= upfilelist;
rs.OK("上传成功!");
}
catch (Exception ex)
{
rs.Not(ex.Message);
}
return rs;
}
}
}