| | |
| | | package com.ruoyi.goods.service.impl; |
| | | |
| | | import com.ruoyi.goods.domain.pojo.GoodsSku; |
| | | import com.ruoyi.goods.mapper.GoodsSkuMapper; |
| | | import com.ruoyi.goods.service.IGoodsSkuService; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | 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.domain.GoodsGroupPurchase; |
| | | import com.ruoyi.goods.domain.GoodsInfoTitleValue; |
| | | 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; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2024-05-16 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | 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(); |
| | | |
| | | /** |
| | | * 保存商品SKU信息。 |
| | | * |
| | | * @param dto 商品SKU的DTO(数据传输对象),包含要保存或更新的商品SKU的详细信息。 |
| | | * @throws JsonProcessingException 当处理JSON数据时发生错误。 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void saveGoods(GoodsSkuDTO dto) throws JsonProcessingException { |
| | | // 使用BeanUtils将DTO对象复制到GoodsSku实体对象 |
| | | GoodsSku goodsSku = BeanUtils.copyBean(dto, GoodsSku.class); |
| | | |
| | | // 分割并处理专辑图片字符串,将其转换为JSON格式 |
| | | String albumStr = processAlbumString(goodsSku.getAlbum()); |
| | | |
| | | goodsSku.setAlbum(albumStr); |
| | | |
| | | // 根据ID判断是进行新增还是更新操作 |
| | | 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); |
| | | } |
| | | |
| | | // 处理商品额外信息,如规格、属性等 |
| | | processGoodsInfoTitleValue(dto, goodsSku); |
| | | } |
| | | |
| | | /** |
| | | * 处理相册字符串。 该方法将输入的相册字符串分割为字符串数组,并将其序列化为JSON字符串形式返回。 如果输入的相册字符串为空或仅包含空白字符,则返回空字符串。 |
| | | * |
| | | * @param album 输入的相册字符串,可能包含多个相册名称,各相册名称之间以逗号分隔。 |
| | | * @return 返回序列化后的相册名称数组的JSON字符串。如果输入为空,则返回空字符串。 |
| | | * @throws JsonProcessingException 如果在序列化过程中发生错误,则抛出此异常。 |
| | | */ |
| | | private String processAlbumString(String album) throws JsonProcessingException { |
| | | if (StringUtils.isEmpty(album)) { |
| | | return ""; // 返回空字符串,避免后续处理出现问题 |
| | | } |
| | | String[] albumArr = album.split(","); |
| | | return objectMapper.writeValueAsString(albumArr); |
| | | } |
| | | |
| | | /** |
| | | * 处理商品信息标题和值的逻辑。 将DTO列表复制到实体列表中,校验其完整性和正确性,然后关联SKU ID,并最后保存到数据库。 |
| | | * |
| | | * @param dto 包含商品信息标题和值的DTO对象 |
| | | * @param goodsSku 商品SKU实体,用于关联商品额外信息 |
| | | * @throws ServiceException 如果校验失败或数据处理出现异常,则抛出服务异常 |
| | | */ |
| | | private void processGoodsInfoTitleValue(GoodsSkuDTO dto, GoodsSku goodsSku) |
| | | throws ServiceException { |
| | | List<GoodsInfoTitleValueDTO> goodsInfoTitleValueDTOList = dto.getGoodsInfoTitleValueDTOList(); |
| | | |
| | | // 复制DTO列表到实体列表 |
| | | List<GoodsInfoTitleValue> goodsInfoTitleValues = BeanUtils.copyList( |
| | | goodsInfoTitleValueDTOList, GoodsInfoTitleValue.class); |
| | | |
| | | // 校验商品额外信息的完整性和正确性 |
| | | CollUtils.check(goodsInfoTitleValues, new Checker<GoodsInfoTitleValue>() { |
| | | @Override |
| | | public void check(GoodsInfoTitleValue data) { |
| | | if (StringUtils.isNull(data.getGoodsInfoTitleId())) { |
| | | throw new ServiceException("商品信息标题id不能为空"); |
| | | } |
| | | if (StringUtils.isEmpty(data.getContent())) { |
| | | throw new ServiceException("商品信息内容不能为空"); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 给每个商品额外信息关联上SKU ID |
| | | for (GoodsInfoTitleValue goodsInfoTitleValue : goodsInfoTitleValues) { |
| | | goodsInfoTitleValue.setGoodsSkuId(goodsSku.getId()); |
| | | } |
| | | |
| | | // 先清除旧的商品信息,再批量保存新的商品信息 |
| | | goodsInfoTitleValueService.removeByGoodsSkuId(goodsSku.getId()); |
| | | goodsInfoTitleValueService.saveBatch(goodsInfoTitleValues); |
| | | } |
| | | |
| | | /** |
| | | * 获取商品SKU分页数据 |
| | | * |
| | | * @param query 查询条件,包括SKU名称、上架状态、页码和页大小 |
| | | * @return 分页数据对象,包含商品SKU的信息 |
| | | */ |
| | | @Override |
| | | public PageDTO<GoodsSkuVO> getGoodsPage(GoodsSkuQuery query) { |
| | | Page<GoodsSku> page = this.lambdaQuery() |
| | | .select(GoodsSku::getId, GoodsSku::getSkuName, GoodsSku::getPrice, |
| | | GoodsSku::getStock, GoodsSku::getSoldQuantity, GoodsSku::getSortNum, |
| | | GoodsSku::getListingStatus) |
| | | .like(StringUtils.isNotEmpty(query.getSkuName()), GoodsSku::getSkuName, |
| | | query.getSkuName()) |
| | | .eq(StringUtils.isNotNull(query.getListingStatus() |
| | | ), GoodsSku::getListingStatus, query.getListingStatus()) |
| | | .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用于指定新的上架状态。 |
| | | */ |
| | | @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(); |
| | | } |
| | | } |