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.

164 lines
4.8 KiB
C#

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
* CLR4.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}");
}
}
}
}