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 { /// /// 商品评价接口实现类Api /// /// @author admin /// @date 2023-07-17 /// [AppService(ServiceType = typeof(IGoodsCommentServiceApi), ServiceLifetime = LifeTime.Transient)] public class GoodsCommentServiceImplApi : BaseService, IGoodsCommentServiceApi { private readonly GoodsCommentRepository _GoodsCommentRepository; public GoodsCommentServiceImplApi(GoodsCommentRepository GoodsCommentRepository) { this._GoodsCommentRepository = GoodsCommentRepository; } #region Api接口代码 /// /// 查询商品评价列表(Api) /// /// /// public async Task> GetGoodsCommentListApi(GoodsCommentQueryDtoApi parm) { //开始拼装查询条件d var predicate = Expressionable.Create(); 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.CommentLevel != 0, s => s.GoodsCommentRatingType == parm.CommentLevel); predicate = predicate.AndIF(parm.HasImage == true, s => s.GoodsCommentImages != "" && s.GoodsCommentImages != null); var query = _GoodsCommentRepository .Queryable() .LeftJoin((s, c) => s.ShopGuid == c.ShopGuid) .LeftJoin((s, c, d) => s.GoodsGuid == d.GoodsGuid) .LeftJoin((s, c, d, f) => s.CustomerGuid == f.CustomerGuid) .Where(predicate.ToExpression()) .OrderBy(s => s.GoodsCommentSort, OrderByType.Asc) .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(); 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; } /// /// 查询商品评价详情(Api) /// /// /// public async Task> GetGoodsDetailsComments(GoodsCommentDtoApi parm) { var query = _GoodsCommentRepository .Queryable() .LeftJoin((s, c) => s.ShopGuid == c.ShopGuid) .LeftJoin((s, c, d) => s.GoodsGuid == d.GoodsGuid) .LeftJoin((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.ToListAsync(); } /// /// 获取商品详情页评论数(Api) /// /// /// public async Task GetGoodsDetailsCommentsCount(GoodsCommentDtoApi parm) { var query = _GoodsCommentRepository .Queryable() .Select((s) => new GoodsCommentCountApiVo { CommentCount = SqlFunc.Subqueryable().Where(s => s.GoodsGuid == parm.SpuId).Count(), BadCount = SqlFunc.Subqueryable().Where(s => s.GoodsCommentRatingType == 3 && s.GoodsGuid == parm.SpuId).Count(), MiddleCount = SqlFunc.Subqueryable().Where(s => s.GoodsCommentRatingType == 2 && s.GoodsGuid == parm.SpuId).Count(), GoodCount = SqlFunc.Subqueryable().Where(s => s.GoodsCommentRatingType == 1 && s.GoodsGuid == parm.SpuId).Count(), HasImageCount = SqlFunc.Subqueryable().Where(s => s.GoodsCommentImages != "" && s.GoodsCommentImages != null && s.GoodsGuid == parm.SpuId).Count() }); var res = await query.FirstAsync(); if (res != null) { if (res.GoodCount > 0) { res.GoodRate = Math.Round((decimal)res.GoodCount / res.CommentCount, 4) * 100; } } return res; } #endregion } }