shgx_tz_mom/ZR.CodeGenerator/CodeGeneratorTool.cs

619 lines
30 KiB
C#
Raw Normal View History

2021-09-07 21:52:44 +08:00
using SqlSugar;
using System;
2021-09-09 18:18:37 +08:00
using System.Collections;
2021-09-07 18:37:21 +08:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ZR.CodeGenerator.CodeGenerator;
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-09 18:18:37 +08:00
private static string[] inputDtoNoField = new string[] { "DeleteMark", "CreateTime", "updateTime", "addtime" };
private static string[] imageFiled = new string[] { "icon", "img", "image", "url", "pic" };
private static string[] selectFiled = new string[] { "status", "type", "state" };
private static string[] radioFiled = new string[] { "status", "state", "is" };
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-09 18:18:37 +08:00
//_option.BaseNamespace = baseNamespace;
_option.DtosNamespace = "ZR.Model";
_option.ModelsNamespace = "ZR.Model";
_option.RepositoriesNamespace = "ZR.Repository";
_option.IServicsNamespace = "ZR.Service";
_option.ServicesNamespace = "ZR.Service";
_option.ApiControllerNamespace = "ZR.Admin.WebApi";
_option.ReplaceTableNameStr = dto.replaceTableNameStr;
2021-09-07 18:37:21 +08:00
//_option.TableList = listTable;
CodeGeneraterService codeGeneraterService = new CodeGeneraterService();
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>
/// <param name="ifExsitedCovered">如果目标文件存在是否覆盖。默认为false</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-09 18:18:37 +08:00
bool ifExsitedCovered = dto.coverd;
2021-09-10 10:44:17 +08:00
var modelTypeName = GetModelName(tableInfo.Name);
2021-09-07 18:37:21 +08:00
var modelTypeDesc = tableInfo.Description;//表描述
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 = "";//输入模型
string outputDtoContent = "";//输出模型
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-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
if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint")
{
2021-09-09 18:18:37 +08:00
vueViewEditFromContent += $" {columnName}: 'true',\n";
vueViewEditFromBindContent += $" this.form.{columnName} = res.data.{0}+''\n";
}
else
{
2021-09-09 18:18:37 +08:00
vueViewEditFromContent += $" {columnName}: undefined,\n";
2021-09-10 18:39:20 +08:00
vueViewEditFromBindContent += $" {columnName}: row.{columnName},\n";
}
2021-09-08 07:48:18 +08:00
//vueViewSaveBindContent += string.Format(" '{0}':this.editFrom.{0},\n", columnName);
2021-09-09 18:18:37 +08:00
if (dbFieldInfo.IsIdentity || dbFieldInfo.IsPrimarykey)
2021-09-08 22:02:05 +08:00
{
2021-09-09 18:18:37 +08:00
primaryKey = columnName;
keyTypeName = dbFieldInfo.DataType;
2021-09-08 22:02:05 +08:00
}
2021-09-09 18:18:37 +08:00
modelContent += GetModelTemplate(dbFieldInfo);
vueViewFormContent += GetVueViewFormContent(dbFieldInfo, columnName);
vueViewListContent += GetTableColumn(dbFieldInfo, columnName);
vueViewEditFromRuleContent += GetFormRules(dbFieldInfo, columnName);
InputDtoContent += GetDtoContent(dbFieldInfo, columnName);
}
if (dto.genFiles.Contains(1))
{
2021-09-10 18:39:20 +08:00
GenerateModels(_option.ModelsNamespace, modelTypeName, tableInfo.Name, modelContent, modelTypeDesc, keyTypeName, ifExsitedCovered);
2021-09-09 18:18:37 +08:00
}
if (dto.genFiles.Contains(2))
{
GenerateInputDto(_option.ModelsNamespace, modelTypeName, modelTypeDesc, InputDtoContent, keyTypeName, ifExsitedCovered);
}
if (dto.genFiles.Contains(3))
{
GenerateRepository(modelTypeName, modelTypeDesc, tableInfo.Name, keyTypeName, ifExsitedCovered);
}
if (dto.genFiles.Contains(4))
{
GenerateIService(_option.ModelsNamespace, modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered);
GenerateService(_option.ModelsNamespace, modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered);
}
if (dto.genFiles.Contains(5))
{
GenerateControllers(modelTypeName, primaryKey, modelTypeDesc, keyTypeName, ifExsitedCovered);
}
if (dto.genFiles.Contains(6))
{
GenerateVueViews(modelTypeName, primaryKey, modelTypeDesc, vueViewListContent, vueViewFormContent, vueViewEditFromContent, vueViewEditFromBindContent, vueViewSaveBindContent, vueViewEditFromRuleContent, ifExsitedCovered);
2021-09-08 07:48:18 +08:00
}
//GenerateIRepository(modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered);
2021-09-07 18:37:21 +08:00
//GenerateOutputDto(modelTypeName, modelTypeDesc, outputDtocontent, ifExsitedCovered);
}
2021-09-09 18:18:37 +08:00
#region Template
//rules
private static string GetFormRules(DbColumnInfo dbFieldInfo, string columnName)
{
string vueViewEditFromRuleContent = "";
//Rule 规则验证
if (!dbFieldInfo.IsNullable && !dbFieldInfo.IsIdentity)
{
vueViewEditFromRuleContent += $" {columnName}: [\n";
vueViewEditFromRuleContent += $" {{ required: true, message:\"{dbFieldInfo.ColumnDescription}\", trigger: \"blur\"}},\n";
//vueViewEditFromRuleContent += " { min: 2, max: 50, message: \"长度在 2 到 50 个字符\", trigger:\"blur\" }\n";
vueViewEditFromRuleContent += " ],\n";
}
return vueViewEditFromRuleContent;
}
//model 属性
private static string GetModelTemplate(DbColumnInfo dbFieldInfo)
2021-09-08 22:02:05 +08:00
{
2021-09-09 18:18:37 +08:00
string columnName = dbFieldInfo.DbColumnName.Substring(0, 1).ToUpper() + dbFieldInfo.DbColumnName.Substring(1);
2021-09-08 22:02:05 +08:00
var modelcontent = "";
modelcontent += " /// <summary>\n";
modelcontent += $" /// 描述 :{dbFieldInfo.ColumnDescription}\n";
modelcontent += $" /// 空值 :{dbFieldInfo.IsNullable}\n";
modelcontent += $" /// 默认 :{dbFieldInfo.DefaultValue}\n";
modelcontent += " /// </summary>\n";
if (dbFieldInfo.IsIdentity || dbFieldInfo.IsPrimarykey)
{
modelcontent += $" [SqlSugar.SugarColumn(IsPrimaryKey = {dbFieldInfo.IsPrimarykey.ToString().ToLower()}, IsIdentity = {dbFieldInfo.IsIdentity.ToString().ToLower()})]\n";
}
modelcontent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r";
return modelcontent;
}
2021-09-09 18:18:37 +08:00
//DTO model
private static string GetDtoContent(DbColumnInfo dbFieldInfo, string columnName)
{
string InputDtoContent = "";
if (!inputDtoNoField.Any(x => x.ToLower().Contains(columnName)) || !dbFieldInfo.IsIdentity)
{
InputDtoContent += " /// <summary>\n";
InputDtoContent += $" /// 设置或获取{dbFieldInfo.ColumnDescription}\n";
InputDtoContent += " /// </summary>\n";
InputDtoContent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r";
}
return InputDtoContent;
}
2021-09-08 22:02:05 +08:00
//form-item
2021-09-09 18:18:37 +08:00
private static string GetVueViewFormContent(DbColumnInfo dbFieldInfo, string columnName)
2021-09-08 22:02:05 +08:00
{
2021-09-09 18:18:37 +08:00
columnName = FirstLowerCase(columnName);
string labelName = GetLabelName(dbFieldInfo.ColumnDescription, columnName);
2021-09-08 22:02:05 +08:00
string vueViewFromContent = "";
if (dbFieldInfo.DataType == "datetime")
{
2021-09-09 18:18:37 +08:00
vueViewFromContent += "";
2021-09-08 22:02:05 +08:00
}
2021-09-09 18:18:37 +08:00
else if (((IList)imageFiled).Contains(columnName))
2021-09-08 22:02:05 +08:00
{
//TODO 图片
}
2021-09-09 18:18:37 +08:00
else if (radioFiled.Any(f => columnName.Contains(f)) && (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint"))
2021-09-08 22:02:05 +08:00
{
2021-09-09 18:18:37 +08:00
vueViewFromContent += $" <el-col :span=\"12\">\n";
vueViewFromContent += $" <el-form-item label=\"{labelName}\" :label-width=\"labelWidth\" prop=\"{columnName}\">";
vueViewFromContent += $" <el-radio-group v-model=\"form.{columnName}\">\n";
vueViewFromContent += " <el-radio v-for=\"dict in statusOptions\" :key=\"dict.dictValue\" :label=\"dict.dictValue\">{{dict.dictLabel}}</el-radio>\n";
2021-09-08 22:02:05 +08:00
vueViewFromContent += " </el-radio-group>\n";
vueViewFromContent += " </el-form-item>\n";
2021-09-09 18:18:37 +08:00
vueViewFromContent += " </el-col>\n";
2021-09-08 22:02:05 +08:00
}
else
{
2021-09-09 18:18:37 +08:00
vueViewFromContent += $" <el-col :span=\"12\">\n";
2021-09-08 22:02:05 +08:00
vueViewFromContent += $" <el-form-item label=\"{ GetLabelName(dbFieldInfo.ColumnDescription, columnName)}\" :label-width=\"labelWidth\" prop=\"{FirstLowerCase(columnName)}\">\n";
vueViewFromContent += $" <el-input v-model=\"form.{FirstLowerCase(columnName)}\" placeholder=\"请输入{GetLabelName(dbFieldInfo.ColumnDescription, columnName)}\" />\n";
vueViewFromContent += " </el-form-item>\n";
2021-09-09 18:18:37 +08:00
vueViewFromContent += " </el-col>\n";
2021-09-08 22:02:05 +08:00
}
return vueViewFromContent;
}
//table-column
private static string GetTableColumn(DbColumnInfo dbFieldInfo, string columnName)
{
2021-09-09 18:18:37 +08:00
columnName = FirstLowerCase(columnName);
string label = GetLabelName(dbFieldInfo.ColumnDescription, columnName);
2021-09-08 22:02:05 +08:00
string vueViewListContent = "";
2021-09-09 18:18:37 +08:00
string showToolTip = dbFieldInfo.DataType.Contains("varchar") ? ":show-overflow-tooltip=\"true\"" : "";
2021-09-08 22:02:05 +08:00
2021-09-09 18:18:37 +08:00
if (imageFiled.Any(f => columnName.ToLower().Contains(f)))
{
vueViewListContent += $" <el-table-column prop=\"{ columnName}\" label=\"图片\">\n";
vueViewListContent += " <template slot-scope=\"scope\">\n";
vueViewListContent += $" <el-image class=\"table-td-thumb\" :src=\"scope.row.{columnName}\" :preview-src-list=\"[scope.row.{columnName}]\"></el-image>\n";
vueViewListContent += " </template>\n";
vueViewListContent += " </el-table-column>\n";
}
else if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint")
2021-09-08 22:02:05 +08:00
{
2021-09-09 18:18:37 +08:00
vueViewListContent += $" <el-table-column prop=\"{columnName}\" label=\"{label}\" width=\"120\" >\n";
2021-09-08 22:02:05 +08:00
vueViewListContent += " <template slot-scope=\"scope\">\n";
2021-09-09 18:18:37 +08:00
vueViewListContent += $" <el-tag :type=\"scope.row.{columnName} === true ? 'success' : 'info'\" disable-transitions >";
vueViewListContent += $" {{scope.row.{columnName}===true?'启用':'禁用'}} </el-tag>\n";
2021-09-08 22:02:05 +08:00
vueViewListContent += " </template>\n";
vueViewListContent += " </el-table-column>\n";
}
else
{
//table-column
2021-09-09 18:18:37 +08:00
vueViewListContent += $" <el-table-column prop=\"{FirstLowerCase(columnName)}\" label=\"{GetLabelName(dbFieldInfo.ColumnDescription, columnName)}\" align=\"center\" width=\"100\" {showToolTip} />\n";
2021-09-08 22:02:05 +08:00
}
return vueViewListContent;
}
2021-09-09 18:18:37 +08:00
#endregion
2021-09-07 18:37:21 +08:00
#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>
private static void GenerateModels(string modelsNamespace, string modelTypeName, string tableName, string modelContent, string modelTypeDesc, string keyTypeName, bool ifExsitedCovered = false)
{
2021-09-08 07:48:18 +08:00
var parentPath = "..";
//../ZR.Model
2021-09-09 18:18:37 +08:00
var servicesPath = parentPath + "\\" + 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-09 18:18:37 +08:00
var fullPath = servicesPath + modelTypeName + ".cs";
Console.WriteLine(fullPath);
2021-09-07 18:37:21 +08:00
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("ModelTemplate.txt");
content = content
.Replace("{ModelsNamespace}", modelsNamespace)
.Replace("{ModelTypeName}", modelTypeName)
.Replace("{TableNameDesc}", modelTypeDesc)
.Replace("{KeyTypeName}", keyTypeName)
2021-09-08 07:48:18 +08:00
.Replace("{PropertyName}", modelContent)
2021-09-07 18:37:21 +08:00
.Replace("{TableName}", tableName);
WriteAndSave(fullPath, content);
}
2021-09-08 07:48:18 +08:00
/// <summary>
/// 生成InputDto文件
/// </summary>
/// <param name="modelsNamespace"></param>
/// <param name="modelTypeName"></param>
/// <param name="modelTypeDesc"></param>
/// <param name="modelContent"></param>
/// <param name="keyTypeName"></param>
/// <param name="ifExsitedCovered">如果目标文件存在是否覆盖。默认为false</param>
private static void GenerateInputDto(string modelsNamespace, string modelTypeName, string modelTypeDesc, string modelContent, string keyTypeName, bool ifExsitedCovered = false)
{
var parentPath = "..";
2021-09-09 18:18:37 +08:00
var servicesPath = parentPath + "\\" + modelsNamespace + "\\Dto\\";
2021-09-08 07:48:18 +08:00
if (!Directory.Exists(servicesPath))
{
Directory.CreateDirectory(servicesPath);
}
// ../ZR.Model/Dto/User.cs
2021-09-09 18:18:37 +08:00
var fullPath = servicesPath + modelTypeName + "Dto.cs";
Console.WriteLine(fullPath);
2021-09-08 07:48:18 +08:00
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("InputDtoTemplate.txt");
content = content
.Replace("{DtosNamespace}", _option.DtosNamespace)
.Replace("{ModelsNamespace}", modelsNamespace)
.Replace("{TableNameDesc}", modelTypeDesc)
.Replace("{KeyTypeName}", keyTypeName)
.Replace("{PropertyName}", modelContent)
.Replace("{ModelTypeName}", modelTypeName);
WriteAndSave(fullPath, content);
}
2021-09-07 18:37:21 +08:00
#endregion
#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>
private static void GenerateRepository(string modelTypeName, string modelTypeDesc, string tableName, string keyTypeName, bool ifExsitedCovered = false)
{
2021-09-09 18:18:37 +08:00
var parentPath = "..";
var repositoryPath = parentPath + "\\" + _option.RepositoriesNamespace + "\\Repositories\\";
if (!Directory.Exists(repositoryPath))
{
Directory.CreateDirectory(repositoryPath);
}
var fullPath = repositoryPath + "\\" + modelTypeName + "Repository.cs";
2021-09-09 18:18:37 +08:00
Console.WriteLine(fullPath);
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("RepositoryTemplate.txt");
content = content.Replace("{ModelsNamespace}", _option.ModelsNamespace)
2021-09-09 18:18:37 +08:00
//.Replace("{IRepositoriesNamespace}", _option.IRepositoriesNamespace)
.Replace("{RepositoriesNamespace}", _option.RepositoriesNamespace)
.Replace("{ModelTypeName}", modelTypeName)
.Replace("{TableNameDesc}", modelTypeDesc)
.Replace("{TableName}", tableName)
.Replace("{KeyTypeName}", keyTypeName);
WriteAndSave(fullPath, content);
}
#endregion
#region Service
/// <summary>
/// 生成IService文件
/// </summary>
/// <param name="modelsNamespace"></param>
/// <param name="modelTypeName"></param>
/// <param name="modelTypeDesc"></param>
/// <param name="keyTypeName"></param>
/// <param name="ifExsitedCovered">如果目标文件存在是否覆盖。默认为false</param>
private static void GenerateIService(string modelsNamespace, string modelTypeName, string modelTypeDesc, string keyTypeName, bool ifExsitedCovered = false)
{
2021-09-09 18:18:37 +08:00
var parentPath = "..";
var iServicesPath = parentPath + "\\" + _option.IServicsNamespace + "\\Business\\IBusService\\";
if (!Directory.Exists(iServicesPath))
{
Directory.CreateDirectory(iServicesPath);
}
2021-09-09 18:18:37 +08:00
var fullPath = $"{iServicesPath}\\I{modelTypeName}Service.cs";
Console.WriteLine(fullPath);
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("IServiceTemplate.txt");
content = content.Replace("{ModelsNamespace}", modelsNamespace)
.Replace("{DtosNamespace}", _option.DtosNamespace)
.Replace("{TableNameDesc}", modelTypeDesc)
.Replace("{IServicsNamespace}", _option.IServicsNamespace)
.Replace("{ModelTypeName}", modelTypeName)
.Replace("{KeyTypeName}", keyTypeName);
WriteAndSave(fullPath, content);
}
/// <summary>
/// 生成Service文件
/// </summary>
/// <param name="modelsNamespace"></param>
/// <param name="modelTypeName"></param>
/// <param name="modelTypeDesc"></param>
/// <param name="keyTypeName"></param>
/// <param name="ifExsitedCovered">如果目标文件存在是否覆盖。默认为false</param>
private static void GenerateService(string modelsNamespace, string modelTypeName, string modelTypeDesc, string keyTypeName, bool ifExsitedCovered = false)
{
2021-09-09 18:18:37 +08:00
var parentPath = "..";
var servicesPath = parentPath + "\\" + _option.ServicesNamespace + "\\Business\\";
if (!Directory.Exists(servicesPath))
{
Directory.CreateDirectory(servicesPath);
}
2021-09-09 18:18:37 +08:00
var fullPath = servicesPath + modelTypeName + "Service.cs";
Console.WriteLine(fullPath);
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("ServiceTemplate.txt");
content = content
.Replace("{IRepositoriesNamespace}", _option.IRepositoriesNamespace)
.Replace("{DtosNamespace}", _option.DtosNamespace)
.Replace("{IServicsNamespace}", _option.IServicsNamespace)
.Replace("{TableNameDesc}", modelTypeDesc)
.Replace("{ModelsNamespace}", modelsNamespace)
.Replace("{ServicesNamespace}", _option.ServicesNamespace)
.Replace("{ModelTypeName}", modelTypeName)
.Replace("{KeyTypeName}", keyTypeName);
WriteAndSave(fullPath, content);
}
#endregion
#region Controller
/// <summary>
/// 生成控制器ApiControllers文件
/// </summary>
/// <param name="modelTypeName">实体类型名称</param>
/// <param name="primaryKey">主键</param>
/// <param name="modelTypeDesc">实体描述</param>
/// <param name="keyTypeName"></param>
/// <param name="ifExsitedCovered">如果目标文件存在是否覆盖。默认为false</param>
private static void GenerateControllers(string modelTypeName, string primaryKey, string modelTypeDesc, string keyTypeName, bool ifExsitedCovered = false)
{
2021-09-09 18:18:37 +08:00
var parentPath = "..";
var servicesPath = parentPath + "\\" + _option.ApiControllerNamespace + "\\Controllers\\business\\";
if (!Directory.Exists(servicesPath))
{
Directory.CreateDirectory(servicesPath);
}
2021-09-09 18:18:37 +08:00
var fullPath = servicesPath + modelTypeName + "Controller.cs";
Console.WriteLine(fullPath);
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("ControllersTemplate.txt");
content = content
//.Replace("{DtosNamespace}", _option.DtosNamespace)
.Replace("<#=ControllerName#>", modelTypeName)
//.Replace("{ModelsNamespace}", _option.ModelsNamespace)
.Replace("<#=FileName#>", modelTypeDesc)
.Replace("<#=ServiceName#>", modelTypeName + "Service")
.Replace("<#=ModelName#>", modelTypeName)
2021-09-09 18:18:37 +08:00
.Replace("<#=Permission#>", modelTypeName.ToLower())
.Replace("{primaryKey}", primaryKey)
.Replace("{KeyTypeName}", keyTypeName);
WriteAndSave(fullPath, content);
}
#endregion
#region Vue页面
/// <summary>
/// 生成Vue页面
/// </summary>
/// <param name="modelTypeName">类名</param>
/// <param name="modelTypeDesc">表/类描述</param>
/// <param name="vueViewListContent"></param>
/// <param name="vueViewFromContent"></param>
/// <param name="vueViewEditFromContent"></param>
/// <param name="vueViewEditFromBindContent"></param>
/// <param name="vueViewSaveBindContent"></param>
/// <param name="vueViewEditFromRuleContent"></param>
/// <param name="ifExsitedCovered">如果目标文件存在是否覆盖。默认为false</param>
private static void GenerateVueViews(string modelTypeName, string primaryKey, string modelTypeDesc, string vueViewListContent, string vueViewFromContent, string vueViewEditFromContent, string vueViewEditFromBindContent, string vueViewSaveBindContent, string vueViewEditFromRuleContent, bool ifExsitedCovered = false)
{
2021-09-10 10:44:17 +08:00
var parentPath = "..\\CodeGenerate";//若要生成到项目中将路径改成 “..\\ZR.Vue\\src”
var servicesPath = parentPath + "\\views\\" + FirstLowerCase(modelTypeName);
if (!Directory.Exists(servicesPath))
{
Directory.CreateDirectory(servicesPath);
}
var fullPath = servicesPath + "\\" + "index.vue";
2021-09-10 10:44:17 +08:00
Console.WriteLine(fullPath);
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
var content = ReadTemplate("VueTemplate.txt");
content = content
2021-09-09 18:18:37 +08:00
.Replace("{fileClassName}", FirstLowerCase(modelTypeName))
.Replace("{VueViewListContent}", vueViewListContent)
.Replace("{VueViewFromContent}", vueViewFromContent)
2021-09-10 18:39:20 +08:00
.Replace("{ModelTypeName}", modelTypeName)
.Replace("{Permission}", modelTypeName.ToLower())
.Replace("{VueViewEditFromContent}", vueViewEditFromContent)
.Replace("{VueViewEditFromBindContent}", vueViewEditFromBindContent)
.Replace("{VueViewSaveBindContent}", vueViewSaveBindContent)
.Replace("{primaryKey}", primaryKey)
.Replace("{VueViewEditFromRuleContent}", vueViewEditFromRuleContent);
WriteAndSave(fullPath, content);
2021-09-10 10:44:17 +08:00
//api js
servicesPath = parentPath + "\\api\\";
Directory.CreateDirectory(servicesPath);
2021-09-09 18:18:37 +08:00
fullPath = servicesPath + "\\" + FirstLowerCase(modelTypeName) + ".js";
2021-09-10 10:44:17 +08:00
Console.WriteLine(fullPath);
if (File.Exists(fullPath) && !ifExsitedCovered)
return;
content = ReadTemplate("VueJsTemplate.txt");
content = content
2021-09-10 18:39:20 +08:00
.Replace("{ModelTypeName}", modelTypeName)
.Replace("{ModelName}", GetModelName(modelTypeName))
2021-09-09 18:18:37 +08:00
.Replace("{ModelTypeDesc}", modelTypeDesc);
//.Replace("{fileClassName}", fileClassName)
WriteAndSave(fullPath, content);
}
#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-08 07:48:18 +08:00
private static string GetModelName(string modelTypeName)
{
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>
private static string FirstLowerCase(string str)
{
2021-09-09 18:18:37 +08:00
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..];
}
2021-09-09 18:18:37 +08:00
/// <summary>
/// 获取前端标签名
/// </summary>
/// <param name="columnDescription"></param>
/// <param name="columnName"></param>
/// <returns></returns>
private static string GetLabelName(string columnDescription, string columnName)
{
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)
{
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
}
}