using Ewide.Application.Entity; 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; using Furion.DynamicApiController; 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; } } /// /// 项目管理相关服务 /// [ApiDescriptionSettings(Name = "HouseProjectInfo", Order = 170)] public class HouseProjectInfoService : IHouseProjectInfoService, ITransient, IDynamicApiController { private readonly IRepository _houseProjectInfoRep; public HouseProjectInfoService(IRepository houseProjectInfoRep) { _houseProjectInfoRep = houseProjectInfoRep; } /// /// 添加项目 /// /// /// [HttpPost("/houseProjectInfo/add")] public async Task AddProject(AddProjectInput input) { await _houseProjectInfoRep.InsertNowAsync(input.Adapt()); } /// /// 删除项目 /// /// /// [HttpPost("/houseProjectInfo/delete")] public async Task DeleteProject(DeleteProjectInput input) { var project = _houseProjectInfoRep.FirstOrDefault(p => p.Id == input.Id); await project.DeleteNowAsync(); } /// /// 编辑项目 /// /// /// [HttpPost("/houseProjectInfo/edit")] public async Task UpdateProject(UpdateProjectInput input) { var project = input.Adapt(); await project.UpdateExcludeAsync(new[] { nameof(BsHouseProjectInfo.AreaId) }, true); } /// /// 通过ID获取项目 /// /// /// [HttpGet("/houseProjectInfo/detailById")] public async Task GetProjectById([Required] string id) { return await _houseProjectInfoRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == id); } /// /// 分页查询 /// /// /// [HttpPost("/houseProjectInfo/page")] public async Task QueryProjectPageList([FromBody] HouseProjectInfoInput input) { var areaID = !string.IsNullOrEmpty(input.AreaId); var areaCodeRep = Db.GetRepository(); var projects = await _houseProjectInfoRep.DetachedEntities .Join(areaCodeRep.DetachedEntities, p => p.AreaId, a => a.Code, (p, a) => new { p, AreaName = a.Name }) .Where(areaID, x => x.p.AreaId == input.AreaId) .Select(x => new { x.p.Id, x.p.Name, x.p.Note, x.p.Sort, x.p.AreaId, x.AreaName, x.p.Type }.Adapt()).ToPagedListAsync(input.PageNo, input.PageSize); return XnPageResult.PageResult(projects); } /// /// /// /// /// [HttpGet("/houseProjectInfo/detail")] public async Task GetProject([FromQuery] QueryProjectInput input) { var user = await _houseProjectInfoRep.DetachedEntities.FirstOrDefaultAsync(p => p.Id == input.Id); var userDto = user.Adapt(); return userDto; } } }