描述
- 在后端项目中添加导入类,类名规则:实体类名+
Import
,例如:SysUserImport
- 点击导入按钮弹窗导入界面,支持同步和异步两种导入方式
配置
// AddApplicationCore
builder.Services.AddScheduler();
builder.Services.AddTransient<ImportTaskJob>(); // 添加Job
// UseApplication
app.Services.UseScheduler(scheduler =>
{
//每5秒执行一次异步导入
scheduler.Schedule<ImportTaskJob>().EveryFiveSeconds();
});
class ImportTaskJob : IInvocable
{
public Task Invoke() => ImportHelper.ExecuteAsync();
}
示例
// 继承 ImportBase
class SysUserImport(ImportContext context) : ImportBase<SysUser>(context)
{
// 初始化栏位,自动生成导入规范
public override void InitColumns()
{
AddColumn(c => c.UserName);
AddColumn(c => c.Name);
AddColumn(c => c.EnglishName);
AddColumn(c => c.Gender);
AddColumn(c => c.Phone);
AddColumn(c => c.Mobile);
AddColumn(c => c.Email);
AddColumn(c => c.Note);
}
// 读取导入文件,实现导入逻辑
public override async Task<Result> ExecuteAsync(AttachInfo file)
{
var models = new List<SysUser>();
var result = ImportHelper.ReadFile<SysUser>(Context, file, item =>
{
var model = new SysUser
{
UserName = item.GetValue(c => c.UserName),
Name = item.GetValue(c => c.Name),
EnglishName = item.GetValue(c => c.EnglishName),
Gender = item.GetValue(c => c.Gender),
Phone = item.GetValue(c => c.Phone),
Mobile = item.GetValue(c => c.Mobile),
Email = item.GetValue(c => c.Email),
Note = item.GetValue(c => c.Note),
Enabled = true
};
var vr = model.Validate(Context);
if (!vr.IsValid)
item.ErrorMessage = vr.Message;
else
models.Add(model);
});
if (!result.IsValid)
return result;
var database = Database;
var info = await database.GetSystemAsync();
return await database.TransactionAsync(Language.Import, async db =>
{
foreach (var item in models)
{
item.OrgNo = db.User.CompNo;
item.Password = Utils.ToMd5(info?.UserDefaultPwd);
await db.SaveAsync(item);
}
});
}
}