模块化
This commit is contained in:
132
Service/Implement/BaseService.cs
Normal file
132
Service/Implement/BaseService.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using ORM;
|
||||
using Service.Interface;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Service.Implement
|
||||
{
|
||||
public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : BaseEntity, new()
|
||||
{
|
||||
protected readonly SqlSugarRepository<TEntity> _repository;
|
||||
|
||||
public BaseService(SqlSugarRepository<TEntity> repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询全部
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetAllAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities.ToListAsync();
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("查询所有数据失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据日期查询全部
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetAllAsyncByDate(DateTime? startDate, DateTime? endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities.Where(x => x.CreateTime >= startDate && x.CreateTime <= endDate).ToListAsync();
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("查询所有数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.OrderBy(d => d.Id)
|
||||
.ToPageListAsync(pageIndex, pageSize, total);
|
||||
total.Value = (int)Math.Ceiling((double)total.Value / pageSize);
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("分页查询数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询,并可以根据日期范围进行过滤
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, DateTime? startDate, DateTime? endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.Where(x=>x.CreateTime>=startDate&&x.CreateTime <= endDate)
|
||||
.OrderBy(d => d.Id)
|
||||
.ToPageListAsync(pageIndex, pageSize, total);
|
||||
total.Value = (int)Math.Ceiling((double)total.Value / pageSize);
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("分页查询数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入单条数据
|
||||
/// </summary>
|
||||
public virtual async Task<Result<bool>> InsertAsync(TEntity entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _repository.Context.Insertable(entity).ExecuteCommandAsync();
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("插入数据失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除单条数据(根据 Id)
|
||||
/// </summary>
|
||||
/// <param name="id">主键 Id</param>
|
||||
public virtual async Task<Result<bool>> DeleteAsync(long id)
|
||||
{
|
||||
if (id <= 0)
|
||||
return Result<bool>.Error("主键 Id 无效,无法删除");
|
||||
|
||||
try
|
||||
{
|
||||
// 删除实体
|
||||
var result = await _repository.Context
|
||||
.Deleteable<TEntity>()
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("删除数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
90
Service/Implement/UserService.cs
Normal file
90
Service/Implement/UserService.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using ORM;
|
||||
using Service.Implement;
|
||||
|
||||
public class UserService: BaseService<UserEntity>
|
||||
{
|
||||
public UserService(SqlSugarRepository<UserEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Result<UserEntity>> GetUserByUserNameAsync(string username)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(username))
|
||||
return Result<UserEntity>.Error("用户名不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
var user = await _repository.Entities
|
||||
.Where(x => x.UserName == username)
|
||||
.FirstAsync();
|
||||
|
||||
if (user == null)
|
||||
return Result<UserEntity>.Error("用户不存在");
|
||||
|
||||
return Result<UserEntity>.Success(user);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<UserEntity>.Error("获取用户失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task<Result<bool>> InsertAsync(UserEntity entity)
|
||||
{
|
||||
if (entity == null)
|
||||
return Result<bool>.Error("用户数据不能为空");
|
||||
|
||||
try
|
||||
{
|
||||
// 唯一性校验
|
||||
bool exists = await _repository.Entities
|
||||
.AnyAsync(x => x.UserName == entity.UserName);
|
||||
|
||||
if (exists)
|
||||
return Result<bool>.Error("用户名已存在");
|
||||
|
||||
var result = await _repository.InsertAsync(entity);
|
||||
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("插入数据失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 更新单条用户记录
|
||||
/// </summary>
|
||||
/// <param name="entity">要更新的用户实体,Id 必须有值</param>
|
||||
public async Task<Result<bool>> UpdateAsync(UserEntity entity)
|
||||
{
|
||||
if (entity == null)
|
||||
return Result<bool>.Error("用户数据不能为空");
|
||||
|
||||
if (entity.Id <= 0)
|
||||
return Result<bool>.Error("主键 Id 无效,无法更新");
|
||||
|
||||
try
|
||||
{
|
||||
// 唯一性校验(可选,如果允许改用户名就检查)
|
||||
bool exists = await _repository.Entities
|
||||
.Where(x => x.UserName == entity.UserName && x.Id != entity.Id)
|
||||
.AnyAsync();
|
||||
|
||||
if (exists)
|
||||
return Result<bool>.Error("用户名已存在");
|
||||
|
||||
// 更新整个实体
|
||||
var result = await _repository.UpdateAsync(entity);
|
||||
|
||||
return Result<bool>.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("更新用户失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user