diff --git a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs index e503f8e..2b5355a 100644 --- a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs @@ -33,11 +33,17 @@ namespace ZR.Admin.WebApi.Controllers private CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService(); private IGenTableService GenTableService; private IGenTableColumnService GenTableColumnService; + private readonly ISysDictDataService SysDictDataService; private IWebHostEnvironment WebHostEnvironment; - public CodeGeneratorController(IGenTableService genTableService, IGenTableColumnService genTableColumnService, IWebHostEnvironment webHostEnvironment) + public CodeGeneratorController( + IGenTableService genTableService, + IGenTableColumnService genTableColumnService, + ISysDictDataService dictDataService, + IWebHostEnvironment webHostEnvironment) { GenTableService = genTableService; GenTableColumnService = genTableColumnService; + SysDictDataService = dictDataService; WebHostEnvironment = webHostEnvironment; } @@ -71,40 +77,6 @@ namespace ZR.Admin.WebApi.Controllers return SUCCESS(vm); } - /// - /// 代码生成器 - /// - /// 数据传输对象 - /// - [HttpPost("genCode")] - [Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)] - [ActionPermissionFilter(Permission = "tool:gen:code")] - public IActionResult Generate([FromBody] GenerateDto dto) - { - if (dto.TableId <= 0) - { - throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); - } - dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode"); - dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd")); - - var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); - genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); - if (!string.IsNullOrEmpty(genTableInfo.Options)) - { - Dictionary options = JsonConvert.DeserializeObject>(genTableInfo.Options); - dto.ParentMenuId = (long)options.GetValueOrDefault("parentMenuId", 0); - } - dto.GenTable = genTableInfo; - //生成代码 - CodeGeneratorTool.Generate(genTableInfo, dto); - //下载文件 - FileHelper.ZipGenCode(dto); - - //HttpContext.Response.Headers.Add("Content-disposition", $"attachment; filename={zipFileName}"); - return SUCCESS(new { zipPath = "/Generatecode/" + dto.ZipFileName, fileName = dto.ZipFileName }); - } - /// /// 获取代码生成表列表 /// @@ -241,6 +213,12 @@ namespace ZR.Admin.WebApi.Controllers } var genTableInfo = GenTableService.GetGenTableInfo(tableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(tableId); + + //var dictList = genTableInfo.Columns.FindAll(x => !string.IsNullOrEmpty(x.DictType)); + //foreach (var item in dictList) + //{ + // item.DictDatas = SysDictDataService.SelectDictDataByType(item.DictType); + //} GenerateDto dto = new(); dto.GenTable = genTableInfo; dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode"); @@ -252,5 +230,41 @@ namespace ZR.Admin.WebApi.Controllers return SUCCESS(dto.GenCodes); } + + /// + /// 生成代码 + /// + /// 数据传输对象 + /// + [HttpPost("genCode")] + [Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)] + [ActionPermissionFilter(Permission = "tool:gen:code")] + public IActionResult Generate([FromBody] GenerateDto dto) + { + if (dto.TableId <= 0) + { + throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); + } + dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode"); + dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd")); + + var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); + genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); + //附加参数,key,value格式 + if (!string.IsNullOrEmpty(genTableInfo.Options)) + { + Dictionary options = JsonConvert.DeserializeObject>(genTableInfo.Options); + dto.ParentMenuId = (long)options.GetValueOrDefault("parentMenuId", 0); + } + dto.GenTable = genTableInfo; + //生成代码 + CodeGeneratorTool.Generate(genTableInfo, dto); + //下载文件 + FileHelper.ZipGenCode(dto); + + //HttpContext.Response.Headers.Add("Content-disposition", $"attachment; filename={zipFileName}"); + return SUCCESS(new { zipPath = "/Generatecode/" + dto.ZipFileName, fileName = dto.ZipFileName }); + } + } } diff --git a/ZR.CodeGenerator/CodeGenerateTemplate.cs b/ZR.CodeGenerator/CodeGenerateTemplate.cs index 895b6d7..491b506 100644 --- a/ZR.CodeGenerator/CodeGenerateTemplate.cs +++ b/ZR.CodeGenerator/CodeGenerateTemplate.cs @@ -74,8 +74,8 @@ namespace ZR.CodeGenerator //时间类型 if (tbColumn.CsharpType == GenConstants.TYPE_DATE) { - replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.BeginTime != null, it => it.{tbColumn.CsharpField} >= parm.BeginTime);\n"; - replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.EndTime != null, it => it.{tbColumn.CsharpField} <= parm.EndTime);\n"; + replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.BeginTime != null, it => it.{tbColumn.CsharpField} >= parm.BeginTime);\n"; + replaceDto.QueryCondition += $" predicate = predicate.AndIF(parm.EndTime != null, it => it.{tbColumn.CsharpField} <= parm.EndTime);\n"; } } replaceDto.QueryProperty += QueryDtoContent; @@ -152,20 +152,23 @@ namespace ZR.CodeGenerator { return sb.ToString(); } - sb.AppendLine(""); - sb.AppendLine(""); + sb.AppendLine(" "); if (dbFieldInfo.HtmlType == GenConstants.HTML_INPUT_NUMBER) { + sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); + sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_DATETIME) { //时间 + sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); + sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_IMAGE_UPLOAD) { @@ -177,54 +180,68 @@ namespace ZR.CodeGenerator sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine(" "); + sb.AppendLine(" "); + } + else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO && !string.IsNullOrEmpty(dbFieldInfo.DictType)) + { + string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue"; + sb.AppendLine(" "); + sb.AppendLine($" "); + sb.AppendLine($" "); + sb.AppendLine($" {{item.dictLabel}}"); + sb.AppendLine(" "); + sb.AppendLine(" "); + sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO) { + sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(" "); sb.AppendLine(" "); + sb.AppendLine(" "); } - //else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO && !string.IsNullOrEmpty(dbFieldInfo.DictType)) - //{ - // sb.AppendLine($" "); - // sb.AppendLine($" "); - // //TODO 根据字典类型循环 - // sb.AppendLine(" "); - // sb.AppendLine(" "); - //} else if (dbFieldInfo.HtmlType == GenConstants.HTML_TEXTAREA) { + sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); + sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_EDITOR) { + sb.AppendLine(" "); sb.AppendLine($" "); - sb.AppendLine($" "); + sb.AppendLine($" "); sb.AppendLine(" "); + sb.AppendLine(" "); } else if (dbFieldInfo.HtmlType == GenConstants.HTML_SELECT && !string.IsNullOrEmpty(dbFieldInfo.DictType)) { string value = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? "parseInt(item.dictValue)" : "item.dictValue"; + sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); sb.AppendLine(" "); + sb.AppendLine(" "); } else { string inputNumTxt = CodeGeneratorTool.IsNumber(dbFieldInfo.CsharpType) ? ".number" : ""; + sb.AppendLine(" "); sb.AppendLine($" "); sb.AppendLine($" "); sb.AppendLine(" "); + sb.AppendLine(" "); } - sb.AppendLine(""); - sb.AppendLine(""); + + sb.AppendLine(" "); return sb.ToString(); } diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index 40f1a30..020d590 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -59,7 +59,6 @@ namespace ZR.CodeGenerator StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); - StringBuilder sb3 = new StringBuilder(); //循环表字段信息 foreach (GenTableColumn dbFieldInfo in listField) @@ -85,12 +84,6 @@ namespace ZR.CodeGenerator { replaceDto.InsertColumn += $" it.{dbFieldInfo.CsharpField}, \n"; } - //TODO 查询 - //if (dbFieldInfo.IsQuery) - //{ - // replaceDto.Querycondition += $"predicate = predicate.And(m => m.{dbFieldInfo.CsharpField}.Contains(parm.Name));"; - //} - if ((dbFieldInfo.HtmlType == GenConstants.HTML_SELECT || dbFieldInfo.HtmlType == GenConstants.HTML_RADIO) && !string.IsNullOrEmpty(dbFieldInfo.DictType)) { sb1.AppendLine($" // {dbFieldInfo.ColumnComment}选项列表"); diff --git a/ZR.CodeGenerator/DbProvider.cs b/ZR.CodeGenerator/DbProvider.cs index 5523ed9..ee7a98f 100644 --- a/ZR.CodeGenerator/DbProvider.cs +++ b/ZR.CodeGenerator/DbProvider.cs @@ -29,10 +29,7 @@ namespace ZR.CodeGenerator if (!string.IsNullOrEmpty(dbName)) { string replaceStr = GetValue(connStr, "database=", ";"); - connStr = connStr.Replace(replaceStr, dbName); - //connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.ConnAdmin); - //dbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.ConnDbType); } var db = new SqlSugarScope(new List() { diff --git a/ZR.Model/System/Generate/GenTableColumn.cs b/ZR.Model/System/Generate/GenTableColumn.cs index 964c770..8c1c284 100644 --- a/ZR.Model/System/Generate/GenTableColumn.cs +++ b/ZR.Model/System/Generate/GenTableColumn.cs @@ -63,5 +63,10 @@ namespace ZR.Model.System.Generate /// 字典类型 /// public string DictType { get; set; } + /// + /// 字典集合 + /// + [SqlSugar.SugarColumn(IsIgnore = true)] + public List DictDatas { get; set; } } } diff --git a/ZR.Model/ZR.Model.csproj b/ZR.Model/ZR.Model.csproj index e075f98..9d0f89b 100644 --- a/ZR.Model/ZR.Model.csproj +++ b/ZR.Model/ZR.Model.csproj @@ -17,6 +17,7 @@ + diff --git a/ZR.Vue/src/views/tool/gen/editTable.vue b/ZR.Vue/src/views/tool/gen/editTable.vue index f6ce1a2..89dc9fd 100644 --- a/ZR.Vue/src/views/tool/gen/editTable.vue +++ b/ZR.Vue/src/views/tool/gen/editTable.vue @@ -184,7 +184,7 @@ export default { // treeParentCode: genTable.treeParentCode, // parentMenuId: genTable.parentMenuId, // }; - console.log(JSON.stringify(genTable)); + // console.log(JSON.stringify(genTable)); updateGenTable(genTable).then((res) => { this.msgSuccess(res.msg); if (res.code === 200) { diff --git a/ZR.Vue/src/views/tool/gen/index.vue b/ZR.Vue/src/views/tool/gen/index.vue index 9765f2a..085450c 100644 --- a/ZR.Vue/src/views/tool/gen/index.vue +++ b/ZR.Vue/src/views/tool/gen/index.vue @@ -166,7 +166,6 @@ export default { }, // 代码预览 handlePreview(row) { - // this.msgError("敬请期待"); previewTable(row.tableId).then((res) => { if (res.code === 200) { this.preview.open = true;