using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Threading.Tasks; using OfficeOpenXml; using VOL.Core.Enums; using VOL.Core.Utilities; using VOL.Core.WorkFlow; using VOL.Entity.DomainModels; using VOL.Entity.SystemModels; namespace VOL.Core.BaseProvider { public abstract class ApplicationServiceBase where TEntity : BaseEntity where TRepository : IRepository { private PropertyInfo[] properties { get; set; } = null; public PropertyInfo[] TProperties { get { properties ??= typeof(TEntity).GetProperties(); return properties; } } public IRepository repository; public ApplicationServiceBase() { } public WebResponseContent baseWebResponse { get; set; } public ApplicationServiceBase(TRepository repository) { baseWebResponse = new WebResponseContent(true); this.repository = repository; } /// /// 主从明细表,一对多明细表编辑信息 /// public ApplicationServiceBaseMultipleTableEntity MultipleTableEntity { get; set; } /// /// 是否开启多租户功能 /// public bool IsMultiTenancy = true; /// /// 自定义原生查询sql,这个对于不想写表达式关联或者复杂查询非常有用 /// 例:QuerySql=$"select * from tb1 as a where a.name='xxxx' x.id in (select b.id from tb2 b)"; /// select * 这里可以自定义,但select 必须返回表所有的列,不能少 /// public string QuerySql = null; /// /// 查询前,对现在有的查询字符串条件增加或删除 /// public Action> QueryRelativeList { get; set; } /// /// 设置查询排序参数及方式,参数格式为: /// Expression>> orderBy = x => new Dictionary() /// {{ x.ID, QueryOrderBy.Asc },{ x.DestWarehouseName, QueryOrderBy.Desc } }; /// 返回的是new Dictionary(){{}}key为排序字段,QueryOrderBy为排序方式 /// public Expression>> OrderByExpression; //查询前,在现有的查询条件上通过表达式修改查询条件 public Func, IQueryable> QueryRelativeExpression { get; set; } /// /// 查询界面table 统计、求和、平均值等 /// 实现方式 ///SummaryExpress = (IQueryable queryable) => // { // return queryable.GroupBy(x => 1).Select(x => new // { // AvgPrice = x.Average(o => o.AvgPrice), // Enable = x.Sum(o => o.Enable) // }).FirstOrDefault(); //}; /// // protected Func, object> SummaryExpress = null; public Func, object> SummaryExpress = null; public Func, Task> SummaryExpressAsync = null; /// /// 页面查询或导出,从数据库查询出来的结果 /// public Action> GetPageDataOnExecuted; public Func, Task> GetPageDataOnExecutedAsync; /// /// 调用新建处理前(SaveModel为传入的原生数据) /// public Func AddOnExecute; /// /// 调用新建保存数据库前处理(已将提交的原生数据转换成了对象) /// Func T为主表数据,object为明细数据(如果需要使用明细对象,请用 object as List转换) /// public Func AddOnExecuting; public Func> AddOnExecutingAsync; /// /// 调用更新方法前处理(SaveModel为传入的原生数据) /// public Func UpdateOnExecute; /// /// 调用更新方法保存数据库前处理 /// (已将提交的原生数据转换成了对象,将明细新增、修改、删除的数据分别用object1/2/3标识出来 ) /// T=更新的主表对象 /// object1=为新增明细的对象,使用时将object as List转换一下 /// object2=为更新明细的对象 /// List=为删除的细的对象Key /// protected Func, WebResponseContent> UpdateOnExecuting; protected Func, Task> UpdateOnExecutingAsync; /// /// 调用更新方法保存数据库后处理 /// **实现当前方法时,内部默认已经开启事务,如果实现的方法操作的是同一数据库,则不需要在UpdateOnExecuted中事务 /// (已将提交的原生数据转换成了对象,将明细新增、修改、删除的数据分别用object1/2/3标识出来 ) /// T=更新的主表对象 /// object1=为新增明细的对象,使用时将object as List转换一下 /// object2=为更新明细的对象 /// List=为删除的细的对象Key /// 此处已开启了DbContext事务(重点),如果还有其他业务处事,直接在这里写EF代码,不需要再开启事务 /// 如果执行的是手写sql请用repository.DbContext.Database.ExecuteSqlCommand()或 repository.DbContext.Set().FromSql执行具体sql语句 /// protected Func, WebResponseContent> UpdateOnExecuted; protected Func, Task> UpdateOnExecutedAsync; /// /// 删除前处理,object[]准备删除的主键 /// protected Func DelOnExecuting; protected Func> DelOnExecutingAsync; /// /// 删除后处理,object[]已删除的主键,此处已开启了DbContext事务(重点),如果还有其他业务处事,直接在这里写EF代码,不需要再开启事务 /// 如果执行的是手写sql请用repository.DbContext.Database.ExecuteSqlCommand()或 repository.DbContext.Set().FromSql执行具体sql语句 /// protected Func DelOnExecuted; protected Func> DelOnExecutedAsync; /// /// 调用新建保存数据库后处理。 /// **实现当前方法时,内部默认已经开启事务,如果实现的方法操作的是同一数据库,则不需要在AddOnExecuted中事务 /// Func T为主表数据,object为明细数据(如果需要使用明细对象,请用 object as List转换) /// 此处已开启了DbContext事务(重点),如果还有其他业务处事,直接在这里写EF代码,不需要再开启事务 /// 如果执行的是手写sql请用repository.DbContext.Database.ExecuteSqlCommand()或 repository.DbContext.Set().FromSql执行具体sql语句 /// protected Func AddOnExecuted; protected Func> AddOnExecutedAsync; /// /// 进入审批流程方法之前 /// public Func AddWorkFlowExecuting; public Func> AddWorkFlowExecutingAsync; /// /// 写入审批流程数据之后 /// list:审批的人id /// public Action> AddWorkFlowExecuted; public Func, Task> AddWorkFlowExecutedAsync; ///默认导出最大表数量:0不限制 protected int Limit { get; set; } = 0; /// /// 自定义上传的文件夹 /// protected string UploadFolder = null; protected bool IsRoot = true; /// /// 导出下载模板,指定要导出的模板列,格式:Expression> exp = x => new { x.字段1, x.字段2 } /// protected Expression> DownLoadTemplateColumns { get; set; } /// /// 指定导入字段的名称 /// protected Expression>> ExcelHeaderMap { get; set; } /// /// 导入excel从第几行开始读取 /// protected int ImportStartRowIndex { get; set; } = 1; /// /// 导入时不验证下拉框数据源的字段值 /// protected Expression> ImportIgnoreSelectValidationColumns; /// /// 导入保存前 /// protected Func, WebResponseContent> ImportOnExecuting; /// /// 导入保存前 /// protected Func, Task> ImportOnExecutingAsync; /// /// 导入保存后 /// protected Func, WebResponseContent> ImportOnExecuted; /// /// 导入保存后 /// protected Func, Task> ImportOnExecutedAsync; /// ///导出前处理,DataTable导出的表数据 ///List导出的数据, List忽略不需要导出的字段 ///此方法不建议使用,由下面ExportColumns委托替代2020.05.07 /// protected Func, List, WebResponseContent> ExportOnExecuting; protected Func, List, Task> ExportOnExecutingAsync; /// /// 导出表数据(界面上导出操作),指定要导出的列,格式:Expression> exp = x => new { x.字段1, x.字段2 } /// protected Expression> ExportColumns { get; set; } /// /// 2022.06.20增加原生excel读取方法(导入时可以自定义读取excel内容) /// string=当前读取的excel单元格的值 /// ExcelWorksheet=excel对象 /// ExcelRange当前excel单元格对象 /// int=当前读取的第几数 /// int=当前读取的第几列 /// string=返回的值 /// protected Func ImportOnReadCellValue; /// /// 审核前处理 /// public Func, WebResponseContent> AuditOnExecuting; public Func, Task> AuditOnExecutingAsync; /// /// 审核后处理 /// public Func, WebResponseContent> AuditOnExecuted; public Func, Task> AuditOnExecutedAsync; /// /// 反核前处理 /// public Func AntiAuditOnExecuting; public Func> AntiAuditOnExecutingAsync; /// /// 反核后处理 /// public Func AntiAuditOnExecuted; public Func> AntiAuditOnExecutedAsync; /// /// 审批流程审核前 /// TEntity:当前审核的数据 /// AuditStatus:审核状态 /// bool:当前数据是否为最后一个人审核 /// public Func AuditWorkFlowExecuting; public Func> AuditWorkFlowExecutingAsync; /// /// 审批流程审核后 /// T:当前审核的数据 /// AuditStatus:审核状态 /// list:下一个节点的审批人id /// bool:当前数据是否为最后一个人审核 /// public Func, bool, WebResponseContent> AuditWorkFlowExecuted; public Func, bool, Task> AuditWorkFlowExecutedAsync; public bool ResponseIsError { get { return !baseWebResponse.Status || baseWebResponse.Code == "-1"; } } /// /// 明细合计 /// /// /// /// protected virtual object GetDetailSummary(IQueryable queryeable) where Detail : class { return null; } public virtual object GetDetailSummaryData(IQueryable queryeable) where Detail : class { return GetDetailSummary(queryeable); } /// /// 明细合计 /// /// /// /// protected virtual Task GetDetailSummaryAsync(IQueryable queryeable) where Detail : class { return Task.FromResult(null); } public virtual async Task GetDetailSummaryDataAsync(IQueryable queryeable) where Detail : class { return await GetDetailSummaryAsync(queryeable); } /// /// 明细表自定义查询 /// /// /// /// /// public virtual IQueryable DetailQuery(IQueryable queryable, List searchParametersList) { return queryable; } public bool IsTableActionLog { get { return typeof(TEntity).IsTableActionLog(); } } } }