using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using HslCommunication.LogNet; using Microsoft.Extensions.DependencyInjection; using Microsoft.WindowsAPICodePack.Dialogs; using SlnMesnac.Config; using SlnMesnac.Generate; using SqlSugar; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.IO; using System.Linq; using System.Windows; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- * 版权所有 (c) 2024 WenJY 保留所有权利。 * CLR版本:4.0.30319.42000 * 机器名称:LAPTOP-E0N2L34V * 命名空间:SlnMesnac.WPF.ViewModel.Generate * 唯一标识:7f1ddac5-3ff3-4974-ac90-d6eb684167c8 * * 创建者:WenJY * 电子邮箱:wenjy@mesnac.com * 创建时间:2024-04-11 09:31:46 * 版本:V1.0.0 * 描述: * *-------------------------------------------------------------------- * 修改人: * 时间: * 修改说明: * * 版本:V1.0.0 *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> namespace SlnMesnac.WPF.ViewModel.Generate { internal class GenerateControlViewModel : ViewModelBase { private readonly AppConfig _appConfig; private readonly GenerateCode _generateCode; public GenerateControlViewModel() { _appConfig = App.ServiceProvider.GetService(); _generateCode = App.ServiceProvider.GetService(); var configIds = _appConfig.sqlConfig.Select(x=>x.configId).ToList(); // 初始化选项列表 Options = new ObservableCollection(); foreach(var configId in configIds) { Options.Add(configId); } QuerySearchCommand = new RelayCommand(Query); CreateCodeCommand = new RelayCommand(CreateCode); } #region 参数定义 private ObservableCollection _options; public ObservableCollection Options { get { return _options; } set { _options = value; RaisePropertyChanged(nameof(Options)); } } private string _selectedOption; public string SelectedOption { get { return _selectedOption; } set { _selectedOption = value; RaisePropertyChanged(nameof(SelectedOption)); } } private ObservableCollection tablesDataGrid; public ObservableCollection TablesDataGrid { get { return tablesDataGrid; } set { tablesDataGrid = value; RaisePropertyChanged(() => TablesDataGrid); } } #endregion #region 事件定义 public RelayCommand QuerySearchCommand { get; set; } public RelayCommand CreateCodeCommand { get;set; } #endregion /// /// 查询事件 /// /// private void Query(string search) { var configId = _selectedOption; if (!string.IsNullOrEmpty(configId)) { var db = App.ServiceProvider.GetService(); var scope = db.AsTenant().GetConnectionScope(configId); List tables = scope.DbMaintenance.GetTableInfoList(false); if(tables != null) { TablesDataGrid = new ObservableCollection(); tables.ForEach(t => { TablesDataGrid.Add(t); }); } } } private void CreateCode(string tableName) { var info = tableName; var configId = _selectedOption; string nameSpace = "SlnMesnac.Repository"; try { using (CommonOpenFileDialog dialog = new CommonOpenFileDialog()) { dialog.IsFolderPicker = true; // 设置为选择文件夹 if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { string selectedPath = dialog.FileName; var res = _generateCode.CreateCode(configId, tableName, selectedPath, nameSpace); if (res) { MessageBox.Show($"{tableName}代码生成成功"); } } } }catch(Exception ex) { MessageBox.Show($"{tableName}代码生成失败:{ex.Message}"); } } } }