DataTable Dt = null;
DataTable ValuesDt = null;
DataTable RelDt = null;
private void SimpleRun()
{
string ErrPage = string.Empty;
try
{
Monitor.Enter(this);//锁定,保持同步
FormPro.progressBar1.Value = 0;
FormPro.CurrProcessTable.Text = "开始生成项目....";
currDBName = this._CB_DB.Text;
CDataBase _HostDataBase = new CDataBase();
//IList<TemplateTable> _ListTemplateTable = new List<TemplateTable>();
IList<TemplateTable> _BuildListTemplateTable = new List<TemplateTable>();
TemplateTable _TempTable = null;
IList<Columns> _ColsList = null;
IList<Relation> retRelation = null;
IList<Relation> retConvertRelation = null;
IList<TemplateTable> HasTemplateTable = CDBHelper.getTable(currDBName.Trim());
_HostDataBase.DbName = currDBName.Trim();
DataTable ColumnsDt = null;
DataTable RelationDt = null;
if (dbConnInfo.DbClient == Model.DbType.Sqlite)
{
RelationDt = new DataTable();
RelationDt.Columns.Add("主表名", typeof(string));
RelationDt.Columns.Add("主键列名", typeof(string));
RelationDt.Columns.Add("外键表名称", typeof(string));
RelationDt.Columns.Add("外键列名", typeof(string));
RelationDt.Columns.Add("级联更新", typeof(bool));
RelationDt.Columns.Add("级联删除", typeof(bool));
DataTable tempDt = null;
DataRow tempirow = null;
for (int i = 0; i < HasTemplateTable.Count; i++)
{
_Current_Table = HasTemplateTable[i].TableName.ToString();
tempDt = CDBHelper.getRelationTable(_Current_Table);
if (tempDt != null && tempDt.Rows.Count > 0)
{
foreach (DataRow reader in tempDt.Rows)
{
tempirow = RelationDt.NewRow();
tempirow["主表名"] = _Current_Table;
tempirow["主键列名"] = reader["from"].ToString();
tempirow["外键表名称"] = reader["to"].ToString();
tempirow["外键列名"] = reader["table"].ToString();
tempirow["级联更新"] = reader["on_update"].ToString() == "NO ACTION" ? false : true;
tempirow["级联删除"] = reader["on_delete"].ToString() == "NO ACTION" ? false : true;
RelationDt.Rows.Add(tempirow);
}
}
}
for (int i = 0; i < HasTemplateTable.Count; i++)
{
_Current_Table = HasTemplateTable[i].TableName.ToString();
ColumnsDt = CDBHelper.getColumnsTable(_Current_Table);
_ColsList = CDBHelper.getColumns(ColumnsDt, _Current_Table);
retRelation = CDBHelper.getRelation(RelationDt, _Current_Table);
retConvertRelation = CDBHelper.getConverRelation(RelationDt, _Current_Table);
_TempTable = new TemplateTable();
foreach (TemplateTable currTemp in HasTemplateTable)
{
if (currTemp.TableName.Trim() == _Current_Table.Trim())
{
_TempTable = currTemp;
}
}
_TempTable.Cols = _ColsList;
_TempTable.RelRelationList = retRelation;
_TempTable.ChildRelationList = retConvertRelation;
_TempTable.RelRelationConfig = GetRels(_Current_Table, RelDt);
int step = 1;
bool FirstKey = true; //多键取第一个为主键
foreach (Columns tempcols in _ColsList)
{
tempcols.RelRelation = GetColumnsRelation(tempcols.ColumnsName, retRelation);
if (ValuesDt != null)
tempcols.Dicts = GetColumnsDicts(_Current_Table, tempcols.ColumnsName, ValuesDt);
if (FirstKey && tempcols.IsKey)
{
_TempTable.Key = tempcols;
FirstKey = false;
}
if (step == 2)
{
_TempTable.Name = tempcols;
}
if (tempcols.Name.ToLower() == "title")
{
_TempTable.Name = tempcols;
}
if (tempcols.Name.ToLower() == _Current_Table.ToLower() + "name")
{
_TempTable.Name = tempcols;
}
if (tempcols.Name.ToLower() == "name")
{
_TempTable.Name = tempcols;
}
step++;
}
_TempTable.TableName = _Current_Table;
_TempTable.TableType = getTableType(_TempTable.TableName);
//_ListTemplateTable.Add(_TempTable);
_BuildListTemplateTable.Add(_TempTable);
}
}
else
{
ColumnsDt = CDBHelper.getColumnsTable(currDBName);
RelationDt = CDBHelper.getRelationTable(currDBName);
//不生成全部项目时按表选择全出现漏项的情况
//数据表要经过关联关系生成完整项目程序不能直接在选项项目中选择数据表操作
for (int i = 0; i < HasTemplateTable.Count; i++)
{
_Current_Table = HasTemplateTable[i].TableName.ToString();
if (_Current_Table == "sysmenu")
{
}
_ColsList = CDBHelper.getColumns(ColumnsDt, _Current_Table);
retRelation = CDBHelper.getRelation(RelationDt, _Current_Table);
retConvertRelation = CDBHelper.getConverRelation(RelationDt, _Current_Table);
_TempTable = new TemplateTable();
_TempTable = HasTemplateTable[i];
_TempTable.Cols = _ColsList;
_TempTable.RelRelationList = retRelation;
_TempTable.ChildRelationList = retConvertRelation;
_TempTable.RelRelationConfig = GetRels(_Current_Table, RelDt);
int step = 1;
bool FirstKey = true; //多键取第一个为主键
foreach (Columns tempcols in _ColsList)
{
tempcols.RelRelation = GetColumnsRelation(tempcols.ColumnsName, retRelation);
if (ValuesDt != null)
tempcols.Dicts = GetColumnsDicts(_Current_Table, tempcols.ColumnsName, ValuesDt);
if (FirstKey && tempcols.IsKey)
{
_TempTable.Key = tempcols;
FirstKey = false;
}
if (step == 2)
{
_TempTable.Name = tempcols;
}
if (tempcols.Name.ToLower() == "title")
{
_TempTable.Name = tempcols;
}
if (tempcols.Name.ToLower() == _Current_Table.ToLower() + "name")
{
_TempTable.Name = tempcols;
}
if (tempcols.Name.ToLower() == "name")
{
_TempTable.Name = tempcols;
}
step++;
}
_TempTable.TableName = _Current_Table;
_TempTable.TableType = getTableType(_TempTable.TableName);
//_ListTemplateTable.Add(_TempTable);
_BuildListTemplateTable.Add(_TempTable);
}
}
if (ColumnsDt != null && ColumnsDt.Rows.Count > 0)
{
ColumnsDt.Dispose();
}
if (RelationDt != null && RelationDt.Rows.Count > 0)
{
RelationDt.Dispose();
}
//_ListTemplateTable = Computer(_ListTemplateTable);
_BuildListTemplateTable = Computer(_BuildListTemplateTable);
//_HostDataBase.TemplateTable = _ListTemplateTable;
_HostDataBase.TemplateTable = _BuildListTemplateTable;
#region 菜单
//一级菜单
List<TableMenu> Menus = new List<TableMenu>();
TableMenu sub = null;
foreach (DataRow dr in Dt.Rows)
{
sub = new TableMenu();
sub.DefualtTable = dr["一级菜单"].ToString();
sub.MenuName = dr["二级菜单"].ToString();
if (dr["点亮"].ToString() == "是")
{
sub.ShowType = "1";
}
else
{
sub.ShowType = "0";
}
//目录
sub.Dir = dr["目录"].ToString();
//一级菜单 二级菜单 页面 页面类型
sub.FirstMenu = dr["一级菜单"].ToString();
sub.SecMenu = dr["二级菜单"].ToString();
sub.PageName = dr["页面"].ToString();
sub.PageType = dr["页面类型"].ToString();
sub.PageTitle = dr["二级菜单"].ToString();
Menus.Add(sub);
}
#endregion
string bootpath = AppDomain.CurrentDomain.BaseDirectory + @"ypControl\";
string[] fileList = System.IO.Directory.GetFileSystemEntries(bootpath);
TempClass HeaderTempClass = new TempClass();
HeaderTempClass.TemplateStr = string.Empty;
HeaderTempClass.TempNameSpace = textBoxNS.Text.Trim();
HeaderTempClass.TempTable = _TempTable;
HeaderTempClass.HostDataBase = _HostDataBase;
HeaderTempClass.CreateVersion = "";
HeaderTempClass.Menus = Menus;
if (!Directory.Exists(folder + "\\ypControl\\"))
{
Directory.CreateDirectory(folder + "\\ypControl\\");
}
foreach (string filepath in fileList)
{
HeaderTempClass.TemplateStr = File.ReadAllText(filepath, EncodingType.GetEncoding(filepath));
F_WriteStr(VelocityEngineBuilder(HeaderTempClass), folder + "\\ypControl\\" + filepath.Replace(bootpath, string.Empty));
}
IList<WebMarkString> _TempList = new List<WebMarkString>();
bootpath = AppDomain.CurrentDomain.BaseDirectory + @"T\";
fileList = System.IO.Directory.GetFileSystemEntries(bootpath);
WebMarkString Current = null;
foreach (string filepath in fileList)
{
using (StreamReader SR = new StreamReader(filepath, EncodingType.GetEncoding(filepath)))
{
Current = new WebMarkString();
Current.Tag = filepath.Replace(bootpath, string.Empty);
Current.Link = SR.ReadToEnd();
_TempList.Add(Current);
}
}
string cutable = string.Empty;
string[] relTables = null;
//把目录下相应模块中的缺省页面创建出来。
DataView dv = new DataView(Dt);
DataTable dtdirs = dv.ToTable(true, "目录");
DataTable dtnew = new DataTable();
DataTable NewDt = Dt.Copy();
foreach (DataRow drdir in dtdirs.Rows)
{
//找到目录下的模块
dtnew = GetNewTable(NewDt, "目录='" + drdir["目录"].ToString() + "'");
dv = new DataView(dtnew);
DataTable dttables = dv.ToTable(true, "模块");
foreach (DataRow dr in dttables.Rows)
{
DataRow[] copydr = NewDt.Select("目录='" + drdir["目录"].ToString() + "' and 模块='" + dr["模块"].ToString() + "'");
DataRow[] exist = NewDt.Select("目录='" + drdir["目录"].ToString() + "' and 模块='" + dr["模块"].ToString() + "' and 页面类型='List'");
if (exist == null || exist.Length == 0)
{
if (copydr != null && copydr.Length > 0)
{
copydr[0]["页面类型"] = "List";
copydr[0]["页面"] = dr["模块"].ToString() + "List";
CreateNotExsits(copydr[0], _TempList, _BuildListTemplateTable, _HostDataBase, Menus);
}
}
exist = NewDt.Select("目录='" + drdir["目录"].ToString() + "' and 模块='" + dr["模块"].ToString() + "' and 页面类型='Edit'");
if (exist == null || exist.Length == 0)
{
if (copydr != null && copydr.Length > 0)
{
copydr[0]["页面类型"] = "Edit";
copydr[0]["页面"] = dr["模块"].ToString() + "Edit";
CreateNotExsits(copydr[0], _TempList, _BuildListTemplateTable, _HostDataBase, Menus);
}
}
exist = NewDt.Select("目录='" + drdir["目录"].ToString() + "' and 模块='" + dr["模块"].ToString() + "' and 页面类型='Show'");
if (exist == null || exist.Length == 0)
{
if (copydr != null && copydr.Length > 0)
{
copydr[0]["页面类型"] = "Show";
copydr[0]["页面"] = dr["模块"].ToString() + "Show";
CreateNotExsits(copydr[0], _TempList, _BuildListTemplateTable, _HostDataBase, Menus);
}
}
}
}
foreach (DataRow dr in Dt.Rows)
{
//模块 参数 编辑页设置字段 显示页设置字段 列表页设置字段
cutable = dr["模块"].ToString().Trim();
relTables = dr["包含模块"].ToString().Split(',');
//string cutable = relTables[0];
//if (cutable.Contains("("))
//{
// string[] item = cutable.Split('(');
// cutable = item[0];
//}
_TempTable = new TemplateTable();
for (int i = 0; i < _BuildListTemplateTable.Count; i++)
{
_Current_Table = _BuildListTemplateTable[i].TableName;
if (cutable.Equals(_Current_Table, StringComparison.CurrentCultureIgnoreCase))
{
_TempTable = _BuildListTemplateTable[i];
}
}
string ClassName = dr["页面"].ToString();
if (ClassName.Equals("default", StringComparison.CurrentCultureIgnoreCase))
{
ClassName = "_" + ClassName;
}
string textBoxFloder = dr["目录"].ToString();
if (textBoxFloder.Length >= 1)
{
textBoxFloder = textBoxFloder.Substring(1);
if (textBoxFloder.Length > 1)
{
if (textBoxFloder.EndsWith("/"))
{
textBoxFloder = textBoxFloder.TrimEnd('/');
}
}
textBoxFloder = textBoxFloder.Replace("/", ".");
}
string CodeName = string.Format("{0}.{1}.{2}", textBoxNS.Text.Trim(), textBoxFloder.Trim(), ClassName);
if (string.IsNullOrWhiteSpace(textBoxFloder.Trim()))
{
CodeName = string.Format("{0}.{1}", textBoxNS.Text.Trim(), ClassName);
}
if (dr["目录"].ToString() != "/")
{
}
string CodeNameSpace = string.Format("{0}.{1}", textBoxNS.Text.Trim(), textBoxFloder.Trim());
if (string.IsNullOrWhiteSpace(textBoxFloder.Trim()))
{
CodeNameSpace = textBoxNS.Text.Trim();
}
if (dr["目录"].ToString() != "/")
{
if (!Directory.Exists(folder + "\\" + dr["目录"].ToString()))
{
Directory.CreateDirectory(folder + "\\" + dr["目录"].ToString());
}
}
string HtmlContent = string.Empty;
string CodeContent = string.Empty;
string DesingerCode = string.Empty;
foreach (WebMarkString listitem in _TempList)
{
if (listitem.Tag == dr["页面类型"].ToString().ToLower() + ".aspx")
{
HtmlContent = listitem.Link;
}
if (listitem.Tag == dr["页面类型"].ToString().ToLower() + ".aspx.cs")
{
CodeContent = listitem.Link;
}
if (listitem.Tag == dr["页面类型"].ToString().ToLower() + ".aspx.designer.cs")
{
DesingerCode = listitem.Link;
}
}
string IsView = dr["点亮"].ToString();//点亮
string IfIsView = dr["显示条件"].ToString();//显示条件
string SessionID = dr["SessionID"].ToString();//SessionID
string AddCart = dr["加入购物车"].ToString();//加入购物车
string Orders = dr["下单"].ToString();//下单
string Payment = dr["支付"].ToString();//支付
TempClass TempClass = new TempClass();
TempClass.TemplateStr = HtmlContent;
TempClass.TempNameSpace = textBoxNS.Text.Trim();
TempClass.TempTable = _TempTable;
TempClass.HostDataBase = _HostDataBase;
TempClass.CreateVersion = "";
TempClass.Menus = Menus;
//点亮 显示条件 SessionID 包含模块 加入购物车 下单 支付
TempClass.RelTables = relTables;
TempClass.CodeNameSpace = CodeNameSpace;
TempClass.ClassName = ClassName;
TempClass.IsView = IsView;
TempClass.IfIsView = IfIsView;
TempClass.SessionID = SessionID;
TempClass.AddCart = AddCart;
TempClass.Orders = Orders;
TempClass.Payment = Payment;
//目录
TempClass.Dir = dr["目录"].ToString();
//一级菜单 二级菜单 页面 页面类型
TempClass.FirstMenu = dr["一级菜单"].ToString();
TempClass.SecMenu = dr["二级菜单"].ToString();
TempClass.PageName = dr["页面"].ToString();
TempClass.PageType = dr["页面类型"].ToString();
TempClass.PageTitle = dr["二级菜单"].ToString();
TempClass.CodeName = CodeName;
//模块 参数 编辑页设置字段 显示页设置字段 列表页设置字段
TempClass.TableName = dr["模块"].ToString();
TempClass.TableValues = dr["参数"].ToString();
TempClass.EditJson = dr["编辑页设置字段"].ToString();
TempClass.ShowJson = dr["显示页设置字段"].ToString();
TempClass.ListJson = dr["列表页设置字段"].ToString();
HtmlContent = VelocityEngineBuilder(TempClass);
TempClass.TemplateStr = CodeContent;
CodeContent = VelocityEngineBuilder(TempClass);
TempClass.TemplateStr = DesingerCode;
DesingerCode = VelocityEngineBuilder(TempClass);
F_WriteStr(HtmlContent, folder + "\\" + dr["目录"].ToString() + "\\" + dr["页面"].ToString() + ".aspx");
F_WriteStr(CodeContent, folder + "\\" + dr["目录"].ToString() + "\\" + dr["页面"].ToString() + ".aspx.cs");
F_WriteStr(DesingerCode, folder + "\\" + dr["目录"].ToString() + "\\" + dr["页面"].ToString() + ".aspx.designer.cs");
}
FormPro.CurrProcessTable.Text = "项目成功生成!";
FormPro.progressBar1.Value = 100;
Application.DoEvents();
Thread.Sleep(5);
Process.Start("explorer.exe", folder);
}
catch (Exception err)
{
MessageBox.Show(ErrPage + ";" + err.Message.ToString());
Thread.CurrentThread.Abort();
}
finally
{
Monitor.Exit(this);
_Thread.Abort();
}
}
private void CreateNotExsits(DataRow dr, IList<WebMarkString> _TempList, IList<TemplateTable> _BuildListTemplateTable, CDataBase _HostDataBase, List<TableMenu> Menus)
{
//模块 参数 编辑页设置字段 显示页设置字段 列表页设置字段
string cutable = dr["模块"].ToString().Trim();
string[] relTables = dr["包含模块"].ToString().Split(',');
//string cutable = relTables[0];
//if (cutable.Contains("("))
//{
// string[] item = cutable.Split('(');
// cutable = item[0];
//}
TemplateTable _TempTable = new TemplateTable();
for (int i = 0; i < _BuildListTemplateTable.Count; i++)
{
_Current_Table = _BuildListTemplateTable[i].TableName;
if (cutable.Equals(_Current_Table, StringComparison.CurrentCultureIgnoreCase))
{
_TempTable = _BuildListTemplateTable[i];
}
}
string ClassName = dr["页面"].ToString();
if (ClassName.Equals("default", StringComparison.CurrentCultureIgnoreCase))
{
ClassName = "_" + ClassName;
}
string textBoxFloder = dr["目录"].ToString();
if (textBoxFloder.Length >= 1)
{
textBoxFloder = textBoxFloder.Substring(1);
if (textBoxFloder.Length > 1)
{
if (textBoxFloder.EndsWith("/"))
{
textBoxFloder = textBoxFloder.TrimEnd('/');
}
}
textBoxFloder = textBoxFloder.Replace("/", ".");
}
string CodeName = string.Format("{0}.{1}.{2}", textBoxNS.Text.Trim(), textBoxFloder.Trim(), ClassName);
if (string.IsNullOrWhiteSpace(textBoxFloder.Trim()))
{
CodeName = string.Format("{0}.{1}", textBoxNS.Text.Trim(), ClassName);
}
if (dr["目录"].ToString() != "/")
{
}
string CodeNameSpace = string.Format("{0}.{1}", textBoxNS.Text.Trim(), textBoxFloder.Trim());
if (string.IsNullOrWhiteSpace(textBoxFloder.Trim()))
{
CodeNameSpace = textBoxNS.Text.Trim();
}
if (dr["目录"].ToString() != "/")
{
if (!Directory.Exists(folder + "\\" + dr["目录"].ToString()))
{
Directory.CreateDirectory(folder + "\\" + dr["目录"].ToString());
}
}
string HtmlContent = string.Empty;
string CodeContent = string.Empty;
string DesingerCode = string.Empty;
foreach (WebMarkString listitem in _TempList)
{
if (listitem.Tag == dr["页面类型"].ToString().ToLower() + ".aspx")
{
HtmlContent = listitem.Link;
}
if (listitem.Tag == dr["页面类型"].ToString().ToLower() + ".aspx.cs")
{
CodeContent = listitem.Link;
}
if (listitem.Tag == dr["页面类型"].ToString().ToLower() + ".aspx.designer.cs")
{
DesingerCode = listitem.Link;
}
}
string IsView = dr["点亮"].ToString();//点亮
string IfIsView = dr["显示条件"].ToString();//显示条件
string SessionID = dr["SessionID"].ToString();//SessionID
string AddCart = dr["加入购物车"].ToString();//加入购物车
string Orders = dr["下单"].ToString();//下单
string Payment = dr["支付"].ToString();//支付
TempClass TempClass = new TempClass();
TempClass.TemplateStr = HtmlContent;
TempClass.TempNameSpace = textBoxNS.Text.Trim();
TempClass.TempTable = _TempTable;
TempClass.HostDataBase = _HostDataBase;
TempClass.CreateVersion = "";
TempClass.Menus = Menus;
//点亮 显示条件 SessionID 包含模块 加入购物车 下单 支付
TempClass.RelTables = relTables;
TempClass.CodeNameSpace = CodeNameSpace;
TempClass.ClassName = ClassName;
TempClass.IsView = IsView;
TempClass.IfIsView = IfIsView;
TempClass.SessionID = SessionID;
TempClass.AddCart = AddCart;
TempClass.Orders = Orders;
TempClass.Payment = Payment;
//目录
TempClass.Dir = dr["目录"].ToString();
//一级菜单 二级菜单 页面 页面类型
TempClass.FirstMenu = dr["一级菜单"].ToString();
TempClass.SecMenu = dr["二级菜单"].ToString();
TempClass.PageName = dr["页面"].ToString();
TempClass.PageType = dr["页面类型"].ToString();
TempClass.PageTitle = dr["二级菜单"].ToString();
TempClass.CodeName = CodeName;
//模块 参数 编辑页设置字段 显示页设置字段 列表页设置字段
TempClass.TableName = dr["模块"].ToString();
TempClass.TableValues = dr["参数"].ToString();
TempClass.EditJson = dr["编辑页设置字段"].ToString();
TempClass.ShowJson = dr["显示页设置字段"].ToString();
TempClass.ListJson = dr["列表页设置字段"].ToString();
HtmlContent = VelocityEngineBuilder(TempClass);
TempClass.TemplateStr = CodeContent;
CodeContent = VelocityEngineBuilder(TempClass);
TempClass.TemplateStr = DesingerCode;
DesingerCode = VelocityEngineBuilder(TempClass);
F_WriteStr(HtmlContent, folder + "\\" + dr["目录"].ToString() + "\\" + dr["页面"].ToString() + ".aspx");
F_WriteStr(CodeContent, folder + "\\" + dr["目录"].ToString() + "\\" + dr["页面"].ToString() + ".aspx.cs");
F_WriteStr(DesingerCode, folder + "\\" + dr["目录"].ToString() + "\\" + dr["页面"].ToString() + ".aspx.designer.cs");
}
private DataTable GetNewTable(DataTable DT, string strCondition)
{
DataTable TempDT = DT.Clone();
DataRow[] Rows = DT.Select(strCondition);
foreach (DataRow DR in Rows)
{
TempDT.ImportRow(DR);
}
return TempDT;
}