2024-09-02 09:31:34 +08:00
|
|
|
|
using Bunny.Common.Context;
|
|
|
|
|
using Bunny.Common.Utils.Net;
|
|
|
|
|
using Bunny.Dao.Dto.System;
|
2024-08-30 16:08:08 +08:00
|
|
|
|
using Bunny.Dao.Entity.System;
|
2024-09-02 09:00:50 +08:00
|
|
|
|
using Bunny.Dao.Model.Result;
|
2024-09-02 09:31:34 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2024-08-09 00:16:40 +08:00
|
|
|
|
|
|
|
|
|
namespace Bunny.Service.IService.Service;
|
|
|
|
|
|
|
|
|
|
public class BlogService : IBlogService
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
[Inject] public required EfCoreContext DbContext { get; set; }
|
2024-08-09 00:16:40 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 添加Blog
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dto"></param>
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
public void AddBlog(Blog dto)
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
DbContext.Add(dto);
|
|
|
|
|
DbContext.SaveChanges();
|
2024-08-09 00:16:40 +08:00
|
|
|
|
}
|
2024-08-09 09:26:32 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询BLog
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<Blog> QueryBlog()
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
return DbContext.Blogs.ToList();
|
2024-08-09 09:26:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 更新Blog内容
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dto"></param>
|
2024-09-02 09:31:34 +08:00
|
|
|
|
public void UpdateBlog(BlogUpdateDto dto)
|
2024-08-09 09:26:32 +08:00
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var blog = new Blog();
|
|
|
|
|
NetUtil.CopyProperties(dto, blog);
|
|
|
|
|
|
|
|
|
|
DbContext.Blogs.Update(blog);
|
|
|
|
|
DbContext.SaveChanges();
|
2024-08-09 09:26:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除BLog
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
public void DeleteBlog(string id)
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var blog = new Blog { Id = Guid.NewGuid() };
|
|
|
|
|
DbContext.Blogs.Remove(blog);
|
|
|
|
|
DbContext.SaveChanges();
|
2024-08-09 09:26:32 +08:00
|
|
|
|
}
|
2024-08-10 04:18:03 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量添加数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url"></param>
|
|
|
|
|
public void AddBatchBlogs(string url)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<Blog>();
|
|
|
|
|
for (var i = 0; i <= 100000; i++)
|
|
|
|
|
list.Add(new Blog
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
|
UpdateTime = DateTime.Now,
|
|
|
|
|
UpdateUserId = Random.Shared.Next(),
|
|
|
|
|
CreateUserId = Random.Shared.NextInt64()
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-02 09:31:34 +08:00
|
|
|
|
DbContext.Blogs.AddRange(list);
|
|
|
|
|
DbContext.SaveChanges();
|
2024-08-10 04:18:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量删除BLog
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void DeleteBatchBlogs()
|
|
|
|
|
{
|
|
|
|
|
var list = new List<Blog>();
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var blog = new Blog();
|
2024-08-10 04:18:03 +08:00
|
|
|
|
list.Add(blog);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 09:31:34 +08:00
|
|
|
|
DbContext.Blogs.RemoveRange(list);
|
|
|
|
|
DbContext.SaveChanges();
|
2024-08-10 04:18:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 批量更新_带事务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UseTransaction()
|
|
|
|
|
{
|
|
|
|
|
// 还可以使用异步的
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var transaction = DbContext.Database.BeginTransaction();
|
2024-08-10 04:18:03 +08:00
|
|
|
|
|
|
|
|
|
// 执行批量更新操作
|
|
|
|
|
var list = new List<Blog>();
|
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var blog = new Blog { Url = "https://learn.microsoft.com/zh-cn/ef/core/saving/transactions" };
|
2024-08-10 04:18:03 +08:00
|
|
|
|
list.Add(blog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新内容
|
2024-09-02 09:31:34 +08:00
|
|
|
|
DbContext.Blogs.UpdateRange(list);
|
|
|
|
|
DbContext.SaveChanges();
|
2024-08-10 04:18:03 +08:00
|
|
|
|
|
|
|
|
|
// 还可以使用异步方式
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 分页查询
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="page"></param>
|
|
|
|
|
/// <param name="limit"></param>
|
|
|
|
|
public PageResult<Blog> QueryPage(int page, int limit)
|
|
|
|
|
{
|
2024-09-02 09:31:34 +08:00
|
|
|
|
var items = DbContext.Blogs.Take(limit).Skip(page).ToList();
|
2024-08-10 04:18:03 +08:00
|
|
|
|
var total = items.Count;
|
|
|
|
|
var pages = (int)Math.Ceiling(total / (double)limit);
|
|
|
|
|
|
|
|
|
|
var blogs = items.Skip((page - 1) * limit).Take(limit).ToList();
|
|
|
|
|
|
|
|
|
|
return new PageResult<Blog>
|
|
|
|
|
{
|
|
|
|
|
PageNo = page,
|
|
|
|
|
Total = total,
|
|
|
|
|
PageSize = limit,
|
|
|
|
|
Pages = pages,
|
|
|
|
|
Data = blogs
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-08-09 00:16:40 +08:00
|
|
|
|
}
|