// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
namespace Admin.NET.Core.CodeGen;
///
/// 自定义模板引擎
///
public class CustomTemplateEngine : ViewEngineModel
{
///
/// 场景
///
public int Scene { get; set; }
///
/// 作者
///
public string AuthorName { get; set; }
///
/// 邮箱
///
public string Email { get; set; }
///
/// 模块名称
///
public string ModuleName { get; set; }
///
/// 命名空间
///
public string NameSpace { get; set; }
///
/// 项目最后名称
///
public string ProjectLastName { get; set; }
///
/// 前端页面目录名
///
public string PagePath { get; set; } = "main";
///
/// 打印模板类型
///
public string PrintType { get; set; }
///
/// 自定义打印模板名
///
public string PrintName { get; set; }
///
/// 是否使用Swagger接口
///
public bool IsApiService { get; set; }
///
/// 库定位器
///
public string ConfigId { get; set; }
///
/// 主表类名
///
public string ClassName { get; set; }
///
/// 首字母小写主表类名
///
public string LowerClassName { get; set; }
///
/// 业务名
///
public string BusName { get; set; }
///
/// 是否有状态字段
///
public bool HasStatus { get; set; }
///
/// 是否有上传字段
///
public bool HasUpload { get; set; }
///
/// 是否有关联表
///
public bool HasJoinTable { get; set; }
///
/// 是否只有Id单主键
///
public bool IsOnlyIdPrimary { get; set; }
///
/// 上级联表字段
///
public string? LastLinkPropertyName { get; set; }
///
/// 下级联表字段
///
public string? NextLinkPropertyName { get; set; }
///
/// 树组件配置
///
public TreeWithTableConfigInput TreeConfig { get; set; }
///
/// 是否是横向布局
///
public bool IsHorizontal { get; set; }
///
/// 对照目标表配置
///
public TableRelationshipConfigInput RelationshipTable { get; set; }
///
/// 从表配置
///
public CustomTemplateEngine SlaveTable { get; set; }
///
/// 所有字段集
///
public List AllFields { get; set; }
///
/// 查询字段集
///
public List QueryFields { get; set; }
///
/// 表格字段集
///
public List TableFields { get; set; }
///
/// 导入导出字段集
///
public List ImportFields { get; set; }
///
/// 主键字段集
///
public List PrimaryFields { get; set; }
///
/// 增改字段集
///
public List AddUpdateFields { get; set; }
///
/// 上传字段集
///
public List UploadFields { get; set; }
///
/// 下拉框字段集
///
public List DropdownFields { get; set; }
///
/// 接口字段集
///
public List ApiFields { get; set; }
///
/// 数据库字段集
///
public List ColumnList { get; set; }
///
/// 获取首字母小写文本
///
///
///
public string GetFirstLower(string text) => text?.ToFirstLetterLowerCase();
///
/// 根据.NET类型获取TypeScript类型
///
///
///
public string GetTypeScriptType(string type) => CodeGenHelper.GetTypeScriptType(type);
///
/// 格式化主键查询条件
/// 例: PrimaryKeysFormat(" || ", "u.{0} == input.{0}")
/// 单主键返回 u.Id == input.Id
/// 组合主键返回 u.Id == input.Id || u.FkId == input.FkId
///
/// 分隔符
/// 模板字符串
/// 字段首字母小写
///
public string PrimaryKeysFormat(string separator, string format, bool lowerFirstLetter = false) => string.Join(separator, PrimaryFields.Select(u => string.Format(format, lowerFirstLetter ? u.LowerPropertyName : u.PropertyName)));
///
/// 根据配置获取前端HttpApi方法名
///
///
///
///
///
public string GetHttpApiMethodName(CodeGenColumnConfig column, string type, CustomTemplateEngine engine = null)
{
engine ??= this;
var entityName = SlaveTable != null && SlaveTable.ApiFields.Any(u => u == column) ? SlaveTable.ClassName : engine.ClassName;
var suffixName = type switch
{
"page" => IsApiService ? "PagePost" : type, // 分页查询
"update" => IsApiService ? "UpdatePost" : type, // 更新记录
"add" => IsApiService ? "AddPost" : type, // 新增记录
"detail" => IsApiService ? "DetailGet" : type, // 详情
"delete" => IsApiService ? "DeletePost" : type, // 删除
"batchDelete" => IsApiService ? "BatchDeletePost" : type, // 批量删除
"setStatus" => IsApiService ? "SetStatusPost" : type, // 设置状态
"exportData" => IsApiService ? "ExportPost" : type, // 数据导出
"importData" => IsApiService ? "ImportPostForm" : type, // 数据导入
"downloadTemplate" => IsApiService ? "ImportGet" : type, // 下载模板
"fkTable" => IsApiService ? $"{column.JoinConfig.EntityName}PagePost" : "page", // 外键
"fkTree" => IsApiService ? $"{column.JoinConfig.EntityName}TreePost" : $"get{column.JoinConfig.EntityName}Tree", // 树
"upload" => IsApiService ? $"Upload{column?.PropertyName}PostForm" : $"upload{column?.PropertyName}", // 上传
_ => throw new Exception("未知接口类型")
};
return IsApiService ? $"getAPI({entityName}Api).api{entityName}{suffixName}" : $"{GetFirstLower(entityName)}Api.{suffixName}";
}
}