using CeramicProjectTool.Model; using CeramicProjectTool.ViewModel; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace CeramicProjectTool.Pages { /// /// PermissionConfigPage.xaml 的交互逻辑 /// public partial class PermissionConfigPage : Page { private PermissonConfigViewModel viewModel; public PermissionConfigPage() { InitializeComponent(); viewModel = new PermissonConfigViewModel(); this.DataContext = viewModel; } private void Page_Loaded(object sender, RoutedEventArgs e) { LoadData(); } private async void LoadData(string searchText = "") { Task> task; if (string.IsNullOrWhiteSpace(searchText)) { task = viewModel.GetPermissons(); } else { task = viewModel.GetPermissons(searchText); } _ = task.ContinueWith((t) => { permissonData.Dispatcher.Invoke(() => { permissonData.ItemsSource = t.Result; }); }); await task; var ruleTask = viewModel.GetRules(); _ = ruleTask.ContinueWith(ruleTask => { RuleListView.Dispatcher.Invoke(() => { RuleListView.ItemsSource = ruleTask.Result; RuleListView.SelectedIndex = 0; }); }); await ruleTask; var moduleTask = viewModel.GetMkList(); _ = moduleTask.ContinueWith(muduleTask => { ModuleList.Dispatcher.Invoke(() => { ModuleList.ItemsSource = muduleTask.Result; ModuleList.SelectedIndex = 0; }); }); } private void searchBox_TextInput(object sender, TextCompositionEventArgs e) { } private void searchBox_KeyDown(object sender, KeyEventArgs e) { var enterKey = Key.Enter; if (e.Key == enterKey) { var searchText = searchBox.Text; if (!string.IsNullOrWhiteSpace(searchText)) { LoadData(searchText); } } } private void RuleListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { var datas = ((ListView)sender).ItemsSource as List; var selectedIndex = ((ListView)sender).SelectedIndex; if (selectedIndex != -1) { viewModel.selectRule = datas[selectedIndex]; LoadPermisson(); } } private void invokePermissonBtn_Click(object sender, RoutedEventArgs e) { var selected = ModulePermissonList.Children; List rulePermissons = new List(); foreach (var item in selected) { if (item is CheckBox checkBox) { if (checkBox.IsChecked ?? false) { var featureId = (int)checkBox.Tag; var rule = viewModel.selectRule; //var module = viewModel.selectMudole; var rulePermisson = new RulePermissonModel { RuleId = rule.Id, FeatureId = featureId }; rulePermissons.Add(rulePermisson); } } } if (rulePermissons.Count > 0) { viewModel.UpdateRulePermisson(rulePermissons); LoadPermisson(); } } private void reinvokePermissonBtn_Click(object sender, RoutedEventArgs e) { var selected = RulePermissonList.Children; List rulePermissons = new List(); foreach (var item in selected) { if (item is CheckBox checkBox) { if (checkBox.IsChecked ?? false) { var permissonId = (int)checkBox.Tag; rulePermissons.Add(permissonId); } } } if (rulePermissons.Count > 0) { viewModel.DeleteRulePermisson(rulePermissons); LoadPermisson(); } } private void ModuleList_SelectionChanged(object sender, SelectionChangedEventArgs e) { var datas = ((ComboBox)sender).ItemsSource as List; var selectedIndex = ((ComboBox)sender).SelectedIndex; if (selectedIndex != -1) { var mkModel = datas?[selectedIndex == -1 ? 0 : selectedIndex]; viewModel.selectMudole = mkModel; LoadPermisson(); } } private async void LoadPermisson() { viewModel.isSelectAll = false; var rule = viewModel.selectRule; var module = viewModel.selectMudole; if (rule == null || module == null) { return; } var rulePremissonTask = viewModel.GetPermissonsByRule(rule.RuleName, module.ModuleName); _ = rulePremissonTask.ContinueWith((t) => { var result = t.Result; viewModel.rulePermissons = result; RulePermissonList.Dispatcher.Invoke(() => { RulePermissonList.Children.Clear(); result.ForEach(item => { var checkBox = new CheckBox { Tag = item.PermissonId, Content = item.FeatureName, //IsChecked = item.IsEnabled }; RulePermissonList.Children.Add(checkBox); }); }); }); await rulePremissonTask; var modulePremissonTask = viewModel.GetPermissons(module.ModuleName); _ = modulePremissonTask.ContinueWith((t) => { var result = t.Result; ModulePermissonList.Dispatcher.Invoke(() => { ModulePermissonList.Children.Clear(); result.ForEach(item => { var exist = viewModel.CheckPermissonExist(item.Feature); if (!exist) { var checkBox = new CheckBox { Tag = item.Id, Content = item.Feature, IsChecked = false }; ModulePermissonList.Children.Add(checkBox); } }); }); }); } //private bool isAllSelected = false; private void selectAllModulePermissonBtn_Click(object sender, RoutedEventArgs e) { viewModel.isSelectAll = !viewModel.isSelectAll; foreach (var item in ModulePermissonList.Children) { if (item is CheckBox checkBox) { if (checkBox.IsEnabled) { ModulePermissonList.Dispatcher.Invoke(() => { checkBox.IsChecked = viewModel.isSelectAll; }); } } } } } }