using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Text; using System.Threading.Tasks; using System.Data; using System.Web; using Newtonsoft.Json.Linq; using YiSha.Data.Repository; using YiSha.CodeGenerator.Model; using YiSha.Util; using YiSha.Util.Extension; using YiSha.Util.Model; using YiSha.Entity.SystemManage; using YiSha.Enum.SystemManage; using YiSha.Business.Cache; using YiSha.Entity; using YiSha.Business.Cache.IBusiness; using YiSha.Enum; namespace YiSha.CodeGenerator.Template { public class SingleTableTemplate { #region GetBaseConfig public BaseConfigModel GetBaseConfig(string path, string userName, string tableName, string tableDescription, List tableFieldList) { path = GetProjectRootPath(path); int defaultField = 2; // 默认显示2个字段 BaseConfigModel baseConfigModel = new BaseConfigModel(); baseConfigModel.TableName = tableName; baseConfigModel.TableNameUpper = tableName; #region FileConfigModel baseConfigModel.FileConfig = new FileConfigModel(); baseConfigModel.FileConfig.ClassPrefix = TableMappingHelper.GetClassNamePrefix(tableName); baseConfigModel.FileConfig.ClassDescription = tableDescription; baseConfigModel.FileConfig.CreateName = userName; baseConfigModel.FileConfig.CreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); baseConfigModel.FileConfig.EntityName = string.Format("{0}Entity", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.EntityMapName = string.Format("{0}Map", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.EntityParamName = string.Format("{0}Param", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.BusinessName = string.Format("{0}BLL", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.IBusinessName = string.Format("I{0}BLL", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.ServiceName = string.Format("{0}Service", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.IServiceName = string.Format("I{0}Service", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.ControllerName = string.Format("{0}Controller", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.PageIndexName = string.Format("{0}Index", baseConfigModel.FileConfig.ClassPrefix); baseConfigModel.FileConfig.PageFormName = string.Format("{0}Form", baseConfigModel.FileConfig.ClassPrefix); #endregion #region OutputConfigModel baseConfigModel.OutputConfig = new OutputConfigModel(); baseConfigModel.OutputConfig.OutputModule = string.Empty; baseConfigModel.OutputConfig.OutputEntity = Path.Combine(path, "YiSha.Entity"); baseConfigModel.OutputConfig.OutputBusiness = Path.Combine(path, "YiSha.Business"); baseConfigModel.OutputConfig.OutputWeb = Path.Combine(path, "YiSha.Web", "YiSha.Admin.Web"); string areasModule = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas"); if (Directory.Exists(areasModule)) { baseConfigModel.OutputConfig.ModuleList = Directory.GetDirectories(areasModule).Select(p => Path.GetFileName(p)).Where(p => p != "DemoManage").ToList(); } else { baseConfigModel.OutputConfig.ModuleList = new List { "TestManage" }; } #endregion #region PageIndexModel baseConfigModel.PageIndex = new PageIndexModel(); baseConfigModel.PageIndex.IsSearch = 1; baseConfigModel.PageIndex.IsPagination = 1; baseConfigModel.PageIndex.ButtonList = new List(); baseConfigModel.PageIndex.ColumnList = new List(); baseConfigModel.PageIndex.ColumnList.AddRange(tableFieldList.Take(defaultField)); #endregion #region PageFormModel baseConfigModel.PageForm = new PageFormModel(); baseConfigModel.PageForm.ShowMode = 1; baseConfigModel.PageForm.FieldList = new List(); baseConfigModel.PageForm.FieldList.AddRange(tableFieldList.Take(defaultField)); #endregion return baseConfigModel; } #endregion #region BuildEntity public string BuildEntity(BaseConfigModel baseConfigModel, DataTable dt) { string baseEntity = GetBaseEntity(dt); StringBuilder sb = new StringBuilder(); sb.AppendLine("using System;"); sb.AppendLine("using Newtonsoft.Json;"); sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;"); sb.AppendLine("using YiSha.Util;"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("实体类", baseConfigModel, sb); sb.AppendLine(" [Table(\"" + baseConfigModel.TableName + "\")]"); sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.EntityName + " : " + baseEntity); sb.AppendLine(" {"); string column = string.Empty; string remark = string.Empty; string datatype = string.Empty; foreach (DataRow dr in dt.Rows) { column = dr["TableColumn"].ToString(); if (BaseField.BaseFieldList.Where(p => p.ToLower() == column.ToLower()).Any()) { // 基础字段不需要生成,继承合适的BaseEntity即可。 continue; } remark = dr["Remark"].ToString(); datatype = dr["Datatype"].ToString(); datatype = TableMappingHelper.GetPropertyDatatype(datatype); sb.AppendLine(" /// "); sb.AppendLine(" /// " + remark); sb.AppendLine(" /// "); sb.AppendLine(" /// "); switch (datatype) { case "long?": sb.AppendLine(" [JsonConverter(typeof(StringJsonConverter))]"); break; case "DateTime?": sb.AppendLine(" [JsonConverter(typeof(DateTimeJsonConverter))]"); break; } sb.AppendLine(" public " + datatype + " " + column + " { get; set; }"); } sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildEntityParam public string BuildEntityParam(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using Newtonsoft.Json;"); sb.AppendLine("using YiSha.Util;"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("实体查询类", baseConfigModel, sb); sb.AppendLine(" public class " + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam")); sb.AppendLine(" {"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildService public string BuildService(BaseConfigModel baseConfigModel, DataTable dt) { string baseEntity = GetBaseEntity(dt); StringBuilder sb = new StringBuilder(); string method = string.Empty; sb.AppendLine("using System;"); sb.AppendLine("using System.Linq;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using System.Linq.Expressions;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using YiSha.Util;"); sb.AppendLine("using YiSha.Util.Extension;"); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using YiSha.Data;"); sb.AppendLine("using YiSha.Data.Repository;"); sb.AppendLine("using YiSha.Enum;"); sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Service." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("服务实现类", baseConfigModel, sb); sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.ServiceName + " : I" + baseConfigModel.FileConfig.IServiceName + ""); sb.AppendLine(" {"); sb.AppendLine(" private DBConnectTypeEnum dbConnectType = " + GetDBConnectTypeEnum(baseConfigModel.DBConnectType) + ";"); sb.AppendLine(" #region 获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取列表数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)"); sb.AppendLine(" {"); sb.AppendLine(" var expression = ListFilterPartial(param);"); sb.AppendLine(" var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression);"); sb.AppendLine(" return list.ToList();"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取分页数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// 分页条件"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination)"); sb.AppendLine(" {"); sb.AppendLine(" var expression = ListFilterPartial(param);"); sb.AppendLine(" var list = await _baseRepository.BaseRepository(dbConnectType).FindList(expression, pagination);"); sb.AppendLine(" return list.ToList();"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取指定列数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 表字段列"); sb.AppendLine(" /// 条件"); sb.AppendLine(" /// 条件参数"); sb.AppendLine(" /// 排序列"); sb.AppendLine(" /// 排序类型"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task> GetListColumn(string column, string where, List parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\")"); sb.AppendLine(" {"); sb.AppendLine(" if (string.IsNullOrEmpty(column) || string.IsNullOrEmpty(where))"); sb.AppendLine(" return new List<" + baseConfigModel.FileConfig.EntityName + ">();"); sb.AppendLine(" if (!string.IsNullOrEmpty(groupBy))"); sb.AppendLine(" groupBy = $\" group by {groupBy} \";"); sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);"); if (baseEntity.Equals("BaseExtensionEntity")) { sb.AppendLine(" string selectSql = $\"select {column} from {tableName} where baseisdelete = @baseisdelete and {where} {groupBy} order by {sort} {sortType}\";"); sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@baseisdelete\", (int)IsDeleteEnum.No, dbConnectType));"); } else { sb.AppendLine(" string selectSql = $\"select {column} from {tableName} where 1 = 1 and {where} {groupBy} order by {sort} {sortType}\";"); } sb.AppendLine(" var result = await _baseRepository.BaseRepository(dbConnectType).FindList<" + baseConfigModel.FileConfig.EntityName + ">(selectSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));"); sb.AppendLine(" return result.ToList();"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键id"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task<" + baseConfigModel.FileConfig.EntityName + "> GetEntity(int id)"); sb.AppendLine(" {"); sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).FindEntity<" + baseConfigModel.FileConfig.EntityName + ">(id);"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 提交数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 保存数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 实体参数"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity)"); sb.AppendLine(" {"); sb.AppendLine(" if (entity.Id.IsNullOrZero())"); sb.AppendLine(" {"); sb.AppendLine(" " + GetSaveFormCreate(baseEntity)); sb.AppendLine(" await _baseRepository.BaseRepository(dbConnectType).Insert(entity);"); sb.AppendLine(" }"); sb.AppendLine(" else"); sb.AppendLine(" {"); sb.AppendLine(" " + GetSaveFormModify(baseEntity)); sb.AppendLine(" await _baseRepository.BaseRepository(dbConnectType).Update(entity);"); sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 保存多条数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 数据列表"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task InsertMany(List<" + baseConfigModel.FileConfig.EntityName + "> list)"); sb.AppendLine(" {"); sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).Insert(list);"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键Id"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task DeleteFormById(string ids, bool del = false)"); sb.AppendLine(" {"); sb.AppendLine(" if (string.IsNullOrWhiteSpace(ids) || !ids.Split(\", \").Any())"); sb.AppendLine(" {"); sb.AppendLine(" return await Task.FromResult(-1);"); sb.AppendLine(" }"); sb.AppendLine(" ids = $\"{ string.Join(\",\", ids.Split(\",\"))}\";"); sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);"); if (baseEntity.Equals("BaseExtensionEntity")) { sb.AppendLine(" string deleteSql = $\"update {tableName} set baseisdelete = @baseisdelete where FIND_IN_SET (id,@id)\";"); sb.AppendLine(" if (del)"); sb.AppendLine(" {"); sb.AppendLine(" deleteSql = $\"delete from {tableName} where FIND_IN_SET (id,@id)\";"); sb.AppendLine(" }"); sb.AppendLine(" List parameters = new List();"); sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@baseisdelete\", (int)IsDeleteEnum.Yes, dbConnectType));"); sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@id\", ids, dbConnectType));"); } else { var where = ""; DBConnectHelper.GetInstance.GetDBConnectInfo(baseConfigModel.DBConnectType, out string dbType, out string dbConnectionString); sb.AppendLine(" List parameters = new List();"); if (dbType.ToLower() == "mysql") { where = "where FIND_IN_SET (id,@id)"; sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@id\", ids, dbConnectType));"); } else where = "where id in ({ids})"; sb.AppendLine(" string deleteSql = $\"delete from {tableName} " + where + "\";"); } sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql(deleteSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task DeleteFormByWhere(string where, List parameters, bool del = false)"); sb.AppendLine(" {"); sb.AppendLine(" if (string.IsNullOrWhiteSpace(where))"); sb.AppendLine(" {"); sb.AppendLine(" return await Task.FromResult(-1);"); sb.AppendLine(" }"); sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);"); if (baseEntity.Equals("BaseExtensionEntity")) { sb.AppendLine(" string deleteSql = $\"update {tableName} set baseisdelete = @baseisdelete where {where}\";"); sb.AppendLine(" if (del)"); sb.AppendLine(" {"); sb.AppendLine(" deleteSql = $\"delete from {tableName} where {where}\";"); sb.AppendLine(" }"); sb.AppendLine(" parameters.Add(DbParameterExtension.CreateDbParameter(\"@baseisdelete\", (int)IsDeleteEnum.Yes, dbConnectType));"); } else { sb.AppendLine(" string deleteSql = $\"delete from {tableName} where {where}\";"); } sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql(deleteSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 修改多条数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 数据列表"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task UpdateMany(List<" + baseConfigModel.FileConfig.EntityName + "> list)"); sb.AppendLine(" {"); sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).Update(list);"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件修改数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 修改字段"); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task UpdateFormByWhere(string setField, string where, List parameters)"); sb.AppendLine(" {"); sb.AppendLine(" if (string.IsNullOrWhiteSpace(setField) || string.IsNullOrWhiteSpace(where))"); sb.AppendLine(" {"); sb.AppendLine(" return await Task.FromResult(-1);"); sb.AppendLine(" }"); sb.AppendLine(" string tableName = typeof(" + baseConfigModel.FileConfig.EntityName + ").GetAttributeValue((TableAttribute ta) => ta.Name);"); sb.AppendLine(" string updateSql = $\"update {tableName} set {setField} where {where}\";"); sb.AppendLine(" return await _baseRepository.BaseRepository(dbConnectType).ExecuteBySql(updateSql, DbParameterExtension.ToDbParameter(parameters.ToArray()));"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 私有方法"); sb.AppendLine(" ///// "); sb.AppendLine(" ///// 列表条件过滤"); sb.AppendLine(" ///// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)"); sb.AppendLine(" ///// "); sb.AppendLine(" ///// "); sb.AppendLine(" ///// "); sb.AppendLine(" //private Expression> ListFilter(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)"); sb.AppendLine(" //{"); sb.AppendLine(" // var expression = LinqExtensions.True<" + baseConfigModel.FileConfig.EntityName + ">();"); sb.AppendLine(" // expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);"); sb.AppendLine(" // if (param != null)"); sb.AppendLine(" // {"); sb.AppendLine(" // }"); sb.AppendLine(" // return expression;"); sb.AppendLine(" //}"); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildService_Partial public string BuildService_Partial(BaseConfigModel baseConfigModel, DataTable dt) { string baseEntity = GetBaseEntity(dt); StringBuilder sb = new StringBuilder(); string method = string.Empty; sb.AppendLine("using System;"); sb.AppendLine("using System.Linq;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using System.Linq.Expressions;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using YiSha.Util;"); sb.AppendLine("using YiSha.Util.Extension;"); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using YiSha.Data;"); sb.AppendLine("using YiSha.Data.Repository;"); sb.AppendLine("using YiSha.Enum;"); sb.AppendLine("using System.ComponentModel.DataAnnotations.Schema;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Service." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("服务实现类(部分类)", baseConfigModel, sb); sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.ServiceName + ""); sb.AppendLine(" {"); sb.AppendLine(" private IRepositoryFactory _baseRepository;"); sb.AppendLine(" public " + baseConfigModel.FileConfig.ServiceName + "(IRepositoryFactory baseRepository)"); sb.AppendLine(" {"); sb.AppendLine(" _baseRepository = baseRepository;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" #region 私有方法"); sb.AppendLine(" /// "); sb.AppendLine(" /// 列表条件过滤"); sb.AppendLine(" /// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)"); sb.AppendLine(" /// "); sb.AppendLine(" /// "); sb.AppendLine(" /// "); sb.AppendLine(" private Expression> ListFilterPartial(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)"); sb.AppendLine(" {"); sb.AppendLine(" var expression = LinqExtensions.True<" + baseConfigModel.FileConfig.EntityName + ">();"); //只有BaseExtensionEntity实体才会存在BaseIsDelete字段 if (baseEntity.Equals("BaseExtensionEntity")) sb.AppendLine(" expression = expression.And(t => t.BaseIsDelete == (int)IsDeleteEnum.No);"); sb.AppendLine(" if (param != null)"); sb.AppendLine(" {"); sb.AppendLine(" }"); sb.AppendLine(" return expression;"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildIService public string BuildIService(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); string method = string.Empty; sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.IService." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("服务接口类", baseConfigModel, sb); sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.ServiceName + ""); sb.AppendLine(" {"); sb.AppendLine(" #region 获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取列表数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// "); sb.AppendLine(" Task> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取分页数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// 分页条件"); sb.AppendLine(" /// "); sb.AppendLine(" Task> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取指定列数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 表字段列"); sb.AppendLine(" /// 条件"); sb.AppendLine(" /// 条件参数"); sb.AppendLine(" /// 排序列"); sb.AppendLine(" /// 排序类型"); sb.AppendLine(" /// "); sb.AppendLine(" Task> GetListColumn(string column, string where, List parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\");"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键id"); sb.AppendLine(" /// "); sb.AppendLine(" Task<" + baseConfigModel.FileConfig.EntityName + "> GetEntity(int id);"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 提交数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 保存数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 实体参数"); sb.AppendLine(" /// "); sb.AppendLine(" Task SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 保存多条数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 数据列表"); sb.AppendLine(" /// "); sb.AppendLine(" Task InsertMany(List<" + baseConfigModel.FileConfig.EntityName + "> list);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键Id"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" Task DeleteFormById(string ids, bool del = false);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" Task DeleteFormByWhere(string where, List parameters, bool del = false);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 修改多条数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 数据列表"); sb.AppendLine(" /// "); sb.AppendLine(" Task UpdateMany(List<" + baseConfigModel.FileConfig.EntityName + "> list);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件修改数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 修改字段"); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// "); sb.AppendLine(" Task UpdateFormByWhere(string setField, string where, List parameters);"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 私有方法"); sb.AppendLine(" ///// "); sb.AppendLine(" ///// 列表条件过滤"); sb.AppendLine(" ///// 建议该方法放在Partial部分类中,因为代码生成时当前类会被覆盖(该方法在生成时将会被注释)"); sb.AppendLine(" ///// "); sb.AppendLine(" ///// "); sb.AppendLine(" ///// "); sb.AppendLine(" //Expression> ListFilter(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param);"); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildIService_Partial public string BuildIService_Partial(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); string method = string.Empty; sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.IService." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("服务接口类(部分类)", baseConfigModel, sb); sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.ServiceName + ""); sb.AppendLine(" {"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildBusiness public string BuildBusiness(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using YiSha.Util.Extension;"); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Business." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("业务实现类", baseConfigModel, sb); sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.BusinessName + " : I" + baseConfigModel.FileConfig.IBusinessName + ""); sb.AppendLine(" {"); sb.AppendLine(" #region 获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取列表数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task>> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)"); sb.AppendLine(" {"); sb.AppendLine(" TData> obj = new TData>();"); sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetList(param);"); sb.AppendLine(" obj.Total = obj.Data.Count;"); sb.AppendLine(" obj.Tag = 1;"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取分页数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// 分页条件"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task>> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination)"); sb.AppendLine(" {"); sb.AppendLine(" TData> obj = new TData>();"); sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetPageList(param, pagination);"); sb.AppendLine(" obj.Total = pagination.TotalCount;"); sb.AppendLine(" obj.Tag = 1;"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取指定列数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 表字段列"); sb.AppendLine(" /// 条件"); sb.AppendLine(" /// 条件参数"); sb.AppendLine(" /// 排序列"); sb.AppendLine(" /// 排序类型"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task>> GetListColumn(string column, string where, List parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\")"); sb.AppendLine(" {"); sb.AppendLine(" TData> obj = new TData>();"); sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetListColumn(column, where, parameters, groupBy, sort, sortType);"); sb.AppendLine(" obj.Total = obj.Data.Count;"); sb.AppendLine(" obj.Tag = 1;"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键id"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task> GetEntity(int id)"); sb.AppendLine(" {"); sb.AppendLine(" TData<" + baseConfigModel.FileConfig.EntityName + "> obj = new TData<" + baseConfigModel.FileConfig.EntityName + ">();"); sb.AppendLine(" obj.Data = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".GetEntity(id);"); sb.AppendLine(" if (obj.Data != null)"); sb.AppendLine(" {"); sb.AppendLine(" obj.Tag = 1;"); sb.AppendLine(" }"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 提交数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 保存数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 实体参数"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task> SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity)"); sb.AppendLine(" {"); sb.AppendLine(" TData obj = new TData();"); sb.AppendLine(" await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".SaveForm(entity);"); sb.AppendLine(" obj.Data = entity.Id;"); sb.AppendLine(" obj.Tag = 1;"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键Id"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task DeleteFormById(string ids, bool del = false)"); sb.AppendLine(" {"); sb.AppendLine(" TData obj = new TData();"); sb.AppendLine(" obj.Tag = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".DeleteFormById(ids, del);"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task DeleteFormByWhere(string where, List parameters, bool del = false)"); sb.AppendLine(" {"); sb.AppendLine(" TData obj = new TData();"); sb.AppendLine(" obj.Tag = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".DeleteFormByWhere(where, parameters, del);"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件修改数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 修改字段"); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// "); sb.AppendLine(" public async Task UpdateFormByWhere(string setField, string where, List parameters)"); sb.AppendLine(" {"); sb.AppendLine(" TData obj = new TData();"); sb.AppendLine(" obj.Tag = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ".UpdateFormByWhere(setField, where, parameters);"); sb.AppendLine(" return obj;"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 私有方法"); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildBusiness_Partial public string BuildBusiness_Partial(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using YiSha.Util.Extension;"); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Business." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("业务实现类(部分类)", baseConfigModel, sb); sb.AppendLine(" public partial class " + baseConfigModel.FileConfig.BusinessName + ""); sb.AppendLine(" {"); sb.AppendLine(" private I" + baseConfigModel.FileConfig.ServiceName + " _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ";"); sb.AppendLine(" public " + baseConfigModel.FileConfig.BusinessName + " (I" + baseConfigModel.FileConfig.ServiceName + " " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ")"); sb.AppendLine(" {"); sb.AppendLine(" _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + " = " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.ServiceName) + ";"); sb.AppendLine(" }"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildIBusiness public string BuildIBusiness(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("namespace YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("业务接口类", baseConfigModel, sb); sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.BusinessName); sb.AppendLine(" {"); sb.AppendLine(" #region 获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取列表数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// "); sb.AppendLine(" Task>> GetList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取分页数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 请求参数"); sb.AppendLine(" /// 分页条件"); sb.AppendLine(" /// "); sb.AppendLine(" Task>> GetPageList(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 获取指定列数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 表字段列"); sb.AppendLine(" /// 条件"); sb.AppendLine(" /// 条件参数"); sb.AppendLine(" /// 排序列"); sb.AppendLine(" /// 排序类型"); sb.AppendLine(" /// "); sb.AppendLine(" Task>> GetListColumn(string column, string where, List parameters, string groupBy = \"\", string sort = \"basecreatetime\", string sortType = \"desc\");"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id获取数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键id"); sb.AppendLine(" /// "); sb.AppendLine(" Task> GetEntity(int id);"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 提交数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 保存数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 实体参数"); sb.AppendLine(" /// "); sb.AppendLine(" Task> SaveForm(" + baseConfigModel.FileConfig.EntityName + " entity);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据主键Id删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 主键Id"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" Task DeleteFormById(string ids, bool del = false);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件删除数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// 是否真删除 true真删除 false软删除 默认false"); sb.AppendLine(" /// "); sb.AppendLine(" Task DeleteFormByWhere(string where, List parameters, bool del = false);"); sb.AppendLine(); sb.AppendLine(" /// "); sb.AppendLine(" /// 根据条件修改数据"); sb.AppendLine(" /// "); sb.AppendLine(" /// 修改字段"); sb.AppendLine(" /// where条件"); sb.AppendLine(" /// 参数"); sb.AppendLine(" /// "); sb.AppendLine(" Task UpdateFormByWhere(string setField, string where, List parameters);"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 私有方法"); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildIBusiness_Partial public string BuildIBusiness_Partial(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using System.Data.Common;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("namespace YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule); sb.AppendLine("{"); SetClassDescription("业务接口类(部分类)", baseConfigModel, sb); sb.AppendLine(" public partial interface I" + baseConfigModel.FileConfig.BusinessName); sb.AppendLine(" {"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildController public string BuildController(BaseConfigModel baseConfigModel) { string modulePrefix = GetModulePrefix(baseConfigModel); string classPrefix = baseConfigModel.FileConfig.ClassPrefix.ToLower(); StringBuilder sb = new StringBuilder(); sb.AppendLine("using System;"); sb.AppendLine("using System.Collections.Generic;"); sb.AppendLine("using System.Threading.Tasks;"); sb.AppendLine("using System.Linq;"); sb.AppendLine("using System.Web;"); sb.AppendLine("using Microsoft.AspNetCore.Mvc;"); sb.AppendLine("using YiSha.Util;"); sb.AppendLine("using YiSha.Util.Model;"); sb.AppendLine("using YiSha.Entity;"); sb.AppendLine("using YiSha.Model;"); sb.AppendLine("using YiSha.Admin.Web.Controllers;"); sb.AppendLine("using YiSha.Entity." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IService." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.Model.Param." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine("using YiSha.IBusiness." + baseConfigModel.OutputConfig.OutputModule + ";"); sb.AppendLine(); sb.AppendLine("namespace YiSha.Admin.Web.Areas." + baseConfigModel.OutputConfig.OutputModule + ".Controllers"); sb.AppendLine("{"); SetClassDescription("控制器类", baseConfigModel, sb); sb.AppendLine(" [Area(\"" + baseConfigModel.OutputConfig.OutputModule + "\")]"); sb.AppendLine(" public class " + baseConfigModel.FileConfig.ControllerName + " : BaseController"); sb.AppendLine(" {"); sb.AppendLine(" private I" + baseConfigModel.FileConfig.IBusinessName + " _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ";"); sb.AppendLine(); sb.AppendLine(" public " + baseConfigModel.FileConfig.ControllerName + "(I" + baseConfigModel.FileConfig.IBusinessName + " " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ")"); sb.AppendLine(" {"); sb.AppendLine(" _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + " = " + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ";"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" #region 视图功能"); sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "view") + "\")]"); sb.AppendLine(" public ActionResult " + baseConfigModel.FileConfig.PageIndexName + "()"); sb.AppendLine(" {"); sb.AppendLine(" return View();"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" public ActionResult " + baseConfigModel.FileConfig.PageFormName + "()"); sb.AppendLine(" {"); sb.AppendLine(" return View();"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 获取数据"); sb.AppendLine(" [HttpGet]"); sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "search") + "\")]"); sb.AppendLine(" public async Task GetListJson(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param)"); sb.AppendLine(" {"); sb.AppendLine(" TData> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".GetList(param);"); sb.AppendLine(" return Json(obj);"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" [HttpGet]"); sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "search") + "\")]"); sb.AppendLine(" public async Task GetPageListJson(" + baseConfigModel.FileConfig.EntityParamName.Replace("Param", "ListParam") + " param, Pagination pagination)"); sb.AppendLine(" {"); sb.AppendLine(" TData> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".GetPageList(param, pagination);"); sb.AppendLine(" return Json(obj);"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" [HttpGet]"); sb.AppendLine(" public async Task GetFormJson(int id)"); sb.AppendLine(" {"); sb.AppendLine(" TData<" + baseConfigModel.FileConfig.EntityName + "> obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".GetEntity(id);"); sb.AppendLine(" return Json(obj);"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(); sb.AppendLine(" #region 提交数据"); sb.AppendLine(" [HttpPost]"); sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "add") + "," + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "edit") + "\")]"); sb.AppendLine(" public async Task SaveFormJson(" + baseConfigModel.FileConfig.EntityName + " entity)"); sb.AppendLine(" {"); sb.AppendLine(" TData obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".SaveForm(entity);"); sb.AppendLine(" return Json(obj);"); sb.AppendLine(" }"); sb.AppendLine(); sb.AppendLine(" [HttpPost]"); sb.AppendLine(" [AuthorizeFilter(\"" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "delete") + "\")]"); sb.AppendLine(" public async Task DeleteFormJson(string ids)"); sb.AppendLine(" {"); sb.AppendLine(" TData obj = await _" + TableMappingHelper.FirstLetterLowercase(baseConfigModel.FileConfig.IBusinessName) + ".DeleteFormById(ids);"); sb.AppendLine(" return Json(obj);"); sb.AppendLine(" }"); sb.AppendLine(" #endregion"); sb.AppendLine(" }"); sb.AppendLine("}"); return sb.ToString(); } #endregion #region BuildIndex public string BuildIndex(BaseConfigModel baseConfigModel) { #region 初始化集合 if (baseConfigModel.PageIndex.ButtonList == null) { baseConfigModel.PageIndex.ButtonList = new List(); } if (baseConfigModel.PageIndex.ColumnList == null) { baseConfigModel.PageIndex.ColumnList = new List(); } #endregion StringBuilder sb = new StringBuilder(); sb.AppendLine("@{"); sb.AppendLine(" Layout = \"~/Views/Shared/_Index.cshtml\";"); sb.AppendLine(" }"); sb.AppendLine("
"); sb.AppendLine("
"); #region 是否显示搜索 if (baseConfigModel.PageIndex.IsSearch == 1) { string fieldName = TextHelper.GetCustomValue(baseConfigModel.PageIndex.ColumnList.FirstOrDefault(), "fieldName"); string fieldNameLower = TableMappingHelper.FirstLetterLowercase(fieldName); sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine("
    "); sb.AppendLine("
  • "); sb.AppendLine(" " + fieldName + ":"); sb.AppendLine("
  • "); sb.AppendLine("
  • "); sb.AppendLine("  搜索"); sb.AppendLine("
  • "); sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine("
"); } #endregion #region 是否显示工具栏 if (baseConfigModel.PageIndex.ButtonList.Where(p => p != "btnSearch").Any()) { sb.AppendLine("
"); if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnAdd").Any()) { sb.AppendLine(" 新增"); } if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnEdit").Any()) { sb.AppendLine(" 修改"); } if (baseConfigModel.PageIndex.ButtonList.Where(p => p == "btnDelete").Any()) { sb.AppendLine(" 删除"); } sb.AppendLine("
"); } #endregion sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine(""); sb.AppendLine(""); return sb.ToString(); } #endregion #region BuildForm public string BuildForm(BaseConfigModel baseConfigModel) { #region 初始化集合 if (baseConfigModel.PageForm.FieldList == null) { baseConfigModel.PageForm.FieldList = new List(); } #endregion StringBuilder sb = new StringBuilder(); sb.AppendLine("@{"); sb.AppendLine(" Layout = \"~/Views/Shared/_FormWhite.cshtml\";"); sb.AppendLine("}"); sb.AppendLine(""); sb.AppendLine("
"); sb.AppendLine("
"); #region 表单控件 if (baseConfigModel.PageForm.FieldList.Count > 0) { string field = string.Empty; string fieldLower = string.Empty; switch (baseConfigModel.PageForm.ShowMode) { case 1: for (int i = 0; i < baseConfigModel.PageForm.FieldList.Count; i++) { field = baseConfigModel.PageForm.FieldList[i]; fieldLower = TableMappingHelper.FirstLetterLowercase(field); sb.AppendLine("
"); sb.AppendLine(" "); sb.AppendLine("
"); sb.AppendLine(" "); sb.AppendLine("
"); sb.AppendLine("
"); } break; case 2: for (int i = 0; i < baseConfigModel.PageForm.FieldList.Count; i++) { field = baseConfigModel.PageForm.FieldList[i]; fieldLower = TableMappingHelper.FirstLetterLowercase(field); if (i % 2 == 0) { sb.AppendLine("
"); } sb.AppendLine(" "); sb.AppendLine("
"); sb.AppendLine(" "); sb.AppendLine("
"); if (i % 2 == 1) { sb.AppendLine("
"); } } break; } } #endregion sb.AppendLine("
"); sb.AppendLine("
"); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); return sb.ToString(); } #endregion #region BuildMenu public string BuildMenu(BaseConfigModel baseConfigModel) { StringBuilder sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine(" 菜单路径:" + baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/" + baseConfigModel.FileConfig.PageIndexName); sb.AppendLine(); string modulePrefix = GetModulePrefix(baseConfigModel); string classPrefix = baseConfigModel.FileConfig.ClassPrefix.ToLower(); sb.AppendLine(" 页面显示权限:" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "view")); sb.AppendLine(); List list = GetButtonAuthorizeList(); if (baseConfigModel.PageIndex.IsSearch == 1) { KeyValue button = list.Where(p => p.Key == "btnSearch").FirstOrDefault(); sb.AppendLine(" 按钮" + button.Description + "权限:" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value)); } foreach (string btn in baseConfigModel.PageIndex.ButtonList) { KeyValue button = list.Where(p => p.Key == btn).FirstOrDefault(); sb.AppendLine(" 按钮" + button.Description + "权限:" + string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value)); } sb.AppendLine(); return sb.ToString(); } #endregion #region CreateCode public async Task> CreateCode(BaseConfigModel baseConfigModel, string code, IMenuCacheBusiness menuCacheBusiness) { List result = new List(); JObject param = code.ToJObject(); #region 实体类 if (!string.IsNullOrEmpty(param["CodeEntity"].ParseToString())) { string codeEntity = HttpUtility.HtmlDecode(param["CodeEntity"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputEntity, "YiSha.Entity", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.EntityName + ".cs"); CreateCodeFile(codePath, codeEntity, out bool isAdd, true); result.Add(new KeyValue { Key = "实体类", Value = codePath }); } #endregion #region 实体查询类 if (!param["CodeEntityParam"].IsEmpty()) { string codeListEntity = HttpUtility.HtmlDecode(param["CodeEntityParam"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputEntity, "YiSha.Model", "Param", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.EntityParamName + ".cs"); CreateCodeFile(codePath, codeListEntity, out bool isAdd); //查询实体不需要生成 result.Add(new KeyValue { Key = "实体查询类", Value = codePath }); } #endregion #region 服务接口类 if (!param["CodeIService"].IsEmpty()) { string codeService = HttpUtility.HtmlDecode(param["CodeIService"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IService", baseConfigModel.OutputConfig.OutputModule, "I" + baseConfigModel.FileConfig.IServiceName + ".cs"); CreateCodeFile(codePath, codeService, out bool isAdd, true); result.Add(new KeyValue { Key = "服务接口类", Value = codePath }); } #endregion #region 服务接口类(部分类) if (!param["CodeIServicePartial"].IsEmpty()) { string codeService = HttpUtility.HtmlDecode(param["CodeIServicePartial"].ToString()); string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IService", baseConfigModel.OutputConfig.OutputModule, "Partial"); CreatePartialFilePath(partialPath); string codePath = Path.Combine(partialPath, "I" + baseConfigModel.FileConfig.IServiceName + ".cs"); CreateCodeFile(codePath, codeService, out bool isAdd); result.Add(new KeyValue { Key = "服务接口类(部分类)", Value = codePath }); } #endregion #region 服务实现类 if (!param["CodeService"].IsEmpty()) { string codeService = HttpUtility.HtmlDecode(param["CodeService"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Service", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.ServiceName + ".cs"); CreateCodeFile(codePath, codeService, out bool isAdd, true); result.Add(new KeyValue { Key = "服务实现类", Value = codePath }); } #endregion #region 服务实现类(部分类) if (!param["CodeServicePartial"].IsEmpty()) { string codeService = HttpUtility.HtmlDecode(param["CodeServicePartial"].ToString()); string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Service", baseConfigModel.OutputConfig.OutputModule, "Partial"); CreatePartialFilePath(partialPath); string codePath = Path.Combine(partialPath, baseConfigModel.FileConfig.ServiceName + ".cs"); CreateCodeFile(codePath, codeService, out bool isAdd); result.Add(new KeyValue { Key = "服务实现类(部分类)", Value = codePath }); } #endregion #region 业务接口类 if (!param["CodeIBusiness"].IsEmpty()) { string codeBusiness = HttpUtility.HtmlDecode(param["CodeIBusiness"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IBusiness", baseConfigModel.OutputConfig.OutputModule, "I" + baseConfigModel.FileConfig.IBusinessName + ".cs"); CreateCodeFile(codePath, codeBusiness, out bool isAdd, true); result.Add(new KeyValue { Key = "业务接口类", Value = codePath }); } #endregion #region 业务接口类(部分类) if (!param["CodeIBusinessPartial"].IsEmpty()) { string codeBusiness = HttpUtility.HtmlDecode(param["CodeIBusinessPartial"].ToString()); string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.IBusiness", baseConfigModel.OutputConfig.OutputModule, "Partial"); CreatePartialFilePath(partialPath); string codePath = Path.Combine(partialPath, "I" + baseConfigModel.FileConfig.IBusinessName + ".cs"); CreateCodeFile(codePath, codeBusiness, out bool isAdd); result.Add(new KeyValue { Key = "业务接口类(部分类)", Value = codePath }); } #endregion #region 业务实现类 if (!param["CodeBusiness"].IsEmpty()) { string codeBusiness = HttpUtility.HtmlDecode(param["CodeBusiness"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Business", baseConfigModel.OutputConfig.OutputModule, baseConfigModel.FileConfig.BusinessName + ".cs"); CreateCodeFile(codePath, codeBusiness, out bool isAdd, true); result.Add(new KeyValue { Key = "业务实现类", Value = codePath }); } #endregion #region 业务实现类(部分类) if (!param["CodeBusinessPartial"].IsEmpty()) { string codeBusiness = HttpUtility.HtmlDecode(param["CodeBusinessPartial"].ToString()); string partialPath = Path.Combine(baseConfigModel.OutputConfig.OutputBusiness, "YiSha.Business", baseConfigModel.OutputConfig.OutputModule, "Partial"); CreatePartialFilePath(partialPath); string codePath = Path.Combine(partialPath, baseConfigModel.FileConfig.BusinessName + ".cs"); CreateCodeFile(codePath, codeBusiness, out bool isAdd); result.Add(new KeyValue { Key = "业务实现类(部分类)", Value = codePath }); } #endregion #region 控制器 if (!param["CodeController"].IsEmpty()) { string codeController = HttpUtility.HtmlDecode(param["CodeController"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas", baseConfigModel.OutputConfig.OutputModule, "Controllers", baseConfigModel.FileConfig.ControllerName + ".cs"); CreateCodeFile(codePath, codeController, out bool isAdd); result.Add(new KeyValue { Key = "控制器", Value = codePath }); } #endregion #region 列表页 if (!param["CodeIndex"].IsEmpty()) { //代码生成器,生成菜单按钮权限默认获取系统数据库数据 DBConnectTypeEnum dbConnectType = DBConnectTypeEnum.SystemDB; string codeIndex = HttpUtility.HtmlDecode(param["CodeIndex"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas", baseConfigModel.OutputConfig.OutputModule, "Views", baseConfigModel.FileConfig.ClassPrefix, baseConfigModel.FileConfig.PageIndexName + ".cshtml"); CreateCodeFile(codePath, codeIndex, out bool isAdd); result.Add(new KeyValue { Key = "列表页", Value = codePath }); if (isAdd) { //列表页面添加成功后,才生成菜单权限 // 生成菜单 RepositoryFactory repositoryFactory = new RepositoryFactory(); List buttonAuthorizeList = GetButtonAuthorizeList(); string menuUrl = baseConfigModel.OutputConfig.OutputModule + "/" + baseConfigModel.FileConfig.ClassPrefix + "/" + baseConfigModel.FileConfig.PageIndexName; string modulePrefix = GetModulePrefix(baseConfigModel); string classPrefix = baseConfigModel.FileConfig.ClassPrefix.ToLower(); MenuEntity menuEntity = new MenuEntity { MenuName = baseConfigModel.FileConfig.ClassDescription, MenuUrl = menuUrl, MenuType = (int)MenuTypeEnum.Menu, Authorize = string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, "view") }; TData obj = await AddMenu(repositoryFactory, menuEntity, dbConnectType); if (obj.Success) { result.Add(new KeyValue { Key = "菜单(刷新页面可见)", Value = menuUrl }); if (baseConfigModel.PageIndex.IsSearch == 1) { // 按钮搜索权限 KeyValue button = buttonAuthorizeList.Where(p => p.Key == "btnSearch").FirstOrDefault(); MenuEntity buttonEntity = new MenuEntity { ParentId = menuEntity.Id, MenuName = baseConfigModel.FileConfig.ClassDescription + button.Description, MenuType = (int)MenuTypeEnum.Button, Authorize = string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value) }; await AddMenu(repositoryFactory, buttonEntity, dbConnectType); } foreach (string btn in baseConfigModel.PageIndex.ButtonList) { KeyValue button = buttonAuthorizeList.Where(p => p.Key == btn).FirstOrDefault(); MenuEntity buttonEntity = new MenuEntity { ParentId = menuEntity.Id, MenuName = baseConfigModel.FileConfig.ClassDescription + button.Description, MenuType = (int)MenuTypeEnum.Button, Authorize = string.Format("{0}:{1}:{2}", modulePrefix, classPrefix, button.Value) }; await AddMenu(repositoryFactory, buttonEntity, dbConnectType); } menuCacheBusiness.Remove(); } } } #endregion #region 表单页 if (!param["CodeForm"].IsEmpty()) { string codeSave = HttpUtility.HtmlDecode(param["CodeForm"].ToString()); string codePath = Path.Combine(baseConfigModel.OutputConfig.OutputWeb, "Areas", baseConfigModel.OutputConfig.OutputModule, "Views", baseConfigModel.FileConfig.ClassPrefix, baseConfigModel.FileConfig.PageFormName + ".cshtml"); CreateCodeFile(codePath, codeSave, out bool isAdd); result.Add(new KeyValue { Key = "表单页", Value = codePath }); } #endregion return result; } private async Task AddMenu(RepositoryFactory repositoryFactory, MenuEntity menuEntity, DBConnectTypeEnum dbConnectType) { TData obj = new TData(); IEnumerable menuList = await repositoryFactory.BaseRepository(dbConnectType).FindList(); if (!menuList.Where(p => p.MenuName == menuEntity.MenuName && p.Authorize == menuEntity.Authorize).Any()) { menuEntity.MenuSort = menuList.Max(p => p.MenuSort) + 1; menuEntity.MenuStatus = 1; await menuEntity.Create(); await repositoryFactory.BaseRepository(dbConnectType).Insert(menuEntity); obj.Tag = 1; } return obj; } #endregion #region 私有方法 #region GetProjectRootPath private string GetProjectRootPath(string path) { path = path.ParseToString(); path = path.Trim('\\'); if (GlobalContext.SystemConfig.Debug) { // 向上找二级 path = Directory.GetParent(path).FullName; path = Directory.GetParent(path).FullName; } return path; } #endregion #region SetClassDescription private void SetClassDescription(string type, BaseConfigModel baseConfigModel, StringBuilder sb) { sb.AppendLine(" /// "); sb.AppendLine(" /// 创 建:" + baseConfigModel.FileConfig.CreateName); sb.AppendLine(" /// 日 期:" + baseConfigModel.FileConfig.CreateDate); sb.AppendLine(" /// 描 述:" + baseConfigModel.FileConfig.ClassDescription + type); sb.AppendLine(" /// "); } #endregion #region GetButtonAuthorizeList private List GetButtonAuthorizeList() { var list = new List(); list.Add(new KeyValue { Key = "btnSearch", Value = "search", Description = "搜索" }); list.Add(new KeyValue { Key = "btnAdd", Value = "add", Description = "新增" }); list.Add(new KeyValue { Key = "btnEdit", Value = "edit", Description = "修改" }); list.Add(new KeyValue { Key = "btnDelete", Value = "delete", Description = "删除" }); return list; } #endregion private string GetModulePrefix(BaseConfigModel baseConfigModel) { return baseConfigModel.OutputConfig.OutputModule.Replace("Manage", string.Empty).ToLower(); } private string GetBaseEntity(DataTable dt) { string entity = string.Empty; var columnList = dt.AsEnumerable().Select(p => p["TableColumn"].ParseToString()).ToList(); bool id = columnList.Where(p => p.ToLower().Equals("Id".ToLower())).Any(); bool baseIsDelete = columnList.Where(p => p.ToLower().Equals("BaseIsDelete".ToLower())).Any(); bool baseVersion = columnList.Where(p => p.ToLower().Equals("BaseVersion".ToLower())).Any(); bool baseModifyTime = columnList.Where(p => p.ToLower().Equals("BaseModifyTime".ToLower())).Any(); bool baseModifierId = columnList.Where(p => p.ToLower().Equals("BaseModifierId".ToLower())).Any(); bool baseCreateTime = columnList.Where(p => p.ToLower().Equals("BaseCreateTime".ToLower())).Any(); bool baseCreatorId = columnList.Where(p => p.ToLower().Equals("BaseCreatorId".ToLower())).Any(); if (!id) { throw new Exception("数据库表必须有主键Id字段"); } if (baseIsDelete && baseVersion && baseModifyTime && baseModifierId && baseCreateTime && baseCreatorId) { entity = "BaseExtensionEntity"; } else if (baseVersion && baseModifyTime && baseModifierId && baseCreateTime && baseCreatorId) { entity = "BaseModifyEntity"; } else if (baseCreateTime && baseCreatorId) { entity = "BaseCreateEntity"; } else { entity = "BaseEntity"; } return entity; } private string GetSaveFormCreate(string entity) { string line = string.Empty; switch (entity) { case "BaseEntity": line = "entity.Create();"; break; case "BaseCreateEntity": line = "await entity.Create();"; break; case "BaseModifyEntity": line = "await entity.Create();"; break; case "BaseExtensionEntity": line = "await entity.Create();"; break; } return line; } private string GetSaveFormModify(string entity) { string line = string.Empty; switch (entity) { case "BaseEntity": line = string.Empty; break; case "BaseCreateEntity": line = string.Empty; break; case "BaseModifyEntity": line = "await entity.Modify();"; break; case "BaseExtensionEntity": line = "await entity.Modify();"; break; } return line; } /// /// 创建部分类文件路径 /// /// private void CreatePartialFilePath(string partialPath) { if (!Directory.Exists(partialPath)) Directory.CreateDirectory(partialPath); } /// /// 创建代码文件 /// /// 地址 /// 内容 /// 是否添加成功 true成功,false未添加 /// 是否覆盖 true覆盖,false不覆盖 默认不覆盖 private void CreateCodeFile(string codePath, string codeContent, out bool isAdd, bool isCovered = false) { isAdd = false; if (!File.Exists(codePath)) { isAdd = true; //代码不存在直接创建 FileHelper.CreateFile(codePath, codeContent); } else { //代码已存在,判断是否需要覆盖 if (isCovered) { isAdd = true; File.Delete(codePath); FileHelper.CreateFile(codePath, codeContent); } } } /// /// 获取数据库操作类型 /// /// /// private string GetDBConnectTypeEnum(DBConnectTypeEnum dbConnectType) { //默认空,如果数据库操作类型不存在,生成的代码则会报错 var result = ""; switch (dbConnectType) { case DBConnectTypeEnum.KjhDB: result = "DBConnectTypeEnum.KjhDB"; break; case DBConnectTypeEnum.SystemDB: result = "DBConnectTypeEnum.SystemDB"; break; case DBConnectTypeEnum.TkDB: result = "DBConnectTypeEnum.TkDB"; break; case DBConnectTypeEnum.ZxDB: result = "DBConnectTypeEnum.ZxDB"; break; } return result; } #endregion } }