Search in sources :

Example 1 with GoodsSku

use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.

the class GoodsSkuServiceImpl method getGoodsSkuVOList.

@Override
public List<GoodsSkuVO> getGoodsSkuVOList(List<GoodsSku> list) {
    List<GoodsSkuVO> goodsSkuVOS = new ArrayList<>();
    for (GoodsSku goodsSku : list) {
        GoodsSkuVO goodsSkuVO = this.getGoodsSkuVO(goodsSku);
        goodsSkuVOS.add(goodsSkuVO);
    }
    return goodsSkuVOS;
}
Also used : GoodsSku(cn.lili.modules.goods.entity.dos.GoodsSku) GoodsSkuVO(cn.lili.modules.goods.entity.vos.GoodsSkuVO)

Example 2 with GoodsSku

use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.

the class GoodsSkuServiceImpl method getGoodsSkuDetail.

@Override
public Map<String, Object> getGoodsSkuDetail(String goodsId, String skuId) {
    Map<String, Object> map = new HashMap<>(16);
    // 获取商品VO
    GoodsVO goodsVO = goodsService.getGoodsVO(goodsId);
    // 如果skuid为空,则使用商品VO中sku信息获取
    if (CharSequenceUtil.isEmpty(skuId) || "undefined".equals(skuId)) {
        skuId = goodsVO.getSkuList().get(0).getId();
    }
    // 从缓存拿商品Sku
    GoodsSku goodsSku = this.getGoodsSkuByIdFromCache(skuId);
    // 如果使用商品ID无法查询SKU则返回错误
    if (goodsVO == null || goodsSku == null) {
        throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
    }
    // 商品下架||商品未审核通过||商品删除,则提示:商品已下架
    if (GoodsStatusEnum.DOWN.name().equals(goodsVO.getMarketEnable()) || !GoodsAuthEnum.PASS.name().equals(goodsVO.getAuthFlag()) || Boolean.TRUE.equals(goodsVO.getDeleteFlag())) {
        throw new ServiceException(ResultCode.GOODS_NOT_EXIST);
    }
    // 获取当前商品的索引信息
    EsGoodsIndex goodsIndex = goodsIndexService.findById(skuId);
    if (goodsIndex == null) {
        goodsIndex = goodsIndexService.getResetEsGoodsIndex(goodsSku, goodsVO.getGoodsParamsDTOList());
    }
    // 商品规格
    GoodsSkuVO goodsSkuDetail = this.getGoodsSkuVO(goodsSku);
    Map<String, Object> promotionMap = goodsIndex.getPromotionMap();
    // 设置当前商品的促销价格
    if (promotionMap != null && !promotionMap.isEmpty()) {
        promotionMap = promotionMap.entrySet().stream().parallel().filter(i -> {
            JSONObject jsonObject = JSONUtil.parseObj(i.getValue());
            // 过滤活动赠送优惠券和无效时间的活动
            return (jsonObject.get("getType") == null || jsonObject.get("getType", String.class).equals(CouponGetEnum.FREE.name())) && (jsonObject.get("startTime") != null && jsonObject.get("startTime", Date.class).getTime() <= System.currentTimeMillis()) && (jsonObject.get("endTime") == null || jsonObject.get("endTime", Date.class).getTime() >= System.currentTimeMillis());
        }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        Optional<Map.Entry<String, Object>> containsPromotion = promotionMap.entrySet().stream().filter(i -> i.getKey().contains(PromotionTypeEnum.SECKILL.name()) || i.getKey().contains(PromotionTypeEnum.PINTUAN.name())).findFirst();
        if (containsPromotion.isPresent()) {
            JSONObject jsonObject = JSONUtil.parseObj(containsPromotion.get().getValue());
            PromotionGoodsSearchParams searchParams = new PromotionGoodsSearchParams();
            searchParams.setSkuId(skuId);
            searchParams.setPromotionId(jsonObject.get("id").toString());
            PromotionGoods promotionsGoods = promotionGoodsService.getPromotionsGoods(searchParams);
            if (promotionsGoods != null && promotionsGoods.getPrice() != null) {
                goodsSkuDetail.setPromotionFlag(true);
                goodsSkuDetail.setPromotionPrice(promotionsGoods.getPrice());
            }
        } else {
            goodsSkuDetail.setPromotionFlag(false);
            goodsSkuDetail.setPromotionPrice(null);
        }
    }
    map.put("data", goodsSkuDetail);
    // 获取分类
    String[] split = goodsSkuDetail.getCategoryPath().split(",");
    map.put("categoryName", categoryService.getCategoryNameByIds(Arrays.asList(split)));
    // 获取规格信息
    map.put("specs", this.groupBySkuAndSpec(goodsVO.getSkuList()));
    map.put("promotionMap", promotionMap);
    // 获取参数信息
    if (goodsVO.getGoodsParamsDTOList() != null && !goodsVO.getGoodsParamsDTOList().isEmpty()) {
        map.put("goodsParamsDTOList", goodsVO.getGoodsParamsDTOList());
    }
    // 记录用户足迹
    if (UserContext.getCurrentUser() != null) {
        FootPrint footPrint = new FootPrint(UserContext.getCurrentUser().getId(), goodsId, skuId);
        String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.VIEW_GOODS.name();
        rocketMQTemplate.asyncSend(destination, footPrint, RocketmqSendCallbackBuilder.commonCallback());
    }
    return map;
}
Also used : GoodsVO(cn.lili.modules.goods.entity.vos.GoodsVO) GoodsSku(cn.lili.modules.goods.entity.dos.GoodsSku) GoodsGalleryService(cn.lili.modules.goods.service.GoodsGalleryService) GoodsVO(cn.lili.modules.goods.entity.vos.GoodsVO) Autowired(org.springframework.beans.factory.annotation.Autowired) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) GoodsSku(cn.lili.modules.goods.entity.dos.GoodsSku) EsGoodsIndex(cn.lili.modules.search.entity.dos.EsGoodsIndex) FootPrint(cn.lili.modules.member.entity.dos.FootPrint) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) EvaluationQueryParams(cn.lili.modules.member.entity.dto.EvaluationQueryParams) PromotionTypeEnum(cn.lili.common.enums.PromotionTypeEnum) ResultCode(cn.lili.common.enums.ResultCode) ServiceImpl(com.baomidou.mybatisplus.extension.service.impl.ServiceImpl) Cache(cn.lili.cache.Cache) JSONObject(cn.hutool.json.JSONObject) EsGoodsIndexService(cn.lili.modules.search.service.EsGoodsIndexService) Collectors(java.util.stream.Collectors) RocketmqSendCallbackBuilder(cn.lili.rocketmq.RocketmqSendCallbackBuilder) GoodsSearchParams(cn.lili.modules.goods.entity.dto.GoodsSearchParams) Goods(cn.lili.modules.goods.entity.dos.Goods) GoodsService(cn.lili.modules.goods.service.GoodsService) GoodsAuthEnum(cn.lili.modules.goods.entity.enums.GoodsAuthEnum) GoodsSkuService(cn.lili.modules.goods.service.GoodsSkuService) UserContext(cn.lili.common.security.context.UserContext) Convert(cn.hutool.core.convert.Convert) GoodsSkuVO(cn.lili.modules.goods.entity.vos.GoodsSkuVO) GoodsSkuMapper(cn.lili.modules.goods.mapper.GoodsSkuMapper) RocketMQTemplate(org.apache.rocketmq.spring.core.RocketMQTemplate) IPage(com.baomidou.mybatisplus.core.metadata.IPage) PageUtil(cn.lili.mybatis.util.PageUtil) GoodsTagsEnum(cn.lili.rocketmq.tags.GoodsTagsEnum) NumberUtil(cn.hutool.core.util.NumberUtil) java.util(java.util) MapUtil(cn.hutool.core.map.MapUtil) SpecValueVO(cn.lili.modules.goods.entity.vos.SpecValueVO) PromotionGoodsSearchParams(cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams) JSONUtil(cn.hutool.json.JSONUtil) CategoryService(cn.lili.modules.goods.service.CategoryService) Service(org.springframework.stereotype.Service) GoodsSkuStockDTO(cn.lili.modules.goods.entity.dto.GoodsSkuStockDTO) GoodsSkuSpecVO(cn.lili.modules.goods.entity.vos.GoodsSkuSpecVO) EvaluationGradeEnum(cn.lili.modules.member.entity.enums.EvaluationGradeEnum) ServiceException(cn.lili.common.exception.ServiceException) PromotionGoodsService(cn.lili.modules.promotion.service.PromotionGoodsService) RocketmqCustomProperties(cn.lili.common.properties.RocketmqCustomProperties) SnowFlake(cn.lili.common.utils.SnowFlake) EsGoodsAttribute(cn.lili.modules.search.entity.dos.EsGoodsAttribute) CachePrefix(cn.lili.cache.CachePrefix) GeneratorEsGoodsIndexEvent(cn.lili.modules.goods.event.GeneratorEsGoodsIndexEvent) MemberEvaluationService(cn.lili.modules.member.service.MemberEvaluationService) GoodsStatusEnum(cn.lili.modules.goods.entity.enums.GoodsStatusEnum) EsIndexUtil(cn.lili.modules.search.utils.EsIndexUtil) LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) PromotionGoods(cn.lili.modules.promotion.entity.dos.PromotionGoods) CharSequenceUtil(cn.hutool.core.text.CharSequenceUtil) CouponGetEnum(cn.lili.modules.promotion.entity.enums.CouponGetEnum) Transactional(org.springframework.transaction.annotation.Transactional) FootPrint(cn.lili.modules.member.entity.dos.FootPrint) EsGoodsIndex(cn.lili.modules.search.entity.dos.EsGoodsIndex) ServiceException(cn.lili.common.exception.ServiceException) JSONObject(cn.hutool.json.JSONObject) JSONObject(cn.hutool.json.JSONObject) PromotionGoods(cn.lili.modules.promotion.entity.dos.PromotionGoods) PromotionGoodsSearchParams(cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams) GoodsSkuVO(cn.lili.modules.goods.entity.vos.GoodsSkuVO)

Example 3 with GoodsSku

use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.

the class GoodsSkuServiceImpl method updateStock.

@Override
@Transactional(rollbackFor = Exception.class)
public void updateStock(String skuId, Integer quantity) {
    GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId);
    if (goodsSku != null) {
        if (quantity <= 0) {
            goodsIndexService.deleteIndexById(goodsSku.getId());
        }
        goodsSku.setQuantity(quantity);
        boolean update = this.update(new LambdaUpdateWrapper<GoodsSku>().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity));
        if (update) {
            cache.remove(CachePrefix.GOODS.getPrefix() + goodsSku.getGoodsId());
        }
        cache.put(GoodsSkuService.getCacheKeys(skuId), goodsSku);
        cache.put(GoodsSkuService.getStockCacheKey(skuId), quantity);
        // 更新商品库存
        List<GoodsSku> goodsSkus = new ArrayList<>();
        goodsSkus.add(goodsSku);
        this.updateGoodsStuck(goodsSkus);
    }
}
Also used : GoodsSku(cn.lili.modules.goods.entity.dos.GoodsSku) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with GoodsSku

use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.

the class GoodsSkuServiceImpl method getGoodsSkuByIdFromCache.

@Override
public GoodsSku getGoodsSkuByIdFromCache(String id) {
    // 获取缓存中的sku
    GoodsSku goodsSku = (GoodsSku) cache.get(GoodsSkuService.getCacheKeys(id));
    // 如果缓存中没有信息,则查询数据库,然后写入缓存
    if (goodsSku == null) {
        goodsSku = this.getById(id);
        if (goodsSku == null) {
            return null;
        }
        cache.put(GoodsSkuService.getCacheKeys(id), goodsSku);
    }
    // 获取商品库存
    Integer integer = (Integer) cache.get(GoodsSkuService.getStockCacheKey(id));
    // 库存不为空,库存与缓存中不一致
    if (integer != null && !goodsSku.getQuantity().equals(integer)) {
        // 写入最新的库存信息
        goodsSku.setQuantity(integer);
        cache.put(GoodsSkuService.getCacheKeys(goodsSku.getId()), goodsSku);
    }
    return goodsSku;
}
Also used : GoodsSku(cn.lili.modules.goods.entity.dos.GoodsSku)

Example 5 with GoodsSku

use of cn.lili.modules.goods.entity.dos.GoodsSku in project lilishop by lilishop.

the class GoodsSkuServiceImpl method update.

@Override
@Transactional(rollbackFor = Exception.class)
public void update(List<Map<String, Object>> skuList, Goods goods, Boolean regeneratorSkuFlag) {
    // 是否存在规格
    if (skuList == null || skuList.isEmpty()) {
        throw new ServiceException(ResultCode.MUST_HAVE_GOODS_SKU);
    }
    List<GoodsSku> newSkuList;
    // 删除旧的sku信息
    if (Boolean.TRUE.equals(regeneratorSkuFlag)) {
        List<GoodsSkuVO> goodsListByGoodsId = getGoodsListByGoodsId(goods.getId());
        List<String> oldSkuIds = new ArrayList<>();
        // 删除旧索引
        for (GoodsSkuVO goodsSkuVO : goodsListByGoodsId) {
            oldSkuIds.add(goodsSkuVO.getId());
            cache.remove(GoodsSkuService.getCacheKeys(goodsSkuVO.getId()));
        }
        this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
        // 删除sku相册
        goodsGalleryService.removeByGoodsId(goods.getId());
        getGoodsListByGoodsId(goods.getId());
        // 添加商品sku
        newSkuList = this.addGoodsSku(skuList, goods);
        // 发送mq消息
        String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.SKU_DELETE.name();
        rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(oldSkuIds), RocketmqSendCallbackBuilder.commonCallback());
    } else {
        newSkuList = new ArrayList<>();
        for (Map<String, Object> map : skuList) {
            GoodsSku sku = null;
            if (map.get("id") != null) {
                sku = this.getGoodsSkuByIdFromCache(map.get("id").toString());
            }
            if (sku == null || map.get("id") == null) {
                sku = new GoodsSku();
            }
            // 设置商品信息
            goodsInfo(sku, goods);
            // 设置商品规格信息
            skuInfo(sku, goods, map, null);
            newSkuList.add(sku);
            // 如果商品状态值不对,则es索引移除
            if (goods.getAuthFlag().equals(GoodsAuthEnum.PASS.name()) && goods.getMarketEnable().equals(GoodsStatusEnum.UPPER.name())) {
                goodsIndexService.deleteIndexById(sku.getId());
                this.clearCache(sku.getId());
            }
        }
        this.remove(new LambdaQueryWrapper<GoodsSku>().eq(GoodsSku::getGoodsId, goods.getId()));
        this.saveOrUpdateBatch(newSkuList);
    }
    this.updateStock(newSkuList);
    if (GoodsAuthEnum.PASS.name().equals(goods.getAuthFlag()) && !newSkuList.isEmpty()) {
        generateEs(goods);
    }
}
Also used : GoodsSku(cn.lili.modules.goods.entity.dos.GoodsSku) ServiceException(cn.lili.common.exception.ServiceException) JSONObject(cn.hutool.json.JSONObject) GoodsSkuVO(cn.lili.modules.goods.entity.vos.GoodsSkuVO) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

GoodsSku (cn.lili.modules.goods.entity.dos.GoodsSku)41 Transactional (org.springframework.transaction.annotation.Transactional)18 ServiceException (cn.lili.common.exception.ServiceException)15 PromotionGoods (cn.lili.modules.promotion.entity.dos.PromotionGoods)11 JSONObject (cn.hutool.json.JSONObject)8 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)8 PromotionTypeEnum (cn.lili.common.enums.PromotionTypeEnum)7 PromotionGoodsSearchParams (cn.lili.modules.promotion.entity.dto.search.PromotionGoodsSearchParams)7 ArrayList (java.util.ArrayList)7 GoodsSkuService (cn.lili.modules.goods.service.GoodsSkuService)6 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 Service (org.springframework.stereotype.Service)6 OrderItem (cn.lili.modules.order.order.entity.dos.OrderItem)5 PromotionGoodsService (cn.lili.modules.promotion.service.PromotionGoodsService)5 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 Cache (cn.lili.cache.Cache)4 ResultCode (cn.lili.common.enums.ResultCode)4 AuthUser (cn.lili.common.security.AuthUser)4