shgx_tz_mom/ZR.CodeGenerator/CodeGeneratorTool.cs

490 lines
21 KiB
C#
Raw Normal View History

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;
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>
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
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>
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
//循环表字段信息
foreach (GenTableColumn dbFieldInfo in listField)
2021-09-07 18:37:21 +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-21 20:31:35 +08:00
replaceDto.VueViewEditFormHtml += $"{columnName}: undefined,\r\n";
}
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
}
//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
#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-09 18:18:37 +08:00
var parentPath = "..";
var repositoryPath = parentPath + "\\" + _option.RepositoriesNamespace + "\\Repositories\\";
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, "");
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)
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);
WriteAndSave(fullPath, content);
2021-09-12 19:57:50 +08:00
return Tuple.Create(fullPath, content);
}
#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-09 18:18:37 +08:00
var parentPath = "..";
var iServicesPath = parentPath + "\\" + _option.IServicsNamespace + "\\Business\\IBusService\\";
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, "");
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)
.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);
WriteAndSave(fullPath, content);
2021-09-12 19:57:50 +08:00
return Tuple.Create(fullPath, content);
}
/// <summary>
/// 生成Service文件
/// </summary>
2021-09-16 17:52:31 +08:00
private static Tuple<string, string> GenerateService(ReplaceDto replaceDto, GenerateDto generateDto)
{
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-16 17:52:31 +08:00
var fullPath = servicesPath + replaceDto.ModelTypeName + "Service.cs";
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, "");
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)
.Replace("{ServicesNamespace}", _option.ServicesNamespace)
2021-09-19 20:50:49 +08:00
.Replace("{ModelTypeName}", replaceDto.ModelTypeName);
WriteAndSave(fullPath, content);
2021-09-12 19:57:50 +08:00
return Tuple.Create(fullPath, content);
}
#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-09 18:18:37 +08:00
var parentPath = "..";
var servicesPath = parentPath + "\\" + _option.ApiControllerNamespace + "\\Controllers\\business\\";
if (!Directory.Exists(servicesPath))
{
Directory.CreateDirectory(servicesPath);
}
2021-09-16 17:52:31 +08:00
var fullPath = servicesPath + replaceDto.ModelTypeName + "Controller.cs";
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, "");
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);
WriteAndSave(fullPath, content);
2021-09-12 19:57:50 +08:00
return Tuple.Create(fullPath, content);
}
#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-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);
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, ""); ;
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);//添加、修改表单验证规则
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, "");
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)
WriteAndSave(fullPath, content);
2021-09-12 19:57:50 +08:00
return Tuple.Create(fullPath, content);
}
#endregion
2021-09-07 18:37:21 +08:00
#region
2021-09-09 18:18:37 +08:00
/// <summary>
/// 如果有前缀替换将前缀替换成空,替换下划线"_"为空再将首字母大写
/// 表名转换成C#类名
2021-09-09 18:18:37 +08:00
/// </summary>
/// <param name="tableName"></param>
2021-09-09 18:18:37 +08:00
/// <returns></returns>
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);
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);
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-08 07:48:18 +08:00
}
return tableName.Substring(0, 1).ToUpper() + tableName[1..].Replace("_", "");
2021-09-08 07:48:18 +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-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>
2021-09-13 18:38:54 +08:00
public 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
}
}