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.

218 lines
7.1 KiB
C#

5 months ago
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
{
/// <summary>
/// 日 期: 2024-06-18
/// 描 述: excel导入接口
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class ImportController : ControllerBase
{
private readonly ImportApp _app;
public ImportController(ImportApp app)
{
_app = app;
}
/// <summary>
/// 获取导入的分页数据
/// </summary>
/// <param name="keyWord">查询关键字</param>
/// <param name="moduleId">菜单id</param>
/// <param name="page"></param>
/// <param name="limit"></param>
/// <returns></returns>
[HttpGet]
public async Task<Response<PageInfo<List<ExcelImport>>>> LoadExcelImportPage(string keyWord, string moduleId, int page, int limit)
{
return await _app.LoadExcelImportPage(keyWord, moduleId, page, limit);
}
/// <summary>
/// 获取表单数据
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<Response<ExcelImportDto>> GetForms(string id)
{
var result = new Response<ExcelImportDto>();
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;
}
/// <summary>
/// 获取导入配置列表根据模块ID
/// </summary>
/// <param name="moduleId">功能模块主键</param>
/// <returns></returns>
[HttpGet]
public async Task<Response<List<ExcelImport>>> GetModuleList(string moduleId)
{
var result = new Response<List<ExcelImport>>();
try
{
result.Result = await _app.GetModuleList(moduleId);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpPost]
public async Task<Response<bool>> Delete(string id)
{
var result = new Response<bool>();
try
{
result = await _app.DeleteEntity(id);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.InnerException?.Message ?? ex.Message;
}
return result;
}
/// <summary>
/// 新增
/// </summary>
///<param name="dto">数据</param>
/// <returns></returns>
[HttpPost]
public async Task<Response<bool>> AddExcelImport(ExcelImportDto dto)
{
var result = new Response<bool>();
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;
}
/// <summary>
/// 更新数据
/// </summary>
///<param name="dto">数据</param>
/// <returns></returns>
[HttpPost]
public async Task<Response<bool>> UpdateExcelImport(string id, ExcelImportDto dto)
{
var result = new Response<bool>();
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 扩展方法
/// <summary>
/// 下载文件
/// </summary>
/// <param name="keyValue">文件id</param>
/// <returns></returns>
[HttpGet]
public async Task<FileStreamResult> 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<ColumnModel>();
//表头
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);
}
/// <summary>
/// excel文件导入通用
/// </summary>
/// <param name="templateId">模板Id</param>
/// <param name="fileId">文件主键</param>
/// <param name="chunks">分片数</param>
/// <param name="ext">文件扩展名</param>
/// <returns></returns>
[HttpPost]
public async Task<Response<bool>> ExecuteImportExcel(string templateId, string path)
{
var result = new Response<bool>();
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
}
}