You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
9.2 KiB
278 lines
9.2 KiB
using AIK.Common.SysCommon;
|
|
using AIK.Models;
|
|
using AIK.Models.SystemSettings;
|
|
using AIK.Service.IService;
|
|
using AIK.Service.Service;
|
|
using AIK.ViewModels.CommonViewModel;
|
|
using AIK.ViewModels.Messages;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace AIK.ViewModels.RemoteGeneration
|
|
{
|
|
public partial class RemoteGenerationViewModel : ObservableObject, IRecipient<LocalLanguageMessage>, ILoadAware
|
|
{
|
|
private readonly IVehDataService _vehDataService;
|
|
private readonly IConfigurationService _configurationService;
|
|
private readonly ILogger<RemoteGenerationViewModel> _logger;
|
|
private List<AlphabetGroup> groups = new();
|
|
[ObservableProperty]
|
|
private ObservableCollection<AlphabetGroup> _filteredGroups = new();
|
|
private readonly IMessenger _messenger;
|
|
[ObservableProperty]
|
|
private string searchText = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private CarfactoryClass? _selectedItem;
|
|
|
|
[ObservableProperty]
|
|
private bool _isLoading = false;
|
|
|
|
private object _midContent;
|
|
public object MidContent
|
|
{
|
|
get => _midContent;
|
|
set => SetProperty(ref _midContent, value);
|
|
}
|
|
[ObservableProperty]
|
|
private CacheInfo _cacheInfo = new CacheInfo();
|
|
[ObservableProperty]
|
|
private bool _isCacheValid;
|
|
|
|
public KeyModelViewModel KeyModelVM { get; }
|
|
|
|
private bool isSingletonInstance = false;
|
|
public RemoteGenerationViewModel(IVehDataService vehDataService, IConfigurationService configurationService,
|
|
IMessenger messenger, KeyModelViewModel _keyModelVM, ILogger<RemoteGenerationViewModel> logger)
|
|
{
|
|
_configurationService = configurationService;
|
|
_vehDataService = vehDataService;
|
|
_logger = logger;
|
|
_messenger = messenger;
|
|
KeyModelVM = _keyModelVM;
|
|
// 注册消息接收
|
|
//_messenger.Register<RequestUpdateMessage>(this);
|
|
WeakReferenceMessenger.Default.Register<LocalLanguageMessage>(this);
|
|
InitializeUI();
|
|
//_ = InitializeData();
|
|
//var s= MacAddressHelper.GetPrimaryMacAddress();
|
|
|
|
}
|
|
#region 初始化
|
|
private void InitializeUI()
|
|
{
|
|
//Type? type = Assembly.Load("AIK.Views")
|
|
// .GetType("AIK.Views.MenuPages." + "CommonPage.KeyModelPage");
|
|
|
|
//if (type != null)
|
|
//{
|
|
// MidContent = Activator.CreateInstance(type);
|
|
//}
|
|
// 快速显示空分组
|
|
groups = CreateEmptyGroups();
|
|
FilteredGroups = new ObservableCollection<AlphabetGroup>(groups);
|
|
}
|
|
|
|
private List<AlphabetGroup> CreateEmptyGroups()
|
|
{
|
|
var list = new List<AlphabetGroup>();
|
|
for (char c = 'A'; c <= 'Z'; c++)
|
|
{
|
|
list.Add(new AlphabetGroup { Letter = c.ToString() });
|
|
}
|
|
return list;
|
|
}
|
|
public async Task OnLoadedAsync()
|
|
{
|
|
if (!isSingletonInstance)
|
|
{
|
|
await LoadDataInBackground();
|
|
isSingletonInstance = true;
|
|
}
|
|
|
|
}
|
|
private async Task LoadDataInBackground()
|
|
{
|
|
try
|
|
{
|
|
var cardata = await _vehDataService.GetVehDataAsync("car", false);
|
|
cardata = cardata.OrderBy(a => a.py.Substring(0, 1)).ToList();
|
|
if (cardata != null && cardata.Count > 0)
|
|
{
|
|
foreach (var item in cardata)
|
|
{
|
|
var firstName = item.py.Substring(0, 1).ToUpper();
|
|
var model = groups.Find(a => a.Letter == firstName);
|
|
item.IsSelected = false;
|
|
if (model != null)
|
|
{
|
|
model.Items.Add(item);
|
|
}
|
|
}
|
|
var carBrandModel = cardata.FirstOrDefault();
|
|
if (carBrandModel != null)
|
|
{
|
|
carBrandModel.IsSelected = true;
|
|
_messenger.Send(new CarSelectMessage(carBrandModel));
|
|
UpdateFilteredGroups();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
FilteredGroups = new ObservableCollection<AlphabetGroup>();
|
|
_messenger.Send(new CarSelectMessage(null));
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "加载汽车品牌数据时出错");
|
|
throw;
|
|
}
|
|
//finally { IsLoading = false; }
|
|
|
|
}
|
|
#endregion
|
|
#region 消息传递
|
|
[RelayCommand]
|
|
private void RequestUpdateB(CarfactoryClass carBrand)
|
|
{
|
|
if (carBrand != null)
|
|
{
|
|
SelectedItem = carBrand;
|
|
foreach (var group in FilteredGroups)
|
|
{
|
|
foreach (var item in group.Items)
|
|
{
|
|
item.IsSelected = false;
|
|
}
|
|
}
|
|
carBrand.IsSelected = true;
|
|
// 发送汽车品牌选择消息
|
|
_messenger.Send(new CarSelectMessage(carBrand));
|
|
|
|
}
|
|
}
|
|
#region 留做全局通知处理
|
|
// 接收请求更新消息
|
|
//public void Receive(RequestUpdateMessage message)
|
|
//{
|
|
// // 处理来自其他组件的更新请求
|
|
// //RefreshData();
|
|
//}
|
|
//[RelayCommand]
|
|
//private void RefreshData()
|
|
//{
|
|
// InitializeData();
|
|
//}
|
|
#endregion
|
|
#endregion
|
|
|
|
#region 页面方法
|
|
|
|
[RelayCommand]
|
|
private void Search()
|
|
{
|
|
UpdateFilteredGroups();
|
|
}
|
|
private void UpdateFilteredGroups()
|
|
{
|
|
FilteredGroups.Clear();
|
|
var TempList = new List<AlphabetGroup>();
|
|
if (string.IsNullOrWhiteSpace(SearchText))
|
|
{
|
|
// 显示所有非空分组
|
|
foreach (var group in groups.Where(g => g.Items.Any()))
|
|
{
|
|
TempList.Add(group);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var searchLower = SearchText.ToLower();
|
|
|
|
foreach (var group in groups)
|
|
{
|
|
var filteredItems = group.Items
|
|
.Where(item =>
|
|
item.brandName?.ToLower().Contains(searchLower) == true ||
|
|
item.engName?.ToLower().Contains(searchLower) == true ||
|
|
item.py?.ToLower().Contains(searchLower) == true)
|
|
.ToList();
|
|
|
|
if (filteredItems.Any())
|
|
{
|
|
var filteredGroup = new AlphabetGroup();
|
|
foreach (var item in filteredItems)
|
|
{
|
|
filteredGroup.Items.Add(item);
|
|
}
|
|
TempList.Add(filteredGroup);
|
|
}
|
|
}
|
|
}
|
|
FilteredGroups = new ObservableCollection<AlphabetGroup>(TempList);
|
|
}
|
|
|
|
// 当搜索文本改变时自动过滤
|
|
partial void OnSearchTextChanged(string value)
|
|
{
|
|
UpdateFilteredGroups();
|
|
}
|
|
[RelayCommand]
|
|
private void WordClicked(CarfactoryClass item)
|
|
{
|
|
if (!string.IsNullOrEmpty(item.py))
|
|
{
|
|
// 这里可以添加您的业务逻辑
|
|
MessageBox.Show($"您点击了: {item.py}", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
|
// 示例:在控制台输出
|
|
System.Diagnostics.Debug.WriteLine($"单词被点击: {item.py}");
|
|
|
|
// 示例:可以在这里添加导航、数据更新等逻辑
|
|
// NavigateToWordDetail(word);
|
|
// UpdateSelectedWord(word);
|
|
}
|
|
}
|
|
#endregion
|
|
/// <summary>
|
|
/// 选中事件
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
[RelayCommand]
|
|
private void SelectItem(CarfactoryClass item)
|
|
{
|
|
SelectedItem = item;
|
|
// 这里可以执行其他选中后的逻辑
|
|
var info = CacheInfo;
|
|
}
|
|
|
|
//语言缓存变更接收
|
|
public void Receive(LocalLanguageMessage message)
|
|
{
|
|
if (message != null)
|
|
{
|
|
InitializeUI();
|
|
_ = LoadDataInBackground();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 品牌分组
|
|
/// </summary>
|
|
public class AlphabetGroup
|
|
{
|
|
public string Letter { get; set; }
|
|
public ObservableCollection<CarfactoryClass> Items { get; set; } = new ObservableCollection<CarfactoryClass>();
|
|
}
|
|
}
|
|
|