131 lines
3.8 KiB
C#
131 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using MyCode.Project.Infrastructure.Exceptions;
|
|
|
|
namespace MyCode.Project.Infrastructure.Imports
|
|
{
|
|
/// <summary>
|
|
/// 导入Excel数据任务
|
|
/// </summary>
|
|
public class ImportExcelProcess<T>:IImportExcelProcess where T:class ,new()
|
|
{
|
|
/// <summary>
|
|
/// 导入数据对应的配置实体
|
|
/// </summary>
|
|
public ImportConfig<T> Config { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// 导入数据方法
|
|
/// </summary>
|
|
private ImportEvent<T> ImportFunc;
|
|
|
|
/// <summary>
|
|
/// 获取数据方法
|
|
/// </summary>
|
|
private GetDataEvent<T> GetDataFunc;
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="ImportExcelProcess{T}"/>类型的实例
|
|
/// </summary>
|
|
/// <param name="config">导入配置</param>
|
|
/// <param name="func">导入方法</param>
|
|
public ImportExcelProcess(ImportConfig<T> config, ImportEvent<T> func)
|
|
{
|
|
this.Config = config;
|
|
this.ImportFunc = func;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化一个<see cref="ImportExcelProcess{T}"/>类型的实例
|
|
/// </summary>
|
|
/// <param name="config">导入配置</param>
|
|
/// <param name="func">获取数据方法</param>
|
|
public ImportExcelProcess(ImportConfig<T> config, GetDataEvent<T> func)
|
|
{
|
|
this.Config = config;
|
|
this.GetDataFunc = func;
|
|
}
|
|
|
|
#region RunImportExcelProcess(执行导入Excel数据的方法)
|
|
/// <summary>
|
|
/// 执行导入Excel数据的方法
|
|
/// </summary>
|
|
public ImportResult RunImportExcelProcess()
|
|
{
|
|
if (ImportFunc == null) { throw new BaseException("没有找到导入数据的方法!"); }
|
|
|
|
ImportResult result=new ImportResult();
|
|
var msg = "";
|
|
|
|
//验证是否字段都有
|
|
if (!this.Config.ValidFormat(ref msg)) { throw new BaseException(msg); }
|
|
|
|
//获取Excel数据
|
|
var sourceData = this.Config.ExcelUtil.ExcelToList();
|
|
|
|
//将对象转成List<T>
|
|
var resultData = this.Config.GetListData(sourceData);
|
|
|
|
//执行导入的方法
|
|
var tmpResult = this.ImportFunc(resultData);
|
|
|
|
var importResult = tmpResult as ImportResult;
|
|
|
|
if (importResult != null)
|
|
{
|
|
result.InsertCount += importResult.InsertCount;
|
|
result.UpdateCount += (tmpResult as ImportResult).UpdateCount;
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region GetImportExcelData(获取导入Excel数据的方法)
|
|
/// <summary>
|
|
/// 获取导入Excel数据的方法
|
|
/// </summary>
|
|
public List<object> GetImportExcelData()
|
|
{
|
|
if (GetDataFunc == null) { throw new BaseException("没有找到导入数据的方法!"); }
|
|
|
|
var msg = "";
|
|
if (!this.Config.ValidFormat(ref msg))
|
|
{
|
|
throw new BaseException(msg);
|
|
}
|
|
//获取Excel数据
|
|
var sourceData = this.Config.ExcelUtil.ExcelToList();
|
|
|
|
List<object> result = new List<object>();
|
|
|
|
var resultData = this.Config.GetListData(sourceData);
|
|
|
|
if (resultData != null)
|
|
{
|
|
var tmpResult = this.GetDataFunc(resultData);
|
|
|
|
if (tmpResult != null)
|
|
{
|
|
result.AddRange(tmpResult);
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|