using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.IO; using Newtonsoft.Json; using System.Windows.Forms.VisualStyles; using GenerateCode_WeiBen_WMS.DataBaseFactory; using GenerateCode_WeiBen_WMS.Const; using GenerateCode_WeiBen_WMS.Model; namespace GenerateCode_GEBrilliantFactory { public partial class MainForm : Form { public static string projectNamePrefix = ""; public static string dataBaseName = ""; public static DataBaseEnum dataBaseEnum = DataBaseEnum.MySql; public MainForm() { InitializeComponent(); this.tbPath.Text = "D:\\C#AutoCreateCodeFile"; this.tb_Primary.Text = "id";//主键名 this.tb_PrimaryDesc.Text = "主键"; this.tb_OrderBy.Text = "ModifyTime";//排序字段 this.tb_ProjectNamePrefix.Text = "Test"; this.tb_TableName.Text = "scms_wmsmaterials";//表名 this.tb_WCF_NameSpacePath.Text = "HIAWms";//WCF项目命名空间 this.tb_FileName.Text = "Xiangzi";//文件前缀名 this.tb_ChinaComment.Text = "物料基础信息表";//中文注释 this.tb_CreatePerson.Text = "shaocx";//创建人 this.tb_EntityName.Text = "WmsMaterial";//实体类名 this.tb_EntityProName.Text = "_wmsMaterial";//实体类对象名 this.tb_EnumList.Text = "PurchaseType|PurchaseTypeEnum|PurchaseTypeDesc,MaterialType|MaterialTypeEnum|MaterialTypeDesc,IsMainBranch|YesNoEnum|IsMainBranchDesc"; this.cmb_DataSource.DropDownStyle = ComboBoxStyle.DropDownList; List itemList = CommonHelper.GetDataSources(); foreach (var item in itemList) { this.cmb_DataSource.Items.Add(item); } this.cmb_DataSource.SelectedIndex = 0; this.cmb_DataBase.DropDownStyle = ComboBoxStyle.DropDownList; List itemList_Db = CommonHelper.GetDataBase(); foreach (var item in itemList_Db) { this.cmb_DataBase.Items.Add(item); } this.cmb_DataBase.SelectedIndex = 0; } /// /// 保存路径对话框 /// /// /// private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); if (DialogResult.OK == fbd.ShowDialog()) { tbPath.Text = fbd.SelectedPath; } } private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } //一键生成所有文件 private void btn_CreateFile_Click(object sender, EventArgs e) { try { projectNamePrefix = this.tb_ProjectNamePrefix.Text; if (projectNamePrefix == "") { MessageBox.Show("请输入项目名前缀!"); this.tb_ProjectNamePrefix.Focus(); return; } if (dataBaseName == "") { MessageBox.Show("请选择数据库!"); return; } string primaryKey = this.tb_Primary.Text.Trim();//主键名 string primaryKeyDesc = this.tb_PrimaryDesc.Text.Trim();//主键描述 string tableName = this.tb_TableName.Text.Trim();//表名 string wcf_NameSpacePath = this.tb_WCF_NameSpacePath.Text.Trim();//WCF项目命名空间 string filePrefixName = this.tb_FileName.Text.Trim();//文件前缀名 string modulelogo = this.tb_Modulelogo.Text.Trim();//模块简写 string chinaComment = this.tb_ChinaComment.Text.Trim();//中文注释 string createPerson = this.tb_CreatePerson.Text.Trim();//创建人 string entityName = this.tb_EntityName.Text.Trim();//实体类名 string tableAlias = this.tb_EntityProName.Text.Trim();//实体类对象名/表别名 string orderByName = this.tb_OrderBy.Text.Trim();//排序字段名称 string routePrefix = this.tb_RoutePrefix.Text.Trim();//WCF路由前缀 string connStr = this.lbl_DataSource.Text.Trim();//数据库连接字符串 if (connStr == "") { MessageBox.Show("请选择数据库源!"); this.cmb_DataSource.Focus(); return; } if (tableName == "") { MessageBox.Show("请输入表名!"); this.tb_TableName.Focus(); return; } if (primaryKey == "") { MessageBox.Show("请输入主键名!"); this.tb_Primary.Focus(); return; } if (routePrefix == "") { MessageBox.Show("请输入WCF路由前缀!"); this.tb_RoutePrefix.Focus(); return; } if (wcf_NameSpacePath == "") { MessageBox.Show("请输入命名空间!"); this.tb_WCF_NameSpacePath.Focus(); return; } //读取Mysql var handler = DataBaseServiceFactory.GetHandle(dataBaseEnum); List columnList = handler.GetColumnList(tableName, connStr); if (columnList.Count == 0) { MessageBox.Show("没有获取到表下面的列集合!"); return; } string groupName = this.tb_GroupName.Text.Trim(); //if (groupName == "") //{ // MessageBox.Show("请输入分组名!"); // this.tb_GroupName.Focus(); // return; //} string addEntityParam = "Add" + modulelogo + "Param";//新增参数类名 string str_generate = ""; bool tf; GenerateCodeParam param = new GenerateCodeParam() { Modulelogo = modulelogo, ChinaComment = chinaComment, NameSpacePath = wcf_NameSpacePath, ColumnNameList = columnList, EntityName = entityName }; //处理枚举类型 List enumList = new List(); var str_enumList = this.tb_EnumList.Text.Trim(); if (str_enumList.IndexOf(',') > -1) { var arr_enumList = str_enumList.Split(','); foreach (var item in arr_enumList) { var arr_single = item.Split('|'); enumList.Add(new EnumModel() { EnumAttrName = arr_single[0], EnumType = arr_single[1], EnumTypeDesc = arr_single[2] }); } } param.EnumList = enumList; //生成Model //CreateModelFile(columnList, tableName, filePrefixName, wcf_NameSpacePath, createPerson, chinaComment, entityName, modulelogo); //CreateModelParamFile(columnList, tableName, filePrefixName, wcf_NameSpacePath, createPerson, chinaComment, entityName, modulelogo); #region 实体类 var entityFilePath = tbPath.Text + @"\server\src\CMS.Plugin." + tb_WCF_NameSpacePath.Text.Trim() + @".Application.Contracts\Dtos\" + tb_EntityName.Text.Trim(); //1、生成 分页查询实体 文件 str_generate = InputModel_Generate.CreateQueryModelLText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"Get{param.EntityName}Input.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //2、生成 CreateDto 文件 str_generate = CreateDto_Generate.CreateText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"{param.EntityName}CreateDto.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //3、生成 UpdateDto 文件 str_generate = UpdateDto_Generate.CreateText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"{param.EntityName}UpdateDto.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //4、生成 CreateOrUpdateDtoBase 文件 str_generate = CreateOrUpdateDtoBase_Generate.CreateText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"{param.EntityName}CreateOrUpdateDtoBase.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //5、生成 Dto 文件 str_generate = Dto_Generate.CreateText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"{param.EntityName}Dto.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //6、生成 Export 文件 str_generate = ExportModel_Generate.CreateText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"{param.EntityName}ExportModel.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //7、生成 Import 文件 str_generate = ImportModel_Generate.CreateText(param); tf = TextHelper.Export2File_V2(entityFilePath, $"{param.EntityName}sImportModel.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); #endregion #region AppSerivice //8、生成 IAppService 文件 var appServiceFilePath = tbPath.Text + @"\server\src\CMS.Plugin." + tb_WCF_NameSpacePath.Text.Trim() + @".Application.Contracts\Services"; str_generate = IAppService_Generate.CreateText(param); tf = TextHelper.Export2File_V2(appServiceFilePath, $"I{param.EntityName}AppService.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); //9、生成 AppService 文件 appServiceFilePath = tbPath.Text + @"\server\src\CMS.Plugin." + tb_WCF_NameSpacePath.Text.Trim() + @".Application\Implements"; str_generate = AppService_Generate.CreateText(param); tf = TextHelper.Export2File_V2(appServiceFilePath, $"I{param.EntityName}AppService.cs", tableName, str_generate, FileType.InputModel, filePrefixName, entityName, modulelogo); #endregion ////生成 OutputModel 文件 //str_generate = OutputModel_Generate.CreateQueryModelLText(modulelogo, chinaComment, columnList, entityName); //tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.OutputModel, filePrefixName, entityName, modulelogo); ////生成Controller文件 ////str_generate = Controller_Generate.CreateText(modulelogo, chinaComment, columnList, entityName, orderByName); ////tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.Controller, filePrefixName, entityName, modulelogo); ////生成Services接口文件 //str_generate = Services_Interface_Generate.CreateText(wcf_NameSpacePath, modulelogo, entityName, chinaComment, addEntityParam); //tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.WCF_InterFace, filePrefixName, entityName, modulelogo); ////生成Services接口实现文件 //str_generate = Services_InterfaceRealize_Generate.CreateText(wcf_NameSpacePath, modulelogo, // entityName, chinaComment, filePrefixName, primaryKey, tableAlias, addEntityParam, columnList, groupName); //tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.WCF_InterFaceRealize, filePrefixName, entityName, modulelogo); /* //生成存储过程文件 str_generate = Procedure_Generate.CreateProcText(tableName, tableAlias, createPerson, chinaComment, primaryKey, filePrefixName, orderByName, modulelogo, columnList); tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.Proc, filePrefixName, entityName, modulelogo); //生成DAL文件 str_generate = DAL_Generate.CreateDALText(filePrefixName, tableName, entityName, createPerson, chinaComment, primaryKey, primaryKeyDesc, modulelogo, tableAlias, columnList); tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.DAL, filePrefixName, entityName, modulelogo); //生成BLL文件 str_generate = BLL_Generate.CreateBLLText(filePrefixName, tableName, entityName, createPerson, chinaComment, primaryKey, primaryKeyDesc, modulelogo, tableAlias, addEntityParam, columnList); tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.BLL, filePrefixName, entityName, modulelogo); //生成AddModel文件 str_generate = AddModel_Generate.CreateAddModelLText(addEntityParam, chinaComment, columnList); tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.AddModelParam, filePrefixName, entityName, modulelogo); //*/ //VUE方法配置 //str_generate = VUE_FunConfig_Generate.CreateText(modulelogo, chinaComment, routePrefix, entityName); //tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.VUE_FunConfig, filePrefixName, entityName, modulelogo); //VUE文件 //str_generate = VUE_Generate.CreateText(tableAlias, modulelogo, primaryKey, columnList, chinaComment // , this.tb_templeteFileDownName.Text, this.tb_importExcelCategroy.Text); //tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.VUEFile, filePrefixName, entityName, modulelogo); //btn_InsertSql_Click(null, null); } catch (Exception ex) { MessageBox.Show("生成文件失败!" + ex.Message); return; } //MessageBox.Show("生成文件成功!"); //成功之后打开文件夹 using (System.Diagnostics.Process.Start(this.tbPath.Text)) { } //*/ } #region 分类文件 /// /// 生成Model /// /// private void CreateModelFile(List strList, string _tableName, string filePrefixName, string nameSpacePath, string createPerson, string chinaComment, string entityName, string modulelogo) { bool tf = TextHelper.Export2File(tbPath.Text, _tableName, Model_Generate.GenerateModel(_tableName, strList, filePrefixName, nameSpacePath, createPerson, chinaComment, entityName), FileType.Model, filePrefixName, entityName, modulelogo); } /// /// 生成Model参数 /// /// private void CreateModelParamFile(List strList, string _tableName, string filePrefixName, string nameSpacePath, string createPerson, string chinaComment, string entityName, string modulelogo) { bool tf = TextHelper.Export2File(tbPath.Text, _tableName, Model_Generate.GenerateModelParam(_tableName, strList, filePrefixName, nameSpacePath, createPerson, chinaComment, entityName), FileType.Model, filePrefixName, entityName, modulelogo); } #endregion /// /// 文本改变事件 /// /// /// private void tb_TableName_TextChanged(object sender, EventArgs e) { string tableName = this.tb_TableName.Text.Trim();//表名 if (tableName != "") { var str = CommonHelper.TitleToUpper(tableName); this.tb_FileName.Text = this.tb_EntityName.Text = str; var index = tableName.IndexOf('_'); if (index > -1) { var moule_str = tableName.Substring(index + 1, tableName.Length - index - 1); this.tb_Modulelogo.Text = moule_str; this.tb_EntityProName.Text = CommonHelper.TitleToLower(moule_str); } else { this.tb_Modulelogo.Text = tableName; this.tb_EntityProName.Text = CommonHelper.TitleToLower(tableName); } } } private void btn_InsertSql_Click(object sender, EventArgs e) { string connStr = this.lbl_DataSource.Text.Trim();//数据库连接字符串 if (connStr == "") { MessageBox.Show("请选择数据库源!"); this.cmb_DataSource.Focus(); return; } var tableName = this.tb_TableName.Text.Trim(); if (tableName == string.Empty) { MessageBox.Show("请输入表名!"); return; } List columnList = StructStrHelper.GetColumnList(tableName, connStr); if (columnList.Count == 0) { MessageBox.Show("没有获取到表下面的列集合!"); return; } string chinaComment = this.tb_ChinaComment.Text.Trim();//中文注释 string createPerson = this.tb_CreatePerson.Text.Trim();//创建人 var str_generate = InsertSQL_Generate.CreateInsertSQLText(tableName, createPerson, chinaComment, columnList); bool tf = TextHelper.Export2File(tbPath.Text, tableName, str_generate, FileType.SQL_Insert, "", "", ""); //MessageBox.Show("生成文件成功!"); //成功之后打开文件夹 using (System.Diagnostics.Process.Start(this.tbPath.Text + "\\" + this.tb_TableName.Text)) { } } private void label13_Click(object sender, EventArgs e) { } private void MainForm_Load(object sender, EventArgs e) { tb_EntityProName.Enabled = false; tb_FileName.Enabled = false; tb_Modulelogo.Enabled = false; tb_WCF_NameSpacePath.Enabled = true; tb_RoutePrefix.Enabled = false; } private void cmb_DataSource_SelectedIndexChanged(object sender, EventArgs e) { string connStr = (this.cmb_DataSource.SelectedItem as ListItem).Value; var name = (this.cmb_DataSource.SelectedItem as ListItem).Text; this.lbl_DataSource.Text = connStr; dataBaseName = name; if (name == "GSiemens_LES") { this.tb_ProjectNamePrefix.Text = "LES"; } else if (name == "GSiemens_WIP") { this.tb_ProjectNamePrefix.Text = "WIP"; } else if (name.Contains("AoSinWms")) { this.tb_ProjectNamePrefix.Text = "AoSinWms"; } else { this.tb_ProjectNamePrefix.Text = "Test"; } } private void ck_IsShowImport_CheckedChanged(object sender, EventArgs e) { //this.tb_templeteFileDownName.Enabled = false; //this.tb_importExcelCategroy.Enabled = false; //if (this.ck_IsShowImport.Checked) //{ // this.tb_templeteFileDownName.Enabled = true; // this.tb_importExcelCategroy.Enabled = true; //} } private void tb_EntityProName_TextChanged(object sender, EventArgs e) { } private void cmb_DataBase_SelectedIndexChanged(object sender, EventArgs e) { string connStr = (this.cmb_DataBase.SelectedItem as ListItem).Value; var name = (this.cmb_DataBase.SelectedItem as ListItem).Text; dataBaseEnum = (DataBaseEnum)Enum.Parse(typeof(DataBaseEnum), name); } } }