mh_lcmk_sms_service/APT.MicroApi/APT.HM.WebApi/Controllers/Api/T4/T4Control.tt
2024-07-12 16:37:09 +08:00

357 lines
12 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#@ 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;
using APT.Infrastructure.Api;
using APT.BaseData.Domain.ApiModel.PF;
namespace APT.HM.WebApi.Controllers.Api
{
<#
List<string> dlls=new List<string>();
List<string> filePaths=new List<string>();
List<TableT4> tables = new List<TableT4>();
List<string> modules=new List<string> ();
dlls.Add(Host.ResolveAssemblyReference("$(SolutionDir)"+"APT.MS.Domain\\"));
dlls.Add(Host.ResolveAssemblyReference("$(SolutionDir)"+"APT.BaseData.Domain\\"));
var moduleFix="HM";
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";
if(tableT4.BaseName.IndexOf("TreeEntityBase<")>-1){
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);
}
}
foreach(var space in modules)
{
WriteLine("using "+space +";");
}
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];
}
}
}
}
#>
#region <#= t4.FixName#>-<#= tableDesc#>
/// <summary>
/// <#= tableDesc#>
/// </summary>
[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);
}
/// <summary>
/// 排序查询所有数据
/// </summary>
/// <param name="filter">过滤实体</param>
/// <returns></returns>
[HttpPost, Route("OrderEntities")]
public JsonActionResult<IEnumerable<<#= t4.Name#>>> OrderEntities([FromBody]KeywordFilter filter)
{
return WitOrderEntities(null, filter);
}
/// <summary>
/// 分页查询数据
/// </summary>
/// <param name="pageFilter">分页过滤实体</param>
/// <returns></returns>
[HttpPost, Route("Paged")]
public PagedActionResult<<#= t4.Name#>> Paged([FromBody]KeywordPageFilter pageFilter)
{
return WitPaged(null, pageFilter);
}
/// <summary>
/// 排序分页查询数据
/// </summary>
/// <param name="pageFilter">分页过滤实体</param>
/// <returns></returns>
[HttpPost, Route("OrderPaged")]
public PagedActionResult<<#= t4.Name#>> OrderPaged([FromBody]KeywordPageFilter pageFilter)
{
return WitOrderPaged(null, pageFilter);
}
/// <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);
}
/// <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
<#}
#>
}
<#+
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);
}
}
#>