using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using Znyc.Cloudcar.Admin.Commons.Cache;
using Znyc.Cloudcar.Admin.Commons.DependencyInjection;
using Znyc.Cloudcar.Admin.Commons.Dtos;
using Znyc.Cloudcar.Admin.Commons.Entitys;
using Znyc.Cloudcar.Admin.Commons.Extensions;
using Znyc.Cloudcar.Admin.Commons.Helpers;
using Znyc.Cloudcar.Admin.Commons.IRepositories;
using Znyc.Cloudcar.Admin.Commons.IServices;
using Znyc.Cloudcar.Admin.Commons.Json;
using Znyc.Cloudcar.Admin.Commons.Mapping;
using Znyc.Cloudcar.Admin.Commons.Pages;
namespace Znyc.Cloudcar.Admin.Commons.Services
{
///
///
///
///
///
public abstract class BaseService : IService, ITransientDependency
where T : Entity
where TODto : class
where TKey : IEquatable
{
private readonly IHttpContextAccessor _accessor;
///
///
protected IRepository repository;
///
///
///
protected BaseService(IRepository iRepository)
{
repository = iRepository;
}
///
///
///
///
protected BaseService(IRepository iRepository, IHttpContextAccessor accessor)
{
_accessor = accessor;
repository = iRepository;
}
///
/// 同步物理删除实体。
///
/// 实体
/// 事务对象
///
public virtual bool Delete(T entity, IDbTransaction trans = null)
{
return repository.Delete(entity);
}
///
/// 同步物理删除实体。
///
/// 主键
/// 事务对象
///
public virtual bool Delete(TKey id, IDbTransaction trans = null)
{
return repository.Delete(id, trans);
}
///
/// 异步物理删除实体。
///
/// 主键
/// 事务对象
///
public virtual Task DeleteAsync(TKey id, IDbTransaction trans = null)
{
return repository.DeleteAsync(id, trans);
}
///
/// 异步物理删除实体。
///
/// 实体
/// 事务对象
///
public virtual Task DeleteAsync(T entity, IDbTransaction trans = null)
{
return repository.DeleteAsync(entity, trans);
}
///
/// 按主键批量删除
///
///
///
///
public virtual bool DeleteBatch(IList ids, IDbTransaction trans = null)
{
return repository.DeleteBatch(ids, trans);
}
///
/// 按条件批量删除
///
/// 条件
///
///
public virtual bool DeleteBatchWhere(string where, IDbTransaction trans = null)
{
return repository.DeleteBatchWhere(where, trans);
}
///
/// 按条件批量删除
///
/// 条件
///
///
public virtual async Task DeleteBatchWhereAsync(string where, IDbTransaction trans = null)
{
return await repository.DeleteBatchWhereAsync(where, trans);
}
///
/// 软删除信息,将IsDeleted设置为1-删除,0-恢复删除
///
/// true为不删除,false删除
/// 主键ID
/// 操作用户
///
///
public virtual bool DeleteSoft(bool bl, TKey id, long userId, IDbTransaction trans = null)
{
return repository.DeleteSoft(bl, id, userId, trans);
}
///
/// 异步软删除信息,bl为true时将IsDeleted设置为0删除,bl为flase时将IsDeleted设置为1-恢复删除
///
/// true为不删除,false删除
/// 主键ID
/// 操作用户
///
///
public virtual async Task DeleteSoftAsync(bool bl, TKey id, long userId, IDbTransaction trans = null)
{
return await repository.DeleteSoftAsync(bl, id, userId, trans);
}
///
/// 异步批量软删除信息,bl为true时将IsDeleted设置为0删除,bl为flase时将IsDeleted设置为1-恢复删除
///
/// true为不删除,false删除
/// c
/// 条件
/// 操作用户
/// 事务对象
///
public virtual async Task DeleteSoftBatchAsync(bool bl, string where, long userId = 0,
IDbTransaction trans = null)
{
return await repository.DeleteSoftBatchAsync(bl, where, userId, trans);
}
///
/// 同步查询单个实体。
///
/// 主键
///
public virtual T Get(TKey id)
{
return repository.Get(id);
}
///
/// 同步查询单个实体。
///
/// 主键
///
public virtual TODto GetOutDto(TKey id)
{
return repository.Get(id).MapTo();
}
///
/// 同步查询单个实体。
///
/// 查询条件
/// 事务对象
///
public virtual T GetWhere(string where, IDbTransaction trans = null)
{
return repository.GetWhere(where);
}
///
/// 同步查询单个实体。
///
/// 查询条件
/// 事务对象
///
public virtual TODto GetOutDtoWhere(string where, IDbTransaction trans = null)
{
return repository.GetWhere(where).MapTo();
}
///
/// 异步查询单个实体。
///
/// 查询条件
/// 事务对象
///
public virtual async Task GetWhereAsync(string where, IDbTransaction trans = null)
{
return await repository.GetWhereAsync(where);
}
///
/// 异步查询单个实体。
///
/// 查询条件
/// 事务对象
///
public virtual async Task GetOutDtoWhereAsync(string where, IDbTransaction trans = null)
{
T info = await repository.GetWhereAsync(where);
return info.MapTo();
}
///
/// 根据查询条件查询前多少条数据
///
/// 多少条数据
/// 查询条件
/// 事务对象
///
public virtual IEnumerable GetListTopWhere(int top, string where = null, IDbTransaction trans = null)
{
return repository.GetListTopWhere(top, where);
}
///
/// 根据查询条件查询前多少条数据
///
/// 多少条数据
/// 查询条件
/// 事务对象
///
public virtual async Task> GetListTopWhereAsync(int top, string where = null,
IDbTransaction trans = null)
{
return await repository.GetListTopWhereAsync(top, where);
}
///
/// 同步查询所有实体。
///
/// 事务对象
///
public virtual IEnumerable GetAll(IDbTransaction trans = null)
{
return repository.GetAll(trans);
}
///
/// 异步步查询所有实体。
///
/// 事务对象
///
public virtual Task> GetAllAsync(IDbTransaction trans = null)
{
return repository.GetAllAsync(trans);
}
///
/// 异步查询单个实体。
///
/// 主键
///
public virtual async Task GetAsync(TKey id)
{
return await repository.GetAsync(id);
}
///
/// 异步查询单个实体。
///
/// 主键
///
public virtual async Task GetOutDtoAsync(TKey id)
{
T info = await repository.GetAsync(id);
return info.MapTo();
}
///
/// 根据查询条件查询数据
///
/// 查询条件
/// 事务对象
///
public virtual IEnumerable GetListWhere(string where = null, IDbTransaction trans = null)
{
return repository.GetListWhere(where, trans);
}
///
/// 异步根据查询条件查询数据
///
/// 查询条件
/// 事务对象
///
public virtual async Task> GetListWhereAsync(string where = null, IDbTransaction trans = null)
{
return await repository.GetListWhereAsync(where, trans);
}
///
/// 同步新增实体。
///
/// 实体
/// 事务对象
///
public virtual long Insert(T entity, IDbTransaction trans = null)
{
return repository.Insert(entity, trans);
}
///
/// 异步步新增实体。
///
/// 实体
/// 事务对象
///
public virtual Task InsertAsync(T entity, IDbTransaction trans = null)
{
return repository.InsertAsync(entity, trans);
}
///
/// 异步新增实体返回主键
///
///
///
///
public virtual Task InsertReturnPrimaryKeyAsync(T entity, IDbTransaction trans = null)
{
return repository.InsertReturnPrimaryKeyAsync(entity, trans);
}
///
/// 同步批量新增实体。
///
/// 实体集合
///
public virtual void Insert(List entities)
{
repository.Insert(entities);
}
///
/// 同步更新实体。
///
/// 实体
/// 主键ID
/// 事务对象
///
public virtual bool Update(T entity, TKey id, IDbTransaction trans = null)
{
return repository.Update(entity, id, trans);
}
///
/// 异步更新实体。
///
/// 实体
/// 主键ID
/// 事务对象
///
public virtual Task UpdateAsync(T entity, TKey id, IDbTransaction trans = null)
{
return repository.UpdateAsync(entity, id, trans);
}
///
/// 更新某一字段值,字段值字符类型
///
/// 字段
/// 字段值字符类型
/// 条件,为空更新所有内容
/// 事务对象
/// 执行成功返回true,否则为false。
public virtual bool UpdateTableField(string strField, string fieldValue, string where,
IDbTransaction trans = null)
{
return repository.UpdateTableField(strField, fieldValue, where, trans);
}
///
/// 更新某一字段值,字段值字符类型
///
/// 字段
/// 字段值字符类型
/// 条件,为空更新所有内容
/// 事务对象
/// 执行成功返回true,否则为false。
public virtual async Task UpdateTableFieldAsync(string strField, string fieldValue, string where,
IDbTransaction trans = null)
{
return await repository.UpdateTableFieldAsync(strField, fieldValue, where, trans);
}
///
/// 更新某一字段值,字段值数字类型
///
/// 字段
/// 字段值数字
/// 条件,为空更新所有内容
/// 事务对象
/// 执行成功返回true,否则为false。
public virtual bool UpdateTableField(string strField, int fieldValue, string where, IDbTransaction trans = null)
{
return repository.UpdateTableField(strField, fieldValue, where, trans);
}
///
/// 更新某一字段值,字段值数字类型
///
/// 字段
/// 字段值数字
/// 条件,为空更新所有内容
/// 事务对象
/// 执行成功返回true,否则为false。
public virtual async Task UpdateTableFieldAsync(string strField, int fieldValue, string where,
IDbTransaction trans = null)
{
return await repository.UpdateTableFieldAsync(strField, fieldValue, where, trans);
}
///
/// 查询软删除的数据,如果查询条件为空,即查询所有软删除的数据
///
/// 查询条件
///
///
public virtual IEnumerable GetAllByIsIsDeleted(string where = null, IDbTransaction trans = null)
{
return repository.GetAllByIsIsDeleted(where, trans);
}
///
/// 查询未软删除的数据,如果查询条件为空,即查询所有未软删除的数据
///
/// 查询条件
///
///
public virtual IEnumerable GetAllByIsNotIsDeleted(string where = null, IDbTransaction trans = null)
{
return repository.GetAllByIsNotIsDeleted(where, trans);
}
///
/// 查询有效的数据,如果查询条件为空,即查询所有有效的数据
///
/// 查询条件
///
///
public virtual IEnumerable GetAllByIsEnabledMark(string where = null, IDbTransaction trans = null)
{
return repository.GetAllByIsEnabledMark(where, trans);
}
///
/// 查询无效的数据,如果查询条件为空,即查询所有无效的数据
///
/// 查询条件
///
///
public virtual IEnumerable GetAllByIsNotEnabledMark(string where = null, IDbTransaction trans = null)
{
return repository.GetAllByIsNotEnabledMark(where, trans);
}
///
/// 查询未软删除且有效的数据,如果查询条件为空,即查询所有数据
///
/// 查询条件
///
///
public virtual IEnumerable GetAllByIsNotDeleteAndEnabledMark(string where = null,
IDbTransaction trans = null)
{
return repository.GetAllByIsNotDeleteAndEnabledMark(where, trans);
}
///
/// 查询软删除的数据,如果查询条件为空,即查询所有软删除的数据
///
/// 查询条件
///
///
public virtual async Task> GetAllByIsIsDeletedAsync(string where = null,
IDbTransaction trans = null)
{
return await repository.GetAllByIsIsDeletedAsync(where, trans);
}
///
/// 查询未软删除的数据,如果查询条件为空,即查询所有未软删除的数据
///
/// 查询条件
///
///
public virtual async Task> GetAllByIsNotIsDeletedAsync(string where = null,
IDbTransaction trans = null)
{
return await repository.GetAllByIsNotIsDeletedAsync(where, trans);
}
///
/// 查询有效的数据,如果查询条件为空,即查询所有有效的数据
///
/// 查询条件
///
///
public virtual async Task> GetAllByIsEnabledMarkAsync(string where = null,
IDbTransaction trans = null)
{
return await repository.GetAllByIsEnabledMarkAsync(where, trans);
}
///
/// 查询无效的数据,如果查询条件为空,即查询所有无效的数据
///
/// 查询条件
///
///
public virtual async Task> GetAllByIsNotEnabledMarkAsync(string where = null,
IDbTransaction trans = null)
{
return await repository.GetAllByIsNotEnabledMarkAsync(where, trans);
}
///
/// 查询未软删除且有效的数据,如果查询条件为空,即查询所有数据
///
/// 查询条件
///
///
public virtual async Task> GetAllByIsNotDeleteAndEnabledMarkAsync(string where = null,
IDbTransaction trans = null)
{
return await repository.GetAllByIsNotDeleteAndEnabledMarkAsync(where, trans);
}
///
/// 设置数据有效性,将IsEnabled设置为1:有效,0-为无效
///
/// true为有效,false无效
/// 主键ID
/// 操作用户
///
///
public virtual bool SetEnabledMark(bool bl, TKey id, long userId = 0, IDbTransaction trans = null)
{
return repository.SetEnabledMark(bl, id, userId, trans);
}
///
/// 异步设置数据有效性,将IsEnabled设置为1:有效,0-为无效
///
/// true为有效,false无效
/// 主键ID
/// 操作用户
///
///
public virtual async Task SetEnabledMarkAsync(bool bl, TKey id, long userId = 0,
IDbTransaction trans = null)
{
return await repository.SetEnabledMarkAsync(bl, id, userId, trans);
}
///
/// 异步按条件设置数据有效性,将IsEnabled设置为1:有效,0-为无效
///
/// true为有效,false无效
/// 条件
/// 操作用户
/// 事务对象
///
public virtual async Task SetEnabledMarkByWhereAsync(bool bl, string where, long userId = 0,
IDbTransaction trans = null)
{
return await repository.SetEnabledMarkByWhereAsync(bl, where, userId, trans);
}
///
/// 异步按条件设置数据的状态,将State设置为0:审核中,1:正常,-1:停用,-2:停用
///
/// 0:审核中,1:正常,-1:停用,-2:停用
/// 条件
/// 操作用户
/// 事务对象
///
public virtual async Task SetStatusByWhereAsync(int bl, string where, long userId = 0,
IDbTransaction trans = null)
{
return await repository.SetStatusByWhereAsync(bl, where, userId, trans);
}
///
/// 异步按条件设置数据有效性,将IsEnabled设置为1:有效,0-为无效
///
///
///
///
///
///
///
public virtual async Task SetEnabledMarkByWhereAsync(bool bl, string where, object paramparameters = null,
long userId = 0, IDbTransaction trans = null)
{
return await repository.SetEnabledMarkByWhereAsync(bl, where, paramparameters, userId, trans);
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 分页实体
/// 事务对象
/// 指定对象的集合
public virtual List FindWithPager(string condition, PagerInfo info, IDbTransaction trans = null)
{
return repository.FindWithPager(condition, info, trans);
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 分页实体
/// 排序字段
/// 事务对象
/// 指定对象的集合
public virtual List FindWithPager(string condition, PagerInfo info, string fieldToSort,
IDbTransaction trans = null)
{
return repository.FindWithPager(condition, info, fieldToSort, trans);
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 分页实体
/// 排序字段
/// 是否降序
/// 事务对象
/// 指定对象的集合
public virtual List FindWithPager(string condition, PagerInfo info, string fieldToSort, bool desc,
IDbTransaction trans = null)
{
return repository.FindWithPager(condition, info, fieldToSort, desc, trans);
}
///
/// 分页查询,自行封装sql语句(仅支持sql server)
/// 非常复杂的查询,可在具体业务模块重写该方法
///
/// 查询条件
/// 分页信息
/// 排序字段
/// 排序方式 true为desc,false为asc
///
///
public virtual List FindWithPagerSql(string condition, PagerInfo info, string fieldToSort, bool desc,
IDbTransaction trans = null)
{
return repository.FindWithPagerSql(condition, info, fieldToSort, desc, trans);
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 分页实体
/// 排序字段
/// 排序方式
/// 事务对象
/// 指定对象的集合
public virtual async Task> FindWithPagerAsync(string condition, PagerInfo info, string fieldToSort,
bool desc, IDbTransaction trans = null)
{
return await repository.FindWithPagerAsync(condition, info, fieldToSort, desc, trans);
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
/// 查询条件变换时请重写该方法。
///
/// 查询的条件
/// 指定对象的集合
public virtual PageResult FindWithPager(SearchInputDto search)
{
bool order = search.Order == "asc" ? false : true;
string where = GetDataPrivilege();
PagerInfo pagerInfo = new PagerInfo
{
CurrenetPageIndex = search.CurrenetPageIndex,
PageSize = search.PageSize
};
List list = repository.FindWithPager(where, pagerInfo, search.Sort, order);
PageResult pageResult = new PageResult
{
CurrentPage = pagerInfo.CurrenetPageIndex,
Items = list.MapTo(),
ItemsPerPage = pagerInfo.PageSize,
TotalItems = pagerInfo.RecordCount
};
return pageResult;
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
/// 查询条件变换时请重写该方法。
///
/// 查询的条件
/// 指定对象的集合
public virtual async Task> FindWithPagerAsync(SearchInputDto search)
{
bool order = search.Order == "asc" ? false : true;
string where = GetDataPrivilege();
PagerInfo pagerInfo = new PagerInfo
{
CurrenetPageIndex = search.CurrenetPageIndex,
PageSize = search.PageSize
};
List list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
PageResult pageResult = new PageResult
{
CurrentPage = pagerInfo.CurrenetPageIndex,
Items = list.MapTo(),
ItemsPerPage = pagerInfo.PageSize,
TotalItems = pagerInfo.RecordCount
};
return pageResult;
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 分页实体
/// 排序字段
/// 事务对象
/// 指定对象的集合
public virtual async Task> FindWithPagerAsync(string condition, PagerInfo info, string fieldToSort,
IDbTransaction trans = null)
{
return await repository.FindWithPagerAsync(condition, info, fieldToSort, trans);
}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 分页实体
/// 事务对象
/// 指定对象的集合
public virtual async Task> FindWithPagerAsync(string condition, PagerInfo info,
IDbTransaction trans = null)
{
return await repository.FindWithPagerAsync(condition, info, trans);
}
///
/// 分页查询,自行封装sql语句(仅支持sql server)
/// 非常复杂的查询,可在具体业务模块重写该方法
///
/// 查询条件
/// 分页信息
/// 排序字段
/// 排序方式 true为desc,false为asc
///
///
public virtual async Task> FindWithPagerSqlAsync(string condition, PagerInfo info, string fieldToSort,
bool desc, IDbTransaction trans = null)
{
return await repository.FindWithPagerAsync(condition, info, fieldToSort, desc, trans);
}
///
/// 分页查询包含用户信息(仅支持sql server)
/// 查询主表别名为t1,用户表别名为t2,在查询字段需要注意使用t1.xxx格式,xx表示主表字段
/// 用户信息主要有用户账号:Account、昵称:UserName、真实姓名:RealName、头像:HeadIcon、手机号:MobilePhone
/// 输出对象请在Dtos中进行自行封装,不能是使用实体Model类
///
/// 查询条件字段需要加表别名
/// 分页信息
/// 排序字段,也需要加表别名
/// 排序方式
/// 事务
///
public virtual async Task> FindWithPagerRelationUserAsync(string condition, PagerInfo info,
string fieldToSort, bool desc, IDbTransaction trans = null)
{
return await repository.FindWithPagerRelationUserAsync(condition, info, fieldToSort, desc, trans);
}
///
/// 分页查询包含用户信息(仅支持sql server)
/// 查询主表别名为t1,用户表别名为t2,在查询字段需要注意使用t1.xxx格式,xx表示主表字段
/// 用户信息主要有用户账号:Account、昵称:UserName、真实姓名:RealName、头像:HeadIcon、手机号:MobilePhone
/// 输出对象请在Dtos中进行自行封装,不能是使用实体Model类
///
/// 查询条件字段需要加表别名
/// 分页信息
/// 排序字段,也需要加表别名
/// 排序方式
/// 事务
///
public virtual List