diff --git a/WaterCloud.CodeGenerator/Template/SingleTableTemplate.cs b/WaterCloud.CodeGenerator/Template/SingleTableTemplate.cs index 797dd7546eb63ae3c059f6c9056802e161d2e8ab..ed82a4f24b78acd93aa4999c8a410237f8beddc5 100644 --- a/WaterCloud.CodeGenerator/Template/SingleTableTemplate.cs +++ b/WaterCloud.CodeGenerator/Template/SingleTableTemplate.cs @@ -131,9 +131,28 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" /// " + remark); sb.AppendLine(" /// "); sb.AppendLine(" /// "); - if (idColumn== column) + if (idColumn == column) { - sb.AppendLine(" [ColumnAttribute(\""+ column + "\", IsPrimaryKey = true)]"); + + if (datatype == "int?") + { + sb.AppendLine(" [AutoIncrement]"); + sb.AppendLine(" [Column(\"" + column + "\", IsPrimaryKey = true)]"); + sb.AppendLine(" public int " + column + " { get; set; }"); + } + else if (datatype == "long?") + { + sb.AppendLine(" [Column(\"" + column + "\", IsPrimaryKey = true)]"); + sb.AppendLine(" public long " + column + " { get; set; }"); + } + else + { + sb.AppendLine(" [Column(\"" + column + "\", IsPrimaryKey = true)]"); + } + } + else + { + sb.AppendLine(" public " + datatype + " " + column + " { get; set; }"); } //switch (datatype) //{ @@ -145,7 +164,7 @@ namespace WaterCloud.CodeGenerator // sb.AppendLine(" [JsonConverter(typeof(DateTimeJsonConverter))]"); // break; //} - sb.AppendLine(" public " + datatype + " " + column + " { get; set; }"); + } sb.AppendLine(" }"); sb.AppendLine("}"); @@ -155,7 +174,7 @@ namespace WaterCloud.CodeGenerator #endregion #region BuildService - public string BuildService(BaseConfigModel baseConfigModel, DataTable dt, string idColumn = "F_Id") + public string BuildService(BaseConfigModel baseConfigModel, DataTable dt, string idColumn = "F_Id",string idType="int",string deleteMarkField="IsDelete",string createTimeField="AddTime") { var baseEntity = GetBaseEntity(baseConfigModel.FileConfig.EntityName, dt, idColumn); StringBuilder sb = new StringBuilder(); @@ -192,7 +211,7 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" //此处需修改"); sb.AppendLine(" cachedata = cachedata.Where(t => t.F_FullName.Contains(keyword) || t.F_EnCode.Contains(keyword)).ToList();"); sb.AppendLine(" }"); - sb.AppendLine(" return cachedata.Where(t => t.F_DeleteMark == false).OrderByDescending(t => t.F_CreatorTime).ToList();"); + sb.AppendLine($" return cachedata.Where(t => t.{deleteMarkField} == false).OrderByDescending(t => t.{createTimeField}).ToList();"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" public async Task> GetLookList(string keyword = \"\")"); @@ -212,10 +231,10 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" //此处需修改"); sb.AppendLine(" list = list.Where(u => u.F_FullName.Contains(keyword) || u.F_EnCode.Contains(keyword)).ToList();"); sb.AppendLine(" }"); - sb.AppendLine(" return GetFieldsFilterData(list.Where(t => t.F_DeleteMark == false).OrderByDescending(t => t.F_CreatorTime).ToList(),className.Substring(0, className.Length - 7));"); + sb.AppendLine($" return GetFieldsFilterData(list.Where(t => t.{deleteMarkField} == false).OrderByDescending(t => t.{createTimeField}).ToList(),className.Substring(0, className.Length - 7));"); sb.AppendLine(" }"); sb.AppendLine(); - sb.AppendLine(" public async Task> GetLookList(SoulPage<"+ baseConfigModel.FileConfig.EntityName + "> pagination,string keyword = \"\")"); + sb.AppendLine(" public async Task> GetLookList(SoulPage<"+ baseConfigModel.FileConfig.EntityName + "> pagination,string keyword = \"\","+idType+" id=\"\")"); sb.AppendLine(" {"); sb.AppendLine(" //获取数据权限"); sb.AppendLine(" var list = GetDataPrivilege(\"u\", className.Substring(0, className.Length - 7));"); @@ -224,7 +243,18 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" //此处需修改"); sb.AppendLine(" list = list.Where(u => u.F_FullName.Contains(keyword) || u.F_EnCode.Contains(keyword));"); sb.AppendLine(" }"); - sb.AppendLine(" list = list.Where(u => u.F_DeleteMark==false);"); + sb.AppendLine($" list = list.Where(u => u.{deleteMarkField}==false);"); + if (idType == "int" || idType == "long") + { + sb.AppendLine(" if(id==0)"); + } + else + { + sb.AppendLine(" if(string.IsNullOrEmpty(id))"); + } + sb.AppendLine(" {"); + sb.AppendLine(" list= list.Where(u=>u." + idColumn + "==id);"); + sb.AppendLine(" }"); sb.AppendLine(" return GetFieldsFilterData(await repository.OrderList(list, pagination),className.Substring(0, className.Length - 7));"); sb.AppendLine(" }"); sb.AppendLine(); @@ -235,7 +265,7 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); - sb.AppendLine(" public async Task<" + baseConfigModel.FileConfig.EntityName + "> GetLookForm(string keyValue)"); + sb.AppendLine(" public async Task<" + baseConfigModel.FileConfig.EntityName + "> GetLookForm(object keyValue)"); sb.AppendLine(" {"); sb.AppendLine(" var cachedata = await repository.CheckCache(cacheKey, keyValue);"); sb.AppendLine(" return GetFieldsFilterData(cachedata,className.Substring(0, className.Length - 7));"); @@ -247,10 +277,36 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" if (string.IsNullOrEmpty(keyValue))"); sb.AppendLine(" {"); sb.AppendLine(" //此处需修改"); - sb.AppendLine(" entity.F_DeleteMark = false;"); + sb.AppendLine($" entity.{deleteMarkField} = false;"); + sb.AppendLine($" entity.{createTimeField} = DateTime.Now;"); + foreach (DataRow dr in dt.Rows) + { + string column = dr["TableColumn"].ToString(); + if (column != idColumn) + { + if (!baseConfigModel.PageForm.FieldList.Keys.Contains(column)) + { + if (column != deleteMarkField && column != createTimeField) + { + sb.AppendLine($" entity.{column} = //添写初始化参数;"); + } + } + } + } if (string.IsNullOrEmpty(baseEntity)) { - sb.AppendLine(" entity."+ idColumn + "=Utils.GuId();"); + if (idType == "int") + { + sb.AppendLine(" entity." + idColumn + "=0;"); + } + else if (idType == "long") + { + sb.AppendLine(" entity." + idColumn + "=0;"); + } + else + { + sb.AppendLine(" entity." + idColumn + "=Utils.GuId();"); + } } else { @@ -264,7 +320,18 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" //此处需修改"); if (string.IsNullOrEmpty(baseEntity)) { - sb.AppendLine(" entity." + idColumn + "=keyValue;"); + if (idType == "int") + { + sb.AppendLine(" entity." + idColumn + "=Convert.ToInt32(keyValue);"); + } + else if (idType == "long") + { + sb.AppendLine(" entity." + idColumn + "=Convert.ToInt64(keyValue);"); + } + else + { + sb.AppendLine(" entity." + idColumn + "=keyValue;"); + } } else { @@ -279,7 +346,7 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" public async Task DeleteForm(string keyValue)"); sb.AppendLine(" {"); sb.AppendLine(" var ids = keyValue.Split(',');"); - sb.AppendLine(" await repository.Delete(t => ids.Contains(t." + idColumn + "));"); + sb.AppendLine(" await repository.Delete(t => ids.Contains(t." + idColumn + ".ToString()));"); sb.AppendLine(" foreach (var item in ids)"); sb.AppendLine(" {"); sb.AppendLine(" await CacheHelper.Remove(cacheKey + item);"); @@ -296,7 +363,7 @@ namespace WaterCloud.CodeGenerator #endregion #region BuildController - public string BuildController(BaseConfigModel baseConfigModel, string idColumn="F_Id") + public string BuildController(BaseConfigModel baseConfigModel, string idColumn="F_Id",string idType="string",string createTimeField="AddTime") { StringBuilder sb = new StringBuilder(); sb.AppendLine("using System;"); @@ -364,7 +431,7 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(" {"); sb.AppendLine(" if (string.IsNullOrEmpty(pagination.field))"); sb.AppendLine(" {"); - sb.AppendLine(" pagination.field = \"F_CreatorTime\";"); + sb.AppendLine($" pagination.field = \"{createTimeField}\";"); sb.AppendLine(" pagination.order = \"desc\";"); sb.AppendLine(" }"); sb.AppendLine(" var data = await _service.GetLookList(pagination,keyword);"); @@ -382,7 +449,7 @@ namespace WaterCloud.CodeGenerator sb.AppendLine(); sb.AppendLine(" [HttpGet]"); sb.AppendLine(" [HandlerAjaxOnly]"); - sb.AppendLine(" public async Task GetFormJson(string keyValue)"); + sb.AppendLine($" public async Task GetFormJson({idType} keyValue)"); sb.AppendLine(" {"); sb.AppendLine(" var data = await _service.GetLookForm(keyValue);"); sb.AppendLine(" return Content(data.ToJson());"); @@ -1254,7 +1321,7 @@ namespace WaterCloud.CodeGenerator } #endregion - private string GetBaseEntity(string EntityName, DataTable dt,string idColumn="F_Id") + private string GetBaseEntity(string EntityName, DataTable dt,string idColumn="F_Id",string deleteMarkField="IsDelete",string AddTime="AddTime") { string entity = string.Empty; var columnList = dt.AsEnumerable().Select(p => p["TableColumn"].ParseToString()).ToList(); diff --git a/WaterCloud.Data/Repository/IRepositoryBase.T.cs b/WaterCloud.Data/Repository/IRepositoryBase.T.cs index f7e7437e73c9bb68d54844de1fbe4077cca737dd..03f32d81feafb60c78235b5f5f41d1d3b66de134 100644 --- a/WaterCloud.Data/Repository/IRepositoryBase.T.cs +++ b/WaterCloud.Data/Repository/IRepositoryBase.T.cs @@ -43,6 +43,6 @@ namespace WaterCloud.DataBase /// /// Task> CheckCacheList(string cacheKey, long old = 0); - Task CheckCache(string cacheKey, string keyValue, long old = 0); + Task CheckCache(string cacheKey, object keyValue, long old = 0); } } diff --git a/WaterCloud.Data/Repository/IRepositoryBase.cs b/WaterCloud.Data/Repository/IRepositoryBase.cs index 819b327a5f08b9d768c1c61d394c203ed9b35659..88a9887685d1f9bc09dc4a5ecd7cbfba6e72d414 100644 --- a/WaterCloud.Data/Repository/IRepositoryBase.cs +++ b/WaterCloud.Data/Repository/IRepositoryBase.cs @@ -141,7 +141,7 @@ namespace WaterCloud.DataBase /// /// /// - Task CheckCache(string cacheKey, string keyValue, long old = 0) where TEntity : class; + Task CheckCache(string cacheKey, object keyValue, long old = 0) where TEntity : class; /// /// 缓存查询列表(大数据表谨慎使用) /// diff --git a/WaterCloud.Data/Repository/RepositoryBase.T.cs b/WaterCloud.Data/Repository/RepositoryBase.T.cs index 9fd8a570fc50396c6f7f2a54632ec8404ede8c03..f89f46bd1f24abc4d8136e7aaecb06a7009471c2 100644 --- a/WaterCloud.Data/Repository/RepositoryBase.T.cs +++ b/WaterCloud.Data/Repository/RepositoryBase.T.cs @@ -156,7 +156,7 @@ namespace WaterCloud.DataBase } return cachedata; } - public async Task CheckCache(string cacheKey, string keyValue, long old = 0) + public async Task CheckCache(string cacheKey, object keyValue, long old = 0) { var cachedata = await CacheHelper.Get(cacheKey + keyValue); if (cachedata == null) diff --git a/WaterCloud.Data/Repository/RepositoryBase.cs b/WaterCloud.Data/Repository/RepositoryBase.cs index 32c6a01dd2b4e9c153e335850d28dc59a7d66fb8..05cf2b03ccfa92b36b00aba8ed1d80fb60f5d91b 100644 --- a/WaterCloud.Data/Repository/RepositoryBase.cs +++ b/WaterCloud.Data/Repository/RepositoryBase.cs @@ -247,7 +247,7 @@ namespace WaterCloud.DataBase return cachedata; } - public async Task CheckCache(string cacheKey, string keyValue, long old = 0) where TEntity : class + public async Task CheckCache(string cacheKey, object keyValue, long old = 0) where TEntity : class { var cachedata = await CacheHelper.Get(cacheKey + keyValue); if (cachedata == null) diff --git a/WaterCloud.Web/Areas/SystemManage/Controllers/CodeGeneratorController.cs b/WaterCloud.Web/Areas/SystemManage/Controllers/CodeGeneratorController.cs index 1b2e581e0f042db3bf3d6ae022d4cc3ea459bf0d..32726cca072602f3e27212358ad1e2b9b11d2e24 100644 --- a/WaterCloud.Web/Areas/SystemManage/Controllers/CodeGeneratorController.cs +++ b/WaterCloud.Web/Areas/SystemManage/Controllers/CodeGeneratorController.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Web; using System.Data; using Microsoft.AspNetCore.Mvc; @@ -137,20 +137,59 @@ namespace WaterCloud.Web.Areas.SystemManage.Controllers baseConfig.PageIndex.ButtonList=ExtList.removeNull(baseConfig.PageIndex.ButtonList); baseConfig.PageIndex.ColumnList.Remove(""); baseConfig.PageForm.FieldList.Remove(""); + string idType = "string"; + //扩展删除字段和创建时间字段 + string[] isDeleteMarkFieldNames = new[] { "F_DeleteMark", "IsDelete" }; + string[] createTimeFieldNames = new[] { "F_CreatorTime", "AddTime" }; + + string isDeleteMarkField = "F_DeleteMark"; + string createTimeField = "F_CreatorTime"; foreach (DataRow dr in dt.Rows) { + if (dr["TableIdentity"].ToString() == "Y") { idcolumn = dr["TableColumn"].ToString(); + string datatype = dr["Datatype"].ToString(); + datatype = TableMappingHelper.GetPropertyDatatype(datatype); + if (datatype == "int?") + { + idType = "int"; + } + else if (datatype == "long?") + { + idType = "long"; + } + else + { + idType = "string"; + } + } + string columnName = dr["TableColumn"].ToString(); + foreach(var isDeleteMarkFieldName in isDeleteMarkFieldNames) + { + if (string.Compare(isDeleteMarkFieldName, columnName, true) == 0) + { + isDeleteMarkField = columnName; + } + } + foreach (var createTimeFieldName in createTimeFieldNames) + { + if (string.Compare(createTimeFieldName, columnName, true) == 0) + { + createTimeField = columnName; + } } } + + string codeEntity = template.BuildEntity(baseConfig, dt, idcolumn); - string codeService = template.BuildService(baseConfig,dt, idcolumn); - string codeController = template.BuildController(baseConfig, idcolumn); + string codeService = template.BuildService(baseConfig,dt, idcolumn,idType,isDeleteMarkField,createTimeField); + string codeController = template.BuildController(baseConfig, idcolumn,idType, createTimeField); string codeIndex = template.BuildIndex(baseConfig, idcolumn); string codeForm = template.BuildForm(baseConfig); string codeDetails = template.BuildDetails(baseConfig); - string codeMenu = template.BuildMenu(baseConfig); + string codeMenu = template.BuildMenu(baseConfig,idcolumn); var json = new { CodeEntity = HttpUtility.HtmlEncode(codeEntity), diff --git a/WaterCloud.WebApi/Filter/AuthorizeFilterAttribute.cs b/WaterCloud.WebApi/Filter/AuthorizeFilterAttribute.cs index c4fe1a6743e117add6a24c99a8ace869d407ba19..4092124c6e7c73823d4d50984db829d0fa3547e9 100644 --- a/WaterCloud.WebApi/Filter/AuthorizeFilterAttribute.cs +++ b/WaterCloud.WebApi/Filter/AuthorizeFilterAttribute.cs @@ -1,15 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; +using System.Diagnostics; using System.Linq; -using System.Net.Http; -using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using System.Reflection; using Microsoft.AspNetCore.Mvc.Filters; -using Newtonsoft.Json; using WaterCloud.Code; +using Microsoft.AspNetCore.Authorization; namespace WaterCloud.WebApi { @@ -36,6 +32,13 @@ namespace WaterCloud.WebApi string token = context.HttpContext.Request.Headers[GlobalContext.SystemConfig.TokenName].ParseToString(); OperatorModel user = OperatorProvider.Provider.GetCurrent(); + var description = + (Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor; + + //添加有允许匿名的Action,可以不用登录访问,如Login/Index + //控制器整体忽略或者单独方法忽略 + var anonymous = description.ControllerTypeInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute)); + var methodanonymous = description.MethodInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute)); if (user != null) { // 根据传入的Token,添加token和客户参数 @@ -61,7 +64,7 @@ namespace WaterCloud.WebApi } } } - else + else if (anonymous == null && methodanonymous == null) { AjaxResult obj = new AjaxResult(); obj.message = "抱歉,没有操作权限";