You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
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.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<AppConfig>();
_generateCode = App.ServiceProvider.GetService<GenerateCode>();
var configIds = _appConfig.sqlConfig.Select(x=>x.configId).ToList();
// 初始化选项列表
Options = new ObservableCollection<string>();
foreach(var configId in configIds)
{
Options.Add(configId);
}
QuerySearchCommand = new RelayCommand<string>(Query);
CreateCodeCommand = new RelayCommand<string>(CreateCode);
}
#region 参数定义
private ObservableCollection<string> _options;
public ObservableCollection<string> 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<DbTableInfo> tablesDataGrid;
public ObservableCollection<DbTableInfo> TablesDataGrid
{
get { return tablesDataGrid; }
set { tablesDataGrid = value; RaisePropertyChanged(() => TablesDataGrid); }
}
#endregion
#region 事件定义
public RelayCommand<string> QuerySearchCommand { get; set; }
public RelayCommand<string> CreateCodeCommand { get;set; }
#endregion
/// <summary>
/// 查询事件
/// </summary>
/// <param name="search"></param>
private void Query(string search)
{
var configId = _selectedOption;
if (!string.IsNullOrEmpty(configId))
{
var db = App.ServiceProvider.GetService<ISqlSugarClient>();
var scope = db.AsTenant().GetConnectionScope(configId);
List<DbTableInfo> tables = scope.DbMaintenance.GetTableInfoList(false);
if(tables != null)
{
TablesDataGrid = new ObservableCollection<DbTableInfo>();
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}");
}
}
}
}