WinFormTools/WinformDevFramework/System/FrmCreateCode.cs

170 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SqlSugar;
using WinformDevFramework.IServices.System;
using WinformDevFramework.Models;
using WinformDevFramework.Models.DBModels;
namespace WinformDevFramework
{
public partial class FrmCreateCode : Form
{
private ISysDataSourceServices _dataSourceServices;
public FrmCreateCode(ISysDataSourceServices sysDataSourceServices)
{
_dataSourceServices = sysDataSourceServices;
InitializeComponent();
}
private void FrmCreateCode_Load(object sender, EventArgs e)
{
//初始化数据库 下拉框
comboBox1.DataSource= _dataSourceServices.Query();
comboBox1.DisplayMember = "ConnectName";
comboBox1.ValueMember = "ID";
this.uiComboTreeViewTable.CheckBoxes = true;
this.uiComboTreeViewTable.DropDownStyle = Sunny.UI.UIDropDownStyle.DropDownList;
uiComboTreeViewTable.ShowClearButton=true;
uiComboTreeViewTable.ShowSelectedAllCheckBox=true;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
sysDataSource dataSource = (sysDataSource)comboBox1.SelectedValue;
var connectionString =
$"Server={dataSource.Host};Database={dataSource.DataBaseName};User Id = {dataSource.Username}; Password={dataSource.Password}";
textBox1.Text= connectionString;
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(comboBox1.Text))
{
MessageBox.Show("请选择数据库!");
return;
}
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = textBox1.Text
,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
try
{
db.Open();
string sqlGetAllTables = "select TABLE_NAME from INFORMATION_SCHEMA.TABLES order by TABLE_NAME";
var tables=db.Ado.SqlQuery<string>(sqlGetAllTables);
uiComboTreeViewTable.TreeView.Nodes.Clear();
foreach (var table in tables)
{
uiComboTreeViewTable.Nodes.Add(table);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (ValidateData())
{
var tables=uiComboTreeViewTable.Text.Split(';');
List<string> ts=new List<string>();
foreach (var table in tables)
{
var t = table.TrimEnd().TrimStart();
if (!string.IsNullOrEmpty(t))
ts.Add(t);
}
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = textBox1.Text
,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
try
{
//生成实体类
db.DbFirst.IsCreateAttribute().Where(p => ts.Contains(p)).CreateClassFile($"{txtUrl.Text}\\Entity", txtNameSpace.Text);
var isc = new IServicesCreate();
var sc = new ServicesCreate();
var irc = new IRepositoryCreate();
var rc = new RepositoryCreate();
//生成Iservices Services IRepository Repository
ts.ForEach(p =>
{
isc.Create(p, txtNameSpace.Text, txtUrl.Text);
sc.Create(p, txtNameSpace.Text, txtUrl.Text);
irc.Create(p, txtNameSpace.Text, txtUrl.Text);
rc.Create(p, txtNameSpace.Text, txtUrl.Text);
});
MessageBox.Show("生成成功");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
}
private void button4_Click(object sender, EventArgs e)
{
TabControl parentTabControl = this.Parent.Parent as TabControl;
TabPage tabpage = this.Parent as TabPage;
parentTabControl.TabPages.Remove(tabpage);
}
private void button2_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string selectedPath = folderBrowserDialog.SelectedPath;
// 在这里可以使用选择的路径进行后续操作
txtUrl.Text= selectedPath;
}
}
}
private bool ValidateData()
{
if (string.IsNullOrEmpty(comboBox1.Text))
{
MessageBox.Show("请选择数据库");
return false;
}
if (string.IsNullOrEmpty(uiComboTreeViewTable.Text))
{
MessageBox.Show("请选择数据表");
return false;
}
if (string.IsNullOrEmpty(txtNameSpace.Text))
{
MessageBox.Show("命名空间不能为空");
return false;
}
if (string.IsNullOrEmpty(txtUrl.Text))
{
MessageBox.Show("请选择文件生成目录");
return false;
}
return true;
}
}
}