/// <summary> /// 树状控件菜单点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TreeNodeMenuItem_Click(object sender, EventArgs e) { try { var menuItem = ((ToolStripMenuItem)sender); var dbInfo = (DBInfo)(this.tvDB.SelectedNode.Parent ?? this.tvDB.SelectedNode).Tag; var ucCG = new UCCodeGenerator(); var ucBCG = new UCBathCodeGenerator(dbInfo); var dt = new DataTable(); switch (menuItem.Text) { case "连接": this.BindNodes(dbInfo, true); break; case "注销": var parentNode = this.tvDB.Nodes[dbInfo.Key]; if (parentNode != null) { this.tvDB.Nodes.Remove(parentNode); this._dbInfoList.Remove(dbInfo); } this.SaveDBInfoList(); break; case "复合查询": this.tempList.IsShowCheckBoxes = false; var queryForm = new CompositeQueryForm(dbInfo); if (queryForm.ShowDialog() == DialogResult.OK) { dt = queryForm.Table; ucCG.BindTableInfo(dt, true, queryForm.Sql); this.AddUserControl(ucCG); } break; case "单表代码生成器": this.tempList.IsShowCheckBoxes = false; dt = TableBll.GetDBTableInfo(dbInfo.ConnectionString, this.tvDB.SelectedNode.Text); ucCG.BindTableInfo(dt, false); this.AddUserControl(ucCG); break; case "批量生成代码": this.tempList.IsShowCheckBoxes = true; this.AddUserControl(ucBCG); break; case "生成权限相关表": if (MessageUnity.ShowQuestionMsg("确定执行该操作吗?") == DialogResult.No) { return; } var sqls = new string[] { Resources.A_Common_RoleInfo, Resources.B_Common_MenuInfo, Resources.C_Common_ActionInfo, Resources.D_Common_RoleAction, Resources.E_Common_User, Resources.F_Common_UserRole, Resources.G_AddColumnDescription }; var result = TableBll.ExecuteSqls(dbInfo.ConnectionString, sqls); if (result) { MessageUnity.ShowMsg("生成成功!"); this.BindNodes(dbInfo, true); } break; default: break; } } catch (Exception ex) { MessageUnity.ShowErrorMsg(ex.Message); } }
private void Core() { var index = 1; var tables = this.lbTargetTable.Items; var tempList = this._selectedTemps; var codeBaseDict = new Dictionary <string, CodeBaseInfo>(); //step1 获取表结构信息 var taskArg1 = new TaskArg(); taskArg1.BeginAction = () => { foreach (string table in tables) { var dt = TableBll.GetDBTableInfo(this._dbInfo.ConnectionString, table); var codeBaseInfo = TableBll.GetCodeBaseInfoByDBTable(dt); codeBaseInfo.NameSpace = this.txtNameSpace.Text; codeBaseInfo.ClassName = TableBll.GetClassName(dt.TableName); codeBaseDict.Add(table, codeBaseInfo); Thread.Sleep(1); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = tables.Count, Msg = string.Format("step1/3 获取 {0} 结构信息......", table) }); } return(null); }; taskArg1.EndAction = (result) => { index = 1; }; //step2 创建模板文件夹 var taskArg2 = new TaskArg(); taskArg2.BeginAction = () => { foreach (var temp in tempList) { //一个模板一个子文件夹 var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } Thread.Sleep(100); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = tempList.Count, Msg = string.Format("step2/3 创建模板文件夹{0}......", temp.DirName) }); } return(null); }; taskArg2.EndAction = (result) => { index = 1; }; //step3 生成代码 var taskArg3 = new TaskArg(); taskArg3.BeginAction = () => { var list = tempList.Where(d => !d.IsRanOnlyOnceByBath); foreach (var temp in list) { foreach (string table in tables) { var codeBaseInfo = codeBaseDict[table]; codeBaseInfo.ClassNameExtend = temp.ClassNameExtend; var output = TableBll.CodeGenerator(codeBaseInfo, temp.Path); var msg = string.Format(@"{0}\{1}", temp.Name, table); if (output.StartsWith("error:")) { this._failureTableList.Add(msg); } else { var fileName = string.IsNullOrEmpty(temp.FileName) ? codeBaseInfo.ClassName : temp.FileName; fileName = string.Format("{0}{1}{2}", fileName, temp.ClassNameExtend, temp.FileNameExtend); //一个模板一个子文件夹 var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName); if (temp.IsCreateChildDirByTableName) { savePath = Path.Combine(savePath, codeBaseInfo.ClassName); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } } savePath = Path.Combine(savePath, fileName); File.WriteAllText(savePath, output, Encoding.UTF8); this._successTableList.Add(msg); } Thread.Sleep(1); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = list.Count() * tables.Count, Msg = string.Format("step3/3 生成代码{0}/{1}......", temp.Name, table) }); } } index = 1; //RanOnlyOnce list = tempList.Where(d => d.IsRanOnlyOnceByBath); foreach (var temp in list) { var output = TableBll.CodeGenerator("dts", codeBaseDict.Values.Select(d => d.ClassName).ToList(), temp.Path, null); var msg = temp.Name; if (output.StartsWith("error:")) { this._failureTableList.Add(msg); } else { var fileName = string.Format("{0}{1}{2}", temp.FileName, temp.ClassNameExtend, temp.FileNameExtend); var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName); savePath = Path.Combine(savePath, fileName); File.WriteAllText(savePath, output, Encoding.UTF8); this._successTableList.Add(msg); Thread.Sleep(1); TaskCenter.Instance.ReportProgress( new TaskReportProgressArg { Index = index++, Count = list.Count(), Msg = string.Format("step3/3 生成代码{0}......", temp.Name) }); } } return(null); }; taskArg3.EndAction = (result) => { this.Invoke(new MethodInvoker(() => { var msg = "导出完成"; //if (e.Error != null) //{ // msg = e.Error.Message; // this._successTableList.Clear(); // this._failureTableList.Clear(); //} //else if (e.Cancelled) // msg = "导出被取消!"; //else // msg = "导出完成!"; this.SetContorlsEnabled(true); var form = new BathCodeGeneratorResultForm(msg, this.txtOutputDirectory.Text, this._successTableList, this._failureTableList); form.ShowDialog(); })); }; TaskCenter.Instance.AddTask(taskArg1); TaskCenter.Instance.AddTask(taskArg2); TaskCenter.Instance.AddTask(taskArg3); TaskCenter.Instance.ExecuteTasks(); }