| | |
| | | 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.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.domain.dto.GoodsInfoTitleValueDTO; |
| | | import com.ruoyi.goods.domain.dto.GoodsSkuDTO; |
| | | import com.ruoyi.goods.domain.pojo.GoodsInfoTitleValue; |
| | | import com.ruoyi.goods.domain.pojo.GoodsSku; |
| | | import com.ruoyi.goods.domain.query.GoodsSkuQuery; |
| | | import com.ruoyi.goods.domain.vo.GoodsSkuVO; |
| | | import com.ruoyi.goods.mapper.GoodsSkuMapper; |
| | | import com.ruoyi.goods.service.IGoodsInfoTitleValueService; |
| | | import com.ruoyi.goods.service.IGoodsSkuService; |
| | | 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.Transactional; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2024-05-16 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | @RequiredArgsConstructor |
| | | public class GoodsSkuServiceImpl extends ServiceImpl<GoodsSkuMapper, GoodsSku> implements IGoodsSkuService { |
| | | |
| | | private final IGoodsInfoTitleValueService goodsInfoTitleValueService; |
| | | 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 { |
| | | 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()) |
| | | .page(new Page<GoodsSku>(query.getPageCurr(), query.getPageSize())); |
| | | |
| | | return PageDTO.of(page, GoodsSkuVO.class); |
| | | } |
| | | |
| | | /** |
| | | * 更新商品SKU的状态。 |
| | | * |
| | | * @param dto 数据传输对象,包含需要更新的SKU的ID和新的上架状态。 其中,ID用于指定要更新的具体SKU,listingStatus用于指定新的上架状态。 |
| | | * @return 无返回值。 |
| | | */ |
| | | @Override |
| | | public void updStatus(ListStatusDTO dto) { |
| | | this.lambdaUpdate() |
| | | .eq(GoodsSku::getId, dto.getId()) |
| | | .set(GoodsSku::getListingStatus, dto.getListingStatus()) |
| | | .update(); |
| | | } |
| | | } |