2021-09-18 16:10:34 +08:00
|
|
|
|
using Infrastructure;
|
2021-09-07 21:52:44 +08:00
|
|
|
|
using System;
|
2021-09-07 18:37:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
2021-09-09 18:18:37 +08:00
|
|
|
|
using ZR.CodeGenerator.Model;
|
2021-09-18 16:10:34 +08:00
|
|
|
|
using ZR.Model.System.Generate;
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ZR.CodeGenerator
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 代码生成器。
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// 根据指定的实体域名空间生成Repositories和Services层的基础代码文件。
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class CodeGeneratorTool
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 代码生成器配置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static CodeGenerateOption _option = new CodeGenerateOption();
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// InputDto输入实体是不包含字段
|
|
|
|
|
|
/// </summary>
|
2021-09-21 20:31:35 +08:00
|
|
|
|
public static readonly string[] inputDtoNoField = new string[] { "createTime", "updateTime", "addtime" };
|
2021-09-13 18:38:54 +08:00
|
|
|
|
public static readonly string[] imageFiled = new string[] { "icon", "img", "image", "url", "pic", "photo" };
|
|
|
|
|
|
public static readonly string[] selectFiled = new string[] { "status", "type", "state", "sex", "gender" };
|
|
|
|
|
|
public static readonly string[] radioFiled = new string[] { "status", "state", "isShow", "isHidden", "ishide" };
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 代码生成器入口方法
|
|
|
|
|
|
/// </summary>
|
2021-09-07 21:52:44 +08:00
|
|
|
|
/// <param name="dbTableInfo"></param>
|
2021-09-09 18:18:37 +08:00
|
|
|
|
/// <param name="dto"></param>
|
2021-09-18 16:10:34 +08:00
|
|
|
|
public static void Generate(GenTable dbTableInfo, GenerateDto dto)
|
2021-09-07 18:37:21 +08:00
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
_option.BaseNamespace = "ZR.";
|
2021-09-13 18:38:54 +08:00
|
|
|
|
//_option.TableList = listTable;
|
|
|
|
|
|
_option.ReplaceTableNameStr = dto.replaceTableNameStr;
|
2021-09-16 17:52:31 +08:00
|
|
|
|
_option.DtosNamespace = _option.BaseNamespace + "Model";
|
|
|
|
|
|
_option.ModelsNamespace = _option.BaseNamespace + "Model";
|
|
|
|
|
|
_option.RepositoriesNamespace = _option.BaseNamespace + "Repository";
|
|
|
|
|
|
_option.IRepositoriesNamespace = _option.BaseNamespace + "Repository";
|
|
|
|
|
|
_option.IServicsNamespace = _option.BaseNamespace + "Service";
|
|
|
|
|
|
_option.ServicesNamespace = _option.BaseNamespace + "Service";
|
|
|
|
|
|
_option.ApiControllerNamespace = _option.BaseNamespace + "Admin.WebApi";
|
2021-09-09 18:18:37 +08:00
|
|
|
|
|
2021-09-18 16:10:34 +08:00
|
|
|
|
GenerateSingle(dbTableInfo?.Columns, dbTableInfo, dto);
|
2021-09-07 18:37:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 单表生成代码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="listField">表字段集合</param>
|
|
|
|
|
|
/// <param name="tableInfo">表信息</param>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
/// <param name="dto"></param>
|
2021-09-18 16:10:34 +08:00
|
|
|
|
public static void GenerateSingle(List<GenTableColumn> listField, GenTable tableInfo, GenerateDto dto)
|
2021-09-07 18:37:21 +08:00
|
|
|
|
{
|
2021-09-19 20:50:49 +08:00
|
|
|
|
string PKName = "id";
|
|
|
|
|
|
string PKType = "int";
|
2021-09-21 20:31:35 +08:00
|
|
|
|
ReplaceDto replaceDto = new();
|
|
|
|
|
|
replaceDto.ModelTypeName = tableInfo.ClassName;//表名对应C# 实体类名
|
|
|
|
|
|
replaceDto.TableName = tableInfo.TableName;
|
|
|
|
|
|
replaceDto.TableDesc = tableInfo.TableComment;
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
2021-09-19 20:50:49 +08:00
|
|
|
|
//循环表字段信息
|
2021-09-18 16:10:34 +08:00
|
|
|
|
foreach (GenTableColumn dbFieldInfo in listField)
|
2021-09-07 18:37:21 +08:00
|
|
|
|
{
|
2021-09-18 16:10:34 +08:00
|
|
|
|
string columnName = dbFieldInfo.ColumnName;
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
2021-09-19 20:50:49 +08:00
|
|
|
|
if (dbFieldInfo.IsInsert || dbFieldInfo.IsEdit)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-21 20:31:35 +08:00
|
|
|
|
replaceDto.VueViewEditFormHtml += $"{columnName}: undefined,\r\n";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
2021-09-18 16:10:34 +08:00
|
|
|
|
if (dbFieldInfo.IsPk || dbFieldInfo.IsIncrement)
|
2021-09-08 22:02:05 +08:00
|
|
|
|
{
|
2021-09-19 20:50:49 +08:00
|
|
|
|
PKName = dbFieldInfo.CsharpField;
|
|
|
|
|
|
PKType = dbFieldInfo.CsharpType;
|
2021-09-08 22:02:05 +08:00
|
|
|
|
}
|
2021-09-18 18:13:28 +08:00
|
|
|
|
//编辑字段
|
2021-09-19 20:50:49 +08:00
|
|
|
|
if (dbFieldInfo.IsEdit)
|
2021-09-10 22:15:01 +08:00
|
|
|
|
{
|
2021-09-21 20:31:35 +08:00
|
|
|
|
replaceDto.UpdateColumn += $"{dbFieldInfo.CsharpField} = model.{dbFieldInfo.CsharpField}, ";
|
|
|
|
|
|
}
|
|
|
|
|
|
//新增字段
|
|
|
|
|
|
if (dbFieldInfo.IsInsert)
|
|
|
|
|
|
{
|
|
|
|
|
|
replaceDto.InsertColumn += $"it.{dbFieldInfo.CsharpField}, ";
|
|
|
|
|
|
}
|
|
|
|
|
|
//查询
|
|
|
|
|
|
//if (dbFieldInfo.IsQuery)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// replaceDto.Querycondition += $"predicate = predicate.And(m => m.{dbFieldInfo.CsharpField}.Contains(parm.Name));";
|
|
|
|
|
|
//}
|
|
|
|
|
|
if (dbFieldInfo.HtmlType == GenConstants.HTML_SELECT && !string.IsNullOrEmpty(dbFieldInfo.DictType))
|
|
|
|
|
|
{
|
|
|
|
|
|
replaceDto.VueDataContent += $"// {dbFieldInfo.ColumnComment}选项列表\n";
|
|
|
|
|
|
replaceDto.VueDataContent += $"{FirstLowerCase(dbFieldInfo.CsharpField)}Options: [],";
|
2021-09-10 22:15:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-21 20:31:35 +08:00
|
|
|
|
replaceDto.QueryProperty += CodeGenerateTemplate.GetQueryDtoProperty(dbFieldInfo);
|
|
|
|
|
|
replaceDto.ModelProperty += CodeGenerateTemplate.GetModelTemplate(dbFieldInfo);
|
|
|
|
|
|
replaceDto.VueViewFormHtml += CodeGenerateTemplate.GetVueViewFormContent(dbFieldInfo);
|
|
|
|
|
|
replaceDto.VueJsMethod += CodeGenerateTemplate.GetVueJsMethod(dbFieldInfo);
|
|
|
|
|
|
replaceDto.VueViewListHtml += CodeGenerateTemplate.GetTableColumn(dbFieldInfo);
|
|
|
|
|
|
replaceDto.VueViewEditFormRuleContent += CodeGenerateTemplate.GetFormRules(dbFieldInfo);
|
|
|
|
|
|
replaceDto.InputDtoProperty += CodeGenerateTemplate.GetDtoProperty(dbFieldInfo);
|
|
|
|
|
|
replaceDto.VueQueryFormHtml += CodeGenerateTemplate.GetQueryFormHtml(dbFieldInfo);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
2021-09-19 20:50:49 +08:00
|
|
|
|
replaceDto.PKName = PKName;
|
|
|
|
|
|
replaceDto.PKType = PKType;
|
2021-09-16 17:52:31 +08:00
|
|
|
|
|
2021-09-09 18:18:37 +08:00
|
|
|
|
if (dto.genFiles.Contains(1))
|
|
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
GenerateModels(replaceDto, dto);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (dto.genFiles.Contains(2))
|
|
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
GenerateInputDto(replaceDto, dto);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (dto.genFiles.Contains(3))
|
|
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
GenerateRepository(replaceDto, dto);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (dto.genFiles.Contains(4))
|
|
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
GenerateIService(replaceDto, dto);
|
|
|
|
|
|
GenerateService(replaceDto, dto);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (dto.genFiles.Contains(5))
|
|
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
GenerateControllers(replaceDto, dto);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (dto.genFiles.Contains(6))
|
|
|
|
|
|
{
|
2021-09-16 17:52:31 +08:00
|
|
|
|
GenerateVueViews(replaceDto, dto);
|
2021-09-08 07:48:18 +08:00
|
|
|
|
}
|
2021-09-08 18:12:08 +08:00
|
|
|
|
//GenerateIRepository(modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered);
|
2021-09-07 18:37:21 +08:00
|
|
|
|
//GenerateOutputDto(modelTypeName, modelTypeDesc, outputDtocontent, ifExsitedCovered);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 生成Model
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成Models文件
|
|
|
|
|
|
/// </summary>
|
2021-09-19 20:50:49 +08:00
|
|
|
|
/// <param name="generateDto"></param>
|
|
|
|
|
|
/// <param name="replaceDto">替换实体</param>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateModels(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-07 18:37:21 +08:00
|
|
|
|
{
|
2021-09-08 07:48:18 +08:00
|
|
|
|
var parentPath = "..";
|
|
|
|
|
|
//../ZR.Model
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var servicesPath = parentPath + "\\" + _option.ModelsNamespace + "\\Models\\";
|
2021-09-07 18:37:21 +08:00
|
|
|
|
if (!Directory.Exists(servicesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(servicesPath);
|
|
|
|
|
|
}
|
2021-09-08 07:48:18 +08:00
|
|
|
|
// ../ZR.Model/Models/User.cs
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var fullPath = servicesPath + replaceDto.ModelTypeName + ".cs";
|
2021-09-09 18:18:37 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, "");
|
2021-09-07 18:37:21 +08:00
|
|
|
|
var content = ReadTemplate("ModelTemplate.txt");
|
|
|
|
|
|
content = content
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelsNamespace}", _option.ModelsNamespace)
|
|
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{TableNameDesc}", replaceDto.TableDesc)
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.PKName)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{PropertyName}", replaceDto.ModelProperty)
|
|
|
|
|
|
.Replace("{TableName}", replaceDto.TableName);
|
2021-09-07 18:37:21 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-07 18:37:21 +08:00
|
|
|
|
}
|
2021-09-08 07:48:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成InputDto文件
|
|
|
|
|
|
/// </summary>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
/// <param name="generateDto"></param>
|
2021-09-19 20:50:49 +08:00
|
|
|
|
/// <param name="replaceDto">替换实体</param>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateInputDto(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-08 07:48:18 +08:00
|
|
|
|
{
|
|
|
|
|
|
var parentPath = "..";
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var servicesPath = parentPath + "\\" + _option.ModelsNamespace + "\\Dto\\";
|
2021-09-08 07:48:18 +08:00
|
|
|
|
if (!Directory.Exists(servicesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(servicesPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
// ../ZR.Model/Dto/User.cs
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var fullPath = servicesPath + replaceDto.ModelTypeName + "Dto.cs";
|
2021-09-09 18:18:37 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-13 18:38:54 +08:00
|
|
|
|
return Tuple.Create(fullPath, ""); ;
|
2021-09-08 07:48:18 +08:00
|
|
|
|
var content = ReadTemplate("InputDtoTemplate.txt");
|
|
|
|
|
|
content = content
|
|
|
|
|
|
.Replace("{DtosNamespace}", _option.DtosNamespace)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelsNamespace}", _option.ModelsNamespace)
|
|
|
|
|
|
.Replace("{TableNameDesc}", replaceDto.TableDesc)
|
|
|
|
|
|
.Replace("{PropertyName}", replaceDto.InputDtoProperty)
|
2021-09-21 20:31:35 +08:00
|
|
|
|
.Replace("{QueryProperty}", replaceDto.QueryProperty)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName);
|
2021-09-08 07:48:18 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-08 07:48:18 +08:00
|
|
|
|
}
|
2021-09-07 18:37:21 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2021-09-08 18:12:08 +08:00
|
|
|
|
#region 生成Repository
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成Repository层代码文件
|
|
|
|
|
|
/// </summary>
|
2021-09-19 20:50:49 +08:00
|
|
|
|
/// <param name="generateDto"></param>
|
|
|
|
|
|
/// <param name="replaceDto">替换实体</param>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateRepository(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
var parentPath = "..";
|
|
|
|
|
|
var repositoryPath = parentPath + "\\" + _option.RepositoriesNamespace + "\\Repositories\\";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
if (!Directory.Exists(repositoryPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(repositoryPath);
|
|
|
|
|
|
}
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var fullPath = repositoryPath + "\\" + replaceDto.ModelTypeName + "Repository.cs";
|
2021-09-09 18:18:37 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, "");
|
2021-09-08 18:12:08 +08:00
|
|
|
|
var content = ReadTemplate("RepositoryTemplate.txt");
|
|
|
|
|
|
content = content.Replace("{ModelsNamespace}", _option.ModelsNamespace)
|
2021-09-09 18:18:37 +08:00
|
|
|
|
//.Replace("{IRepositoriesNamespace}", _option.IRepositoriesNamespace)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
.Replace("{RepositoriesNamespace}", _option.RepositoriesNamespace)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{TableNameDesc}", replaceDto.TableDesc)
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{TableName}", replaceDto.TableName);
|
|
|
|
|
|
|
2021-09-08 18:12:08 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 生成Service
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成IService文件
|
|
|
|
|
|
/// </summary>
|
2021-09-19 20:50:49 +08:00
|
|
|
|
/// <param name="generateDto"></param>
|
|
|
|
|
|
/// <param name="replaceDto">替换实体</param>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateIService(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
var parentPath = "..";
|
|
|
|
|
|
var iServicesPath = parentPath + "\\" + _option.IServicsNamespace + "\\Business\\IBusService\\";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
if (!Directory.Exists(iServicesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(iServicesPath);
|
|
|
|
|
|
}
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var fullPath = $"{iServicesPath}\\I{replaceDto.ModelTypeName}Service.cs";
|
2021-09-09 18:18:37 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-13 18:38:54 +08:00
|
|
|
|
return Tuple.Create(fullPath, "");
|
2021-09-08 18:12:08 +08:00
|
|
|
|
var content = ReadTemplate("IServiceTemplate.txt");
|
2021-09-16 17:52:31 +08:00
|
|
|
|
content = content.Replace("{ModelsNamespace}", _option.ModelsNamespace)
|
|
|
|
|
|
.Replace("{TableNameDesc}", replaceDto.TableDesc)
|
2021-09-10 22:15:01 +08:00
|
|
|
|
.Replace("{DtosNamespace}", _option.DtosNamespace)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
.Replace("{IServicsNamespace}", _option.IServicsNamespace)
|
2021-09-10 22:15:01 +08:00
|
|
|
|
.Replace("{RepositoriesNamespace}", _option.RepositoriesNamespace)
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName);
|
|
|
|
|
|
|
2021-09-08 18:12:08 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成Service文件
|
|
|
|
|
|
/// </summary>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateService(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
var parentPath = "..";
|
|
|
|
|
|
var servicesPath = parentPath + "\\" + _option.ServicesNamespace + "\\Business\\";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
if (!Directory.Exists(servicesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(servicesPath);
|
|
|
|
|
|
}
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var fullPath = servicesPath + replaceDto.ModelTypeName + "Service.cs";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, "");
|
2021-09-08 18:12:08 +08:00
|
|
|
|
var content = ReadTemplate("ServiceTemplate.txt");
|
|
|
|
|
|
content = content
|
|
|
|
|
|
.Replace("{IRepositoriesNamespace}", _option.IRepositoriesNamespace)
|
|
|
|
|
|
.Replace("{DtosNamespace}", _option.DtosNamespace)
|
|
|
|
|
|
.Replace("{IServicsNamespace}", _option.IServicsNamespace)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{TableNameDesc}", replaceDto.TableDesc)
|
|
|
|
|
|
.Replace("{ModelsNamespace}", _option.ModelsNamespace)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
.Replace("{ServicesNamespace}", _option.ServicesNamespace)
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName);
|
|
|
|
|
|
|
2021-09-08 18:12:08 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 生成Controller
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成控制器ApiControllers文件
|
|
|
|
|
|
/// </summary>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateControllers(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
var parentPath = "..";
|
|
|
|
|
|
var servicesPath = parentPath + "\\" + _option.ApiControllerNamespace + "\\Controllers\\business\\";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
if (!Directory.Exists(servicesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(servicesPath);
|
|
|
|
|
|
}
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var fullPath = servicesPath + replaceDto.ModelTypeName + "Controller.cs";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, "");
|
2021-09-08 18:12:08 +08:00
|
|
|
|
var content = ReadTemplate("ControllersTemplate.txt");
|
|
|
|
|
|
content = content
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ApiControllerNamespace}", _option.ApiControllerNamespace)
|
|
|
|
|
|
.Replace("{ServicesNamespace}", _option.ServicesNamespace)
|
2021-09-10 22:15:01 +08:00
|
|
|
|
.Replace("{ModelsNamespace}", _option.ModelsNamespace)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{TableDesc}", replaceDto.TableDesc)
|
|
|
|
|
|
.Replace("{ModelName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{Permission}", replaceDto.ModelTypeName.ToLower())
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{PrimaryKey}", replaceDto.PKName)
|
2021-09-21 20:31:35 +08:00
|
|
|
|
.Replace("{UpdateColumn}", replaceDto.UpdateColumn)
|
|
|
|
|
|
.Replace("{InsertColumn}", replaceDto.InsertColumn)
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.PKType);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 生成Vue页面
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 生成Vue页面
|
2021-09-16 17:52:31 +08:00
|
|
|
|
private static Tuple<string, string> GenerateVueViews(ReplaceDto replaceDto, GenerateDto generateDto)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-10 22:15:01 +08:00
|
|
|
|
//var parentPath = "..\\CodeGenerate";//若要生成到项目中将路径改成 “..\\ZR.Vue\\src”
|
|
|
|
|
|
var parentPath = "..\\ZR.Vue\\src";
|
2021-09-16 17:52:31 +08:00
|
|
|
|
var servicesPath = parentPath + "\\views\\" + FirstLowerCase(replaceDto.ModelTypeName);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
if (!Directory.Exists(servicesPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(servicesPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
var fullPath = servicesPath + "\\" + "index.vue";
|
2021-09-10 10:44:17 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, ""); ;
|
2021-09-08 18:12:08 +08:00
|
|
|
|
var content = ReadTemplate("VueTemplate.txt");
|
|
|
|
|
|
content = content
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{fileClassName}", FirstLowerCase(replaceDto.ModelTypeName))
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{VueViewListContent}", replaceDto.VueViewListHtml)//查询 table列
|
|
|
|
|
|
.Replace("{VueViewFormContent}", replaceDto.VueViewFormHtml)//添加、修改表单
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{Permission}", replaceDto.ModelTypeName.ToLower())
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{VueViewEditFormContent}", replaceDto.VueViewEditFormHtml)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{vueJsMethod}", replaceDto.VueJsMethod)
|
2021-09-21 20:31:35 +08:00
|
|
|
|
.Replace("{vueQueryFormHtml}", replaceDto.VueQueryFormHtml)
|
|
|
|
|
|
.Replace("{VueDataContent}", replaceDto.VueDataContent)
|
2021-09-13 18:38:54 +08:00
|
|
|
|
//.Replace("{VueViewSaveBindContent}", vueViewSaveBindContent)
|
2021-09-19 20:50:49 +08:00
|
|
|
|
.Replace("{primaryKey}", FirstLowerCase(replaceDto.PKName))
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{VueViewEditFormRuleContent}", replaceDto.VueViewEditFormRuleContent);//添加、修改表单验证规则
|
2021-09-08 18:12:08 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
|
|
|
|
|
|
2021-09-10 10:44:17 +08:00
|
|
|
|
//api js
|
|
|
|
|
|
servicesPath = parentPath + "\\api\\";
|
|
|
|
|
|
Directory.CreateDirectory(servicesPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
fullPath = servicesPath + "\\" + FirstLowerCase(replaceDto.ModelTypeName) + ".js";
|
2021-09-10 10:44:17 +08:00
|
|
|
|
Console.WriteLine(fullPath);
|
2021-09-16 17:52:31 +08:00
|
|
|
|
if (File.Exists(fullPath) && !generateDto.coverd)
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, "");
|
2021-09-08 18:12:08 +08:00
|
|
|
|
content = ReadTemplate("VueJsTemplate.txt");
|
|
|
|
|
|
content = content
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{ModelTypeDesc}", replaceDto.TableDesc);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
//.Replace("{fileClassName}", fileClassName)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
WriteAndSave(fullPath, content);
|
2021-09-12 19:57:50 +08:00
|
|
|
|
return Tuple.Create(fullPath, content);
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2021-09-07 18:37:21 +08:00
|
|
|
|
#region 帮助方法
|
|
|
|
|
|
|
2021-09-09 18:18:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 如果有前缀替换将前缀替换成空,替换下划线"_"为空再将首字母大写
|
2021-09-18 16:10:34 +08:00
|
|
|
|
/// 表名转换成C#类名
|
2021-09-09 18:18:37 +08:00
|
|
|
|
/// </summary>
|
2021-09-18 16:10:34 +08:00
|
|
|
|
/// <param name="tableName"></param>
|
2021-09-09 18:18:37 +08:00
|
|
|
|
/// <returns></returns>
|
2021-09-18 16:10:34 +08:00
|
|
|
|
public static string GetClassName(string tableName)
|
2021-09-08 07:48:18 +08:00
|
|
|
|
{
|
2021-09-19 20:50:49 +08:00
|
|
|
|
bool autoRemovePre = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_autoPre, false);
|
|
|
|
|
|
string tablePrefix = ConfigUtils.Instance.GetAppConfig<string>(GenConstants.Gen_tablePrefix);
|
2021-09-18 16:10:34 +08:00
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre)
|
2021-09-08 07:48:18 +08:00
|
|
|
|
{
|
2021-09-19 20:50:49 +08:00
|
|
|
|
string[] searcList = tablePrefix.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
2021-09-18 16:10:34 +08:00
|
|
|
|
for (int i = 0; i < searcList.Length; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrEmpty(searcList[i].ToString()))
|
|
|
|
|
|
{
|
2021-09-19 20:50:49 +08:00
|
|
|
|
tableName = tableName.Replace(searcList[i], "");
|
2021-09-18 16:10:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-08 07:48:18 +08:00
|
|
|
|
}
|
2021-09-18 16:10:34 +08:00
|
|
|
|
return tableName.Substring(0, 1).ToUpper() + tableName[1..].Replace("_", "");
|
2021-09-08 07:48:18 +08:00
|
|
|
|
}
|
2021-09-18 16:10:34 +08:00
|
|
|
|
|
2021-09-09 18:18:37 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 首字母转小写,输出前端
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="str"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-09-13 18:38:54 +08:00
|
|
|
|
public static string FirstLowerCase(string str)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..];
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
2021-09-09 18:18:37 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取前端标签名
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="columnDescription"></param>
|
|
|
|
|
|
/// <param name="columnName"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-09-13 18:38:54 +08:00
|
|
|
|
public static string GetLabelName(string columnDescription, string columnName)
|
2021-09-08 18:12:08 +08:00
|
|
|
|
{
|
|
|
|
|
|
return string.IsNullOrEmpty(columnDescription) ? columnName : columnDescription;
|
|
|
|
|
|
}
|
2021-09-07 18:37:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从代码模板中读取内容
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="templateName">模板名称,应包括文件扩展名称。比如:template.txt</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static string ReadTemplate(string templateName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
|
string fullName = $"{path}\\Template\\{templateName}";
|
|
|
|
|
|
string temp = fullName;
|
|
|
|
|
|
string str = "";
|
|
|
|
|
|
if (!File.Exists(temp))
|
|
|
|
|
|
{
|
|
|
|
|
|
return str;
|
|
|
|
|
|
}
|
|
|
|
|
|
StreamReader sr = null;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
sr = new StreamReader(temp);
|
|
|
|
|
|
str = sr.ReadToEnd(); // 读取文件
|
|
|
|
|
|
}
|
|
|
|
|
|
catch { }
|
|
|
|
|
|
sr?.Close();
|
|
|
|
|
|
sr?.Dispose();
|
|
|
|
|
|
return str;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 写文件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fileName"></param>
|
|
|
|
|
|
/// <param name="content"></param>
|
|
|
|
|
|
private static void WriteAndSave(string fileName, string content)
|
|
|
|
|
|
{
|
2021-09-08 18:12:08 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
//实例化一个文件流--->与写入文件相关联
|
|
|
|
|
|
using var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
|
|
|
|
|
|
//实例化一个StreamWriter-->与fs相关联
|
|
|
|
|
|
using var sw = new StreamWriter(fs);
|
|
|
|
|
|
//开始写入
|
|
|
|
|
|
sw.Write(content);
|
|
|
|
|
|
//清空缓冲区
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
//关闭流
|
|
|
|
|
|
sw.Close();
|
|
|
|
|
|
fs.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("写入文件出错了:" + ex.Message);
|
|
|
|
|
|
}
|
2021-09-07 18:37:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|