mh_jy_safe/APT.MicroApi/APT.OP.WebApi/Controllers/Api/T4/T4Control.tt

359 lines
12 KiB
Plaintext
Raw Permalink Normal View History

2025-08-25 09:56:57 +08:00
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.ComponentModel" #>
<#@ output extension=".cs" #>
//------------------------------------------------------------------------------
// T4模板文件基础接口类
// 此代码由T4模板自动生成
// 对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//------------------------------------------------------------------------------
using APT.Infrastructure.Core;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using APT.Utility;
2025-09-23 15:56:16 +08:00
using APT.Infrastructure.Api;
2025-08-25 09:56:57 +08:00
using APT.BaseData.Domain.ApiModel.PF;
2025-09-23 15:56:16 +08:00
namespace APT.OP.WebApi.Controllers.Api
{
2025-08-25 09:56:57 +08:00
<#
2025-09-23 15:56:16 +08:00
List<string> dlls=new List<string>();
List<string> filePaths=new List<string>();
List<TableT4> tables = new List<TableT4>();
List<string> modules=new List<string> ();
2025-08-25 09:56:57 +08:00
2025-09-23 15:56:16 +08:00
dlls.Add(Host.ResolveAssemblyReference("$(SolutionDir)"+"APT.MS.Domain\\"));
dlls.Add(Host.ResolveAssemblyReference("$(SolutionDir)"+"APT.BaseData.Domain\\"));
2025-08-25 09:56:57 +08:00
var moduleFix="OP";
foreach(var dll in dlls){
GetFileName(filePaths,dll);
}
foreach(var p in filePaths)
{
var classText = File.ReadAllText(p, Encoding.Default);
if (!string.IsNullOrEmpty(classText))
{
int classTextIndex = 0;
var space = GetElement(classText, "namespace", "{", ref classTextIndex);
if (!string.IsNullOrEmpty(space) && space.IndexOf("Domain.Entities", StringComparison.OrdinalIgnoreCase) > -1)
{
var classEleTxt = GetElement(classText, string.Empty, ":", ref classTextIndex);
classTextIndex--;
var baseClassTxt=GetElement(classText, ":", "{", ref classTextIndex);
if (!string.IsNullOrEmpty(classEleTxt))
{
TableT4 tableT4 = new TableT4();
tableT4.BaseName = baseClassTxt;
tableT4.BaseApiName="AuthorizeApiController";
2025-09-23 15:56:16 +08:00
if(tableT4.BaseName.IndexOf("TreeEntityBase<")>-1){
2025-08-25 09:56:57 +08:00
tableT4.BaseApiName="AuthorizeTreeApiController";
}
tableT4.TableNameSpace = space;
tables.Add(tableT4);
int classEleTxtIndex = 0;
var attrTxt= GetElement(classEleTxt, "[", "]", ref classEleTxtIndex);
while (!string.IsNullOrEmpty(attrTxt))
{
if(attrTxt=="IgnoreT4")
{
tableT4.IgnoreT4=true;
}
tableT4.Attrs.Add(attrTxt);
attrTxt = GetElement(classEleTxt, "[", "]", ref classEleTxtIndex);
}
if(tableT4.IgnoreT4)
continue;
var classEleTxt1 = GetElement(classEleTxt, "class", "", ref classEleTxtIndex);
if (!string.IsNullOrEmpty(classEleTxt1))
tableT4.Name = classEleTxt1;
}
}
}
}
foreach(var t4 in tables)
{
if(t4.IgnoreT4) continue;
var newName = t4.Name.ToLower();
var newNameAry = newName.Split(new char[] { '_' });
if (newNameAry.Any() && newNameAry.Length >= 2)
{
t4.ModuleName = newNameAry[1].ToUpper();
if(t4.ModuleName.IndexOf(moduleFix)<0)
continue;
for (var n = 2; n < newNameAry.Length; n++)
{
t4.FixName += System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(newNameAry[n]);
}
}
if(string.IsNullOrEmpty(t4.ModuleName)||string.IsNullOrEmpty(t4.FixName))continue;
if(!modules.Any(i=>i==t4.TableNameSpace)){
modules.Add(t4.TableNameSpace);
}
}
2025-09-23 15:56:16 +08:00
foreach(var space in modules)
{
WriteLine("using "+space +";");
}
2025-08-25 09:56:57 +08:00
foreach(var t4 in tables)
{
if(string.IsNullOrEmpty(t4.ModuleName)||string.IsNullOrEmpty(t4.FixName)||t4.IgnoreT4)continue;
var tableDesc = "";
if (t4.Attrs.Any())
{
foreach(var classAttr in t4.Attrs)
{
if (classAttr.IndexOf("Description", StringComparison.OrdinalIgnoreCase) > -1)
{
var p1 = GetAttrParams(classAttr);
if(p1!=null&&p1.Length>0){
tableDesc=p1[0];
}
}
}
}
2025-09-23 15:56:16 +08:00
#>
#region <#= t4.FixName#>-<#= tableDesc#>
/// <summary>
2025-08-25 09:56:57 +08:00
/// <#= tableDesc#>
/// </summary>
2025-09-23 15:56:16 +08:00
[Route("api/<#= t4.ModuleName#>/<#= t4.FixName#>")]
public partial class <#= t4.FixName#>Controller : <#=t4.BaseApiName#><<#= t4.Name#>>
{
/// <summary>
/// 查询所有数据
/// </summary>
/// <param name="filter">过滤实体</param>
/// <returns></returns>
[HttpPost, Route("Entities")]
public JsonActionResult<IEnumerable<<#= t4.Name#>>> Entities([FromBody]KeywordFilter filter)
{
return WitEntities(null, filter);
}
2025-08-25 09:56:57 +08:00
/// <summary>
2025-09-23 15:56:16 +08:00
/// 排序查询所有数据
/// </summary>
/// <param name="filter">过滤实体</param>
/// <returns></returns>
2025-08-25 09:56:57 +08:00
2025-09-23 15:56:16 +08:00
[HttpPost, Route("OrderEntities")]
public JsonActionResult<IEnumerable<<#= t4.Name#>>> OrderEntities([FromBody]KeywordFilter filter)
{
return WitOrderEntities(null, filter);
}
2025-08-25 09:56:57 +08:00
2025-09-23 15:56:16 +08:00
/// <summary>
/// 分页查询数据
/// </summary>
/// <param name="pageFilter">分页过滤实体</param>
/// <returns></returns>
[HttpPost, Route("Paged")]
public PagedActionResult<<#= t4.Name#>> Paged([FromBody]KeywordPageFilter pageFilter)
{
return WitPaged(null, pageFilter);
}
2025-08-25 09:56:57 +08:00
2025-09-23 15:56:16 +08:00
/// <summary>
/// 排序分页查询数据
/// </summary>
/// <param name="pageFilter">分页过滤实体</param>
/// <returns></returns>
[HttpPost, Route("OrderPaged")]
public PagedActionResult<<#= t4.Name#>> OrderPaged([FromBody]KeywordPageFilter pageFilter)
{
return WitOrderPaged(null, pageFilter);
}
2025-08-25 09:56:57 +08:00
2025-09-23 15:56:16 +08:00
/// <summary>
/// 根据主键删除数据
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
[HttpGet, Route("Delete")]
public JsonActionResult<bool> Delete(string id)
{
return WitRealDelete(id);
}
/// <summary>
/// 更新或新增数据
/// </summary>
/// <param name="entity">对象实体</param>
/// <returns></returns>
[HttpPost, Route("Update")]
public JsonActionResult<bool> Update([FromBody]<#= t4.Name#> entity)
{
return WitUpdate(entity);
}
/// <summary>
/// 批量更新
/// </summary>
/// <param name="entity">对象实体</param>
/// <returns></returns>
[HttpPost, Route("BatchUpdate")]
public JsonActionResult<bool> BatchUpdate([FromBody] BatchUpdateModel<<#= t4.Name#>> entity)
{
return WitBantchUpdate(entity?.Data);
}
2025-08-25 09:56:57 +08:00
2025-09-23 15:56:16 +08:00
/// <summary>
/// 批量删除数据
/// </summary>
/// <param name="ids">id字符串id用逗号分隔</param>
/// <returns></returns>
[HttpGet, Route("BatchDelete")]
public JsonActionResult<bool> BatchDelete(string ids)
{
return WitRealBatchDelete(ids);
}
/// <summary>
/// 获得单条实体数据
/// </summary>
/// <param name="filter">过滤实体</param>
/// <returns></returns>
[HttpPost, Route("Get")]
public JsonActionResult<<#= t4.Name#>> Get([FromBody] KeywordFilter filter)
{
return WitEntity(null, filter);
}
<#
if(t4.BaseApiName=="AuthorizeTreeApiController"){
#>
/// <summary>
/// 获得树形实体数据
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
[HttpPost, Route("TreeData")]
public JsonActionResult<IEnumerable<TreeNode<<#= t4.Name#>>>> TreeData([FromBody] KeywordFilter filter)
{
return WitTreeOrderEntities(null, filter);
}
<#
}
#>
}
#endregion
<#}
2025-08-25 09:56:57 +08:00
#>
}
<#+
class TableT4
{
public string TableNameSpace { get; set; }
public string ModuleName { get; set; }
public string FixName { get; set; }
public string Name { get; set; }
public string BaseName { get; set; }
public string BaseApiName{get;set;}
public bool IgnoreT4{get;set;}
public List<FieldT4> Fields = new List<FieldT4>();
public List<string> MapDatas = new List<string>();
public List<string> Attrs = new List<string>();
public TableT4()
{
MapDatas = new List<string>();
Fields = new List<FieldT4>();
Attrs = new List<string>();
}
}
class FieldT4
{
public FieldT4()
{
Attrs = new List<string>();
}
public string Name { get; set; }
public List<string> Attrs = new List<string>();
}
private static string GetElement(string txt,string startStr,string endStr, ref int index)
{
var tempStartIndex=string.IsNullOrEmpty(startStr)?index:
txt.IndexOf(startStr, index, StringComparison.OrdinalIgnoreCase);
var tempEndIndex = string.IsNullOrEmpty(endStr) ? txt.Length : txt.IndexOf(endStr, index, StringComparison.OrdinalIgnoreCase);
if (tempStartIndex != -1&&tempEndIndex!=-1)
{
var tempStartIndex1 = string.IsNullOrEmpty(startStr) ? -1 :
txt.IndexOf(startStr, tempStartIndex+1, StringComparison.OrdinalIgnoreCase);
if (tempStartIndex1 != -1&&tempStartIndex1< tempEndIndex)
tempEndIndex = string.IsNullOrEmpty(endStr) ? txt.Length :
txt.IndexOf(endStr, tempEndIndex+1, StringComparison.OrdinalIgnoreCase);
int startStrLength = (string.IsNullOrEmpty(startStr) ? 0 : startStr.Length);
int endStrLength = (string.IsNullOrEmpty(endStr) ? 0 : endStr.Length);
index = tempEndIndex + endStrLength;
string ret= txt.Substring(tempStartIndex+startStrLength, tempEndIndex-tempStartIndex- startStrLength);
return ret.Trim();
}
return string.Empty;
}
private static string GetAttrParam(string str)
{
int lefIndex = str.IndexOf("(");
int rigthIndex = str.LastIndexOf(")");
if(lefIndex!=-1&&rigthIndex!=-1)
{
var ret= str.Substring(lefIndex + 1, rigthIndex - 1- lefIndex);
return ret.Trim();
}
return string.Empty;
}
private static string[] GetAttrParams(string str)
{
var ret = GetAttrParam(str);
if (string.IsNullOrEmpty(ret)) return null;
return ret.Split(',').Select(t => t.Trim().Replace("\"", "")).ToArray();
}
private static bool ToBoolean(string str)
{
if (string.IsNullOrEmpty(str)) return false;
try
{
return Convert.ToBoolean(str);
}catch{
return false;
}
}
public static void GetFileName(List<string> filePaths, string path)
{
DirectoryInfo root = new DirectoryInfo(path);
foreach (FileInfo f in root.GetFiles())
{
if (f.Extension.ToLower()==".cs".ToLower())
{
filePaths.Add(f.FullName);
}
}
foreach (DirectoryInfo d in root.GetDirectories())
{
GetFileName(filePaths, d.FullName);
}
}
2025-09-23 15:56:16 +08:00
#>