2021-09-07 21:52:44 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
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-07 18:37:21 +08:00
|
|
|
|
using ZR.CodeGenerator.Service;
|
|
|
|
|
|
|
|
|
|
|
|
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-13 18:38:54 +08:00
|
|
|
|
public static readonly string[] inputDtoNoField = new string[] { "DeleteMark", "CreateTime", "updateTime", "addtime" };
|
|
|
|
|
|
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>
|
|
|
|
|
|
public static void Generate(DbTableInfo 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-16 17:52:31 +08:00
|
|
|
|
CodeGeneraterService codeGeneraterService = new();
|
2021-09-09 18:18:37 +08:00
|
|
|
|
List<DbColumnInfo> listField = codeGeneraterService.GetColumnInfo(dto.dbName, dbTableInfo.Name);
|
|
|
|
|
|
GenerateSingle(listField, dbTableInfo, dto);
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
|
|
|
|
|
//GenerateDtoProfile(_option.ModelsNamespace, profileContent, ifExsitedCovered);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 单表生成代码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="listField">表字段集合</param>
|
|
|
|
|
|
/// <param name="tableInfo">表信息</param>
|
2021-09-16 17:52:31 +08:00
|
|
|
|
/// <param name="dto"></param>
|
2021-09-09 18:18:37 +08:00
|
|
|
|
public static void GenerateSingle(List<DbColumnInfo> listField, DbTableInfo tableInfo, GenerateDto dto)
|
2021-09-07 18:37:21 +08:00
|
|
|
|
{
|
2021-09-10 22:15:01 +08:00
|
|
|
|
var modelTypeName = GetModelClassName(tableInfo.Name);//表名对应C# 实体类名
|
2021-09-08 18:12:08 +08:00
|
|
|
|
var primaryKey = "id";//主键
|
2021-09-07 21:52:44 +08:00
|
|
|
|
|
2021-09-08 22:02:05 +08:00
|
|
|
|
string keyTypeName = "int";//主键数据类型
|
2021-09-09 18:18:37 +08:00
|
|
|
|
string modelContent = "";//数据库模型字段
|
|
|
|
|
|
string InputDtoContent = "";//输入模型
|
2021-09-13 18:38:54 +08:00
|
|
|
|
//string outputDtoContent = "";//输出模型
|
2021-09-10 22:15:01 +08:00
|
|
|
|
string updateColumn = "";//修改数据映射字段
|
2021-09-07 18:37:21 +08:00
|
|
|
|
string vueViewListContent = string.Empty;//Vue列表输出内容
|
2021-09-09 18:18:37 +08:00
|
|
|
|
string vueViewFormContent = string.Empty;//Vue表单输出内容
|
2021-09-07 18:37:21 +08:00
|
|
|
|
string vueViewEditFromContent = string.Empty;//Vue变量输出内容
|
|
|
|
|
|
string vueViewEditFromBindContent = string.Empty;//Vue显示初始化输出内容
|
|
|
|
|
|
string vueViewSaveBindContent = string.Empty;//Vue保存时输出内容
|
|
|
|
|
|
string vueViewEditFromRuleContent = string.Empty;//Vue数据校验
|
2021-09-13 18:38:54 +08:00
|
|
|
|
string vueJsMethod = string.Empty;//Vue js自定义方法
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
2021-09-07 21:52:44 +08:00
|
|
|
|
foreach (DbColumnInfo dbFieldInfo in listField)
|
2021-09-07 18:37:21 +08:00
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
string columnName = FirstLowerCase(dbFieldInfo.DbColumnName);
|
2021-09-07 18:37:21 +08:00
|
|
|
|
|
2021-09-08 18:12:08 +08:00
|
|
|
|
if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint")
|
|
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
vueViewEditFromContent += $" {columnName}: 'true',\n";
|
2021-09-10 22:15:01 +08:00
|
|
|
|
//vueViewEditFromBindContent += $" this.form.{columnName} = res.data.{0}+''\n";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2021-09-09 18:18:37 +08:00
|
|
|
|
vueViewEditFromContent += $" {columnName}: undefined,\n";
|
2021-09-10 22:15:01 +08:00
|
|
|
|
//vueViewEditFromBindContent += $" {columnName}: row.{columnName},\n";
|
2021-09-08 18:12:08 +08:00
|
|
|
|
}
|
2021-09-08 07:48:18 +08:00
|
|
|
|
//vueViewSaveBindContent += string.Format(" '{0}':this.editFrom.{0},\n", columnName);
|
2021-09-10 22:15:01 +08:00
|
|
|
|
//主键
|
2021-09-09 18:18:37 +08:00
|
|
|
|
if (dbFieldInfo.IsIdentity || dbFieldInfo.IsPrimarykey)
|
2021-09-08 22:02:05 +08:00
|
|
|
|
{
|
2021-09-10 22:15:01 +08:00
|
|
|
|
primaryKey = columnName.Substring(0, 1).ToUpper() + columnName[1..];
|
2021-09-09 18:18:37 +08:00
|
|
|
|
keyTypeName = dbFieldInfo.DataType;
|
2021-09-08 22:02:05 +08:00
|
|
|
|
}
|
2021-09-10 22:15:01 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var tempColumnName = columnName.Substring(0, 1).ToUpper() + columnName[1..];
|
|
|
|
|
|
updateColumn += $" {tempColumnName} = parm.{tempColumnName},\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dbFieldInfo.DbColumnName = columnName;
|
2021-09-13 18:38:54 +08:00
|
|
|
|
modelContent += CodeGenerateTemplate.GetModelTemplate(dbFieldInfo);
|
|
|
|
|
|
vueViewFormContent += CodeGenerateTemplate.GetVueViewFormContent(dbFieldInfo);
|
|
|
|
|
|
vueJsMethod += CodeGenerateTemplate.GetVueJsMethod(dbFieldInfo);
|
|
|
|
|
|
vueViewListContent += CodeGenerateTemplate.GetTableColumn(dbFieldInfo);
|
|
|
|
|
|
vueViewEditFromRuleContent += CodeGenerateTemplate.GetFormRules(dbFieldInfo);
|
|
|
|
|
|
InputDtoContent += CodeGenerateTemplate.GetDtoContent(dbFieldInfo);
|
2021-09-09 18:18:37 +08:00
|
|
|
|
}
|
2021-09-16 17:52:31 +08:00
|
|
|
|
ReplaceDto replaceDto = new();
|
|
|
|
|
|
replaceDto.KeyTypeName = keyTypeName;
|
|
|
|
|
|
replaceDto.PrimaryKey = primaryKey;
|
|
|
|
|
|
replaceDto.ModelTypeName = modelTypeName;
|
|
|
|
|
|
replaceDto.ModelProperty = modelContent;
|
|
|
|
|
|
replaceDto.TableName = tableInfo.Name;
|
|
|
|
|
|
replaceDto.TableDesc = tableInfo.Description;
|
|
|
|
|
|
replaceDto.InputDtoProperty = InputDtoContent;
|
|
|
|
|
|
replaceDto.updateColumn = updateColumn;
|
|
|
|
|
|
replaceDto.VueJsMethod = vueJsMethod;
|
|
|
|
|
|
replaceDto.VueViewEditFormContent = vueViewEditFromContent;
|
|
|
|
|
|
replaceDto.VueViewFormContent = vueViewFormContent;
|
|
|
|
|
|
replaceDto.VueViewEditFormRuleContent = vueViewEditFromRuleContent;
|
|
|
|
|
|
replaceDto.VueViewListContent = vueViewListContent;
|
|
|
|
|
|
|
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>
|
|
|
|
|
|
/// <param name="modelsNamespace">命名空间</param>
|
|
|
|
|
|
/// <param name="modelTypeName">类名</param>
|
|
|
|
|
|
/// <param name="tableName">表名称</param>
|
|
|
|
|
|
/// <param name="modelTypeDesc">表描述</param>
|
|
|
|
|
|
/// <param name="modelContent">数据库表实体内容</param>
|
|
|
|
|
|
/// <param name="keyTypeName">主键数据类型</param>
|
|
|
|
|
|
/// <param name="ifExsitedCovered">如果目标文件存在,是否覆盖。默认为false</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)
|
|
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.KeyTypeName)
|
|
|
|
|
|
.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>
|
|
|
|
|
|
/// <param name="replaceDto"></param>
|
|
|
|
|
|
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("{KeyTypeName}", replaceDto.KeyTypeName)
|
|
|
|
|
|
.Replace("{PropertyName}", replaceDto.InputDtoProperty)
|
|
|
|
|
|
.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>
|
|
|
|
|
|
/// <param name="modelTypeName"></param>
|
|
|
|
|
|
/// <param name="modelTypeDesc"></param>
|
|
|
|
|
|
/// <param name="tableName">表名</param>
|
|
|
|
|
|
/// <param name="keyTypeName"></param>
|
|
|
|
|
|
/// <param name="ifExsitedCovered">如果目标文件存在,是否覆盖。默认为false</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)
|
|
|
|
|
|
.Replace("{TableName}", replaceDto.TableName)
|
|
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.KeyTypeName);
|
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-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-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.KeyTypeName);
|
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-16 17:52:31 +08:00
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.KeyTypeName);
|
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())
|
|
|
|
|
|
.Replace("{PrimaryKey}", replaceDto.PrimaryKey)
|
|
|
|
|
|
.Replace("{UpdateColumn}", replaceDto.updateColumn)
|
|
|
|
|
|
.Replace("{KeyTypeName}", replaceDto.KeyTypeName);
|
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))
|
|
|
|
|
|
.Replace("{VueViewListContent}", replaceDto.VueViewListContent)//查询 table列
|
|
|
|
|
|
.Replace("{VueViewFormContent}", replaceDto.VueViewFormContent)//添加、修改表单
|
|
|
|
|
|
.Replace("{ModelTypeName}", replaceDto.ModelTypeName)
|
|
|
|
|
|
.Replace("{Permission}", replaceDto.ModelTypeName.ToLower())
|
|
|
|
|
|
.Replace("{VueViewEditFormContent}", replaceDto.VueViewEditFormContent)
|
|
|
|
|
|
.Replace("{vueJsMethod}", replaceDto.VueJsMethod)
|
2021-09-13 18:38:54 +08:00
|
|
|
|
//.Replace("{VueViewEditFromBindContent}", vueViewEditFromBindContent)
|
|
|
|
|
|
//.Replace("{VueViewSaveBindContent}", vueViewSaveBindContent)
|
2021-09-16 17:52:31 +08:00
|
|
|
|
.Replace("{primaryKey}", FirstLowerCase(replaceDto.PrimaryKey))
|
|
|
|
|
|
.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>
|
|
|
|
|
|
/// 如果有前缀替换将前缀替换成空,替换下划线"_"为空再将首字母大写
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="modelTypeName"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2021-09-13 18:38:54 +08:00
|
|
|
|
public static string GetModelClassName(string modelTypeName)
|
2021-09-08 07:48:18 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrEmpty(_option.ReplaceTableNameStr))
|
|
|
|
|
|
{
|
|
|
|
|
|
modelTypeName = modelTypeName.Replace(_option.ReplaceTableNameStr.ToString(), "");
|
|
|
|
|
|
}
|
|
|
|
|
|
modelTypeName = modelTypeName.Replace("_", "");
|
2021-09-10 18:39:20 +08:00
|
|
|
|
modelTypeName = modelTypeName.Substring(0, 1).ToUpper() + modelTypeName[1..];
|
2021-09-08 07:48:18 +08:00
|
|
|
|
return modelTypeName;
|
|
|
|
|
|
}
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|