using DocumentFormat.OpenXml.Office2010.Excel;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Helpers.Excel;
using Infrastructure.Helpers.Excel.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.CodeTable;
using OpenAuth.App.CodeTable.Response;
using OpenAuth.App.Import;
using OpenAuth.App.Import.Response;
using OpenAuth.Repository.Domain;
using System.Data;
using System.Linq;
namespace OpenAuth.WebApi.Controllers
{
///
/// 日 期: 2024-06-18
/// 描 述: excel导入接口
///
[Route("api/[controller]/[action]")]
[ApiController]
public class ImportController : ControllerBase
{
private readonly ImportApp _app;
public ImportController(ImportApp app)
{
_app = app;
}
///
/// 获取导入的分页数据
///
/// 查询关键字
/// 菜单id
///
///
///
[HttpGet]
public async Task>>> LoadExcelImportPage(string keyWord, string moduleId, int page, int limit)
{
return await _app.LoadExcelImportPage(keyWord, moduleId, page, limit);
}
///
/// 获取表单数据
///
/// 主键
///
[HttpGet]
public async Task> GetForms(string id)
{
var result = new Response();
try
{
result.Result = new ExcelImportDto();
result.Result.Entity = await _app.GetEntity(id);
result.Result.List = await _app.GetFieldList(id);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
///
/// 获取导入配置列表根据模块ID
///
/// 功能模块主键
///
[HttpGet]
public async Task>> GetModuleList(string moduleId)
{
var result = new Response>();
try
{
result.Result = await _app.GetModuleList(moduleId);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
///
/// 删除数据
///
/// 主键
///
[HttpPost]
public async Task> Delete(string id)
{
var result = new Response();
try
{
result = await _app.DeleteEntity(id);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
///
/// 新增
///
///数据
///
[HttpPost]
public async Task> AddExcelImport(ExcelImportDto dto)
{
var result = new Response();
try
{
result = await _app.SaveEntity(string.Empty, dto.Entity, dto.List);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
///
/// 更新数据
///
///数据
///
[HttpPost]
public async Task> UpdateExcelImport(string id, ExcelImportDto dto)
{
var result = new Response();
try
{
result = await _app.SaveEntity(id, dto.Entity, dto.List);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
#region 扩展方法
///
/// 下载文件
///
/// 文件id
///
[HttpGet]
public async Task DownSchemeFile(string id)
{
var templateInfo = await _app.GetEntity(id);
var fileds = await _app.GetFieldList(id);
//设置导出格式
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = templateInfo.Name + ".xls";
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnEntity = new List();
//表头
DataTable dt = new DataTable();
foreach (var col in fileds)
{
if (col.RelationType != 1 && col.RelationType != 4 && col.RelationType != 5 && col.RelationType != 6 && col.RelationType != 7)
{
excelconfig.ColumnEntity.Add(new ColumnModel()
{
Column = col.Name,
ExcelColumn = col.ColName,
Alignment = "center",
});
dt.Columns.Add(col.Name, typeof(string));
}
}
return File(ExcelHelper.ExportMemoryStream(dt, excelconfig), "application/ms-excel", excelconfig.FileName);
}
///
/// excel文件导入(通用)
///
/// 模板Id
/// 文件主键
/// 分片数
/// 文件扩展名
///
[HttpPost]
public async Task> ExecuteImportExcel(string templateId, string path)
{
var result = new Response();
try
{
// 获取excel文件数据(暂时只支持但sheet导入,后续升级)
DataTable dt = ExcelHelper.ExcelImport(path);
var res2 = await _app.ImportTable(templateId, dt);
var data = new
{
Success = res2.snum,
Fail = res2.fnum,
data = res2.elist
};
result.Result = true;
return result;
}
catch (Exception ex) { }
{
result.Result = false;
result.Message = "导入数据失败!";
return result;
}
}
#endregion
}
}