CustomViewEngine.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. namespace Admin.NET.Core.Service;
  2. public class CustomViewEngine : ViewEngineModel
  3. {
  4. private readonly ISqlSugarClient _db;
  5. public CustomViewEngine()
  6. {
  7. }
  8. public CustomViewEngine(ISqlSugarClient db)
  9. {
  10. _db = db;
  11. }
  12. /// <summary>
  13. /// 库定位器
  14. /// </summary>
  15. public string ConfigId { get; set; } = SqlSugarConst.ConfigId;
  16. public string AuthorName { get; set; }
  17. public string BusName { get; set; }
  18. public string NameSpace { get; set; }
  19. public string ClassName { get; set; }
  20. public string ProjectLastName { get; set; }
  21. public string LowerClassName
  22. {
  23. get
  24. {
  25. return ClassName[..1].ToLower() + ClassName[1..]; // 首字母小写
  26. }
  27. }
  28. public List<CodeGenConfig> QueryWhetherList { get; set; }
  29. public List<CodeGenConfig> TableField { get; set; }
  30. public bool IsJoinTable { get; set; }
  31. public bool IsUpload { get; set; }
  32. private List<ColumnOuput> ColumnList { get; set; }
  33. public string GetColumnNetType(object tbName, object colName)
  34. {
  35. ColumnList = GetColumnListByTableName(tbName.ToString());
  36. var col = ColumnList.Where(c => c.ColumnName == colName.ToString()).FirstOrDefault();
  37. return col.NetType;
  38. }
  39. public List<ColumnOuput> GetColumnListByTableName(string tableName)
  40. {
  41. // 多库代码生成切换库
  42. var provider = _db.AsTenant().GetConnectionScope(ConfigId != SqlSugarConst.ConfigId ? ConfigId : SqlSugarConst.ConfigId);
  43. // 获取实体类型属性
  44. var entityType = provider.DbMaintenance.GetTableInfoList().FirstOrDefault(u => u.Name == tableName);
  45. if (entityType == null) return null;
  46. // 按原始类型的顺序获取所有实体类型属性(不包含导航属性,会返回null)
  47. return provider.DbMaintenance.GetColumnInfosByTableName(entityType.Name).Select(u => new ColumnOuput
  48. {
  49. ColumnName = u.DbColumnName,
  50. ColumnKey = u.IsPrimarykey.ToString(),
  51. DataType = u.DataType.ToString(),
  52. NetType = CodeGenUtil.ConvertDataType(u.DataType.ToString()),
  53. ColumnComment = u.ColumnDescription
  54. }).ToList();
  55. }
  56. }