feat(control): add grid extended lookup column search

This commit is contained in:
2026-07-31 10:38:40 +08:00
parent e06a67f002
commit 004778f2a8
@@ -38,6 +38,8 @@ namespace Lskj.Control
new Dictionary<string, ExtendedReturnPopupContext>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, DataTable> mExtendedReturnSchemas =
new Dictionary<string, DataTable>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, string> mExtendedReturnSchemaSqls =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, string> mExtendedReturnDisplayFields =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, ExtendedLookupRequestState> mExtendedReturnQueryStates =
@@ -70,8 +72,10 @@ namespace Lskj.Control
public DateTime DueTime;
public string FieldName;
public string Keyword;
public string SearchField;
public string SourceSql;
public string ConnectionString;
public bool SchemaOnly;
public GridColumnModel Model;
public PopupContainerEdit Editor;
public ExtendedReturnPopupContext PopupContext;
@@ -80,6 +84,7 @@ namespace Lskj.Control
private sealed class ExtendedLookupQueryResult
{
public DataTable Schema;
public DataTable Table;
public Exception Error;
}
@@ -340,9 +345,31 @@ namespace Lskj.Control
string actualValueText = GetExtendedReturnBusinessValueText(context);
context.Popup.PrepareForOpen();
string sourceSql = ResolveExtendedReturnSourceSql(model, context.BusinessRow);
DataTable cachedSchema = TryGetExtendedReturnSchema(model.FieldName, sourceSql);
if (cachedSchema != null)
{
ConfigureExtendedReturnSearchColumns(context, cachedSchema);
}
if (actualValueText.Length > 0)
{
StartExtendedReturnSearch(context, actualValueText);
QueueExtendedReturnSearch(
context.OwnerEdit,
context,
sourceSql,
actualValueText,
string.Empty,
false);
}
else if (cachedSchema == null)
{
QueueExtendedReturnSearch(
context.OwnerEdit,
context,
sourceSql,
string.Empty,
string.Empty,
true);
}
else
{
@@ -426,7 +453,7 @@ namespace Lskj.Control
try
{
StartExtendedReturnSearch(context, popup.SearchText);
StartExtendedReturnSearch(context, popup.SearchText, popup.SelectedSearchField);
}
catch (Exception ex)
{
@@ -434,18 +461,27 @@ namespace Lskj.Control
}
}
private void StartExtendedReturnSearch(ExtendedReturnPopupContext context, string keyword)
private void StartExtendedReturnSearch(
ExtendedReturnPopupContext context,
string keyword,
string searchField)
{
if (context == null || context.Model == null || context.OwnerEdit == null) return;
keyword = (keyword ?? string.Empty).Trim();
CancelExtendedReturnSearch(context.Model.FieldName);
context.Popup.ResultGrid.DataSource = null;
context.Popup.ClearResultAndFilter();
if (keyword.Length == 0) return;
ValidateExtendedReturnBusinessConfiguration(context.Model, context.BusinessRow);
string sourceSql = ResolveExtendedReturnSourceSql(context.Model, context.BusinessRow);
QueueExtendedReturnSearch(context.OwnerEdit, context, sourceSql, keyword);
QueueExtendedReturnSearch(
context.OwnerEdit,
context,
sourceSql,
keyword,
searchField,
false);
}
private void ExtendedReturnSearch_ClearRequested(object sender, EventArgs e)
@@ -622,7 +658,9 @@ namespace Lskj.Control
PopupContainerEdit edit,
ExtendedReturnPopupContext context,
string sourceSql,
string keyword)
string keyword,
string searchField,
bool schemaOnly)
{
GridColumnModel model = context.Model;
ExtendedLookupRequest request;
@@ -636,8 +674,10 @@ namespace Lskj.Control
DueTime = DateTime.UtcNow,
FieldName = model.FieldName,
Keyword = keyword,
SearchField = searchField,
SourceSql = sourceSql,
ConnectionString = SqlHelper._connection.ConnectionString,
SchemaOnly = schemaOnly,
Model = model,
Editor = edit,
PopupContext = context,
@@ -781,13 +821,18 @@ namespace Lskj.Control
try
{
DataTable schema = GetExtendedReturnSchema(request.FieldName, request.SourceSql, request.ConnectionString);
result.Schema = schema;
IList<ExtendedReturnFieldMapping> mappings = ExtendedReturnSupport.ParseResultFields(request.Model.ResultFields);
ValidateExtendedReturnSourceColumns(schema.Columns, request.Model, mappings);
string searchSql = ExtendedReturnSupport.BuildSearchSql(request.SourceSql, schema.Columns, ExtendedLookupMaxRows);
result.Table = ExecuteExtendedReturnQuery(
searchSql,
ExtendedReturnSupport.BuildLikeParameterValue(request.Keyword),
request.ConnectionString);
if (!request.SchemaOnly)
{
string searchSql = ExtendedReturnSupport.BuildSearchSql(
request.SourceSql, schema.Columns, ExtendedLookupMaxRows, request.SearchField);
result.Table = ExecuteExtendedReturnQuery(
searchSql,
ExtendedReturnSupport.BuildLikeParameterValue(request.Keyword),
request.ConnectionString);
}
}
catch (Exception ex)
{
@@ -802,7 +847,13 @@ namespace Lskj.Control
lock (mExtendedReturnSyncRoot)
{
DataTable cachedSchema;
if (mExtendedReturnSchemas.TryGetValue(fieldName, out cachedSchema)) return cachedSchema;
string cachedSql;
if (mExtendedReturnSchemas.TryGetValue(fieldName, out cachedSchema) &&
mExtendedReturnSchemaSqls.TryGetValue(fieldName, out cachedSql) &&
string.Equals(cachedSql, sourceSql, StringComparison.Ordinal))
{
return cachedSchema;
}
schemaGeneration = mExtendedReturnSchemaGeneration;
}
@@ -810,15 +861,38 @@ namespace Lskj.Control
lock (mExtendedReturnSyncRoot)
{
DataTable cachedSchema;
if (mExtendedReturnSchemas.TryGetValue(fieldName, out cachedSchema)) return cachedSchema;
string cachedSql;
if (mExtendedReturnSchemas.TryGetValue(fieldName, out cachedSchema) &&
mExtendedReturnSchemaSqls.TryGetValue(fieldName, out cachedSql) &&
string.Equals(cachedSql, sourceSql, StringComparison.Ordinal))
{
return cachedSchema;
}
if (schemaGeneration == mExtendedReturnSchemaGeneration)
{
mExtendedReturnSchemas[fieldName] = schema;
mExtendedReturnSchemaSqls[fieldName] = sourceSql;
}
}
return schema;
}
private DataTable TryGetExtendedReturnSchema(string fieldName, string sourceSql)
{
lock (mExtendedReturnSyncRoot)
{
DataTable cachedSchema;
string cachedSql;
if (mExtendedReturnSchemas.TryGetValue(fieldName, out cachedSchema) &&
mExtendedReturnSchemaSqls.TryGetValue(fieldName, out cachedSql) &&
string.Equals(cachedSql, sourceSql, StringComparison.Ordinal))
{
return cachedSchema;
}
return null;
}
}
private static DataTable ExecuteExtendedReturnQuery(
string commandText,
string keywordParameterValue,
@@ -870,11 +944,13 @@ namespace Lskj.Control
if (state.Version != request.Version) return;
}
ConfigureExtendedReturnSearchColumns(request.PopupContext, result.Schema);
if (result.Error != null)
{
MessageUtil.Show("扩展搜索查询失败:" + result.Error.Message);
return;
}
if (request.SchemaOnly) return;
lock (mExtendedReturnSyncRoot)
{
@@ -904,6 +980,27 @@ namespace Lskj.Control
}
}
private void ConfigureExtendedReturnSearchColumns(
ExtendedReturnPopupContext context,
DataTable sourceSchema)
{
if (context == null || context.Popup == null || context.Model == null || sourceSchema == null) return;
ExtendedReturnSearchPopup popup = context.Popup;
List<KeyValuePair<string, string>> options = new List<KeyValuePair<string, string>>();
IList<ExtendedReturnFieldMapping> mappings =
ExtendedReturnSupport.ParseResultFields(context.Model.ResultFields);
foreach (DataColumn column in sourceSchema.Columns)
{
if (!ExtendedReturnSupport.IsSearchableColumn(column.ColumnName)) continue;
options.Add(new KeyValuePair<string, string>(
column.ColumnName,
GetExtendedReturnColumnCaption(column.ColumnName, mappings)));
}
popup.SetSearchColumns(options);
}
private void ConfigureExtendedReturnViewColumns(ExtendedReturnPopupContext context, DataTable table)
{
if (context == null || context.Popup == null || table == null || context.Model == null) return;
@@ -972,7 +1069,7 @@ namespace Lskj.Control
}
context.OwnerEdit.EditValue = null;
context.Popup.ResultGrid.DataSource = null;
context.Popup.ClearResultAndFilter();
if (context.OwnerEdit.IsPopupOpen) context.OwnerEdit.ClosePopup();
this.GridView.PostEditor();
this.GridView.UpdateCurrentRow();
@@ -1002,7 +1099,7 @@ namespace Lskj.Control
ExtendedReturnPopupContext context = GetExtendedReturnPopupContext(model.FieldName);
CancelExtendedReturnSearch(model.FieldName);
SetExtendedReturnEditorBusinessValue(context);
context.Popup.ResultGrid.DataSource = null;
context.Popup.ClearResultAndFilter();
this.GridView.RefreshRow(this.GridView.FocusedRowHandle);
}
catch (Exception ex)
@@ -1192,12 +1289,13 @@ namespace Lskj.Control
lock (mExtendedReturnSyncRoot)
{
mExtendedReturnSchemas.Clear();
mExtendedReturnSchemaSqls.Clear();
mExtendedReturnSchemaGeneration++;
}
foreach (ExtendedReturnPopupContext context in mExtendedReturnPopupContexts.Values)
{
context.Popup.ResultGrid.DataSource = null;
context.Popup.ClearResultAndFilter();
}
}