| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.fasterxml.jackson.core.JsonProcessingException; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.ruoyi.common.core.enums.ListingStatusEnum; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.core.utils.page.BeanUtils; |
| | | import com.ruoyi.common.core.utils.page.Checker; |
| | | import com.ruoyi.common.core.utils.page.CollUtils; |
| | | import com.ruoyi.common.core.utils.page.PageDTO; |
| | | import com.ruoyi.goods.controller.management.DTO.GoodsInfoTitleValueDTO; |
| | | import com.ruoyi.goods.controller.management.DTO.GoodsSkuDTO; |
| | | import com.ruoyi.goods.controller.management.DTO.GoodsSkuQuery; |
| | | import com.ruoyi.goods.controller.management.VO.GoodsSkuVO; |
| | | import com.ruoyi.goods.controller.management.dto.GoodsInfoTitleValueDTO; |
| | | import com.ruoyi.goods.controller.management.dto.GoodsSkuDTO; |
| | | import com.ruoyi.goods.controller.management.dto.GoodsSkuQuery; |
| | | import com.ruoyi.goods.controller.management.vo.GoodsSkuVO; |
| | | import com.ruoyi.goods.domain.GoodsGroupPurchase; |
| | | import com.ruoyi.goods.domain.GoodsInfoTitleValue; |
| | | import com.ruoyi.goods.domain.GoodsSku; |
| | | import com.ruoyi.goods.mapper.GoodsSkuMapper; |
| | | import com.ruoyi.goods.service.IGoodsGroupPurchaseService; |
| | | import com.ruoyi.goods.service.IGoodsInfoTitleValueService; |
| | | import com.ruoyi.goods.service.IGoodsSeckillService; |
| | | import com.ruoyi.goods.service.IGoodsSkuService; |
| | | import com.ruoyi.system.api.domain.GoodsSeckill; |
| | | import com.ruoyi.system.api.domain.GoodsSku; |
| | | import com.ruoyi.system.api.domain.dto.ListStatusDTO; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | |
| | | public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> implements IGoodsSkuService { |
| | | |
| | | private final IGoodsInfoTitleValueService goodsInfoTitleValueService; |
| | | private final IGoodsSeckillService goodsSeckillService; |
| | | private final IGoodsGroupPurchaseService goodsGroupPurchaseService; |
| | | private static final ObjectMapper objectMapper = new ObjectMapper(); |
| | | |
| | | /** |
| | |
| | | if (Objects.isNull(dto.getId())) { |
| | | this.save(goodsSku); |
| | | } else { |
| | | GoodsSku goodsSkuOrg = this.getById(dto.getId()); |
| | | if (StringUtils.isNull(goodsSkuOrg)) { |
| | | throw new ServiceException("商品不存在"); |
| | | } |
| | | this.updateById(goodsSku); |
| | | } |
| | | |
| | |
| | | query.getSkuName()) |
| | | .eq(StringUtils.isNotNull(query.getListingStatus() |
| | | ), GoodsSku::getListingStatus, query.getListingStatus()) |
| | | .page(new Page<GoodsSku>(query.getPageCurr(), query.getPageSize())); |
| | | |
| | | .eq(query.getQueryType().equals(1), GoodsSku::getListingStatus, |
| | | ListingStatusEnum.ON_SHELVES) |
| | | .page(new Page<>(query.getPageCurr(), query.getPageSize())); |
| | | return PageDTO.of(page, GoodsSkuVO.class); |
| | | } |
| | | |
| | |
| | | * 更新商品SKU的状态。 |
| | | * |
| | | * @param dto 数据传输对象,包含需要更新的SKU的ID和新的上架状态。 其中,ID用于指定要更新的具体SKU,listingStatus用于指定新的上架状态。 |
| | | * @return 无返回值。 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW) |
| | | public void updStatus(ListStatusDTO dto) { |
| | | this.lambdaUpdate() |
| | | .eq(GoodsSku::getId, dto.getId()) |
| | | .set(GoodsSku::getListingStatus, dto.getListingStatus()) |
| | | .update(); |
| | | //关联的秒杀商品和团购商品同步下架 |
| | | if (dto.getListingStatus().equals(ListingStatusEnum.REMOVED_FROM_THE_SHELF)) { |
| | | updateGoodsStatus(dto); |
| | | } |
| | | } |
| | | |
| | | private void updateGoodsStatus(ListStatusDTO dto) { |
| | | goodsSeckillService.lambdaUpdate() |
| | | .set(GoodsSeckill::getListingStatus, |
| | | ListingStatusEnum.REMOVED_FROM_THE_SHELF) |
| | | .eq(GoodsSeckill::getListingStatus, ListingStatusEnum.ON_SHELVES) |
| | | .eq(GoodsSeckill::getGoodsSkuId, dto.getId()).update(); |
| | | goodsGroupPurchaseService.lambdaUpdate() |
| | | .set(GoodsGroupPurchase::getListingStatus, |
| | | ListingStatusEnum.REMOVED_FROM_THE_SHELF) |
| | | .eq(GoodsGroupPurchase::getListingStatus, |
| | | ListingStatusEnum.ON_SHELVES) |
| | | .eq(GoodsGroupPurchase::getGoodsSkuId, dto.getId()).update(); |
| | | } |
| | | |
| | | /** |
| | | * 根据商品名称查询商品SKU列表。 |
| | | * |
| | | * @param goodsSkuName 商品名称 |
| | | * @return List<GoodsSku>商品SKU列表 |
| | | */ |
| | | @Override |
| | | public List<GoodsSku> getGoodsByName(String goodsSkuName) { |
| | | return this.lambdaQuery() |
| | | .like(StringUtils.isNotEmpty(goodsSkuName), GoodsSku::getSkuName, goodsSkuName) |
| | | .list(); |
| | | } |
| | | |
| | | /** |
| | | * 扣减商品库存 |
| | | * |
| | | * @param goodsSkuId 商品SKU ID |
| | | * @param auctionStock 拍卖库存 |
| | | */ |
| | | @Override |
| | | public void deductStock(Long goodsSkuId, Integer auctionStock) { |
| | | GoodsSku goodsSku = this.getById(goodsSkuId); |
| | | if (StringUtils.isNull(goodsSku)) { |
| | | throw new ServiceException("商品不存在"); |
| | | } |
| | | if (goodsSku.getStock() < auctionStock) { |
| | | throw new ServiceException("库存不足"); |
| | | } |
| | | // 更新商品库存 |
| | | this.lambdaUpdate().set(GoodsSku::getStock, goodsSku.getStock() - auctionStock) |
| | | .ge(GoodsSku::getStock, auctionStock).eq(GoodsSku::getId, goodsSku.getId()) |
| | | .update(); |
| | | } |
| | | |
| | | @Override |
| | | public void returningStock(Long goodsSkuId, Integer auctionStock) { |
| | | GoodsSku goodsSku = this.getById(goodsSkuId); |
| | | if (StringUtils.isNull(goodsSku)) { |
| | | throw new ServiceException("商品不存在"); |
| | | } |
| | | // 更新商品库存 |
| | | this.lambdaUpdate() |
| | | .set(auctionStock > 0, GoodsSku::getStock, goodsSku.getStock() + auctionStock) |
| | | .eq(GoodsSku::getId, goodsSku.getId()) |
| | | .update(); |
| | | } |
| | | } |