| | |
| | | using Furion; |
| | | using Furion.DatabaseAccessor; |
| | | using Furion.DistributedIDGenerator; |
| | | using Furion.FriendlyException; |
| | | using Mapster; |
| | | using Microsoft.EntityFrameworkCore; |
| | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | | using Microsoft.EntityFrameworkCore.Diagnostics; |
| | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | using Microsoft.Extensions.Configuration; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Linq.Expressions; |
| | | using System.Text; |
| | | using System.Threading; |
| | | using System.Threading.Tasks; |
| | | |
| | | namespace FlexJobApi.Core |
| | | { |
| | | /// <summary> |
| | | /// 数据库工具 |
| | | /// </summary> |
| | | public static class DbUtils |
| | | { |
| | | /// <summary> |
| | | /// 删除数据 |
| | | /// </summary> |
| | | /// <typeparam name="TEntity"></typeparam> |
| | | /// <param name="request"></param> |
| | | /// <param name="query"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public static async Task<int> DeleteData<TEntity>(this DeleteDataCommand request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null, CancellationToken cancellationToken = default) |
| | | where TEntity : CommonEntity, new() |
| | | { |
| | | var rep = Db.GetRepository<TEntity>(); |
| | | var q = rep.AsQueryable(); |
| | | if (query != null) q = query(q); |
| | | var entities = await q |
| | | .Where(it => request.Ids.Contains(it.Id)) |
| | | .ToListAsync(cancellationToken); |
| | | return entities.Any() |
| | | ? await rep.DeleteNowAsync(entities, cancellationToken) |
| | | : 0; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 保存数据 |
| | | /// </summary> |
| | | /// <typeparam name="TEntity"></typeparam> |
| | | /// <typeparam name="TRequest"></typeparam> |
| | | /// <param name="request"></param> |
| | | /// <param name="checkExist"></param> |
| | | /// <param name="cancellationToken"></param> |
| | | /// <returns></returns> |
| | | public static async Task<Guid> SaveData<TEntity, TRequest>(this TRequest request, Func<IQueryable<TEntity>, TEntity, TRequest, bool> checkExist = null, CancellationToken cancellationToken = default) |
| | | where TEntity : CommonEntity, new() |
| | | where TRequest : SaveDataCommand, new() |
| | | { |
| | | var xmlDoc = await XmlDocUtils.GetXmlDocAsync(); |
| | | var summary = await typeof(TEntity).GetSummary(xmlDoc); |
| | | var rep = Db.GetRepository<TEntity>(); |
| | | if (request.Id.HasValue) |
| | | { |
| | | var entity = await rep.AsQueryable().FirstOrDefaultAsync(it => it.Id == request.Id, cancellationToken); |
| | | if (entity == null) throw Oops.Oh(EnumErrorCodeType.s404, $"该{summary ?? "信息"}"); |
| | | if (checkExist != null && checkExist(rep.AsQueryable().AsNoTracking(), entity, request)) throw Oops.Oh(EnumErrorCodeType.s405, $"该{summary ?? "信息"}"); |
| | | request.Adapt(entity); |
| | | await rep.UpdateAsync(entity); |
| | | return entity.Id; |
| | | } |
| | | else |
| | | { |
| | | var entity = new TEntity(); |
| | | if (checkExist != null && checkExist(rep.AsQueryable().AsNoTracking(), entity, request)) throw Oops.Oh(EnumErrorCodeType.s405, $"该{summary ?? "信息"}"); |
| | | request.Adapt(entity); |
| | | await rep.InsertAsync(entity); |
| | | return entity.Id; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 生成实体 |
| | | /// </summary> |
| | |
| | | modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda); |
| | | } |
| | | } |
| | | |
| | | Console.WriteLine("数据库链接地址:" + App.Configuration.GetConnectionString("FlexJobApi")); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 数据变更事件 |
| | | /// </summary> |
| | | /// <param name="eventData"></param> |
| | | /// <param name="result"></param> |
| | | public static void SavingChangesEvent(DbContextEventData eventData, InterceptionResult<int> result) |
| | | { |
| | | // 获取当前事件对应上下文 |