package cc.mrbird.febs.mall.service.impl; import cc.mrbird.febs.common.enumerates.DataDictionaryEnum; import cc.mrbird.febs.common.exception.FebsException; import cc.mrbird.febs.common.utils.LoginUserUtil; import cc.mrbird.febs.mall.conversion.MallGoodsConversion; import cc.mrbird.febs.mall.dto.ApiMallGoodsCommentDto; import cc.mrbird.febs.mall.dto.MallGoodsQueryDto; import cc.mrbird.febs.mall.entity.*; import cc.mrbird.febs.mall.mapper.*; import cc.mrbird.febs.mall.service.IApiMallGoodsService; import cc.mrbird.febs.mall.vo.*; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; /** * @author wzy * @date 2021-09-17 **/ @Slf4j @Service @RequiredArgsConstructor public class ApiMallGoodsServiceImpl extends ServiceImpl implements IApiMallGoodsService { private final MallGoodsImagesMapper goodsImagesMapper; private final MallGoodsCommentMapper mallGoodsCommentMapper; private final DataDictionaryCustomMapper dataDictionaryCustomMapper; private final MallCarriageRuleMapper mallCarriageRuleMapper; private final MallCarriageRuleInfoMapper mallCarriageRuleInfoMapper; /** * 根据查询条件分页查找商城商品列表,并为每个商品添加排名 * 排名基于商品的销售状态、正常状态和销量进行排序 * * @param queryDto 查询条件封装对象,包含分页信息和查询参数 * @return 分页的商品列表,每个商品包含其排名 */ @Override public IPage findMallGoodsListInPage(MallGoodsQueryDto queryDto) { // 获取当前登录的用户信息 MallMember loginUser = LoginUserUtil.getLoginUser(); // 如果用户已登录,设置查询条件中的会员ID if (loginUser != null) { queryDto.setMemberId(loginUser.getId()); } // 初始化分页对象 Page page = new Page<>(queryDto.getPageNow(), queryDto.getPageSize()); // 调用Mapper方法,根据查询条件分页获取商品列表 IPage result = this.baseMapper.selectMallGoodsListQueryInPage(queryDto, page); // 初始化商品查询条件,筛选出正在销售的、状态正常的商品,并按销量降序排序,限制结果数量 LambdaQueryWrapper goodsQuery = new LambdaQueryWrapper<>(); goodsQuery.eq(MallGoods::getIsSale, 1) .eq(MallGoods::getIsNormal, 1) .select(MallGoods::getId) .orderByDesc(MallGoods::getVolume) .last("limit 5"); // 执行查询,获取商品列表 List mallGoods = this.baseMapper.selectList(goodsQuery); // 初始化商品ID与排名的映射关系 Map goodsMap = new HashMap<>(); AtomicInteger rank = new AtomicInteger(1); // 遍历商品列表,为每个商品分配排名 mallGoods.forEach(item -> { goodsMap.put(item.getId(), rank.getAndIncrement()); }); // 遍历查询结果中的商品,设置每个商品的排名 result.getRecords().forEach(item -> { Integer goodsRank = goodsMap.get(item.getId()); // 如果商品有排名,则设置排名 if (goodsRank != null) { item.setRank(goodsRank); } }); // 返回分页查询结果 return result; } @Override public List findMallGoodsListNoPage(MallGoodsQueryDto queryDto) { return this.baseMapper.selectMallGoodsListQueryNoPage(queryDto); } @Override public ApiMallCarriageRuleVo findMallCarriageRuleById(Long id) { ApiMallCarriageRuleVo apiMallCarriageRuleVo = new ApiMallCarriageRuleVo(); MallCarriageRule mallCarriageRule = mallCarriageRuleMapper.selectById(id); if(ObjectUtil.isEmpty(mallCarriageRule)){ throw new FebsException("商品不存在"); } apiMallCarriageRuleVo.setName(mallCarriageRule.getName()); List mallCarriageRuleInfos = mallCarriageRuleInfoMapper.selectMallCarriageRuleInfoByRuleIdAndAreaAddress(id, null); if(CollUtil.isNotEmpty(mallCarriageRuleInfos)){ List apiMallCarriageRuleInfoVos = new ArrayList<>(); for(MallCarriageRuleInfo mallCarriageRuleInfo : mallCarriageRuleInfos){ ApiMallCarriageRuleInfoVo apiMallCarriageRuleInfoVo = new ApiMallCarriageRuleInfoVo(); apiMallCarriageRuleInfoVo.setAreaAddress(mallCarriageRuleInfo.getAreaAddress()); apiMallCarriageRuleInfoVo.setBasicPrice(mallCarriageRuleInfo.getBasicPrice()); apiMallCarriageRuleInfoVo.setBasicCnt(mallCarriageRuleInfo.getBasicCnt()); apiMallCarriageRuleInfoVo.setMoreCnt(mallCarriageRuleInfo.getMoreCnt()); apiMallCarriageRuleInfoVo.setMorePrice(mallCarriageRuleInfo.getMorePrice()); apiMallCarriageRuleInfoVos.add(apiMallCarriageRuleInfoVo); } apiMallCarriageRuleVo.setApiMallCarriageRuleInfoVos(apiMallCarriageRuleInfoVos); } return apiMallCarriageRuleVo; } @Override public MallGoodsDetailsVo findMallGoodsDetailsById(Long id) { MallGoods mallGoods = this.baseMapper.selectGoodsDetailById(id); if (mallGoods == null) { throw new FebsException("商品不存在"); } List images = goodsImagesMapper.selectGoodsImagesByGoodsId(mallGoods.getId()); MallGoodsDetailsVo mallGoodsDetailsVo = MallGoodsConversion.INSTANCE.entityToDetailsVo(mallGoods); mallGoodsDetailsVo.setMainImage(mallGoods.getThumb()); List styles = mallGoodsDetailsVo.getStyles(); if(ObjectUtil.isNotEmpty(styles)){ for(GoodsDetailsStyleVo goodsDetailsStyleVo : styles){ List skus = goodsDetailsStyleVo.getSkus(); if(CollUtil.isNotEmpty(skus) ){ for(GoodsDetailsSkuVo goodsDetailsSkuVo : skus){ if(StrUtil.isNotEmpty(goodsDetailsSkuVo.getSample())){ goodsDetailsSkuVo.setSampleFlag(true); }else{ goodsDetailsSkuVo.setSampleFlag(false); } } } } } if (CollUtil.isNotEmpty(mallGoods.getStyles())) { Map stockAndVolume = this.baseMapper.selectGoodsStockAndVolume(id); mallGoodsDetailsVo.setStock(stockAndVolume.get("stock").intValue()); mallGoodsDetailsVo.setVolume(stockAndVolume.get("volume").intValue()); } mallGoodsDetailsVo.setImages(images); QueryWrapper objectQueryWrapper = new QueryWrapper<>(); objectQueryWrapper.eq("goods_id",id); Integer commentCount = mallGoodsCommentMapper.selectCount(objectQueryWrapper); mallGoodsDetailsVo.setCommentCount(commentCount); return mallGoodsDetailsVo; } @Override public IPage findMallGoodsCommentByGoodsId(ApiMallGoodsCommentDto queryDto) { Page page = new Page<>(queryDto.getPageNow(), queryDto.getPageSize()); MallGoodsComment mallGoodsComment = new MallGoodsComment(); mallGoodsComment.setGoodsId(queryDto.getGoodsId()); mallGoodsComment.setCommentType(queryDto.getCommentType()); return this.baseMapper.selectMallGoodsCommentListQueryInPage(page,mallGoodsComment); } @Override public ApiMallGoodsDeliveryVo findDeliverySetting() { ApiMallGoodsDeliveryVo apiMallGoodsDeliveryVo = new ApiMallGoodsDeliveryVo(); DataDictionaryCustom deliverySetting = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.HOME_DELIVERY_AMOUNT.getType(), DataDictionaryEnum.HOME_DELIVERY_AMOUNT.getCode()); if(ObjectUtil.isNotNull(deliverySetting)){ apiMallGoodsDeliveryVo.setHomeDeliveryServiceAmount(Double.parseDouble(deliverySetting.getValue())); } List homeDeliverySettings = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.FRIST_COST_LEVEL.getType()); if(CollUtil.isNotEmpty(homeDeliverySettings)){ List apiMallGoodsDeliverySettingVos = new ArrayList<>(); for(DataDictionaryCustom dic : homeDeliverySettings){ String apiMallGoodsDeliverySettingVoJson = dic.getValue(); ApiMallGoodsDeliverySettingVo apiMallGoodsDeliverySettingVo = JSONUtil.toBean(apiMallGoodsDeliverySettingVoJson, ApiMallGoodsDeliverySettingVo.class); apiMallGoodsDeliverySettingVos.add(apiMallGoodsDeliverySettingVo); } apiMallGoodsDeliveryVo.setApiMallGoodsDeliverySettingVos(apiMallGoodsDeliverySettingVos); } return apiMallGoodsDeliveryVo; } @Override public MallGoodsCommentVo findMallGoodsCommentLevelByGoodsId(Long id) { return this.baseMapper.findMallGoodsCommentLevelByGoodsId(id); } }