Initial_commit_SecMPS_v2
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
//using VolPro.Core.Dapper;
|
||||
//using VolPro.Core.DBManager;
|
||||
//using VolPro.Core.EFDbContext;
|
||||
//using VolPro.Core.Extensions;
|
||||
//using VolPro.Entity.DomainModels;
|
||||
//using Microsoft.AspNetCore.Mvc;
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Newtonsoft.Json;
|
||||
//using System;
|
||||
//using System.Data;
|
||||
//using System.Data.SqlClient;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace VolPro.Core.BaseProvider.DictionaryComponent
|
||||
//{
|
||||
// /// <summary>
|
||||
// /// 组件视图,参照:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1
|
||||
// /// 与Controller命名一样必须以ViewComponent结尾
|
||||
// /// </summary>
|
||||
// public class DictionaryViewComponent : ViewComponent
|
||||
// {
|
||||
// public async Task<IViewComponentResult> InvokeAsync(string dropDownIds)
|
||||
// {
|
||||
// if (string.IsNullOrEmpty(dropDownIds))
|
||||
// return null;
|
||||
|
||||
// string[] dicNos = dropDownIds.Split(',');
|
||||
// StringBuilder stringBuilder = new StringBuilder();
|
||||
// SysDbContext context = DBServerProvider.GetEFDbContext();
|
||||
|
||||
|
||||
// var dicData = await context.Queryable<Sys_Dictionary>()
|
||||
// .LeftJoin<Sys_DictionaryList>((o, cus) => o.Dic_ID == cus.Dic_ID)
|
||||
// .Where(d => dicNos.Contains(d.DicNo))
|
||||
// .Select((d, list) => new { list.DicValue, list.DicName, d.Config, d.DbSql, list.OrderNo, d.DicNo })
|
||||
// .ToListAsync();
|
||||
// foreach (var item in dicData.GroupBy(x => x.DicNo))
|
||||
// {
|
||||
// stringBuilder.AppendLine($" var optionConfig{item.Key} = {item.Select(x => x.Config).FirstOrDefault()}");
|
||||
|
||||
// string dbSql = item.Select(s => s.DbSql).FirstOrDefault();
|
||||
|
||||
// stringBuilder.AppendLine($@" var dataSource{item.Key} = {
|
||||
// (!string.IsNullOrEmpty(dbSql)
|
||||
// ? DBServerProvider.GetSqlDapper().QueryList<object>(dbSql, null).Serialize()
|
||||
// : item.OrderByDescending(o => o.OrderNo).
|
||||
// Select(s => new { s.DicName, s.DicValue }).ToList()
|
||||
// .Serialize())
|
||||
// }.convertToValueText(optionConfig{item.Key})");
|
||||
// stringBuilder.AppendLine($" optionConfig{item.Key}.data = dataSource{item.Key};");
|
||||
// }
|
||||
// ViewBag.Dic = stringBuilder.ToString();
|
||||
// return View("~/Views/Shared/Dictionary.cshtml");
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
328
api_sqlsugar/VolPro.Core/BaseProvider/IRepository.cs
Normal file
328
api_sqlsugar/VolPro.Core/BaseProvider/IRepository.cs
Normal file
@@ -0,0 +1,328 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using VolPro.Core.Dapper;
|
||||
using VolPro.Core.EFDbContext;
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Utilities;
|
||||
using VolPro.Entity.SystemModels;
|
||||
|
||||
namespace VolPro.Core.BaseProvider
|
||||
{
|
||||
public interface IRepository<TEntity> where TEntity : BaseEntity
|
||||
{
|
||||
|
||||
BaseDbContext BaseDbContext { get; }
|
||||
|
||||
/// <summary>
|
||||
/// EF DBContext
|
||||
/// </summary>
|
||||
ISqlSugarClient DbContext { get; }
|
||||
|
||||
ISqlSugarClient SqlSugarClient { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 执行事务。将在执行的方法带入Action
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent DbContextBeginTransaction(Func<WebResponseContent> action);
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="where">查询条件</param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
List<TEntity> Find(Expression<Func<TEntity, bool>> where, bool filterDeleted = true);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="orderBySelector">排序字段,数据格式如:
|
||||
/// orderBy = x => new Dictionary<object, bool>() {
|
||||
/// { x.BalconyName,QueryOrderBy.Asc},
|
||||
/// { x.TranCorpCode1,QueryOrderBy.Desc}
|
||||
/// };
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
TEntity FindFirst(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true);
|
||||
|
||||
ISugarQueryable<TEntity> WhereIF([NotNull] Expression<Func<TEntity, object>> field, string value, LinqExpressionType linqExpression = LinqExpressionType.Equal);
|
||||
|
||||
/// <summary>
|
||||
/// if判断查询
|
||||
/// </summary>
|
||||
/// 查询示例,value不为null时参与条件查询
|
||||
/// string value = null;
|
||||
/// repository.WhereIF(value!=null,x=>x.Creator==value);
|
||||
/// <param name="checkCondition"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
ISugarQueryable<TEntity> WhereIF(bool checkCondition, Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// if判断查询
|
||||
/// </summary>
|
||||
/// 查询示例,value不为null时参与条件查询
|
||||
/// string value = null;
|
||||
/// repository.WhereIF<Sys_User>(value!=null,x=>x.Creator==value);
|
||||
/// <param name="checkCondition"></param>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
ISugarQueryable<T> WhereIF<T>(bool checkCondition, Expression<Func<T, bool>> predicate) where T : class, new();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate">where条件</param>
|
||||
/// <param name="orderBy">排序字段,数据格式如:
|
||||
/// orderBy = x => new Dictionary<object, bool>() {
|
||||
/// { x.BalconyName,QueryOrderBy.Asc},
|
||||
/// { x.TranCorpCode1,QueryOrderBy.Desc}
|
||||
/// };
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
ISugarQueryable<TEntity> FindAsIQueryable(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, Dictionary<object, QueryOrderBy>>> orderBy = null, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
/// 通过条件查询数据
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate">查询条件</param>
|
||||
/// <param name="selector">返回类型如:Find(x => x.UserName == loginInfo.userName, p => new { uname = p.UserName });</param>
|
||||
/// <returns></returns>
|
||||
List<T> Find<T>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, T>> selector, bool filterDeleted = true);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据条件,返回查询的类
|
||||
/// </summary>
|
||||
/// <typeparam name="TFind"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
List<TFind> Find<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class,new();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TFind"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<TFind> FindAsyncFirst<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class, new();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
///<param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<TEntity> FindAsyncFirst(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TFind"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<List<TFind>> FindAsync<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class, new();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
///<param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<TEntity> FindFirstAsync(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<List<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="selector"></param>
|
||||
///<param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<List<T>> FindAsync<T>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, T>> selector, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="selector"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<T> FindFirstAsync<T>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, T>> selector, bool filterDeleted = true);
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
bool Exists(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TExists"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
bool Exists<TExists>(Expression<Func<TExists, bool>> predicate, bool filterDeleted = true) where TExists : class, new();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TExists"></typeparam>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="filterDeleted">是否过滤逻辑删除的数据,默认过</param>
|
||||
/// <returns></returns>
|
||||
Task<bool> ExistsAsync<TExists>(Expression<Func<TExists, bool>> predicate, bool filterDeleted = true) where TExists : class, new();
|
||||
|
||||
ISugarQueryable<TEntity> Include<TProperty>(Expression<Func<TEntity, TProperty>> incluedProperty) where TProperty : new();
|
||||
|
||||
|
||||
|
||||
|
||||
ISugarQueryable<TFind> IQueryablePage<TFind>(int pageIndex, int pagesize, out int rowcount, Expression<Func<TFind, bool>> predicate, Expression<Func<TEntity, Dictionary<object, QueryOrderBy>>> orderBy, bool returnRowCount = true) where TFind : class, new();
|
||||
|
||||
|
||||
ISugarQueryable<TEntity> IQueryablePage(ISugarQueryable<TEntity> queryable, int pageIndex, int pagesize, out int rowcount, Dictionary<string, QueryOrderBy> orderBy, bool returnRowCount = true);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="properties">指定更新字段:x=>new {x.Name,x.Enable}</param>
|
||||
/// <param name="saveChanges">是否保存</param>
|
||||
/// <returns></returns>
|
||||
|
||||
int Update(TEntity entity, Expression<Func<TEntity, object>> properties, bool saveChanges = false);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="properties">指定更新字段:x=>new {x.Name,x.Enable}</param>
|
||||
/// <param name="saveChanges">是否保存</param>
|
||||
/// <returns></returns>
|
||||
int Update<TSource>(TSource entity, Expression<Func<TSource, object>> properties, bool saveChanges = false) where TSource : class, new();
|
||||
|
||||
int Update<TSource>(TSource entity, bool saveChanges = false) where TSource : class, new();
|
||||
|
||||
int Update<TSource>(TSource entity, string[] properties, bool saveChanges = false) where TSource : class, new();
|
||||
|
||||
int UpdateRange<TSource>(IEnumerable<TSource> entities, bool saveChanges = false) where TSource : class, new();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="properties">指定更新字段:x=>new {x.Name,x.Enable}</param>
|
||||
/// <param name="saveChanges">是否保存</param>
|
||||
/// <returns></returns>
|
||||
int UpdateRange<TSource>(IEnumerable<TSource> models, Expression<Func<TSource, object>> properties, bool saveChanges = false) where TSource : class, new();
|
||||
|
||||
int UpdateRange<TSource>(IEnumerable<TSource> entities, string[] properties, bool saveChanges = false) where TSource : class, new();
|
||||
|
||||
|
||||
/// <summary>
|
||||
///修改时同时对明细的添加、删除、修改
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="updateDetail">是否修改明细</param>
|
||||
/// <param name="delNotExist">是否删除明细不存在的数据</param>
|
||||
/// <param name="updateMainFields">主表指定修改字段</param>
|
||||
/// <param name="updateDetailFields">明细指定修改字段</param>
|
||||
/// <param name="saveChange">是否保存</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent UpdateRange<Detail>(TEntity entity,
|
||||
bool updateDetail = false,
|
||||
bool delNotExist = false,
|
||||
Expression<Func<TEntity, object>> updateMainFields = null,
|
||||
Expression<Func<Detail, object>> updateDetailFields = null,
|
||||
bool saveChange = false) where Detail : class, new();
|
||||
|
||||
void Delete(TEntity model, bool saveChanges = false);
|
||||
void Delete<T>(T model, bool saveChanges = false) where T : class, new();
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="keys"></param>
|
||||
/// <param name="delList">是否将子表的数据也删除</param>
|
||||
/// <param name="saveChange">是否执行保存数据库</param>
|
||||
/// <returns></returns>
|
||||
int DeleteWithKeys(object[] keys, bool saveChange = true);
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据并设置自增
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
void AddWithSetIdentity(TEntity entity);
|
||||
|
||||
void AddWithSetIdentity<T>(T entity) where T : class, new();
|
||||
|
||||
void Add(TEntity entities, bool SaveChanges = false);
|
||||
|
||||
void Add<T>(T entities, bool saveChanges = false) where T : class, new();
|
||||
|
||||
// void AddRange(IEnumerable<TEntity> entities, bool SaveChanges = false);
|
||||
|
||||
|
||||
|
||||
void AddRange<T>(List<T> entities, bool saveChanges = false)
|
||||
where T : class, new();
|
||||
|
||||
|
||||
int SaveChanges();
|
||||
|
||||
Task<int> SaveChangesAsync();
|
||||
|
||||
|
||||
|
||||
int ExecuteSqlCommand(string sql, params SugarParameter[] SugarParameters);
|
||||
|
||||
List<TEntity> FromSql(string sql, params SugarParameter[] SugarParameters);
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql
|
||||
/// 使用方式 FormattableString sql=$"select * from xx where name ={xx} and pwd={xx1} ",
|
||||
/// FromSqlInterpolated内部处理sql注入的问题,直接在{xx}写对应的值即可
|
||||
/// 注意:sql必须 select * 返回所有TEntity字段,
|
||||
/// </summary>
|
||||
/// <param name="formattableString"></param>
|
||||
/// <returns></returns>
|
||||
// ISugarQueryable<TEntity> FromSqlInterpolated([System.Diagnostics.CodeAnalysis.NotNull] FormattableString sql);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 取消上下文跟踪(2021.08.22)
|
||||
/// 更新报错时,请调用此方法:The instance of entity type 'XXX' cannot be tracked because another instance with the same key value for {'XX'} is already being tracked.
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
void Detached(TEntity entity);
|
||||
void DetachedRange(IEnumerable<TEntity> entities);
|
||||
}
|
||||
}
|
||||
189
api_sqlsugar/VolPro.Core/BaseProvider/IService.cs
Normal file
189
api_sqlsugar/VolPro.Core/BaseProvider/IService.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using VolPro.Core.CacheManager;
|
||||
using VolPro.Core.Utilities;
|
||||
using VolPro.Core.WorkFlow;
|
||||
using VolPro.Entity.DomainModels;
|
||||
using VolPro.Entity.SystemModels;
|
||||
|
||||
namespace VolPro.Core.BaseProvider
|
||||
{
|
||||
public interface IService<T> where T : BaseEntity
|
||||
{
|
||||
|
||||
CacheManager.ICacheService CacheContext { get; }
|
||||
Microsoft.AspNetCore.Http.HttpContext Context { get; }
|
||||
string WorkFlowTableName { get; set; }
|
||||
|
||||
ISugarQueryable<T> FindAsIQueryable(Expression<Func<T, bool>> predicate, bool filterDeleted = true);
|
||||
/// <summary>
|
||||
/// 将前端table的查询条件转换为查询ISugarQueryable
|
||||
/// </summary>
|
||||
/// <param name="options">前端查询参数</param>
|
||||
/// <param name="useTenancy">是否使用数据隔离</param>
|
||||
/// <returns></returns>
|
||||
ISugarQueryable<T> FindAsIQueryable(PageDataOptions options, bool useTenancy = true);
|
||||
|
||||
/// <summary>
|
||||
/// 查询
|
||||
/// </summary>
|
||||
/// <param name="pageData"></param>
|
||||
/// <returns></returns>
|
||||
PageGridData<T> GetPageData(PageDataOptions pageData);
|
||||
|
||||
object GetDetailPage(PageDataOptions pageData);
|
||||
|
||||
WebResponseContent Upload(List<IFormFile> files);
|
||||
|
||||
WebResponseContent DownLoadTemplate();
|
||||
|
||||
WebResponseContent Import(List<IFormFile> files);
|
||||
/// <summary>
|
||||
/// 导出
|
||||
/// </summary>
|
||||
/// <param name="pageData"></param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent Export(PageDataOptions pageData);
|
||||
|
||||
/// <summary>
|
||||
/// 新增
|
||||
/// </summary>
|
||||
/// <param name="saveDataModel">主表与子表的数据</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent Add(SaveModel saveDataModel);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="entity">保存的实体</param>
|
||||
/// <param name="validationEntity">是否对实体进行校验</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent AddEntity(T entity, bool validationEntity = true);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TDetail"></typeparam>
|
||||
/// <param name="entity">保存的实体</param>
|
||||
/// <param name="list">保存的明细</param>
|
||||
/// <param name="validationEntity">是否对实体进行校验</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent Add<TDetail>(T entity, List<TDetail> list = null, bool validationEntity = true) where TDetail : class, new();
|
||||
/// <summary>
|
||||
/// 编辑
|
||||
/// </summary>
|
||||
/// <param name="saveDataModel">主表与子表的数据</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent Update(SaveModel saveDataModel);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除数据
|
||||
/// </summary>
|
||||
/// <param name="keys">删除的主键</param>
|
||||
/// <param name="delList">是否删除对应明细(默认会删除明细)</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent Del(object[] keys, bool delList = true);
|
||||
|
||||
WebResponseContent Audit(object[] id, int? auditStatus, string auditReason);
|
||||
|
||||
/// <summary>
|
||||
/// 撤销审批
|
||||
/// </summary>
|
||||
/// <param name="keys"></param>
|
||||
/// <param name="status">1=撤销,-1终止</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent CancelAudit(AntiData antiData, int status);
|
||||
|
||||
/// <summary>
|
||||
/// 催办
|
||||
/// </summary>
|
||||
/// <param name="keys"></param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent UrgentAudit(object[] keys);
|
||||
/// <summary>
|
||||
/// 反审
|
||||
/// </summary>
|
||||
/// <param name="antiData"></param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent AntiAudit(AntiData antiData);
|
||||
|
||||
/// <summary>
|
||||
/// 重新生成流程或者回退流程
|
||||
/// </summary>
|
||||
/// <param name="id">表数据id</param>
|
||||
/// <param name="msg">重写流程的日志信息</param>
|
||||
/// <param name="flowWriteState">重新开始或回退到上一级节点</param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent RestartWorkFlowAudit(object id, string msg, FlowWriteState flowWriteState);
|
||||
/// <summary>
|
||||
/// 提交审批数据 2023.11.12
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="msg"></param>
|
||||
/// <param name="flowWriteState"></param>
|
||||
/// <returns></returns>
|
||||
WebResponseContent SubmitWorkFlowAudit(object[] ids);
|
||||
bool AddProcese(T entity);
|
||||
|
||||
(string, T, bool) ApiValidate(string bizContent, Expression<Func<T, object>> expression = null);
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput"></typeparam>
|
||||
/// <param name="bizContent"></param>
|
||||
/// <param name="expression">对指属性验证格式如:x=>new { x.UserName,x.Value }</param>
|
||||
/// <returns>(string,TInput, bool) string:返回验证消息,TInput:bizContent序列化后的对象,bool:验证是否通过</returns>
|
||||
(string, TInput, bool) ApiValidateInput<TInput>(string bizContent, Expression<Func<TInput, object>> expression);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput"></typeparam>
|
||||
/// <param name="bizContent"></param>
|
||||
/// <param name="expression">对指属性验证格式如:x=>new { x.UserName,x.Value }</param>
|
||||
/// <param name="validateExpression">对指定的字段只做合法性判断比如长度是是否超长</param>
|
||||
/// <returns>(string,TInput, bool) string:返回验证消息,TInput:bizContent序列化后的对象,bool:验证是否通过</returns>
|
||||
(string, TInput, bool) ApiValidateInput<TInput>(string bizContent, Expression<Func<TInput, object>> expression, Expression<Func<TInput, object>> validateExpression);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 将数据源映射到新的数据中,List<TSource>映射到List<TResult>或TSource映射到TResult
|
||||
/// 目前只支持Dictionary或实体类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource"></typeparam>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="resultExpression">只映射返回对象的指定字段</param>
|
||||
/// <param name="sourceExpression">只映射数据源对象的指定字段</param>
|
||||
/// 过滤条件表达式调用方式:List表达式x => new { x[0].MenuName, x[0].Menu_Id},表示指定映射MenuName,Menu_Id字段
|
||||
/// List<Sys_Menu> list = new List<Sys_Menu>();
|
||||
/// list.MapToObject<List<Sys_Menu>, List<Sys_Menu>>(x => new { x[0].MenuName, x[0].Menu_Id}, null);
|
||||
///
|
||||
///过滤条件表达式调用方式:实体表达式x => new { x.MenuName, x.Menu_Id},表示指定映射MenuName,Menu_Id字段
|
||||
/// Sys_Menu sysMenu = new Sys_Menu();
|
||||
/// sysMenu.MapToObject<Sys_Menu, Sys_Menu>(x => new { x.MenuName, x.Menu_Id}, null);
|
||||
/// <returns></returns>
|
||||
TResult MapToEntity<TSource, TResult>(TSource source, Expression<Func<TResult, object>> resultExpression,
|
||||
Expression<Func<TSource, object>> sourceExpression = null) where TResult : class;
|
||||
|
||||
/// <summary>
|
||||
/// 将一个实体的赋到另一个实体上,应用场景:
|
||||
/// 两个实体,a a1= new a();b b1= new b(); a1.P=b1.P; a1.Name=b1.Name;
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource"></typeparam>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="result"></param>
|
||||
/// <param name="expression">指定对需要的字段赋值,格式x=>new {x.Name,x.P},返回的结果只会对Name与P赋值</param>
|
||||
void MapValueToEntity<TSource, TResult>(TSource source, TResult result, Expression<Func<TResult, object>> expression = null) where TResult : class;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
656
api_sqlsugar/VolPro.Core/BaseProvider/RepositoryBase.cs
Normal file
656
api_sqlsugar/VolPro.Core/BaseProvider/RepositoryBase.cs
Normal file
@@ -0,0 +1,656 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.DirectoryServices;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using VolPro.Core.Configuration;
|
||||
using VolPro.Core.Dapper;
|
||||
using VolPro.Core.DBManager;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
using VolPro.Core.EFDbContext;
|
||||
using VolPro.Core.Enums;
|
||||
using VolPro.Core.Extensions;
|
||||
using VolPro.Core.Services;
|
||||
using VolPro.Core.Utilities;
|
||||
using VolPro.Entity;
|
||||
using VolPro.Entity.SystemModels;
|
||||
|
||||
namespace VolPro.Core.BaseProvider
|
||||
{
|
||||
public abstract class RepositoryBase<TEntity> where TEntity : BaseEntity, new()
|
||||
{
|
||||
public RepositoryBase(BaseDbContext dbContext)
|
||||
{
|
||||
this.DefaultDbContext = dbContext;
|
||||
}
|
||||
|
||||
private BaseDbContext DefaultDbContext { get; set; }
|
||||
public BaseDbContext BaseDbContext
|
||||
{
|
||||
get
|
||||
{
|
||||
return DefaultDbContext;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual ISqlSugarClient DbContext
|
||||
{
|
||||
get { return DefaultDbContext.SqlSugarClient; }
|
||||
}
|
||||
|
||||
public virtual ISqlSugarClient SqlSugarClient
|
||||
{
|
||||
get
|
||||
{
|
||||
return DefaultDbContext.SqlSugarClient;
|
||||
}
|
||||
}
|
||||
private ISugarQueryable<TEntity> DBSet
|
||||
{
|
||||
get { return BaseDbContext.Set<TEntity>(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行事务
|
||||
/// </summary>
|
||||
/// <param name="action">如果返回false则回滚事务(可自行定义规则)</param>
|
||||
/// <returns></returns>
|
||||
public virtual WebResponseContent DbContextBeginTransaction(Func<WebResponseContent> action)
|
||||
{
|
||||
if (DbContext.Ado.IsAnyTran())
|
||||
{
|
||||
return action();
|
||||
}
|
||||
WebResponseContent webResponse = new WebResponseContent();
|
||||
try
|
||||
{
|
||||
DbContext.Ado.BeginTran();
|
||||
|
||||
webResponse = action();
|
||||
if (webResponse.Status)
|
||||
{
|
||||
DbContext.Ado.CommitTran();
|
||||
}
|
||||
else
|
||||
{
|
||||
DbContext.Ado.RollbackTran();
|
||||
|
||||
}
|
||||
return webResponse;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DbContext.Ado.RollbackTran();
|
||||
string message = ex.Message + ex?.InnerException + ex?.StackTrace;
|
||||
if (HttpContext.Current.GetService<Microsoft.AspNetCore.Hosting.IWebHostEnvironment>().IsDevelopment())
|
||||
{
|
||||
return webResponse.Error(message);
|
||||
}
|
||||
Logger.Error(message);
|
||||
return webResponse.Error("处理异常");
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool Exists<TExists>(Expression<Func<TExists, bool>> predicate, bool filterDeleted = true) where TExists : class, new()
|
||||
{
|
||||
return BaseDbContext.Set<TExists>(filterDeleted).Any(predicate);
|
||||
}
|
||||
|
||||
public virtual Task<bool> ExistsAsync<TExists>(Expression<Func<TExists, bool>> predicate, bool filterDeleted = true) where TExists : class, new()
|
||||
{
|
||||
return BaseDbContext.Set<TExists>(filterDeleted).AnyAsync(predicate);
|
||||
}
|
||||
|
||||
public virtual bool Exists(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
var query = BaseDbContext.Set<TEntity>(filterDeleted);
|
||||
if (typeof(TEntity).GetSugarSplitTable() != null)
|
||||
{
|
||||
return query.SplitTable().Any(predicate);
|
||||
}
|
||||
return query.Any(predicate);
|
||||
}
|
||||
|
||||
public virtual Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
return BaseDbContext.Set<TEntity>(filterDeleted).AnyAsync(predicate);
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询字段不为null或者为空
|
||||
/// </summary>
|
||||
/// <param name="field">x=>new {x.字段}</param>
|
||||
/// <param name="value">查询的类</param>
|
||||
/// <param name="linqExpression">查询类型</param>
|
||||
/// <returns></returns>
|
||||
public virtual ISugarQueryable<TEntity> WhereIF([NotNull] Expression<Func<TEntity, object>> field, string value, LinqExpressionType linqExpression = LinqExpressionType.Equal)
|
||||
{
|
||||
return BaseDbContext.Set<TEntity>().WhereNotEmpty(field, value, linqExpression);
|
||||
}
|
||||
|
||||
public virtual ISugarQueryable<TEntity> WhereIF(bool checkCondition, Expression<Func<TEntity, bool>> predicate)
|
||||
{
|
||||
if (checkCondition)
|
||||
{
|
||||
return BaseDbContext.Set<TEntity>().Where(predicate);
|
||||
}
|
||||
return BaseDbContext.Set<TEntity>();
|
||||
}
|
||||
|
||||
public virtual ISugarQueryable<T> WhereIF<T>(bool checkCondition, Expression<Func<T, bool>> predicate) where T : class, new()
|
||||
{
|
||||
if (checkCondition)
|
||||
{
|
||||
return BaseDbContext.Set<T>().Where(predicate);
|
||||
}
|
||||
return BaseDbContext.Set<T>();
|
||||
}
|
||||
|
||||
|
||||
public virtual List<TFind> Find<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class, new()
|
||||
{
|
||||
return BaseDbContext.Set<TFind>(filterDeleted).Where(predicate).ToList();
|
||||
}
|
||||
|
||||
public virtual async Task<TFind> FindAsyncFirst<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class, new()
|
||||
{
|
||||
return await FindAsISugarQueryable(predicate, filterDeleted).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> FindAsyncFirst(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
return await FindAsISugarQueryable<TEntity>(predicate, filterDeleted).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<List<TFind>> FindAsync<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class, new()
|
||||
{
|
||||
return await FindAsISugarQueryable<TFind>(predicate, filterDeleted).ToListAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<List<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
return await FindAsISugarQueryable(predicate, filterDeleted).ToListAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<TEntity> FindFirstAsync(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
return await FindAsISugarQueryable(predicate, filterDeleted).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<List<T>> FindAsync<T>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, T>> selector, bool filterDeleted = true)
|
||||
{
|
||||
return await FindAsISugarQueryable(predicate, filterDeleted).Select(selector).ToListAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<T> FindFirstAsync<T>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, T>> selector, bool filterDeleted = true)
|
||||
{
|
||||
return await FindAsISugarQueryable(predicate, filterDeleted).Select(selector).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
private ISugarQueryable<TFind> FindAsISugarQueryable<TFind>(Expression<Func<TFind, bool>> predicate, bool filterDeleted = true) where TFind : class,new()
|
||||
{
|
||||
return BaseDbContext.Set<TFind>(filterDeleted).Where(predicate);
|
||||
}
|
||||
|
||||
|
||||
public virtual List<T> Find<T>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, T>> selector, bool filterDeleted = true)
|
||||
{
|
||||
return BaseDbContext.Set<TEntity>(filterDeleted).Where(predicate).Select(selector).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
/// 单表查询
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
public virtual List<TEntity> Find(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
return FindAsISugarQueryable(predicate, filterDeleted).ToList();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name=""></param>
|
||||
/// <param name="orderBy">排序字段</param>
|
||||
/// <returns></returns>
|
||||
public virtual TEntity FindFirst(Expression<Func<TEntity, bool>> predicate, bool filterDeleted = true)
|
||||
{
|
||||
return BaseDbContext.Set<TEntity>(filterDeleted).Where(predicate).FirstOrDefault();
|
||||
}
|
||||
|
||||
|
||||
public ISugarQueryable<TEntity> FindAsIQueryable(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, Dictionary<object, QueryOrderBy>>> orderBy = null, bool filterDeleted = true)
|
||||
{
|
||||
//if (orderBy != null)
|
||||
// return DbContext.Set<TEntity>().Where(predicate).GetISugarQueryableOrderBy(orderBy.GetExpressionToDic());
|
||||
return DbContext.Set<TEntity>(filterDeleted).Where(predicate);
|
||||
}
|
||||
|
||||
public ISugarQueryable<TEntity> Include<TProperty>(Expression<Func<TEntity, TProperty>> incluedProperty) where TProperty : new()
|
||||
{
|
||||
return DbContext.Set<TEntity>().Include(incluedProperty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通过条件查询返回指定列的数据(将TEntity映射到匿名或实体T)
|
||||
///var result = Sys_UserRepository.GetInstance.Find(x => x.UserName == loginInfo.userName, p => new { uname = p.UserName });
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pagesize"></param>
|
||||
/// <param name="rowcount"></param>
|
||||
/// <param name="predicate">查询条件</param>
|
||||
/// <param name="orderBySelector">多个排序字段key为字段,value为升序/降序</param>
|
||||
/// <returns></returns>
|
||||
public virtual ISugarQueryable<TFind> IQueryablePage<TFind>(int pageIndex, int pagesize, out int rowcount, Expression<Func<TFind, bool>> predicate, Expression<Func<TEntity, Dictionary<object, QueryOrderBy>>> orderBy, bool returnRowCount = true) where TFind : class, new()
|
||||
{
|
||||
pageIndex = pageIndex <= 0 ? 1 : pageIndex;
|
||||
pagesize = pagesize <= 0 ? 10 : pagesize;
|
||||
if (predicate == null)
|
||||
{
|
||||
predicate = x => 1 == 1;
|
||||
}
|
||||
var _db = DbContext.Set<TFind>();
|
||||
rowcount = returnRowCount ? _db.Count(predicate) : 0;
|
||||
return DbContext.Set<TFind>().Where(predicate)
|
||||
.GetISugarQueryableOrderBy(orderBy.GetExpressionToDic())
|
||||
.Skip((pageIndex - 1) * pagesize)
|
||||
.Take(pagesize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页排序
|
||||
/// </summary>
|
||||
/// <param name="queryable"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pagesize"></param>
|
||||
/// <param name="rowcount"></param>
|
||||
/// <param name="orderBy"></param>
|
||||
/// <returns></returns>
|
||||
public virtual ISugarQueryable<TEntity> IQueryablePage(ISugarQueryable<TEntity> queryable, int pageIndex, int pagesize, out int rowcount, Dictionary<string, QueryOrderBy> orderBy, bool returnRowCount = true)
|
||||
{
|
||||
pageIndex = pageIndex <= 0 ? 1 : pageIndex;
|
||||
pagesize = pagesize <= 0 ? 10 : pagesize;
|
||||
rowcount = returnRowCount ? queryable.Count() : 0;
|
||||
return queryable.GetISugarQueryableOrderBy<TEntity>(orderBy)
|
||||
.Skip((pageIndex - 1) * pagesize)
|
||||
.Take(pagesize);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新表数据
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="saveChanges">是否保存</param>
|
||||
/// <param name="properties">格式 Expression<Func<entityt, object>> expTree = x => new { x.字段1, x.字段2 };</param>
|
||||
public virtual int Update(TEntity entity, Expression<Func<TEntity, object>> properties, bool saveChanges = false)
|
||||
{
|
||||
return Update<TEntity>(entity, properties, saveChanges);
|
||||
}
|
||||
|
||||
public virtual int Update<TSource>(TSource entity, Expression<Func<TSource, object>> properties, bool saveChanges = false) where TSource : class, new()
|
||||
{
|
||||
return UpdateRange(new List<TSource>
|
||||
{
|
||||
entity
|
||||
}, properties, saveChanges);
|
||||
}
|
||||
|
||||
|
||||
public virtual int Update<TSource>(TSource entity, string[] properties, bool saveChanges = false) where TSource : class, new()
|
||||
{
|
||||
return UpdateRange<TSource>(new List<TSource>() { entity }, properties, saveChanges);
|
||||
}
|
||||
public virtual int Update<TSource>(TSource entity, bool saveChanges = false) where TSource : class, new()
|
||||
{
|
||||
return UpdateRange<TSource>(new List<TSource>() { entity }, new string[0], saveChanges);
|
||||
}
|
||||
public virtual int UpdateRange<TSource>(IEnumerable<TSource> entities, Expression<Func<TSource, object>> properties, bool saveChanges = false) where TSource : class, new()
|
||||
{
|
||||
return UpdateRange<TSource>(entities, properties?.GetExpressionProperty(), saveChanges);
|
||||
}
|
||||
public virtual int UpdateRange<TSource>(IEnumerable<TSource> entities, bool saveChanges = false) where TSource : class, new()
|
||||
{
|
||||
return UpdateRange<TSource>(entities, new string[0], saveChanges);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新表数据
|
||||
/// </summary>
|
||||
/// <param name="models"></param>
|
||||
/// <param name="properties">格式 Expression<Func<entityt, object>> expTree = x => new { x.字段1, x.字段2 };</param>
|
||||
public int UpdateRange<TSource>(IEnumerable<TSource> entities, string[] properties, bool saveChanges = false) where TSource : class, new()
|
||||
{
|
||||
return DbContext.UpdateRange(entities, properties, saveChanges);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="updateDetail">是否修改明细</param>
|
||||
/// <param name="delNotExist">是否删除明细不存在的数据</param>
|
||||
/// <param name="updateMainFields">主表指定修改字段</param>
|
||||
/// <param name="updateDetailFields">明细指定修改字段</param>
|
||||
/// <param name="saveChange">是否保存</param>
|
||||
/// <returns></returns>
|
||||
public virtual WebResponseContent UpdateRange<Detail>(TEntity entity,
|
||||
bool updateDetail = false,
|
||||
bool delNotExist = false,
|
||||
Expression<Func<TEntity, object>> updateMainFields = null,
|
||||
Expression<Func<Detail, object>> updateDetailFields = null,
|
||||
bool saveChange = false) where Detail : class, new()
|
||||
{
|
||||
WebResponseContent webResponse = new WebResponseContent();
|
||||
Update(entity, updateMainFields);
|
||||
string message = "";
|
||||
if (updateDetail)
|
||||
{
|
||||
PropertyInfo[] properties = typeof(TEntity).GetProperties();
|
||||
PropertyInfo detail = properties.Where(x => x.PropertyType.Name == "List`1").ToList().FirstOrDefault();
|
||||
if (detail != null)
|
||||
{
|
||||
PropertyInfo key = properties.GetKeyProperty();
|
||||
object obj = detail.GetValue(entity);
|
||||
Type detailType = typeof(TEntity).GetCustomAttribute<EntityAttribute>().DetailTable[0];
|
||||
var list = obj as List<Detail>;
|
||||
if (list.Count > 0)
|
||||
{
|
||||
message = UpdateDetail<Detail>(list, key.Name, key.GetValue(entity), updateDetailFields, delNotExist);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!saveChange) return webResponse.OK();
|
||||
|
||||
DbContext.SaveChanges();
|
||||
return webResponse.OK("修改成功,明细" + message, entity);
|
||||
}
|
||||
private string UpdateDetail<TDetail>(List<TDetail> list,
|
||||
string keyName,
|
||||
object keyValue,
|
||||
Expression<Func<TDetail, object>> updateDetailFields = null,
|
||||
bool delNotExist = false) where TDetail : class, new()
|
||||
{
|
||||
if (list == null) return "";
|
||||
PropertyInfo property = typeof(TDetail).GetKeyProperty();
|
||||
string detailKeyName = property.Name;
|
||||
var details = DbContext.Set<TDetail>();
|
||||
Expression<Func<TDetail, object>> selectExpression = detailKeyName.GetExpression<TDetail, object>();
|
||||
Expression<Func<TDetail, bool>> whereExpression = keyName.CreateExpression<TDetail>(keyValue, LinqExpressionType.Equal);
|
||||
//这里有问题, Expression<Func<TDetail, object>>会转换为查询所有字段20231020
|
||||
//List<object> detailKeys = details.Where(whereExpression).Select(selectExpression).ToList();
|
||||
|
||||
List<object> detailKeys = details.Where(whereExpression).ToList().Select(selectExpression.Compile()).ToList();
|
||||
//获取主键默认值
|
||||
//string keyDefaultVal = property.PropertyType==typeof(string)?"": property.PropertyType.Assembly.CreateInstance(property.PropertyType.FullName).ToString();
|
||||
string keyDefaultVal = "";
|
||||
if (property.PropertyType != typeof(string))
|
||||
keyDefaultVal = property.PropertyType.Assembly.CreateInstance(property.PropertyType.FullName).ToString();
|
||||
int addCount = 0;
|
||||
int editCount = 0;
|
||||
int delCount = 0;
|
||||
PropertyInfo mainKeyProperty = typeof(TDetail).GetProperty(keyName);
|
||||
|
||||
var detailKeyPro = typeof(TDetail).GetKeyProperty();
|
||||
IdWorker worker = null;
|
||||
bool stringKey = false;
|
||||
if (detailKeyPro.PropertyType == typeof(string))
|
||||
{
|
||||
stringKey = true;
|
||||
if (AppSetting.UseSnow)
|
||||
{
|
||||
worker = new IdWorker();
|
||||
}
|
||||
}
|
||||
List<TDetail> addList = new List<TDetail>();
|
||||
List<TDetail> updateList = new List<TDetail>();
|
||||
List<object> keys = new List<object>();
|
||||
list.ForEach(x =>
|
||||
{
|
||||
object val = property.GetValue(x) ?? "";
|
||||
//主键是默认值的为新增的数据
|
||||
if (val.ToString() == keyDefaultVal)
|
||||
{
|
||||
x.SetCreateDefaultVal();
|
||||
//设置主表的值,也可以不设置
|
||||
mainKeyProperty.SetValue(x, keyValue);
|
||||
if (stringKey)
|
||||
{
|
||||
if (worker != null)
|
||||
{
|
||||
detailKeyPro.SetValue(x, worker.NextId().ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
detailKeyPro.SetValue(x, Guid.NewGuid().ToString());
|
||||
}
|
||||
}
|
||||
// DbContext.Insertable(x).AddQueue();
|
||||
addList.Add(x);
|
||||
addCount++;
|
||||
}
|
||||
else//修改的数据
|
||||
{
|
||||
//获取所有修改的key,如果从数据库查来的key,不在修改中的key,则为删除的数据
|
||||
keys.Add(val);
|
||||
x.SetModifyDefaultVal();
|
||||
// Update<TDetail>(x, updateDetailFields);
|
||||
updateList.Add(x);
|
||||
// repository.DbContext.Entry<TDetail>(x).State = EntityState.Modified;
|
||||
editCount++;
|
||||
}
|
||||
});
|
||||
//删除
|
||||
if (delNotExist)
|
||||
{
|
||||
detailKeys.Where(x => !keys.Contains(x)).ToList().ForEach(d =>
|
||||
{
|
||||
delCount++;
|
||||
TDetail detail = Activator.CreateInstance<TDetail>();
|
||||
property.SetValue(detail, d);
|
||||
DbContext.Deleteable<TDetail>(detail).AddQueue();
|
||||
for (int i = 0; i < list.Count(); i++)
|
||||
{
|
||||
if (property.GetValue(list[i]) == d)
|
||||
{
|
||||
list.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
DbContext.Insertable<TDetail>(addList).ExecuteCommand();
|
||||
if (updateDetailFields == null)
|
||||
{
|
||||
DbContext.Updateable<TDetail>(updateList).AddQueue();
|
||||
}
|
||||
else
|
||||
{
|
||||
DbContext.Updateable<TDetail>(updateList).UpdateColumns(updateDetailFields.GetExpressionToArray<TDetail>()).ExecuteCommand();
|
||||
}
|
||||
return $"修改[{editCount}]条,新增[{addCount}]条,删除[{delCount}]条";
|
||||
}
|
||||
|
||||
public virtual void Delete(TEntity model, bool saveChanges = false)
|
||||
{
|
||||
if (typeof(TEntity).GetSugarSplitTable() != null)
|
||||
{
|
||||
DbContext.Deleteable(model).SplitTable().ExecuteCommand();
|
||||
return;
|
||||
}
|
||||
DbContext.Deleteable(model).AddQueue();
|
||||
if (saveChanges)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Delete<T>(T model, bool saveChanges) where T : class, new()
|
||||
{
|
||||
if (typeof(T).GetSugarSplitTable() != null)
|
||||
{
|
||||
DbContext.Deleteable(model).SplitTable().ExecuteCommand();
|
||||
return;
|
||||
}
|
||||
DbContext.Deleteable(model).AddQueue();
|
||||
if (saveChanges)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 通过主键批量删除
|
||||
/// </summary>
|
||||
/// <param name="keys">主键key</param>
|
||||
/// <param name="delList">是否连明细一起删除</param>
|
||||
/// <returns></returns>
|
||||
public virtual int DeleteWithKeys(object[] keys, bool saveChange = false)
|
||||
{
|
||||
var keyPro = typeof(TEntity).GetKeyProperty();
|
||||
List<TEntity> list = new List<TEntity>();
|
||||
foreach (var key in keys.Distinct())
|
||||
{
|
||||
TEntity entity = Activator.CreateInstance<TEntity>();
|
||||
keyPro.SetValue(entity, key.ChangeType(keyPro.PropertyType));
|
||||
list.Add(entity);
|
||||
}
|
||||
if (typeof(TEntity).GetSugarSplitTable() != null)
|
||||
{
|
||||
DbContext.Deleteable(list).SplitTable().ExecuteCommand();
|
||||
return keys.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
DbContext.Deleteable(list).AddQueue();
|
||||
}
|
||||
if (saveChange)
|
||||
{
|
||||
DbContext.SaveChanges();
|
||||
}
|
||||
return keys.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据并设置自增
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
public virtual void AddWithSetIdentity(TEntity entity)
|
||||
{
|
||||
AddWithSetIdentity<TEntity>(entity);
|
||||
}
|
||||
public virtual void AddWithSetIdentity<T>(T entity) where T : class, new()
|
||||
{
|
||||
if (typeof(T).GetSugarSplitTable() != null)
|
||||
{
|
||||
DbContext.Insertable(entity).SplitTable().ExecuteCommand();
|
||||
return;
|
||||
}
|
||||
DbContext.Insertable(entity).ExecuteReturnEntity();
|
||||
}
|
||||
public virtual void Add(TEntity entities, bool saveChanges = false)
|
||||
{
|
||||
AddRange(new List<TEntity>() { entities }, saveChanges);
|
||||
}
|
||||
|
||||
public virtual void Add<T>(T entities, bool saveChanges = false) where T : class, new()
|
||||
{
|
||||
DbContext.Insertable(entities).AddQueue();
|
||||
if (saveChanges) DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual void AddRange(List<TEntity> entities, bool saveChanges = false)
|
||||
{
|
||||
AddRange<TEntity>(entities, saveChanges);
|
||||
}
|
||||
|
||||
public virtual void AddRange<T>(List<T> entities, bool saveChanges = false) where T : class, new()
|
||||
{
|
||||
if (AppSetting.UseSnow)
|
||||
{
|
||||
PropertyInfo keyPro = typeof(T).GetKeyProperty();
|
||||
if (keyPro.PropertyType == typeof(long))
|
||||
{
|
||||
//生成雪花id
|
||||
var idWorker = new IdWorker();
|
||||
foreach (var item in entities)
|
||||
{
|
||||
keyPro.SetValue(item, idWorker.NextId());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (typeof(T).GetSugarSplitTable() != null)
|
||||
{
|
||||
DbContext.Insertable(entities).SplitTable().ExecuteCommand();
|
||||
return;
|
||||
}
|
||||
DbContext.Insertable(entities).AddQueue();
|
||||
if (saveChanges) DbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual int SaveChanges()
|
||||
{
|
||||
return BaseDbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public virtual Task<int> SaveChangesAsync()
|
||||
{
|
||||
return BaseDbContext.SqlSugarClient.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public virtual int ExecuteSqlCommand(string sql, params SugarParameter[] SugarParameters)
|
||||
{
|
||||
return DbContext.Ado.ExecuteCommand(sql, SugarParameters);
|
||||
// return DbContext.Database.ExecuteSqlRaw(sql, SugarParameters);
|
||||
}
|
||||
|
||||
public virtual List<TEntity> FromSql(string sql, params SugarParameter[] SugarParameters)
|
||||
{
|
||||
return DbContext.Ado.SqlQuery<TEntity>(sql, SugarParameters).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行sql
|
||||
/// 使用方式 FormattableString sql=$"select * from xx where name ={xx} and pwd={xx1} ",
|
||||
/// FromSqlInterpolated内部处理sql注入的问题,直接在{xx}写对应的值即可
|
||||
/// 注意:sql必须 select * 返回所有TEntity字段,
|
||||
/// </summary>
|
||||
/// <param name="formattableString"></param>
|
||||
/// <returns></returns>
|
||||
//public virtual ISugarQueryable<TEntity> FromSqlInterpolated([NotNull] FormattableString sql)
|
||||
//{
|
||||
// //DBSet.FromSqlInterpolated(sql).Select(x => new { x,xxx}).ToList();
|
||||
// return DbContext.Ado.SqlQuery<TEntity>(sql);
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 取消上下文跟踪
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
public virtual void Detached(TEntity entity)
|
||||
{
|
||||
// DbContext.Entry(entity).State = EntityState.Detached;
|
||||
}
|
||||
public virtual void DetachedRange(IEnumerable<TEntity> entities)
|
||||
{
|
||||
//foreach (var entity in entities)
|
||||
//{
|
||||
// DbContext.Entry(entity).State = EntityState.Detached;
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.IO;
|
||||
using VolPro.Core.Extensions;
|
||||
using VolPro.Core.Extensions.AutofacManager;
|
||||
|
||||
namespace VolPro.Core.BaseProvider.ServerMapPath
|
||||
{
|
||||
public interface IPathProvider : IDependency
|
||||
{
|
||||
string MapPath(string path);
|
||||
string MapPath(string path, bool rootPath);
|
||||
IWebHostEnvironment GetHostingEnvironment();
|
||||
}
|
||||
|
||||
public class PathProvider : IPathProvider
|
||||
{
|
||||
private IWebHostEnvironment _hostingEnvironment;
|
||||
|
||||
public PathProvider(IWebHostEnvironment environment)
|
||||
{
|
||||
_hostingEnvironment = environment;
|
||||
}
|
||||
public IWebHostEnvironment GetHostingEnvironment()
|
||||
{
|
||||
return _hostingEnvironment;
|
||||
}
|
||||
|
||||
public string MapPath(string path)
|
||||
{
|
||||
return MapPath(path, false);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <param name="rootPath">获取wwwroot路径</param>
|
||||
/// <returns></returns>
|
||||
public string MapPath(string path, bool rootPath)
|
||||
{
|
||||
if (rootPath)
|
||||
{
|
||||
if (_hostingEnvironment.WebRootPath == null)
|
||||
{
|
||||
_hostingEnvironment.WebRootPath = _hostingEnvironment.ContentRootPath + "/wwwroot".ReplacePath();
|
||||
}
|
||||
return Path.Combine(_hostingEnvironment.WebRootPath, path).ReplacePath();
|
||||
}
|
||||
return Path.Combine(_hostingEnvironment.ContentRootPath, path).ReplacePath();
|
||||
}
|
||||
}
|
||||
}
|
||||
2777
api_sqlsugar/VolPro.Core/BaseProvider/ServiceBase.cs
Normal file
2777
api_sqlsugar/VolPro.Core/BaseProvider/ServiceBase.cs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user