Optimize PageModel

This commit is contained in:
__Leo__ 2022-03-04 15:31:33 +08:00
parent 831299929e
commit d166b02cd5
3 changed files with 55 additions and 26 deletions

View File

@ -59,20 +59,15 @@ namespace Blog.Core.Controllers
}; };
} }
[NonAction] [NonAction]
public MessageModel<PageModel<T>> SuccessPage<T>(int page, int dataCount, List<T> data, int pageCount, string msg = "获取成功") public MessageModel<PageModel<T>> SuccessPage<T>(int page, int dataCount, int pageSize, List<T> data, int pageCount, string msg = "获取成功")
{ {
return new MessageModel<PageModel<T>>() return new MessageModel<PageModel<T>>()
{ {
success = true, success = true,
msg = msg, msg = msg,
response = new PageModel<T>() response = new PageModel<T>(page, dataCount, pageSize, data)
{
page = page,
dataCount = dataCount,
data = data,
pageCount = pageCount,
}
}; };
} }
[NonAction] [NonAction]
@ -83,13 +78,7 @@ namespace Blog.Core.Controllers
{ {
success = true, success = true,
msg = msg, msg = msg,
response = new PageModel<T>() response = pageModel
{
page = pageModel.page,
dataCount = pageModel.dataCount,
data = pageModel.data,
pageCount = pageModel.pageCount,
}
}; };
} }
} }

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using AutoMapper;
using System;
using System.Collections.Generic;
namespace Blog.Core.Model namespace Blog.Core.Model
{ {
@ -14,7 +16,7 @@ namespace Blog.Core.Model
/// <summary> /// <summary>
/// 总页数 /// 总页数
/// </summary> /// </summary>
public int pageCount { get; set; } = 6; public int pageCount => (int)Math.Ceiling((decimal)dataCount / PageSize);
/// <summary> /// <summary>
/// 数据总数 /// 数据总数
/// </summary> /// </summary>
@ -22,12 +24,53 @@ namespace Blog.Core.Model
/// <summary> /// <summary>
/// 每页大小 /// 每页大小
/// </summary> /// </summary>
public int PageSize { set; get; } public int PageSize { set; get; } = 20;
/// <summary> /// <summary>
/// 返回数据 /// 返回数据
/// </summary> /// </summary>
public List<T> data { get; set; } public List<T> data { get; set; }
public PageModel() { }
public PageModel(int page, int dataCount, int pageSize, List<T> data)
{
this.page = page;
this.dataCount = dataCount;
PageSize = pageSize;
this.data = data;
}
public PageModel<TOut> ConvertTo<TOut>()
{
return new PageModel<TOut>(page, dataCount, PageSize, default);
}
public PageModel<TOut> ConvertTo<TOut>(IMapper mapper)
{
var model = ConvertTo<TOut>();
if (data != null)
{
model.data = mapper.Map<List<TOut>>(data);
}
return model;
}
public PageModel<TOut> ConvertTo<TOut>(IMapper mapper, Action<IMappingOperationOptions> options)
{
var model = ConvertTo<TOut>();
if (data != null)
{
model.data = mapper.Map<List<TOut>>(data, options);
}
return model;
}
} }
} }

View File

@ -453,8 +453,7 @@ namespace Blog.Core.Repository.Base
.WhereIF(whereExpression != null, whereExpression) .WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount); .ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel<TEntity>(intPageIndex, totalCount, intPageSize, list);
return new PageModel<TEntity>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
} }
@ -510,8 +509,7 @@ namespace Blog.Core.Repository.Base
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression) .WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount); .ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel<TResult>(intPageIndex, totalCount, intPageSize, list);
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
} }
/// <summary> /// <summary>
@ -543,8 +541,7 @@ namespace Blog.Core.Repository.Base
.OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds) .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
.WhereIF(whereExpression != null, whereExpression) .WhereIF(whereExpression != null, whereExpression)
.ToPageListAsync(intPageIndex, intPageSize, totalCount); .ToPageListAsync(intPageIndex, intPageSize, totalCount);
int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt(); return new PageModel<TResult>(intPageIndex, totalCount, intPageSize, list);
return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
} }
//var exp = Expressionable.Create<ProjectToUser>() //var exp = Expressionable.Create<ProjectToUser>()