【优化】优化前端获取随机推荐商品数据的检索方式,手动随机改成sqlsugar数据库随机。

This commit is contained in:
jianweie code
2024-06-07 23:27:41 +08:00
parent 24f532ff0c
commit ca2502d319

View File

@@ -888,133 +888,43 @@ namespace CoreCms.Net.Repository
/// <returns></returns> /// <returns></returns>
public async Task<List<GoodListDTO>> GetGoodsRecommendList(int number, bool isRecommend = false) public async Task<List<GoodListDTO>> GetGoodsRecommendList(int number, bool isRecommend = false)
{ {
var list = new List<GoodListDTO>(); var list = await DbClient.Queryable<CoreCmsGoods, CoreCmsProducts>((good, pd) => new JoinQueryInfos(
JoinType.Left, good.id == pd.goodsId))
if (isRecommend) .Where((good, pd) => pd.isDefalut == true && pd.isDel == false && good.isRecommend == isRecommend && good.isDel == false && good.isMarketable == true)
{ .Select((good, pd) => new GoodListDTO
var ids = await DbClient.Queryable<CoreCmsGoods>().Where(p => p.isDel == false && p.isMarketable == true && p.isRecommend == true)
.Select(p => p.id).ToArrayAsync();
var dbIds = new List<int>();
if (!ids.Any()) return list;
{ {
if (ids.Length < number) id = good.id,
{ name = good.name,
number = ids.Length; brief = good.brief,
} image = !SqlFunc.IsNullOrEmpty(good.image) ? good.image : EmptyOrNullImagePath.GoodImage,
video = good.video,
goodsCategoryId = good.goodsCategoryId,
goodsTypeId = good.goodsTypeId,
brandId = good.brandId,
isNomalVirtual = good.isNomalVirtual,
unit = good.unit,
commentsCount = good.commentsCount,
viewCount = good.viewCount,
buyCount = SqlFunc.Subqueryable<CoreCmsOrderItem>()
.LeftJoin<CoreCmsOrder>((sOrderItem, sOrder) => sOrder.orderId == sOrderItem.orderId)
.Where((sOrderItem, sOrder) => sOrderItem.goodsId == good.id && (sOrder.payStatus == (int)GlobalEnumVars.OrderPayStatus.Yes || sOrder.payStatus == (int)GlobalEnumVars.OrderPayStatus.PartialYes))
.Sum((sOrderItem, sOrder) => sOrderItem.nums),
sort = good.sort,
labelIds = good.labelIds,
createTime = good.createTime,
isRecommend = good.isRecommend,
isHot = good.isHot,
price = pd.price,
mktprice = pd.mktprice,
stock = pd.stock,
pointsDeduction = pd.pointsDeduction,
points = pd.points,
weight = pd.weight,
initialSales = good.initialSales,
})
.MergeTable()
.OrderBy(p => SqlFunc.GetRandom()).Take(number).ToListAsync();
for (int i = 0; i < number; i++)
{
var id = GetRandomNumber(ids);
while (dbIds.Contains(id))//判断集合中有没有生成的随机数如果有则重新生成一个随机数直到生成的随机数list集合中没有才退出循环
{
id = GetRandomNumber(ids);
}
dbIds.Add(id);
}
if (dbIds.Any())
{
list = await DbClient.Queryable<CoreCmsGoods, CoreCmsProducts>((good, pd) => new JoinQueryInfos(
JoinType.Left, good.id == pd.goodsId))
.Where((good, pd) => pd.isDefalut == true && pd.isDel == false && good.isRecommend == true && good.isDel == false && good.isMarketable == true)
.Select((good, pd) => new GoodListDTO
{
id = good.id,
name = good.name,
brief = good.brief,
image = !SqlFunc.IsNullOrEmpty(good.image) ? good.image : EmptyOrNullImagePath.GoodImage,
video = good.video,
goodsCategoryId = good.goodsCategoryId,
goodsTypeId = good.goodsTypeId,
brandId = good.brandId,
isNomalVirtual = good.isNomalVirtual,
unit = good.unit,
commentsCount = good.commentsCount,
viewCount = good.viewCount,
buyCount = SqlFunc.Subqueryable<CoreCmsOrderItem>()
.LeftJoin<CoreCmsOrder>((sOrderItem, sOrder) => sOrder.orderId == sOrderItem.orderId)
.Where((sOrderItem, sOrder) => sOrderItem.goodsId == good.id && (sOrder.payStatus == (int)GlobalEnumVars.OrderPayStatus.Yes || sOrder.payStatus == (int)GlobalEnumVars.OrderPayStatus.PartialYes))
.Sum((sOrderItem, sOrder) => sOrderItem.nums),
sort = good.sort,
labelIds = good.labelIds,
createTime = good.createTime,
isRecommend = good.isRecommend,
isHot = good.isHot,
price = pd.price,
mktprice = pd.mktprice,
stock = pd.stock,
pointsDeduction = pd.pointsDeduction,
points = pd.points,
weight = pd.weight,
initialSales = good.initialSales,
})
.MergeTable()
.Where(p => dbIds.Contains(p.id)).ToListAsync();
}
}
}
else
{
var ids = await DbClient.Queryable<CoreCmsGoods>().Where(p => p.isDel == false && p.isMarketable == true)
.Select(p => p.id).ToArrayAsync();
var dbIds = new List<int>();
if (!ids.Any()) return list;
{
if (ids.Length < number)
{
number = ids.Length;
}
for (int i = 0; i < number; i++)
{
var id = GetRandomNumber(ids);
while (dbIds.Contains(id))//判断集合中有没有生成的随机数如果有则重新生成一个随机数直到生成的随机数list集合中没有才退出循环
{
id = GetRandomNumber(ids);
}
dbIds.Add(id);
}
if (dbIds.Any())
{
list = await DbClient.Queryable<CoreCmsGoods, CoreCmsProducts>((good, pd) => new JoinQueryInfos(
JoinType.Left, good.id == pd.goodsId))
.Where((good, pd) => pd.isDefalut == true && pd.isDel == false && good.isRecommend == true && good.isDel == false && good.isMarketable == true)
.Select((good, pd) => new GoodListDTO
{
id = good.id,
name = good.name,
brief = good.brief,
image = !SqlFunc.IsNullOrEmpty(good.image) ? good.image : EmptyOrNullImagePath.GoodImage,
video = good.video,
goodsCategoryId = good.goodsCategoryId,
goodsTypeId = good.goodsTypeId,
brandId = good.brandId,
isNomalVirtual = good.isNomalVirtual,
unit = good.unit,
commentsCount = good.commentsCount,
viewCount = good.viewCount,
buyCount = SqlFunc.Subqueryable<CoreCmsOrderItem>()
.LeftJoin<CoreCmsOrder>((sOrderItem, sOrder) => sOrder.orderId == sOrderItem.orderId)
.Where((sOrderItem, sOrder) => sOrderItem.goodsId == good.id && (sOrder.payStatus == (int)GlobalEnumVars.OrderPayStatus.Yes || sOrder.payStatus == (int)GlobalEnumVars.OrderPayStatus.PartialYes))
.Sum((sOrderItem, sOrder) => sOrderItem.nums),
sort = good.sort,
labelIds = good.labelIds,
createTime = good.createTime,
isRecommend = good.isRecommend,
isHot = good.isHot,
price = pd.price,
mktprice = pd.mktprice,
stock = pd.stock,
pointsDeduction = pd.pointsDeduction,
points = pd.points,
weight = pd.weight,
initialSales = good.initialSales,
})
.MergeTable()
.Where(p => dbIds.Contains(p.id)).ToListAsync();
}
}
}
return list; return list;
} }