Config Controller 的添加

master
lch 6 months ago
parent 2672cce124
commit 7169abb8df

@ -0,0 +1,7 @@
namespace Common
{
public class Class1
{
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,23 @@
namespace Common.Const
{
/// <summary>
/// SqlSugar相关常量
/// </summary>
public class SqlSugarConst
{
/// <summary>
/// 默认主数据库标识(默认租户)
/// </summary>
public const string MainConfigId = "1300000000001";
/// <summary>
/// 默认日志数据库标识
/// </summary>
public const string LogConfigId = "1300000000002";
/// <summary>
/// 默认表主键
/// </summary>
public const string PrimaryKey = "Id";
}
}

@ -0,0 +1,23 @@
using System.ComponentModel;
namespace Common.Enum
{
/// <summary>
/// 是否枚举
/// </summary>
[Description("是否枚举")]
public enum YesNoEnum
{
/// <summary>
/// 是
/// </summary>
[Description("是")]
Y = 1,
/// <summary>
/// 否
/// </summary>
[Description("否")]
N = 2
}
}

@ -0,0 +1,87 @@
using SqlSugar;
namespace Entity.Base
{
/// <summary>
/// 框架实体基类Id
/// </summary>
public abstract class EntityBaseId
{
/// <summary>
/// 雪花Id
/// </summary>
[SugarColumn(ColumnName = "id", ColumnDescription = "主键Id", IsPrimaryKey = true, IsIdentity = false)]
public virtual long Id { get; set; }
}
/// <summary>
/// 框架实体基类
/// </summary>
[SugarIndex("index_{table}_CT", nameof(CreateTime), OrderByType.Asc)]
public abstract class EntityBase : EntityBaseId, IDeletedFilter
{
/// <summary>
/// 创建时间
/// </summary>
[SugarColumn(ColumnDescription = "创建时间", IsOnlyIgnoreUpdate = true, ColumnName = "create_time")]
public virtual DateTime? CreateTime { get; set; }
/// <summary>
/// 更新时间
/// </summary>
[SugarColumn(ColumnDescription = "更新时间", ColumnName = "update_time")]
public virtual DateTime? UpdateTime { get; set; }
/// <summary>
/// 创建者Id
/// </summary>
[SugarColumn(ColumnDescription = "创建者Id", IsOnlyIgnoreUpdate = true, ColumnName = "create_user_id")]
public virtual long? CreateUserId { get; set; }
/// <summary>
/// 创建者姓名
/// </summary>
[SugarColumn(ColumnDescription = "创建者姓名", Length = 64, IsOnlyIgnoreUpdate = true, ColumnName = "create_user_name")]
public virtual string? CreateUserName { get; set; }
/// <summary>
/// 修改者Id
/// </summary>
[SugarColumn(ColumnDescription = "修改者Id", ColumnName = "update_user_id")]
public virtual long? UpdateUserId { get; set; }
/// <summary>
/// 修改者姓名
/// </summary>
[SugarColumn(ColumnDescription = "修改者姓名", Length = 64, ColumnName = "update_user_name")]
public virtual string? UpdateUserName { get; set; }
/// <summary>
/// 软删除
/// </summary>
[SugarColumn(ColumnDescription = "软删除", ColumnName = "is_delete")]
public virtual bool IsDelete { get; set; } = false;
}
/// <summary>
/// 业务数据实体基类(数据权限)
/// </summary>
public abstract class EntityBaseData : EntityBase, IOrgIdFilter
{
/// <summary>
/// 创建者部门Id
/// </summary>
[SugarColumn(ColumnDescription = "创建者部门Id", IsOnlyIgnoreUpdate = true, ColumnName = "creat_org_id")]
public virtual long? CreateOrgId { get; set; }
/// <summary>
/// 创建者部门名称
/// </summary>
[SugarColumn(ColumnDescription = "创建者部门名称", Length = 64, IsOnlyIgnoreUpdate = true, ColumnName = "creat_org_name")]
public virtual string? CreateOrgName { get; set; }
}
}

@ -0,0 +1,34 @@
namespace Entity.Base
{
/// <summary>
/// 租户Id接口过滤器
/// </summary>
public interface ITenantIdFilter
{
/// <summary>
/// 租户Id
/// </summary>
long? TenantId { get; set; }
}
/// <summary>
/// 假删除接口过滤器
/// </summary>
public interface IDeletedFilter
{
/// <summary>
/// 软删除
/// </summary>
bool IsDelete { get; set; }
}
/// <summary>
/// 机构Id接口过滤器
/// </summary>
internal interface IOrgIdFilter
{
/// <summary>
/// 创建者部门Id
/// </summary>
long? CreateOrgId { get; set; }
}
}

@ -0,0 +1,157 @@
using System.Linq.Expressions;
using SqlSugar;
namespace Entity.Base
{
/// <summary>
/// 分页泛型集合
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class SqlSugarPagedList<TEntity>
{
/// <summary>
/// 页码
/// </summary>
public int Page { get; set; }
/// <summary>
/// 页容量
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 总条数
/// </summary>
public int Total { get; set; }
/// <summary>
/// 总页数
/// </summary>
public int TotalPages { get; set; }
/// <summary>
/// 当前页集合
/// </summary>
public IEnumerable<TEntity> Items { get; set; }
/// <summary>
/// 是否有上一页
/// </summary>
public bool HasPrevPage { get; set; }
/// <summary>
/// 是否有下一页
/// </summary>
public bool HasNextPage { get; set; }
}
/// <summary>
/// 分页拓展类
/// </summary>
public static class SqlSugarPagedExtensions
{
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{T}"/>对象</param>
/// <param name="pageIndex">当前页码从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <param name="expression">查询结果 Select 表达式</param>
/// <returns></returns>
public static SqlSugarPagedList<TResult> ToPagedList<TEntity, TResult>(this ISugarQueryable<TEntity> query,
int pageIndex, int pageSize,
Expression<Func<TEntity, TResult>> expression)
{
var total = 0;
var items = query.ToPageList(pageIndex, pageSize, ref total, expression);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> query,
int pageIndex, int pageSize)
{
var total = 0;
var items = query.ToPageList(pageIndex, pageSize, ref total);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <param name="expression">查询结果 Select 表达式</param>
/// <returns></returns>
public static async Task<SqlSugarPagedList<TResult>> ToPagedListAsync<TEntity, TResult>(
this ISugarQueryable<TEntity> query, int pageIndex, int pageSize,
Expression<Func<TEntity, TResult>> expression)
{
RefAsync<int> total = 0;
var items = await query.ToPageListAsync(pageIndex, pageSize, total, expression);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="query"><see cref="ISugarQueryable{TEntity}"/>对象</param>
/// <param name="pageIndex">当前页码从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(
this ISugarQueryable<TEntity> query, int pageIndex, int pageSize)
{
RefAsync<int> total = 0;
var items = await query.ToPageListAsync(pageIndex, pageSize, total);
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 分页拓展
/// </summary>
/// <param name="list">集合对象</param>
/// <param name="pageIndex">当前页码从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this IEnumerable<TEntity> list, int pageIndex,
int pageSize)
{
var total = list.Count();
var items = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
return CreateSqlSugarPagedList(items, total, pageIndex, pageSize);
}
/// <summary>
/// 创建 <see cref="SqlSugarPagedList{TEntity}"/> 对象
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="items">分页内容的对象集合</param>
/// <param name="total">总条数</param>
/// <param name="pageIndex">当前页码从1开始</param>
/// <param name="pageSize">页码容量</param>
/// <returns></returns>
public static SqlSugarPagedList<TEntity> CreateSqlSugarPagedList<TEntity>(IEnumerable<TEntity> items, int total,
int pageIndex, int pageSize)
{
var totalPages = pageSize > 0 ? (int)Math.Ceiling(total / (double)pageSize) : 0;
return new SqlSugarPagedList<TEntity>
{
Page = pageIndex,
PageSize = pageSize,
Items = items,
Total = total,
TotalPages = totalPages,
HasNextPage = pageIndex < totalPages,
HasPrevPage = pageIndex - 1 > 0
};
}
}
}

@ -0,0 +1,61 @@
using System.ComponentModel.DataAnnotations;
using Common.Enum;
using Entity.Base;
using SqlSugar;
namespace Entity.DbModel.System.SysBaseObject
{
/// <summary>
/// 系统参数配置表
/// </summary>
[SugarTable("sys_config")]
public partial class SysConfig : EntityBase
{
/// <summary>
/// 名称
/// </summary>
[SugarColumn(ColumnDescription = "名称", Length = 64, ColumnName = "name")]
[Required, MaxLength(64)]
public virtual string Name { get; set; }
/// <summary>
/// 编码
/// </summary>
[SugarColumn(ColumnDescription = "编码", Length = 64, ColumnName = "code")]
[MaxLength(64)]
public string? Code { get; set; }
/// <summary>
/// 属性值
/// </summary>
[SugarColumn(ColumnDescription = "属性值", Length = 64, ColumnName = "value")]
[MaxLength(64)]
public string? Value { get; set; }
/// <summary>
/// 是否是内置参数Y-是N-否)
/// </summary>
[SugarColumn(ColumnDescription = "是否是内置参数", ColumnName = "sys_flag")]
public YesNoEnum SysFlag { get; set; }
/// <summary>
/// 分组编码
/// </summary>
[SugarColumn(ColumnDescription = "分组编码", Length = 64, ColumnName = "group_code")]
[MaxLength(64)]
public string? GroupCode { get; set; }
/// <summary>
/// 排序
/// </summary>
[SugarColumn(ColumnDescription = "排序", ColumnName = "order_no")]
public int OrderNo { get; set; } = 100;
/// <summary>
/// 备注
/// </summary>
[SugarColumn(ColumnDescription = "备注", Length = 256, ColumnName = "remark")]
[MaxLength(256)]
public string? Remark { get; set; }
}
}

@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace Entity.Dto.Req
{
/// <summary>
/// 主键Id输入参数
/// </summary>
public class BaseIdReq
{
/// <summary>
/// 主键Id
/// </summary>
[Required(ErrorMessage = "Id不能为空")]
//[DataValidation(ValidationTypes.Numeric)]
public virtual long Id { get; set; }
}
}

@ -0,0 +1,34 @@
namespace Entity.Dto.Req
{
/// <summary>
/// 全局分页查询输入参数
/// </summary>
public class BasePageReq
{
/// <summary>
/// 当前页码
/// </summary>
public virtual int Page { get; set; } = 1;
/// <summary>
/// 页码容量
/// </summary>
//[Range(0, 100, ErrorMessage = "页码容量超过最大限制")]
public virtual int PageSize { get; set; } = 20;
/// <summary>
/// 排序字段
/// </summary>
public virtual string Field { get; set; } = "";
/// <summary>
/// 排序方向
/// </summary>
public virtual string OrderDirection { get; set; } = "Desc";
/// <summary>
/// 降序排序
/// </summary>
public virtual string DescStr { get; set; } = "descending";
}
}

@ -0,0 +1,38 @@
using Entity.DbModel.System.SysBaseObject;
namespace Entity.Dto.Req
{
public class ConfigReq : BaseIdReq
{
}
public class PageConfigReq : BasePageReq
{
/// <summary>
/// 名称
/// </summary>
public string Name { get; set; } = "";
/// <summary>
/// 编码
/// </summary>
public string Code { get; set; } = "";
/// <summary>
/// 分组编码
/// </summary>
public string GroupCode { get; set; } = "";
}
public class AddConfigReq : SysConfig
{
}
public class UpdateConfigReq : AddConfigReq
{
}
public class DeleteConfigReq : BaseIdReq
{
}
}

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
@ -12,7 +12,14 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Furion.Extras.ObjectMapper.Mapster" Version="4.9.3.4" />
<PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.115" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
<ProjectReference Include="..\HybirdFrameworkCore\HybirdFrameworkCore.csproj" />
</ItemGroup>
</Project>

@ -123,4 +123,9 @@ public class RedisHelper : IDisposable
var db = GetConnect().GetDatabase(_defaultDB);
return await db.StringSetAsync(key, value);
}
public bool Remove(string key)
{
var db = GetConnect().GetDatabase(_defaultDB);
return db.KeyDelete(key);
}
}

@ -1,4 +1,5 @@
using System.Linq.Expressions;
using Entity.Dto.Req;
using HybirdFrameworkCore.Entity;
using SqlSugar;
@ -58,6 +59,44 @@ public abstract class BaseRepository<T> where T : class, new()
.WithNoLockOrNot(blUseNoLock)
.ToList();
}
public async Task<List<T>> QueryPageAsync(
bool isWhere1, Expression<Func<T, bool>> expression1,
bool isWhere2, Expression<Func<T, bool>> expression2,
bool isWhere3, Expression<Func<T, bool>> expression3,
int pageNumber, int pageSize, RefAsync<int> totalNumber,
PageConfigReq input, bool blUseNoLock = false)
{
var page = await DbBaseClient
.Queryable<T>()
.WhereIF(isWhere1, expression1)
.WhereIF(isWhere2, expression2)
.WhereIF(isWhere3, expression3)
.OrderBuilder(input)
.WithNoLockOrNot(blUseNoLock)
.ToPageListAsync(pageNumber, pageSize, totalNumber);
return page;
}
public async Task<int> UpdateAsync(T updateObj, bool ignoreAllNullColumns)
{
return await DbBaseClient
.Updateable(updateObj)
.IgnoreColumns(ignoreAllNullColumns)
.ExecuteCommandAsync();
}
public async Task<List<TResult>> QueryByGroupByAsync<TResult>(
Expression<Func<T, object>> expression,
Expression<Func<T, TResult>> expression2
)
{
return await DbBaseClient
.Queryable<T>()
.GroupBy(expression)
.Select(expression2)
.ToListAsync();
}
/// <summary>
/// 根据主值列表查询单条数据
@ -1013,7 +1052,8 @@ public abstract class BaseRepository<T> where T : class, new()
/// <returns></returns>
public IPage<T> QueryIPageByCause(QueryPageModel page, Expression<Func<T, bool>> predicate)
{
if (null == predicate) {
if (null == predicate)
{
return this.QueryIPage(page);
}
int totalCount = 0;

@ -0,0 +1,408 @@
using System.Reflection;
using Common.Const;
using Entity.Base;
using Entity.Dto.Req;
using Mapster;
using MapsterMapper;
using SqlSugar;
namespace Repository
{
public static class RepositoryExtension
{
/// <summary>
/// 实体假删除 _rep.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static int FakeDelete<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
{
return repository.Context.FakeDelete(entity);
}
/// <summary>
/// 实体假删除 db.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static int FakeDelete<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
{
return db.Updateable(entity).AS().ReSetValue(x => { x.IsDelete = true; })
.IgnoreColumns(ignoreAllNullColumns: true)
.EnableDiffLogEvent() // 记录差异日志
.UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
.ExecuteCommand();
}
/// <summary>
/// 实体集合批量假删除 _rep.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static int FakeDelete<T>(this ISugarRepository repository, List<T> entity) where T : EntityBase, new()
{
return repository.Context.FakeDelete(entity);
}
/// <summary>
/// 实体集合批量假删除 db.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static int FakeDelete<T>(this ISqlSugarClient db, List<T> entity) where T : EntityBase, new()
{
return db.Updateable(entity).AS().ReSetValue(x => { x.IsDelete = true; })
.IgnoreColumns(ignoreAllNullColumns: true)
.EnableDiffLogEvent() // 记录差异日志
.UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
.ExecuteCommand();
}
/// <summary>
/// 实体假删除异步 _rep.FakeDeleteAsync(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
{
return repository.Context.FakeDeleteAsync(entity);
}
/// <summary>
/// 实体假删除 db.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
{
return db.Updateable(entity).AS().ReSetValue(x => { x.IsDelete = true; })
.IgnoreColumns(ignoreAllNullColumns: true)
.EnableDiffLogEvent() // 记录差异日志
.UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
.ExecuteCommandAsync();
}
/// <summary>
/// 实体集合批量假删除异步 _rep.FakeDeleteAsync(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this ISugarRepository repository, List<T> entity) where T : EntityBase, new()
{
return repository.Context.FakeDeleteAsync(entity);
}
/// <summary>
/// 实体集合批量假删除 db.FakeDelete(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> FakeDeleteAsync<T>(this ISqlSugarClient db, List<T> entity) where T : EntityBase, new()
{
return db.Updateable(entity).AS().ReSetValue(x => { x.IsDelete = true; })
.IgnoreColumns(ignoreAllNullColumns: true)
.EnableDiffLogEvent() // 记录差异日志
.UpdateColumns(x => new { x.IsDelete, x.UpdateTime, x.UpdateUserId }) // 允许更新的字段-AOP拦截自动设置UpdateTime、UpdateUserId
.ExecuteCommandAsync();
}
/// <summary>
/// 排序方式(默认降序)
/// </summary>
/// <param name="queryable"></param>
/// <param name="pageInput"> </param>
/// <param name="prefix"> </param>
/// <param name="defaultSortField"> 默认排序字段 </param>
/// <param name="descSort"> 是否降序 </param>
/// <returns> </returns>
public static ISugarQueryable<T> OrderBuilder<T>(this ISugarQueryable<T> queryable, BasePageReq pageInput, string prefix = "", string defaultSortField = "Id", bool descSort = true)
{
// 约定默认每张表都有Id排序
var orderStr = string.IsNullOrWhiteSpace(defaultSortField) ? "" : $"{prefix}{defaultSortField}" + (descSort ? " Desc" : " Asc");
TypeAdapterConfig typeAdapterConfig = new();
typeAdapterConfig.ForType<T, BasePageReq>().IgnoreNullValues(true);
Mapper mapper = new(typeAdapterConfig); // 务必将mapper设为单实例
var nowPagerInput = mapper.Map<BasePageReq>(pageInput);
// 排序是否可用-排序字段和排序顺序都为非空才启用排序
if (!string.IsNullOrEmpty(nowPagerInput.Field) && !string.IsNullOrEmpty(nowPagerInput.OrderDirection))
{
var col = queryable.Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(u => u.PropertyName.Equals(nowPagerInput.Field, StringComparison.CurrentCultureIgnoreCase));
orderStr = col != null
? $"{prefix}{col.DbColumnName} {(nowPagerInput.OrderDirection == nowPagerInput.DescStr ? "Desc" : "Asc")}"
: $"{prefix}{nowPagerInput.Field} {(nowPagerInput.OrderDirection == nowPagerInput.DescStr ? "Desc" : "Asc")}";
}
return queryable.OrderByIF(!string.IsNullOrWhiteSpace(orderStr), orderStr);
}
/// <summary>
/// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <param name="ignoreAllNullColumns"></param>
/// <returns></returns>
public static int UpdateWithDiffLog<T>(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
{
return repository.Context.UpdateWithDiffLog(entity, ignoreAllNullColumns);
}
/// <summary>
/// 更新实体并记录差异日志 _rep.UpdateWithDiffLog(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <param name="ignoreAllNullColumns"></param>
/// <returns></returns>
public static int UpdateWithDiffLog<T>(this ISqlSugarClient db, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
{
return db.Updateable(entity).AS()
.IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns)
.EnableDiffLogEvent()
.ExecuteCommand();
}
/// <summary>
/// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <param name="ignoreAllNullColumns"></param>
/// <returns></returns>
public static Task<int> UpdateWithDiffLogAsync<T>(this ISugarRepository repository, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
{
return repository.Context.UpdateWithDiffLogAsync(entity, ignoreAllNullColumns);
}
/// <summary>
/// 更新实体并记录差异日志 _rep.UpdateWithDiffLogAsync(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <param name="ignoreAllNullColumns"></param>
/// <returns></returns>
public static Task<int> UpdateWithDiffLogAsync<T>(this ISqlSugarClient db, T entity, bool ignoreAllNullColumns = true) where T : EntityBase, new()
{
return db.Updateable(entity)
.IgnoreColumns(ignoreAllNullColumns: ignoreAllNullColumns)
.EnableDiffLogEvent()
.ExecuteCommandAsync();
}
/// <summary>
/// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static int InsertWithDiffLog<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
{
return repository.Context.InsertWithDiffLog(entity);
}
/// <summary>
/// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static int InsertWithDiffLog<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
{
return db.Insertable(entity).AS().EnableDiffLogEvent().ExecuteCommand();
}
/// <summary>
/// 新增实体并记录差异日志 _rep.InsertWithDiffLogAsync(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="repository"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> InsertWithDiffLogAsync<T>(this ISugarRepository repository, T entity) where T : EntityBase, new()
{
return repository.Context.InsertWithDiffLogAsync(entity);
}
/// <summary>
/// 新增实体并记录差异日志 _rep.InsertWithDiffLog(entity)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="db"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static Task<int> InsertWithDiffLogAsync<T>(this ISqlSugarClient db, T entity) where T : EntityBase, new()
{
return db.Insertable(entity).AS().EnableDiffLogEvent().ExecuteCommandAsync();
}
/// <summary>
/// 多库查询
/// </summary>
/// <param name="queryable"></param>
/// <returns> </returns>
public static ISugarQueryable<T> AS<T>(this ISugarQueryable<T> queryable)
{
var info = GetTableInfo<T>();
return queryable.AS<T>($"{info.Item1}.{info.Item2}");
}
/// <summary>
/// 多库查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <param name="queryable"></param>
/// <returns></returns>
public static ISugarQueryable<T, T2> AS<T, T2>(this ISugarQueryable<T, T2> queryable)
{
var info = GetTableInfo<T2>();
return queryable.AS<T2>($"{info.Item1}.{info.Item2}");
}
/// <summary>
/// 多库更新
/// </summary>
/// <param name="updateable"></param>
/// <returns> </returns>
public static IUpdateable<T> AS<T>(this IUpdateable<T> updateable) where T : EntityBase, new()
{
var info = GetTableInfo<T>();
return updateable.AS($"{info.Item1}.{info.Item2}");
}
/// <summary>
/// 多库新增
/// </summary>
/// <param name="insertable"></param>
/// <returns> </returns>
public static IInsertable<T> AS<T>(this IInsertable<T> insertable) where T : EntityBase, new()
{
var info = GetTableInfo<T>();
return insertable.AS($"{info.Item1}.{info.Item2}");
}
/// <summary>
/// 多库删除
/// </summary>
/// <param name="deleteable"></param>
/// <returns> </returns>
public static IDeleteable<T> AS<T>(this IDeleteable<T> deleteable) where T : EntityBase, new()
{
var info = GetTableInfo<T>();
return deleteable.AS($"{info.Item1}.{info.Item2}");
}
/// <summary>
/// 根据实体类型获取表信息
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static Tuple<string, string> GetTableInfo<T>()
{
var entityType = typeof(T);
var attr = entityType.GetCustomAttribute<TenantAttribute>();
var configId = attr == null ? SqlSugarConst.MainConfigId : attr.configId.ToString();
var tableName = entityType.GetCustomAttribute<SugarTable>().TableName;
return new Tuple<string, string>(configId, tableName);
}
/// <summary>
/// 禁用过滤器-适用于更新和删除操作(只对当前请求有效,禁止使用异步)
/// </summary>
/// <param name="repository"></param>
/// <param name="action">禁止异步</param>
/// <returns></returns>
public static void RunWithoutFilter(this ISugarRepository repository, Action action)
{
repository.Context.QueryFilter.ClearAndBackup(); // 清空并备份过滤器
action.Invoke();
repository.Context.QueryFilter.Restore(); // 还原过滤器
// 用例
//_rep.RunWithoutFilter(() =>
//{
// 执行更新或者删除
// 禁止使用异步函数
//});
}
/// <summary>
/// 忽略租户
/// </summary>
/// <param name="queryable"></param>
/// <param name="ignore">是否忽略 默认true</param>
/// <returns> </returns>
public static ISugarQueryable<T> IgnoreTenant<T>(this ISugarQueryable<T> queryable, bool ignore = true)
{
return ignore ? queryable.ClearFilter<ITenantIdFilter>() : queryable;
}
/// <summary>
/// 导航+子表过滤 创建一个扩展函数默认是Class不支持Where
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="thisValue"></param>
/// <param name="whereExpression"></param>
/// <returns></returns>
public static List<T> Where<T>(this T thisValue, Func<T, bool> whereExpression) where T : class, new()
{
return new List<T>() { thisValue };
}
/// <summary>
/// 导航+子表过滤 创建一个扩展函数默认是Class不支持WhereIF
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="thisValue"></param>
/// <param name="isWhere"></param>
/// <param name="whereExpression"></param>
/// <returns></returns>
public static List<T> WhereIF<T>(this T thisValue, bool isWhere, Func<T, bool> whereExpression) where T : class, new()
{
return new List<T>() { thisValue };
}
/// <summary>
/// 只更新某些列
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="R"></typeparam>
/// <param name="updateable"></param>
/// <returns></returns>
public static IUpdateable<T> OnlyUpdateColumn<T, R>(this IUpdateable<T> updateable) where T : EntityBase, new() where R : class, new()
{
if (updateable.UpdateBuilder.UpdateColumns == null)
updateable.UpdateBuilder.UpdateColumns = new List<string>();
foreach (PropertyInfo info in typeof(R).GetProperties())
{
// 判断是否是相同属性
if (typeof(T).GetProperty(info.Name) != null)
updateable.UpdateBuilder.UpdateColumns.Add(info.Name);
}
return updateable;
}
}
}

@ -0,0 +1,16 @@
using Entity.DbModel.System.SysBaseObject;
using HybirdFrameworkCore.Autofac.Attribute;
using SqlSugar;
namespace Repository.System
{
[Scope("SingleInstance")]
public class SysConfigRepository : BaseRepository<SysConfig>
{
private ISqlSugarClient DbBaseClient;
public SysConfigRepository(ISqlSugarClient sqlSugar) : base(sqlSugar)
{
DbBaseClient = sqlSugar;
}
}
}

@ -18,6 +18,7 @@
<PackageReference Include="DotNetty.Handlers" Version="0.7.5" />
<PackageReference Include="DotNetty.Transport" Version="0.7.5" />
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.115" />
</ItemGroup>

@ -0,0 +1,155 @@
using System.ComponentModel;
using Autofac;
using Common.Enum;
using Entity.Base;
using Entity.DbModel.System.SysBaseObject;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Redis;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Repository.System;
using SqlSugar;
namespace Service.System
{
[Scope("SingleInstance")]
public class SysConfigService : BaseServices<SysConfig>
{
private readonly SysConfigRepository _sysConfigRep;
public SysConfigService(SysConfigRepository sysConfigRep)
{
_sysConfigRep = sysConfigRep;
}
RedisHelper redisHelper = AppInfo.Container.Resolve<RedisHelper>();
/// <summary>
/// 获取参数配置分页列表 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<SqlSugarPagedList<SysConfig>> Page(PageConfigReq input)
{
RefAsync<int> total = 0;
var items = await _sysConfigRep.QueryPageAsync(
!string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name),
!string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code),
!string.IsNullOrEmpty(input.GroupCode), u => u.GroupCode.Equals(input.GroupCode),
input.Page, input.PageSize, total, input);
return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize);
}
/// <summary>
/// 获取参数配置列表 🔖
/// </summary>
/// <returns></returns>
[DisplayName("获取参数配置列表")]
public async Task<List<SysConfig>> GetList()
{
return await _sysConfigRep.QueryAsync();
}
/// <summary>
/// 获取分组列表 🔖
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetGroupList()
{
return await _sysConfigRep.QueryByGroupByAsync<string>(u => u.GroupCode, u => u.GroupCode);
}
/// <summary>
/// 增加参数配置 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
//[ApiDescriptionSettings(Name = "Add"), HttpPost]
public async Task<string> AddConfig(AddConfigReq input)
{
string result = "";
var isExist = await _sysConfigRep.QueryByClauseAsync(u => u.Name == input.Name || u.Code == input.Code);
if (isExist != null)
result = "已存在同名或同编码参数配置";
int insertSesult = await _sysConfigRep.InsertAsync(input.Adapt<SysConfig>());
if (insertSesult > 0)
result = "增加参数配置成功";
return result;
}
/// <summary>
/// 更新参数配置 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
// [ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新参数配置")]
public async Task<string> UpdateConfig(UpdateConfigReq input)
{
string result = "";
var isExist = await _sysConfigRep.QueryByClauseAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
if (isExist != null)
result = "已存在同名或同编码参数配置";
var config = input.Adapt<SysConfig>();
int updateResult = await _sysConfigRep.UpdateAsync(config,true);
if (updateResult > 0)
result = "更新参数配置成功";
redisHelper.Remove(config.Code);
return result;
}
/// <summary>
/// 删除参数配置 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
//[ApiDescriptionSettings(Name = "Delete"), HttpPost]
[DisplayName("删除参数配置")]
public async Task<string> DeleteConfig(DeleteConfigReq input)
{
string result = "";
var config = await _sysConfigRep.QueryByClauseAsync(u => u.Id == input.Id);
if (config.SysFlag == YesNoEnum.Y) // 禁止删除系统参数
result = "禁止删除系统参数";
bool deleteResult = await _sysConfigRep.DeleteAsync(config);
if (deleteResult)
result = "删除参数配置成功";
redisHelper.Remove(config.Code);
return result;
}
/// <summary>
/// 批量删除参数配置 🔖
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
// [ApiDescriptionSettings(Name = "BatchDelete"), HttpPost]
[DisplayName("批量删除参数配置")]
public async Task BatchDeleteConfig(List<long> ids)
{
foreach (var id in ids)
{
var config = await _sysConfigRep.QueryByClauseAsync(u => u.Id == id);
if (config.SysFlag == YesNoEnum.Y) // 禁止删除系统参数
continue;
await _sysConfigRep.DeleteAsync(config);
redisHelper.Remove(config.Code);
}
}
/// <summary>
/// 获取参数配置详情 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取参数配置详情")]
public async Task<SysConfig> GetDetail([FromQuery] ConfigReq input)
{
return await _sysConfigRep.QueryByClauseAsync(u => u.Id == input.Id);
}
}
}

@ -0,0 +1,76 @@

using Entity.Base;
using Entity.DbModel.System.SysBaseObject;
using Entity.Dto.Req;
using Microsoft.AspNetCore.Mvc;
using Service.System;
namespace WebStarter.Controllers.System
{
[Produces("application/json")]
[ApiController]
public class SysConfigController
{
private readonly SysConfigService _sysConfigService;
public SysConfigController(SysConfigService sysConfigService)
{
_sysConfigService = sysConfigService;
}
[HttpPost]
[Route("/api/sysConfig/page")]
public async Task<SqlSugarPagedList<SysConfig>> Page(PageConfigReq input)
{
return await _sysConfigService.Page(input);
}
[HttpGet]
[Route("/api/sysConfig/list")]
public async Task<List<SysConfig>> GetList()
{
return await _sysConfigService.GetList();
}
[HttpGet]
[Route("/api/sysConfig/detail")]
public async Task<SysConfig> GetDetail([FromQuery] ConfigReq input)
{
return await _sysConfigService.GetDetail(input);
}
[HttpGet]
[Route("/api/sysConfig/groupList")]
public async Task<List<string>> GetGroupList()
{
return await _sysConfigService.GetGroupList();
}
[HttpPost]
[Route("/api/sysConfig/add")]
public async Task Add(AddConfigReq input)
{
await _sysConfigService.AddConfig(input);
}
[HttpPost]
[Route("/api/sysConfig/update")]
public async Task Update(UpdateConfigReq input)
{
await _sysConfigService.UpdateConfig(input);
}
[HttpPost]
[Route("/api/sysConfig/delete")]
public async Task Delete(DeleteConfigReq input)
{
await _sysConfigService.DeleteConfig(input);
}
[HttpPost]
[Route("/api/sysConfig/batchDelete")]
public async Task BatchDelete(List<long> input)
{
await _sysConfigService.BatchDeleteConfig(input);
}
}
}
Loading…
Cancel
Save