192 lines
7.3 KiB
C#
192 lines
7.3 KiB
C#
using CloudBuilder.CodeGenerator.Config;
|
|
using CloudBuilder.CodeGenerator.Data;
|
|
using CloudBuilder.CodeGenerator.Entity;
|
|
using CloudBuilder.Core.DatabaseAccessor.Entity;
|
|
using CloudBuilder.Core.DependencyInjection.Form;
|
|
using CloudBuilder.Core.Extensions;
|
|
using CloudBuilder.Core.Utility;
|
|
using Krypton.Toolkit;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Reflection.Metadata.Ecma335;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace CloudBuilder.CodeGenerator
|
|
{
|
|
public partial class TableControl : UserControl, IUserControl
|
|
{
|
|
private readonly IApplicationService service;
|
|
private readonly ConfigUserControl configUserControl;
|
|
private IRepository<SysTableSchemaViewEntity> repository;
|
|
public SysTableSchemaViewEntity[] sysTableSchemas;
|
|
public SysTableSchemaViewEntity[] sysTableFields;
|
|
private SysTable[] sysTables;
|
|
private DataTable sysTableField;
|
|
|
|
public TableControl(IApplicationService service)
|
|
{
|
|
InitializeComponent();
|
|
this.service = service;
|
|
tableNameGv.AutoGenerateColumns = false;
|
|
fieldNameGv.AutoGenerateColumns = false;
|
|
}
|
|
|
|
private void loadDbBut_Click(object sender, EventArgs e)
|
|
{
|
|
LoadData();
|
|
}
|
|
|
|
public void LoadData()
|
|
{
|
|
repository = service.GetRepository<IRepository<SysTableSchemaViewEntity>>();
|
|
sysTableSchemas = repository.DetachedEntities.ToArray();
|
|
|
|
if (sysTableSchemas == null || sysTableSchemas.Length == 0) return;
|
|
|
|
sysTables = sysTableSchemas.Select(x => x.TableName).OrderBy(x => x).Distinct()
|
|
.Select(x => new SysTable() { TableName = x, Selected = "N" }).ToArray();
|
|
|
|
tableNameGv.DataSource = sysTables;
|
|
}
|
|
|
|
public SysTableSchemaViewEntity[] GetDataSource()
|
|
{
|
|
sysTableField = (DataTable)fieldNameGv.DataSource;
|
|
return DataTableConvertor.ConvertToList<SysTableSchemaViewEntity>(sysTableField).Where(x => x.Operation == 0).ToArray();
|
|
|
|
}
|
|
|
|
private void tableNameGv_CellClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
if (e.RowIndex < 0) return;
|
|
if (e.ColumnIndex != selectedCol.Index) return;
|
|
|
|
if (tableNameGv.CurrentRow.Cells[tableNameCol.Name].Value == System.DBNull.Value) return;
|
|
|
|
foreach (DataGridViewRow dgrow in tableNameGv.Rows)
|
|
{
|
|
if (tableNameGv.CurrentRow.Cells[tableNameCol.Name].EditedFormattedValue == dgrow.Cells[tableNameCol.Name].EditedFormattedValue) continue;
|
|
|
|
dgrow.Cells[selectedCol.Name].Value = "N";
|
|
}
|
|
|
|
sysTableFields = sysTableSchemas.Where(x => x.TableName == (string)tableNameGv.CurrentRow.Cells[tableNameCol.Name].Value).ToArray();
|
|
|
|
sysTableField = DataTableConvertor.ToDataTable<SysTableSchemaViewEntity>(sysTableFields);
|
|
fieldNameGv.DataSource = sysTableField;
|
|
|
|
SetDataGrivViewCellColor((string)tableNameGv.CurrentRow.Cells[tableNameCol.Name].Value);
|
|
}
|
|
|
|
private void SetDataGrivViewCellColor(string tableName)
|
|
{
|
|
ConfigUserControl configUserControl = service.ServiceProvider.GetKeyedService<ConfigUserControl>("ConfigUserControl");
|
|
NamespaceObject namespaceObject = configUserControl.toolInitConfig.NamespaceObjects.Where(x => x.Assembly == configUserControl.toolInitConfig.CurrentAssembly
|
|
&& x.TemplateType == "Entity").FirstOrDefault();
|
|
|
|
if (namespaceObject == null) return;
|
|
|
|
string path = Path.Combine(namespaceObject.OutFilePath, tableName.StringToProerty() + namespaceObject.FileNameSuffix);
|
|
|
|
//目录先处理对比Entity.cs
|
|
if (string.IsNullOrEmpty(path)) return;
|
|
|
|
if (!File.Exists(path)) return;
|
|
List<SysTableSchemaViewEntity> ents = new List<SysTableSchemaViewEntity>();
|
|
|
|
string[] lines = File.ReadAllLines(path);
|
|
SysTableSchemaViewEntity ent;
|
|
bool key = false;
|
|
foreach (string l in lines)
|
|
{
|
|
if (l.Contains("[Key]"))
|
|
{
|
|
key = true;
|
|
continue;
|
|
}
|
|
if (!l.Contains("[Column")) continue;
|
|
ent = new SysTableSchemaViewEntity();
|
|
ent.TableName = tableName;
|
|
ent.FieldName = l.Split('"')[1];//.Replace("[Column(\"", "").Replace("\")]", "").Trim();
|
|
if (key) ent.Pk = 1;
|
|
ents.Add(ent);
|
|
key = false;
|
|
}
|
|
|
|
|
|
if (ents == null || ents.Count == 0) return;
|
|
string fileName = null;
|
|
|
|
foreach (DataGridViewRow row in fieldNameGv.Rows)
|
|
{
|
|
row.DefaultCellStyle.BackColor = Color.White;
|
|
|
|
fileName = (string)row.Cells[fieldNameCol.Index].Value;
|
|
if (string.IsNullOrEmpty(fileName)) continue;
|
|
|
|
ent = ents.Where(x => x.FieldName == fileName).FirstOrDefault();
|
|
if (ent == null)
|
|
{
|
|
row.DefaultCellStyle.BackColor = Color.Pink;
|
|
row.Cells[operationCol.Index].Value = 1;
|
|
}
|
|
else
|
|
{
|
|
if (ent.Pk > 0 && Convert.ToInt32(row.Cells[pkCol.Index].Value) == 0)
|
|
{
|
|
row.DefaultCellStyle.BackColor = Color.Red;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void dtSearchBut_Click(object sender, EventArgs e)
|
|
{
|
|
if (sysTableSchemas == null) return;
|
|
|
|
var sysTable = DataTableConvertor.ToDataTable<SysTable>(sysTables.Where(x => x.TableName.Contains(dtTxt.Text.ToLower().Trim())).ToArray());
|
|
tableNameGv.DataSource = sysTable;
|
|
}
|
|
|
|
private void dtResetBut_Click(object sender, EventArgs e)
|
|
{
|
|
if (sysTableSchemas == null) return;
|
|
var sysTable = DataTableConvertor.ToDataTable<SysTable>(sysTables);
|
|
tableNameGv.DataSource = sysTable;
|
|
}
|
|
|
|
private void fieldSearchBut_Click(object sender, EventArgs e)
|
|
{
|
|
if (sysTableSchemas == null) return;
|
|
if (sysTableField == null) return;
|
|
|
|
sysTableFields = sysTableSchemas.Where(x => x.TableName == (string)tableNameGv.CurrentRow.Cells[tableNameCol.Name].Value
|
|
&& x.FieldName.StartsWith(fieldTxt.Text.ToLower().Trim())).ToArray();
|
|
|
|
sysTableField = DataTableConvertor.ToDataTable<SysTableSchemaViewEntity>(sysTableFields);
|
|
|
|
fieldNameGv.DataSource = sysTableField;
|
|
}
|
|
|
|
private void fieldResetBut_Click(object sender, EventArgs e)
|
|
{
|
|
if (sysTableSchemas == null) return;
|
|
if (sysTableField == null) return;
|
|
sysTableField.Rows.Clear();
|
|
|
|
sysTableFields = sysTableSchemas.Where(x => x.TableName == (string)tableNameGv.CurrentRow.Cells[tableNameCol.Name].Value).ToArray();
|
|
|
|
sysTableField = DataTableConvertor.ToDataTable<SysTableSchemaViewEntity>(sysTableFields);
|
|
fieldNameGv.DataSource = sysTableField;
|
|
}
|
|
}
|
|
|
|
}
|