1- using BotSharp . Abstraction . Files . Enums ;
2- using BotSharp . Abstraction . Files . Models ;
3- using BotSharp . Abstraction . Files . Utilities ;
4- using BotSharp . Abstraction . Routing ;
5- using BotSharp . Plugin . ExcelHandler . Models ;
6- using BotSharp . Plugin . ExcelHandler . Services ;
1+ using BotSharp . Plugin . ExcelHandler . Settings ;
72using NPOI . SS . UserModel ;
83using NPOI . XSSF . UserModel ;
94using System . Linq . Dynamic . Core ;
105
116namespace BotSharp . Plugin . ExcelHandler . Functions ;
127
13- public class HandleExcelRequestFn : IFunctionCallback
8+ public class ReadExcelFn : IFunctionCallback
149{
1510 public string Name => "util-excel-handle_excel_request" ;
16- public string Indication => "Handling excel request " ;
11+ public string Indication => "Reading excel" ;
1712
18- private readonly IServiceProvider _serviceProvider ;
13+ private readonly IServiceProvider _services ;
1914 private readonly IFileStorageService _fileStorage ;
20- private readonly ILogger < HandleExcelRequestFn > _logger ;
15+ private readonly ILogger < ReadExcelFn > _logger ;
2116 private readonly BotSharpOptions _options ;
22- private readonly IMySqlService _mySqlService ;
17+ private readonly IDbService _dbService ;
18+ private readonly ExcelHandlerSettings _settings ;
2319
20+ private HashSet < string > _excelFileTypes ;
2421
25- private HashSet < string > _excelMimeTypes ;
26- private double _excelRowSize = 0 ;
27- private double _excelColumnSize = 0 ;
28- private string _tableName = "tempTable" ;
29- private string _currentFileName = string . Empty ;
30- private List < string > _headerColumns = new List < string > ( ) ;
31- private List < string > _columnTypes = new List < string > ( ) ;
32-
33- public HandleExcelRequestFn (
34- IServiceProvider serviceProvider ,
35- IFileStorageService fileStorage ,
36- ILogger < HandleExcelRequestFn > logger ,
22+ public ReadExcelFn (
23+ IServiceProvider services ,
24+ ILogger < ReadExcelFn > logger ,
3725 BotSharpOptions options ,
38- IMySqlService mySqlService
39- )
26+ ExcelHandlerSettings settings ,
27+ IFileStorageService fileStorage ,
28+ IEnumerable < IDbService > dbServices )
4029 {
41- _serviceProvider = serviceProvider ;
42- _fileStorage = fileStorage ;
30+ _services = services ;
4331 _logger = logger ;
4432 _options = options ;
45- _mySqlService = mySqlService ;
33+ _settings = settings ;
34+ _fileStorage = fileStorage ;
35+ _dbService = dbServices . FirstOrDefault ( x => x . Provider == _settings . DbProvider ) ;
4636 }
4737
48-
4938 public async Task < bool > Execute ( RoleDialogModel message )
5039 {
5140 var args = JsonSerializer . Deserialize < LlmContextIn > ( message . FunctionArgs , _options . JsonSerializerOptions ) ;
52- var conv = _serviceProvider . GetRequiredService < IConversationService > ( ) ;
53- var states = _serviceProvider . GetRequiredService < IConversationStateService > ( ) ;
54- var routingCtx = _serviceProvider . GetRequiredService < IRoutingContext > ( ) ;
41+ var conv = _services . GetRequiredService < IConversationService > ( ) ;
42+ var states = _services . GetRequiredService < IConversationStateService > ( ) ;
43+ var routingCtx = _services . GetRequiredService < IRoutingContext > ( ) ;
5544
56- if ( _excelMimeTypes . IsNullOrEmpty ( ) )
57- {
58- _excelMimeTypes = FileUtility . GetMimeFileTypes ( new List < string > { "excel" , "spreadsheet" } ) . ToHashSet < string > ( ) ;
59- }
45+ Init ( ) ;
6046
6147 var dialogs = routingCtx . GetDialogs ( ) ;
6248 if ( dialogs . IsNullOrEmpty ( ) )
@@ -71,15 +57,23 @@ public async Task<bool> Execute(RoleDialogModel message)
7157 return true ;
7258 }
7359
74- var resultList = GetResponeFromDialogs ( dialogs ) ;
75- message . Content = GenerateSqlExecutionSummary ( resultList ) ;
60+ var results = GetResponeFromDialogs ( dialogs ) ;
61+ message . Content = GenerateSqlExecutionSummary ( results ) ;
7662 states . SetState ( "excel_import_result" , message . Content ) ;
7763 dialogs . ForEach ( x => x . Files = null ) ;
7864 return true ;
7965 }
8066
8167
8268 #region Private Methods
69+ private void Init ( )
70+ {
71+ if ( _excelFileTypes . IsNullOrEmpty ( ) )
72+ {
73+ _excelFileTypes = FileUtility . GetMimeFileTypes ( [ "excel" , "spreadsheet" ] ) . ToHashSet ( ) ;
74+ }
75+ }
76+
8377 private bool AssembleFiles ( string conversationId , List < RoleDialogModel > dialogs )
8478 {
8579 if ( dialogs . IsNullOrEmpty ( ) )
@@ -88,7 +82,7 @@ private bool AssembleFiles(string conversationId, List<RoleDialogModel> dialogs)
8882 }
8983
9084 var messageIds = dialogs . Select ( x => x . MessageId ) . Distinct ( ) . ToList ( ) ;
91- var contentTypes = FileUtility . GetContentFileTypes ( mimeTypes : _excelMimeTypes ) ;
85+ var contentTypes = FileUtility . GetContentFileTypes ( mimeTypes : _excelFileTypes ) ;
9286 var excelFiles = _fileStorage . GetMessageFiles ( conversationId , messageIds , options : new ( )
9387 {
9488 Sources = [ FileSource . User ] ,
@@ -123,59 +117,61 @@ private bool AssembleFiles(string conversationId, List<RoleDialogModel> dialogs)
123117
124118 private List < SqlContextOut > GetResponeFromDialogs ( List < RoleDialogModel > dialogs )
125119 {
120+ var sqlCommands = new List < SqlContextOut > ( ) ;
126121 var dialog = dialogs . Last ( x => ! x . Files . IsNullOrEmpty ( ) ) ;
127- var sqlCommandList = new List < SqlContextOut > ( ) ;
122+
128123 foreach ( var file in dialog . Files )
129124 {
130- if ( file == null || string . IsNullOrWhiteSpace ( file . FileStorageUrl ) ) continue ;
125+ if ( string . IsNullOrWhiteSpace ( file ? . FileStorageUrl ) )
126+ {
127+ continue ;
128+ }
131129
132130 string extension = Path . GetExtension ( file . FileStorageUrl ) ;
133- if ( ! _excelMimeTypes . Contains ( extension ) ) continue ;
134-
135- _currentFileName = Path . GetFileName ( file . FileStorageUrl ) ;
131+ if ( ! _excelFileTypes . Contains ( extension ) )
132+ {
133+ continue ;
134+ }
136135
137136 var binary = _fileStorage . GetFileBytes ( file . FileStorageUrl ) ;
138- var workbook = ConvertToWorkBook ( binary . ToArray ( ) ) ;
137+ var workbook = ConvertToWorkBook ( binary ) ;
139138
140- var currentCommandList = _mySqlService . WriteExcelDataToDB ( workbook ) ;
141- sqlCommandList . AddRange ( currentCommandList ) ;
139+ var currentCommands = _dbService . WriteExcelDataToDB ( workbook ) ;
140+ sqlCommands . AddRange ( currentCommands ) ;
142141 }
143- return sqlCommandList ;
142+ return sqlCommands ;
144143 }
145144
146- private string GenerateSqlExecutionSummary ( List < SqlContextOut > messageList )
145+ private string GenerateSqlExecutionSummary ( List < SqlContextOut > results )
147146 {
148147 var stringBuilder = new StringBuilder ( ) ;
149- if ( messageList . Any ( x => x . isSuccessful ) )
148+ if ( results . Any ( x => x . isSuccessful ) )
150149 {
151150 stringBuilder . Append ( "---Success---" ) ;
152151 stringBuilder . Append ( "\r \n " ) ;
153- foreach ( var message in messageList . Where ( x => x . isSuccessful ) )
152+ foreach ( var result in results . Where ( x => x . isSuccessful ) )
154153 {
155- stringBuilder . Append ( message . Message ) ;
154+ stringBuilder . Append ( result . Message ) ;
156155 stringBuilder . Append ( "\r \n \r \n " ) ;
157156 }
158157 }
159- if ( messageList . Any ( x => ! x . isSuccessful ) )
158+ if ( results . Any ( x => ! x . isSuccessful ) )
160159 {
161160 stringBuilder . Append ( "---Failed---" ) ;
162161 stringBuilder . Append ( "\r \n " ) ;
163- foreach ( var message in messageList . Where ( x => ! x . isSuccessful ) )
162+ foreach ( var result in results . Where ( x => ! x . isSuccessful ) )
164163 {
165- stringBuilder . Append ( message . Message ) ;
164+ stringBuilder . Append ( result . Message ) ;
166165 stringBuilder . Append ( "\r \n " ) ;
167166 }
168167 }
169168 return stringBuilder . ToString ( ) ;
170169 }
171170
172- private IWorkbook ConvertToWorkBook ( byte [ ] bytes )
171+ private IWorkbook ConvertToWorkBook ( BinaryData binary )
173172 {
174- IWorkbook workbook ;
175- using ( var fileStream = new MemoryStream ( bytes ) )
176- {
177- workbook = new XSSFWorkbook ( fileStream ) ;
178- }
173+ using var fileStream = new MemoryStream ( binary . ToArray ( ) ) ;
174+ IWorkbook workbook = new XSSFWorkbook ( fileStream ) ;
179175 return workbook ;
180176 }
181177 #endregion
0 commit comments