<#@ 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.FM.WebApi.Controllers.Api { <# List dlls=new List(); List filePaths=new List(); List tables = new List(); List modules=new List (); dlls.Add(Host.ResolveAssemblyReference("$(SolutionDir)"+"APT.MS.Domain\\")); dlls.Add(Host.ResolveAssemblyReference("$(SolutionDir)"+"APT.BaseData.Domain\\")); var moduleFix="FM"; 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#> /// /// <#= tableDesc#> /// [Route("api/<#= t4.ModuleName#>/<#= t4.FixName#>")] public partial class <#= t4.FixName#>Controller : <#=t4.BaseApiName#><<#= t4.Name#>> { /// /// 查询所有数据 /// /// 过滤实体 /// [HttpPost, Route("Entities")] public JsonActionResult>> Entities([FromBody]KeywordFilter filter) { return WitEntities(null, filter); } /// /// 排序查询所有数据 /// /// 过滤实体 /// [HttpPost, Route("OrderEntities")] public JsonActionResult>> OrderEntities([FromBody]KeywordFilter filter) { return WitOrderEntities(null, filter); } /// /// 分页查询数据 /// /// 分页过滤实体 /// [HttpPost, Route("Paged")] public PagedActionResult<<#= t4.Name#>> Paged([FromBody]KeywordPageFilter pageFilter) { return WitPaged(null, pageFilter); } /// /// 排序分页查询数据 /// /// 分页过滤实体 /// [HttpPost, Route("OrderPaged")] public PagedActionResult<<#= t4.Name#>> OrderPaged([FromBody]KeywordPageFilter pageFilter) { return WitOrderPaged(null, pageFilter); } /// /// 根据主键删除数据 /// /// 主键ID /// [HttpGet, Route("Delete")] public JsonActionResult Delete(string id) { return WitRealDelete(id); } /// /// 更新或新增数据 /// /// 对象实体 /// [HttpPost, Route("Update")] public JsonActionResult Update([FromBody]<#= t4.Name#> entity) { return WitUpdate(entity); } /// /// 批量更新 /// /// 对象实体 /// [HttpPost, Route("BatchUpdate")] public JsonActionResult BatchUpdate([FromBody] BatchUpdateModel<<#= t4.Name#>> entity) { return WitBantchUpdate(entity?.Data); } /// /// 批量删除数据 /// /// id字符串(id用逗号分隔) /// [HttpGet, Route("BatchDelete")] public JsonActionResult BatchDelete(string ids) { return WitRealBatchDelete(ids); } /// /// 获得单条实体数据 /// /// 过滤实体 /// [HttpPost, Route("Get")] public JsonActionResult<<#= t4.Name#>> Get([FromBody] KeywordFilter filter) { return WitEntity(null, filter); } <# if(t4.BaseApiName=="AuthorizeTreeApiController"){ #> /// /// 获得树形实体数据 /// /// /// [HttpPost, Route("TreeData")] public JsonActionResult>>> 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 Fields = new List(); public List MapDatas = new List(); public List Attrs = new List(); public TableT4() { MapDatas = new List(); Fields = new List(); Attrs = new List(); } } class FieldT4 { public FieldT4() { Attrs = new List(); } public string Name { get; set; } public List Attrs = new List(); } 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 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); } } #>