diff --git a/Api/Ewide.Application/Ewide.Application.csproj b/Api/Ewide.Application/Ewide.Application.csproj index 7653b96..5dadc0f 100644 --- a/Api/Ewide.Application/Ewide.Application.csproj +++ b/Api/Ewide.Application/Ewide.Application.csproj @@ -19,6 +19,7 @@ + diff --git a/Api/Ewide.Application/Ewide.Application.xml b/Api/Ewide.Application/Ewide.Application.xml index 4a7d351..46d1c55 100644 --- a/Api/Ewide.Application/Ewide.Application.xml +++ b/Api/Ewide.Application/Ewide.Application.xml @@ -4,6 +4,12 @@ Ewide.Application + + + 读取数据库的所有表 并拿到数据 + + + 项目管理相关服务 diff --git a/Api/Ewide.Application/Service/HouseProjectInfo/HouseProjectInfoService.cs b/Api/Ewide.Application/Service/HouseProjectInfo/HouseProjectInfoService.cs index 11168eb..2f22946 100644 --- a/Api/Ewide.Application/Service/HouseProjectInfo/HouseProjectInfoService.cs +++ b/Api/Ewide.Application/Service/HouseProjectInfo/HouseProjectInfoService.cs @@ -2,6 +2,9 @@ using Ewide.Application.Service.HouseProjectInfo.Dto; using Ewide.Core; using Ewide.Core.Service; +using Ewide.Core.Util; +using Ewide.EntityFramework.Core; +using Furion; using Furion.DatabaseAccessor; using Furion.DatabaseAccessor.Extensions; using Furion.DependencyInjection; @@ -10,15 +13,67 @@ using Furion.FriendlyException; using Mapster; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using MySqlConnector; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Data; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Ewide.Application.Service.HouseProjectInfo { + [ApiDescriptionSettings(Name = "xml", Order = 3)] + public class XMLHandler : IDynamicApiController, ITransient + { + private readonly DefaultDbContext _db; + + public XMLHandler(DefaultDbContext db) + { + _db = db; + } + [HttpGet("/xml/detail")] + public async Task XMLDoneAsync() + { + XmlSerializerUtil xmlHandler = new XmlSerializerUtil(); + xmlHandler.WriteDataSeed(); + //拿到数据保存到 XML文件 + //Dictionary dicDataBase = await GetDataBase(); + //foreach (KeyValuePair item in dicDataBase) + //{ + // xmlHandler.WriteXML(item.Value.GetType(), item.Value, item.Key); + //} + } + /// + /// 读取数据库的所有表 并拿到数据 + /// + /// + private async Task> GetDataBase() + { + Dictionary dicDataBase = new Dictionary(); + var defaultConnection = App.Configuration["ConnectionStrings:DefaultConnection"]; + //获取 所有继承 IEntity 接口的表 + IEnumerable entityTypedb = _db.Model.GetEntityTypes().Where(x => typeof(IEntity).IsAssignableFrom(x.ClrType)).ToList(); + + foreach (IEntityType item in entityTypedb) + { + MethodInfo mi = _db.GetType().GetMethods().FirstOrDefault(s => s.Name == "Set"); + MethodInfo miConstructed = mi.MakeGenericMethod(item.ClrType); + object objList = miConstructed.Invoke(_db, null); + var objsource = item.ClrType.MakeList(objList); + dicDataBase.Add(item.ClrType.Name, objsource); + } + await _db.Database.CloseConnectionAsync(); + return dicDataBase; + } + } + + /// /// 项目管理相关服务 /// @@ -51,7 +106,7 @@ namespace Ewide.Application.Service.HouseProjectInfo [HttpPost("/houseProjectInfo/delete")] public async Task DeleteProject(DeleteProjectInput input) { - var project = _houseProjectInfoRep.FirstOrDefault(p => p.Id == input.Id) ; + var project = _houseProjectInfoRep.FirstOrDefault(p => p.Id == input.Id); await project.DeleteNowAsync(); } @@ -64,7 +119,7 @@ namespace Ewide.Application.Service.HouseProjectInfo public async Task UpdateProject(UpdateProjectInput input) { var project = input.Adapt(); - await project.UpdateExcludeAsync(new[] { nameof(BsHouseProjectInfo.AreaCode) }, true); + await project.UpdateExcludeAsync(new[] { nameof(BsHouseProjectInfo.AreaId) }, true); } /// @@ -105,7 +160,7 @@ namespace Ewide.Application.Service.HouseProjectInfo { var user = await _houseProjectInfoRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == input.Id); var userDto = user.Adapt(); - + return userDto; } diff --git a/Api/Ewide.Core/Entity/SysEmpExtOrgPos.cs b/Api/Ewide.Core/Entity/SysEmpExtOrgPos.cs index 382ac38..8cb388a 100644 --- a/Api/Ewide.Core/Entity/SysEmpExtOrgPos.cs +++ b/Api/Ewide.Core/Entity/SysEmpExtOrgPos.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Serialization; namespace Ewide.Core { @@ -24,6 +25,7 @@ namespace Ewide.Core /// /// 一对一引用(员工) /// + [XmlIgnore] public SysEmp SysEmp { get; set; } /// @@ -36,6 +38,7 @@ namespace Ewide.Core /// /// 一对一引用(机构) /// + [XmlIgnore] public SysOrg SysOrg { get; set; } /// @@ -48,6 +51,7 @@ namespace Ewide.Core /// /// 一对一引用(职位) /// + [XmlIgnore] public SysPos SysPos { get; set; } public void Configure(EntityTypeBuilder entityBuilder, DbContext dbContext, Type dbContextLocator) diff --git a/Api/Ewide.Core/Entity/SysEmpPos.cs b/Api/Ewide.Core/Entity/SysEmpPos.cs index 0943333..e1a2d14 100644 --- a/Api/Ewide.Core/Entity/SysEmpPos.cs +++ b/Api/Ewide.Core/Entity/SysEmpPos.cs @@ -22,6 +22,7 @@ namespace Ewide.Core /// /// 一对一引用(员工) /// + [XmlIgnore] public SysEmp SysEmp { get; set; } /// diff --git a/Api/Ewide.Core/Entity/SysRoleArea.cs b/Api/Ewide.Core/Entity/SysRoleArea.cs index 71031b9..ccb7505 100644 --- a/Api/Ewide.Core/Entity/SysRoleArea.cs +++ b/Api/Ewide.Core/Entity/SysRoleArea.cs @@ -7,6 +7,7 @@ using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Serialization; namespace Ewide.Core { @@ -19,9 +20,11 @@ namespace Ewide.Core /// [Comment("角色Id")] public string SysRoleId { get; set; } + [XmlIgnore] public SysRole SysRole { get; set; } [Comment("系统使用的区域代码")] public string AreaCode { get; set; } + [XmlIgnore] public SysAreaCode Area { get; set; } } diff --git a/Api/Ewide.Core/Entity/SysRoleDataScope.cs b/Api/Ewide.Core/Entity/SysRoleDataScope.cs index 6c90d2b..888ea77 100644 --- a/Api/Ewide.Core/Entity/SysRoleDataScope.cs +++ b/Api/Ewide.Core/Entity/SysRoleDataScope.cs @@ -1,6 +1,7 @@ using Furion.DatabaseAccessor; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Serialization; namespace Ewide.Core { @@ -33,6 +34,7 @@ namespace Ewide.Core /// /// 一对一引用(系统机构) /// + [XmlIgnore] public SysOrg SysOrg { get; set; } } } diff --git a/Api/Ewide.Core/Entity/SysRoleMenu.cs b/Api/Ewide.Core/Entity/SysRoleMenu.cs index bec0913..b7fe258 100644 --- a/Api/Ewide.Core/Entity/SysRoleMenu.cs +++ b/Api/Ewide.Core/Entity/SysRoleMenu.cs @@ -1,6 +1,7 @@ using Furion.DatabaseAccessor; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Serialization; namespace Ewide.Core { @@ -21,6 +22,7 @@ namespace Ewide.Core /// /// 一对一引用(系统用户) /// + [XmlIgnore] public SysRole SysRole { get; set; } /// @@ -33,6 +35,7 @@ namespace Ewide.Core /// /// 一对一引用(系统菜单) /// + [XmlIgnore] public SysMenu SysMenu { get; set; } } } diff --git a/Api/Ewide.Core/Entity/SysUserArea.cs b/Api/Ewide.Core/Entity/SysUserArea.cs index 181abb6..7d7421e 100644 --- a/Api/Ewide.Core/Entity/SysUserArea.cs +++ b/Api/Ewide.Core/Entity/SysUserArea.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Serialization; namespace Ewide.Core { @@ -15,10 +16,12 @@ namespace Ewide.Core [Comment("用户Id")] [Column("SysUserId", TypeName = "varchar(36)")] public string SysUserId { get; set; } + [XmlIgnore] public SysUser SysUser { get; set; } [Comment("系统使用的区域代码")] [MaxLength(10)] public string AreaCode { get; set; } + [XmlIgnore] public SysAreaCode Area { get; set; } } } diff --git a/Api/Ewide.Core/Entity/SysUserDataScope.cs b/Api/Ewide.Core/Entity/SysUserDataScope.cs index 71f6a57..9dc6db1 100644 --- a/Api/Ewide.Core/Entity/SysUserDataScope.cs +++ b/Api/Ewide.Core/Entity/SysUserDataScope.cs @@ -1,6 +1,7 @@ using Furion.DatabaseAccessor; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Serialization; namespace Ewide.Core { @@ -21,6 +22,7 @@ namespace Ewide.Core /// /// 一对一引用(系统用户) /// + [XmlIgnore] public SysUser SysUser { get; set; } /// @@ -33,6 +35,7 @@ namespace Ewide.Core /// /// 一对一引用(系统机构) /// + [XmlIgnore] public SysOrg SysOrg { get; set; } } } diff --git a/Api/Ewide.Core/Entity/SysUserRole.cs b/Api/Ewide.Core/Entity/SysUserRole.cs index 64f8882..4fd3894 100644 --- a/Api/Ewide.Core/Entity/SysUserRole.cs +++ b/Api/Ewide.Core/Entity/SysUserRole.cs @@ -1,6 +1,7 @@ using Furion.DatabaseAccessor; using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Serialization; namespace Ewide.Core { @@ -21,6 +22,7 @@ namespace Ewide.Core /// /// 一对一引用(系统用户) /// + [XmlIgnore] public SysUser SysUser { get; set; } /// @@ -33,6 +35,7 @@ namespace Ewide.Core /// /// 一对一引用(系统角色) /// + [XmlIgnore] public SysRole SysRole { get; set; } } } diff --git a/Api/Ewide.Core/Ewide.Core.csproj b/Api/Ewide.Core/Ewide.Core.csproj index 4f037e0..b291410 100644 --- a/Api/Ewide.Core/Ewide.Core.csproj +++ b/Api/Ewide.Core/Ewide.Core.csproj @@ -54,6 +54,7 @@ + diff --git a/Api/Ewide.Core/SeedDataXml/SysEmpExtOrgPos.xml b/Api/Ewide.Core/SeedDataXml/SysEmpExtOrgPos.xml new file mode 100644 index 0000000..adb5ebd --- /dev/null +++ b/Api/Ewide.Core/SeedDataXml/SysEmpExtOrgPos.xml @@ -0,0 +1,28 @@ + + + + d0ead3dc-5096-4e15-bc6d-f640be5301ec + 12d888de-f55d-4c88-b0a0-7c3510664d97 + 269236c4-d74e-4e54-9d50-f6f61580a197 + + + d0ead3dc-5096-4e15-bc6d-f640be5301ec + 8a2271d6-5bda-4544-bdd3-27e53a8b418e + 46c68a62-f119-4ff7-b621-0bbd77504538 + + + d0ead3dc-5096-4e15-bc6d-f640be5301ec + 127c0a5d-43ac-4370-b313-082361885aca + 5bd8c466-2bca-4386-a551-daac78e3cee8 + + + d0ead3dc-5096-4e15-bc6d-f640be5301ec + f236ab2d-e1b5-4e9d-844f-a59ec32c20e4 + d89a3afe-e6ba-4018-bdae-3c98bb47ad66 + + + 16a74726-e156-499f-9942-0e0e24ad0c3f + f236ab2d-e1b5-4e9d-844f-a59ec32c20e4 + 269236c4-d74e-4e54-9d50-f6f61580a197 + + \ No newline at end of file diff --git a/Api/Ewide.Core/Util/TypeHelper.cs b/Api/Ewide.Core/Util/TypeHelper.cs new file mode 100644 index 0000000..ca14160 --- /dev/null +++ b/Api/Ewide.Core/Util/TypeHelper.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Ewide.Core.Util +{ + public static class TypeHelper + { + /// + /// 动态创建List 泛型 + /// + /// Type类 + /// 数据源 + /// + public static object MakeList(this Type t, params object[] items) + { + Type type = typeof(List<>).MakeGenericType(t); + object list = Activator.CreateInstance(type); + System.Collections.IList ilist = list as System.Collections.IList; + foreach (object o in items) + ilist.Add(o); + return list; + } + /// + /// 动态创建List 泛型 + /// + /// Type类 + /// 数据源 + /// + public static object MakeList(this Type t, Object objList) + { + Type type = typeof(List<>).MakeGenericType(t); + object list = Activator.CreateInstance(type); + System.Collections.IList ilist = list as System.Collections.IList; + var collection = objList as System.Collections.IEnumerable; + if (collection != null) + { + foreach (object obj in collection) + { + ilist.Add(obj); + } + } + return list; + } + /// + /// 动态创建List 泛型s + /// + /// Type类 + /// 数据源 + /// + public static object DataTableToList(this Type hostType, DataTable table) + { + Type type = typeof(List<>).MakeGenericType(hostType); + object list = Activator.CreateInstance(type); + System.Collections.IList ilist = list as System.Collections.IList; + + object host = Activator.CreateInstance(hostType); + string tempName = string.Empty; + foreach (DataRow item in table.Rows) + { + PropertyInfo[] proList = hostType.GetProperties(); + foreach (PropertyInfo propertyInfo in proList) + { + tempName = propertyInfo.Name; + if (table.Columns.Contains(tempName)) + { + Object value = item[tempName]; + if (value!=null) + { + propertyInfo.SetValue(host,value, null); + } + } + } + ilist.Add(host); + } + return list; + } + } +} diff --git a/Api/Ewide.Core/Util/XmlSerializerUtil.cs b/Api/Ewide.Core/Util/XmlSerializerUtil.cs index 4135440..44af1fc 100644 --- a/Api/Ewide.Core/Util/XmlSerializerUtil.cs +++ b/Api/Ewide.Core/Util/XmlSerializerUtil.cs @@ -1,4 +1,5 @@  +using Furion; using System; using System.Collections.Generic; using System.IO; @@ -15,12 +16,24 @@ namespace Ewide.Core.Util { public class XmlSerializerUtil { - static string[] str = AppDomain.CurrentDomain.BaseDirectory.Split("ewide_core"); - static string path = str[0]+ @"ewide_core\Api\Ewide.Core\SeedDataXml\"; + /// + /// XML 要读取的路径 + /// + private string DirPath { set; get; } + /// + /// 当前程序集 + /// + private string currentAssemblyName { get { return "Ewide.Core"; } } + /// + /// 读取文件夹下所有XML + /// add-migration init -c defaultDbContext 备用数据 + /// + /// public Dictionary ReaderALL() { + string dirPath = CurrentDirPath(); Dictionary dic = new Dictionary(); - DirectoryInfo folder = new DirectoryInfo(path); + DirectoryInfo folder = new DirectoryInfo(dirPath); foreach (FileInfo file in folder.GetFiles()) { //除去.xml 后缀 @@ -34,16 +47,12 @@ namespace Ewide.Core.Util } return dic; } - private object Deserialize(string xmlContent, Type[] Arrtype) - { - object obj = null; - using (StringReader sr = new StringReader(xmlContent)) - { - XmlSerializer xmldes = new XmlSerializer(typeof(List), Arrtype); - obj = xmldes.Deserialize(sr); - } - return obj; - } + /// + /// 反序列化 + /// + /// + /// XML内容 + /// private object Deserialize(Type type, string xmlContent) { object obj = null; @@ -54,20 +63,32 @@ namespace Ewide.Core.Util } return obj; } - public string StreamRead(string path) + /// + /// 数据流读取 + /// + /// + /// + private string StreamRead(string path) { using (StreamReader reader = new StreamReader(path, Encoding.Default)) { return reader.ReadToEnd(); } } + /// + /// 反射 根据类名 反射整个类 得到实例 + /// + /// + /// private object AppInfo(string name) { - string assemblyName = "Ewide.Core"; - string nameSpace = "Ewide.Core"; - string fullName = nameSpace + "." + name; - object ect = Assembly.Load(assemblyName).CreateInstance(fullName); - return ect; + if (name == "BsHouseProjectInfo") //BsHouseProjectInfo 这个类 写在 application 程序集 + { + string fullNamei = string.Join('.', "Ewide.Application.Entity", name); + return Assembly.Load("Ewide.Application").CreateInstance(fullNamei); + } + string fullName = string.Join('.', currentAssemblyName, name); + return Assembly.Load(currentAssemblyName).CreateInstance(fullName); } /// @@ -78,13 +99,72 @@ namespace Ewide.Core.Util /// XML名称 public void WriteXML(Type type, object data, string className) { - - string writePath = path + className + ".xml"; + string writePath = CurrentDirPath(className); using (FileStream fs = new FileStream(writePath, FileMode.OpenOrCreate)) { XmlSerializer ser = new XmlSerializer(type); ser.Serialize(fs, data); } } + /// + /// 要操作的文件夹目录 + /// + /// + private string CurrentDirPath() + { + // 在for 循环里, 不用每次都读取 下面的代码 + if (DirPath != null) + return DirPath; + + string targetPath = String.Empty; + string appConfigPath = App.Configuration["WriteXmlPath:ReadSelect"] == "SeedData" ? App.Configuration["WriteXmlPath:SeedData"] : App.Configuration["WriteXmlPath:DataBase"]; + string path = Directory.GetCurrentDirectory(); + DirectoryInfo dirInfo = new DirectoryInfo(path); + foreach (DirectoryInfo item in dirInfo.Parent.GetDirectories()) + { + if (item.Name == currentAssemblyName) + { + targetPath = item.GetDirectories(appConfigPath).FirstOrDefault().FullName; + break; + } + } + DirPath = targetPath; + return targetPath; + } + /// + /// 根据类名 创建一个XML文件 写入数据 + /// + /// + /// + private string CurrentDirPath(string className) + { + string path = CurrentDirPath(); + return string.Join(@"\", path, className + ".xml"); + } + /// + /// 写入种子数据 --不常用 + /// + public void WriteDataSeed() + { + Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name.Contains("Ewide.Core")); + Type[] types = assembly.GetTypes(); + foreach (Type type in types) + { + //if (type.Name.EndsWith("SeedData")) + if(type.Name.EndsWith("SysEmpExtOrgPos")) + { + object obHelper = Activator.CreateInstance(type); + MethodInfo methodinfo = type.GetMethod("HasData"); + object objs = methodinfo.Invoke(obHelper, new object[] { null, null }); + Type objType = objs.GetType(); + //if (objType.Name == "SysEmpExtOrgPos[]" || objType.Name == "SysConfig[]" || objType.Name == "SysDictData[]" || objType.Name == "SysDictType[]" || objType.Name == "SysEmpPos[]" || objType.Name == "SysEmp[]" || objType.Name == "SysMenu[]" || objType.Name == "SysOrg[]" || objType.Name == "SysPos[]" || objType.Name == "SysRole[]" || objType.Name == "SysTenant[]" || objType.Name == "SysTimer[]" || objType.Name == "SysUser[]") + if (objType.Name == "SysEmpExtOrgPos[]") + { + string className = objType.Name.Remove(objType.Name.Length - 2, 2); + WriteXML(objType, objs, className); + } + } + } + } } } diff --git a/Api/Ewide.Core/applicationconfig.json b/Api/Ewide.Core/applicationconfig.json index 13a369b..54c8b9b 100644 --- a/Api/Ewide.Core/applicationconfig.json +++ b/Api/Ewide.Core/applicationconfig.json @@ -22,6 +22,11 @@ "ExpiredTime": 1440, // 过期时间,long 类型,单位分钟,默认20分钟 "ClockSkew": 5 // 过期时间容错值,long 类型,单位秒,默认5秒 }, + "WriteXmlPath": { + "SeedData": "SeedDataXml", + "DataBase": "DataBaseXml", + "ReadSelect": "SeedData" // SeedData 和 DataBase 两个中间选 + }, "Cache": { "CacheType": "MemoryCache", // RedisCache "RedisConnectionString": "127.0.0.1:6379,password=,defaultDatabase=2" diff --git a/Api/Ewide.EntityFramework.Core/DbContexts/DefaultDbContext.cs b/Api/Ewide.EntityFramework.Core/DbContexts/DefaultDbContext.cs index 58d0477..e7b4403 100644 --- a/Api/Ewide.EntityFramework.Core/DbContexts/DefaultDbContext.cs +++ b/Api/Ewide.EntityFramework.Core/DbContexts/DefaultDbContext.cs @@ -179,7 +179,10 @@ namespace Ewide.EntityFramework.Core data.Add(obj); } } - modelBuilder.Entity(item.Key).HasData(data); ; + if (data.Count>0) + { + modelBuilder.Entity(item.Key).HasData(data); + } } } } diff --git a/Api/Ewide.EntityFramework.Core/dbsettings.Development.json b/Api/Ewide.EntityFramework.Core/dbsettings.Development.json index 61f8830..ef06ef9 100644 --- a/Api/Ewide.EntityFramework.Core/dbsettings.Development.json +++ b/Api/Ewide.EntityFramework.Core/dbsettings.Development.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "DefaultConnection": "Data Source=localhost;Port=3306;Database=ewide;User ID=root;Password=a45683926;pooling=true;sslmode=none;CharSet=utf8;" + "DefaultConnection": "Data Source=localhost;Port=3306;Database=ewide;User ID=root;Password=root;pooling=true;sslmode=none;CharSet=utf8;" } } \ No newline at end of file