66 lines
2.1 KiB
C#
66 lines
2.1 KiB
C#
using CloudBuilder.AI.Service;
|
|
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
|
using CloudBuilder.Core.DependencyInjection.Task;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Transactions;
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
namespace CloudBuilder.Topshelf.Task.AI
|
|
{
|
|
//task:CharacterInsertTask encoding:GB2312 file_path:D:\NET8\AI\Novel.txt
|
|
public class CharacterInsertTask : IScheduleTask
|
|
{
|
|
private readonly IApplicationService service;
|
|
|
|
public CharacterInsertTask(IApplicationService service)
|
|
{
|
|
this.service = service;
|
|
}
|
|
|
|
public void Run(Dictionary<string, string> bodyDict)
|
|
{
|
|
IAiCharacterService aiCharacterService = service.ServiceProvider.GetService<IAiCharacterService>();
|
|
|
|
string encoding = "utf-8";
|
|
|
|
if (bodyDict.ContainsKey("encoding"))
|
|
encoding = bodyDict["encoding"];
|
|
else
|
|
Console.WriteLine("文件字符集"+ encoding);
|
|
|
|
string fileName = null;
|
|
if (bodyDict.ContainsKey("encoding"))
|
|
fileName = bodyDict["file_path"];
|
|
else
|
|
Console.WriteLine("文件路径不存在");
|
|
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
string content = File.ReadAllText(fileName, Encoding.GetEncoding(encoding));
|
|
|
|
var transactionOptions = new TransactionOptions
|
|
{
|
|
IsolationLevel = IsolationLevel.ReadCommitted,
|
|
Timeout = TimeSpan.FromSeconds(60 * 10)
|
|
};
|
|
|
|
using (TransactionScope _transactionScope = new TransactionScope(
|
|
TransactionScopeOption.Required,
|
|
transactionOptions,
|
|
TransactionScopeAsyncFlowOption.Enabled))
|
|
{
|
|
aiCharacterService.Insert(content);
|
|
|
|
_transactionScope.Complete();
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|