feat 添加商品评价接口

This commit is contained in:
lwh 2023-07-18 13:15:44 +08:00
parent aa3f9191f9
commit 14a1dfcd6d
7 changed files with 506 additions and 1 deletions

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using ARW.Model.Models.Business.GoodsManager.GoodsComments;
namespace ARW.Model.Dto.Api.GoodsManager.GoodsComments
{
/// <summary>
/// 商品评价查询对象Api
///
/// @author admin
/// @date 2023-07-17
/// </summary>
public class GoodsCommentQueryDtoApi : PagerInfo
{
public long? ShopGuid { get; set; }
[Required(ErrorMessage = "商品Id不能为空")]
public long SpuId { get; set; }
public int? GoodsCommentRatingType { get; set; }
}
/// <summary>
/// 商品详情页评价输入对象Api
///
/// @author admin
/// @date 2023-07-17
/// </summary>
public class GoodsCommentDtoApi
{
[Required(ErrorMessage = "商品Id不能为空")]
public long SpuId { get; set; }
}
}

View File

@ -0,0 +1,101 @@
using Newtonsoft.Json;
using OfficeOpenXml.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace ARW.Model.Vo.Api.GoodsManager.GoodsComments
{
/// <summary>
/// 商品评价展示对象Api
///
/// @author admin
/// @date 2023-07-17
/// </summary>
public class GoodsCommentVoApi
{
/// <summary>
/// 评价Id
/// </summary>
public int Uid { get; set; }
/// <summary>
/// 描述 : 商品Id
/// </summary>
[JsonConverter(typeof(ValueToStringConverter))]
public long SpuId { get; set; }
/// <summary>
/// 描述 : 商品SkuId
/// </summary>
[JsonConverter(typeof(ValueToStringConverter))]
public long SkuId { get; set; }
/// <summary>
/// 描述 : 商品规格
/// </summary>
public long SpecInfo { get; set; }
/// <summary>
/// 描述 :评分分数
/// </summary>
public float CommentScore { get; set; }
/// <summary>
/// 描述 :评价内容
/// </summary>
public string CommentContent { get; set; }
/// <summary>
/// 描述 :评价图片
/// </summary>
public string GoodsCommentImages { get; set; }
/// <summary>
/// 描述 :评价
/// </summary>
public List<ResourcesItem> CommentResources { get; set; }
/// <summary>
/// 描述 : 评价用户名称
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 描述 :评价用户头像
/// </summary>
public string UserHeadUrl { get; set; }
/// <summary>
/// 描述 : 是否匿名
/// </summary>
public int IsAnonymity { get; set; }
/// <summary>
/// 描述 : 回复
/// </summary>
public string SellerReply { get; set; }
/// <summary>
/// 商品所选Sku
/// </summary>
public string GoodsDetailInfo { get; set; }
public string CommentTime { get; set; }
}
public class ResourcesItem
{
public string Src { get; set; }
public string Type { get; set; }
public string CoverSrc { get; set; }
}
}

View File

@ -0,0 +1,50 @@
using Newtonsoft.Json;
using OfficeOpenXml.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace ARW.Model.Vo.Api.GoodsManager.GoodsComments
{
/// <summary>
/// 商品详情页评论数展示对象Api
///
/// @author lwh
/// @date 2023-07-18
/// </summary>
public class GoodsCommentCountApiVo
{
/// <summary>
/// 评价总数
/// </summary>
public int CommentCount { get; set; }
/// <summary>
/// 描述 : 差评总数
/// </summary>
public int BadCount { get; set; }
/// <summary>
/// 描述 : 中评总数
/// </summary>
public int MiddleCount { get; set; }
/// <summary>
/// 描述 : 好评总数
/// </summary>
public int GoodCount { get; set; }
/// <summary>
/// 描述 : 带图总数
/// </summary>
public int HasImageCount { get; set; }
/// <summary>
/// 好评率
/// </summary>
public decimal GoodRate { get; set; }
}
}

View File

@ -0,0 +1,178 @@
using Infrastructure.Attribute;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ARW.Model;
using ARW.Repository;
using ARW.Repository.Business.GoodsManager.GoodsComments;
using ARW.Service.Api.IBusinessService.GoodsManager.GoodsComments;
using ARW.Model.Dto.Api.GoodsManager.GoodsComments;
using ARW.Model.Models.Business.GoodsManager.GoodsComments;
using ARW.Model.Vo.Api.GoodsManager.GoodsComments;
using ARW.Model.Models.Business.Custom.Customers;
using ARW.Model.Models.Business.GoodsManager.Goodss;
using ARW.Model.Models.Business.ShopManager.Shops;
using System.IO;
using ARW.Model.System;
namespace ARW.Service.Api.BusinessService.GoodsManager.GoodsComments
{
/// <summary>
/// 商品评价接口实现类Api
///
/// @author admin
/// @date 2023-07-17
/// </summary>
[AppService(ServiceType = typeof(IGoodsCommentServiceApi), ServiceLifetime = LifeTime.Transient)]
public class GoodsCommentServiceImplApi : BaseService<GoodsComment>, IGoodsCommentServiceApi
{
private readonly GoodsCommentRepository _GoodsCommentRepository;
public GoodsCommentServiceImplApi(GoodsCommentRepository GoodsCommentRepository)
{
this._GoodsCommentRepository = GoodsCommentRepository;
}
#region Api接口代码
/// <summary>
/// 查询商品评价列表(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<PagedInfo<GoodsCommentVoApi>> GetGoodsCommentListApi(GoodsCommentQueryDtoApi parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create<GoodsComment>();
predicate = predicate.AndIF(parm.ShopGuid != null, s => s.ShopGuid == parm.ShopGuid);
predicate = predicate.AndIF(parm.SpuId != 0, s => s.GoodsGuid == parm.SpuId);
predicate = predicate.AndIF(parm.GoodsCommentRatingType != null, s => s.GoodsCommentRatingType == parm.GoodsCommentRatingType);
var query = _GoodsCommentRepository
.Queryable()
.LeftJoin<Shop>((s, c) => s.ShopGuid == c.ShopGuid)
.LeftJoin<Goods>((s, c, d) => s.GoodsGuid == d.GoodsGuid)
.LeftJoin<Customer>((s, c, d, f) => s.CustomerGuid == f.CustomerGuid)
.Where(predicate.ToExpression())
.OrderBy(s => s.GoodsCommentSort, OrderByType.Desc)
.Select((s, c, d, f) => new GoodsCommentVoApi
{
Uid = s.GoodsCommentId,
SpuId = s.GoodsGuid,
CommentScore = s.GoodsCommentRating,
CommentContent = s.GoodsCommentContent,
GoodsCommentImages = s.GoodsCommentImages,
UserName = f.CustomerNickname,
UserHeadUrl = f.CustomerAvatar,
SellerReply = s.GoodsCommentRecoverContent,
GoodsDetailInfo = "",
CommentTime = s.Create_time.ToString("yyyy/MM/dd HH:mm"),
});
var list = await query.ToPageAsync(parm);
foreach (var item in list.Result)
{
if (!string.IsNullOrEmpty(item.GoodsCommentImages))
{
var imagesList = item.GoodsCommentImages.Split(',');
var resourcesList = new List<ResourcesItem>();
foreach (var image in imagesList)
{
var resourcesItem = new ResourcesItem
{
Src = image,
Type = "",
CoverSrc = ""
};
string extension = Path.GetExtension(image).ToLower();
if (extension == ".mp4" || extension == ".avi")
{
resourcesItem.Type = "video";
// 解析视频封面并赋值给 CoverSrc
// 你可以根据具体需求调用相应的方法来获取视频封面
//resourcesItem.CoverSrc = GetVideoCover(image);
}
else
{
resourcesItem.Type = "image";
}
resourcesList.Add(resourcesItem);
}
item.CommentResources = resourcesList;
}
}
return list;
}
/// <summary>
/// 查询商品评价详情(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<string> GetGoodsDetailsComments(GoodsCommentDtoApi parm)
{
var query = _GoodsCommentRepository
.Queryable()
.LeftJoin<Shop>((s, c) => s.ShopGuid == c.ShopGuid)
.LeftJoin<Goods>((s, c, d) => s.GoodsGuid == d.GoodsGuid)
.LeftJoin<Customer>((s, c, d, f) => s.CustomerGuid == f.CustomerGuid)
.Where(s => s.GoodsGuid == parm.SpuId)
.OrderBy(s => s.GoodsCommentSort) // 按 GoodsCommentSort 升序排序
.Select((s, c, d, f) => new GoodsCommentVoApi
{
Uid = s.GoodsCommentId,
SpuId = s.GoodsGuid,
CommentScore = s.GoodsCommentRating,
CommentContent = s.GoodsCommentContent,
GoodsCommentImages = s.GoodsCommentImages,
UserName = f.CustomerNickname,
UserHeadUrl = f.CustomerAvatar,
SellerReply = s.GoodsCommentRecoverContent,
GoodsDetailInfo = "",
}).Take(1);
return await query.ToJsonAsync();
}
/// <summary>
/// 获取商品详情页评论数(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<GoodsCommentCountApiVo> GetGoodsDetailsCommentsCount(GoodsCommentDtoApi parm)
{
var query = _GoodsCommentRepository
.Queryable()
.Select((s) => new GoodsCommentCountApiVo
{
CommentCount = SqlFunc.Subqueryable<GoodsComment>().Where(s => s.GoodsGuid == parm.SpuId).Count(),
BadCount = SqlFunc.Subqueryable<GoodsComment>().Where(s => s.GoodsCommentRatingType == 3 && s.GoodsGuid == parm.SpuId).Count(),
MiddleCount = SqlFunc.Subqueryable<GoodsComment>().Where(s => s.GoodsCommentRatingType == 2 && s.GoodsGuid == parm.SpuId).Count(),
GoodCount = SqlFunc.Subqueryable<GoodsComment>().Where(s => s.GoodsCommentRatingType == 1 && s.GoodsGuid == parm.SpuId).Count(),
HasImageCount = SqlFunc.Subqueryable<GoodsComment>().Where(s => s.GoodsCommentImages != "" && s.GoodsCommentImages != null && s.GoodsGuid == parm.SpuId).Count()
});
var res = await query.FirstAsync();
res.GoodRate = Math.Round((decimal)res.GoodCount / res.CommentCount, 4) * 100;
return res;
}
#endregion
}
}

View File

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ARW.Model;
using ARW.Model.Dto.Api.GoodsManager.GoodsComments;
using ARW.Model.Models.Business.GoodsManager.GoodsComments;
using ARW.Model.Vo.Api.GoodsManager.GoodsComments;
namespace ARW.Service.Api.IBusinessService.GoodsManager.GoodsComments
{
/// <summary>
/// 商品评价接口类Api
///
/// @author admin
/// @date 2023-07-17
/// </summary>
public interface IGoodsCommentServiceApi : IBaseService<GoodsComment>
{
/// <summary>
/// 获取商品评价分页列表(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
Task<PagedInfo<GoodsCommentVoApi>> GetGoodsCommentListApi(GoodsCommentQueryDtoApi parm);
/// <summary>
/// 获取商品评价详情(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
Task<string> GetGoodsDetailsComments(GoodsCommentDtoApi parm);
/// <summary>
/// 获取商品详情页评论数(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
Task<GoodsCommentCountApiVo> GetGoodsDetailsCommentsCount(GoodsCommentDtoApi parm);
}
}

View File

@ -0,0 +1,94 @@
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ARW.Admin.WebApi.Extensions;
using ARW.Admin.WebApi.Filters;
using ARW.Common;
using ARW.Admin.WebApi.Controllers;
using ARW.Model.Dto.Api.GoodsManager.GoodsComments;
using ARW.Service.Api.IBusinessService.GoodsManager.GoodsComments;
using ARW.Model.Models.Business.GoodsManager.GoodsComments;
using ARW.Model.Vo.Api.GoodsManager.GoodsComments;
using Microsoft.AspNetCore.Authorization;
using Geocoding;
namespace ARW.WebApi.Controllers.Api.GoodsManager.GoodsComments
{
/// <summary>
/// 商品评价控制器Api
///
/// @author admin
/// @date 2023-07-17
/// </summary>
//[Verify]
[Route("api/[controller]")]
public class GoodsCommentApiController : BaseController
{
private readonly IGoodsCommentServiceApi _GoodsCommentServiceApi;
/// <summary>
/// 依赖注入
/// </summary>
/// <param name="GoodsCommentServiceApi">商品评价商品评价Api服务</param>
public GoodsCommentApiController(IGoodsCommentServiceApi GoodsCommentServiceApi)
{
_GoodsCommentServiceApi = GoodsCommentServiceApi;
}
/// <summary>
/// 获取商品评价列表(Api)
/// </summary>
/// <param name="parm">查询参数</param>
/// <returns></returns>
[HttpGet("getGoodsCommentList")]
public async Task<IActionResult> GetGoodsCommentListApi([FromQuery] GoodsCommentQueryDtoApi parm)
{
var res = await _GoodsCommentServiceApi.GetGoodsCommentListApi(parm);
return SUCCESS(res);
}
/// <summary>
/// 获取商品详情页评论(Api)
/// </summary>
/// <param name="parm">查询参数</param>
/// <returns></returns>
[HttpGet("getGoodsDetailsComments")]
public async Task<IActionResult> GetGoodsDetailsComments([FromQuery] GoodsCommentDtoApi parm)
{
//if (parm == null) throw new CustomException("参数错误!");
var res = await _GoodsCommentServiceApi.GetGoodsDetailsComments(parm);
if (res != "[]")
{
res = res.Remove(0, 1);
res = res.Substring(0, res.Length - 1);
var data = res.FromJSON<GoodsCommentVoApi>();
return SUCCESS(data);
}
else
{
return SUCCESS(res);
}
}
/// <summary>
/// 获取商品详情页评论数(Api)
/// </summary>
/// <param name="parm">查询参数</param>
/// <returns></returns>
[HttpGet("getGoodsDetailsCommentsCount")]
public async Task<IActionResult> GetGoodsDetailsCommentsCount([FromQuery] GoodsCommentDtoApi parm)
{
var res = await _GoodsCommentServiceApi.GetGoodsDetailsCommentsCount(parm);
return SUCCESS(res);
}
}
}

View File

@ -147,7 +147,7 @@ $end
}).Take(1);
return query.ToJsonAsync();
return await query.ToJsonAsync();
}