| | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.auction.controller.management.dto.AuctionGoodsDTO; |
| | | import com.ruoyi.auction.controller.management.dto.AuctionGoodsQuery; |
| | | import com.ruoyi.auction.controller.management.vo.AuctionBidRecordVO; |
| | | import com.ruoyi.auction.controller.management.vo.AuctionGoodsVO; |
| | | import com.ruoyi.auction.domain.AuctionBidRecord; |
| | | import com.ruoyi.auction.domain.AuctionGoods; |
| | | import com.ruoyi.auction.mapper.AuctionGoodsMapper; |
| | | import com.ruoyi.auction.service.IAuctionBidRecordService; |
| | | import com.ruoyi.auction.service.IAuctionGoodsService; |
| | | import com.ruoyi.common.core.constant.SecurityConstants; |
| | | import com.ruoyi.common.core.enums.AuctionStartStatusEnum; |
| | | 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.PageDTO; |
| | | import com.ruoyi.system.api.domain.GoodsSku; |
| | | import com.ruoyi.system.api.domain.Member; |
| | | import com.ruoyi.system.api.feignClient.GoodsSkuClient; |
| | | import com.ruoyi.system.api.feignClient.MemberClient; |
| | | import io.seata.spring.annotation.GlobalTransactional; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @RequiredArgsConstructor |
| | | public class AuctionGoodsServiceImpl extends ServiceImpl<AuctionGoodsMapper, AuctionGoods> implements IAuctionGoodsService { |
| | | |
| | | @Resource |
| | | private GoodsSkuClient goodsSkuClient; |
| | | |
| | | private final GoodsSkuClient goodsSkuClient; |
| | | private final IAuctionBidRecordService auctionBidRecordService; |
| | | private final MemberClient memberClient; |
| | | /** |
| | | * 获取拍卖商品列表的分页数据 |
| | | * |
| | |
| | | public PageDTO<AuctionGoodsVO> getAuctionGoodsPage(AuctionGoodsQuery query) { |
| | | Set<Long> goodsSkuIdList = null; |
| | | if (StringUtils.isNotEmpty(query.getGoodsSkuName())) { |
| | | List<GoodsSku> goodsSku = goodsSkuClient.getGoodsByName(query.getGoodsSkuName()) |
| | | List<GoodsSku> goodsSku = goodsSkuClient.getGoodsByName(query.getGoodsSkuName(), |
| | | SecurityConstants.INNER) |
| | | .getData(); |
| | | goodsSkuIdList = goodsSku.stream().map(GoodsSku::getId) |
| | | .collect(Collectors.toSet()); |
| | |
| | | } |
| | | |
| | | @Override |
| | | @GlobalTransactional(rollbackFor = Exception.class) |
| | | @Transactional |
| | | public void saveAuctionGoods(AuctionGoodsDTO dto) { |
| | | Long id = dto.getId(); |
| | | Long goodsSkuId = dto.getGoodsSkuId(); |
| | | AuctionGoods auctionGoods = BeanUtils.copyBean(dto, AuctionGoods.class); |
| | | // 添加 |
| | | if (StringUtils.isNull(id)) { |
| | | if (StringUtils.isNull(goodsSkuId)) { |
| | | throw new ServiceException("商品id不能为空"); |
| | | } |
| | | GoodsSku goodsSku = goodsSkuClient.getGoodsSkuOne(goodsSkuId).getData(); |
| | | GoodsSku goodsSku = goodsSkuClient.getGoodsSkuOne(goodsSkuId, SecurityConstants.INNER) |
| | | .getData(); |
| | | if (StringUtils.isNull(goodsSku)) { |
| | | throw new ServiceException("商品不存在"); |
| | | } |
| | | AuctionGoods auctionGoods = BeanUtils.copyBean(dto, AuctionGoods.class); |
| | | if (goodsSku.getStock() < dto.getAuctionStock()) { |
| | | throw new ServiceException("商品库存不足"); |
| | | } |
| | | this.save(auctionGoods); |
| | | |
| | | // 扣减商品库存 |
| | | goodsSkuClient.deductStock(goodsSkuId, dto.getAuctionStock(), SecurityConstants.INNER); |
| | | } else { |
| | | // 编辑 |
| | | |
| | | AuctionGoods auctionGoodsOrg = this.getById(id); |
| | | if (StringUtils.isNull(auctionGoodsOrg)) { |
| | | throw new ServiceException("拍卖商品不存在"); |
| | | } |
| | | if (auctionGoodsOrg.getStartStatus().equals(AuctionStartStatusEnum.IN_AUCTION)) { |
| | | throw new ServiceException("该商品正在拍卖中,不能修改"); |
| | | } |
| | | GoodsSku goodsSku = goodsSkuClient.getGoodsSkuOne(goodsSkuId, SecurityConstants.INNER) |
| | | .getData(); |
| | | if (StringUtils.isNull(goodsSku)) { |
| | | throw new ServiceException("商品不存在"); |
| | | } |
| | | if (goodsSku.getStock() + auctionGoodsOrg.getAuctionStock() < dto.getAuctionStock()) { |
| | | throw new ServiceException("商品库存不足"); |
| | | } |
| | | // 编辑商品时需要先回退商品库存,再扣减最新的商品库存 |
| | | goodsSkuClient.returningStock(auctionGoodsOrg.getGoodsSkuId(), |
| | | auctionGoodsOrg.getAuctionStock(), SecurityConstants.INNER); |
| | | auctionGoodsOrg.setStartingPrice(dto.getStartingPrice()); |
| | | auctionGoodsOrg.setMinimumMarkupAmount(dto.getMinimumMarkupAmount()); |
| | | auctionGoodsOrg.setStartTime(dto.getStartTime()); |
| | | auctionGoodsOrg.setEndTime(dto.getEndTime()); |
| | | auctionGoodsOrg.setAuctionStock(dto.getAuctionStock()); |
| | | auctionGoodsOrg.setBond(dto.getBond()); |
| | | auctionGoodsOrg.setAuthentication(dto.getAuthentication()); |
| | | auctionGoodsOrg.setDescription(dto.getDescription()); |
| | | auctionGoodsOrg.setShareTitle(dto.getShareTitle()); |
| | | auctionGoodsOrg.setSharePic(dto.getSharePic()); |
| | | this.updateById(auctionGoodsOrg); |
| | | // 扣减库存 |
| | | goodsSkuClient.deductStock(auctionGoodsOrg.getGoodsSkuId(), |
| | | auctionGoodsOrg.getAuctionStock(), SecurityConstants.INNER); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 查看拍卖商品详情 |
| | | * |
| | | * @param id 拍卖商品id |
| | | * @return AuctionGoodsVO |
| | | */ |
| | | @Override |
| | | public AuctionGoodsVO getAuctionGoodsById(Long id) { |
| | | AuctionGoods auctionGoods = this.getById(id); |
| | | if (StringUtils.isNull(auctionGoods)) { |
| | | throw new ServiceException("拍卖商品不存在"); |
| | | } |
| | | AuctionGoodsVO auctionGoodsVO = BeanUtils.copyBean(auctionGoods, AuctionGoodsVO.class); |
| | | if (!auctionGoods.getStartStatus().equals(AuctionStartStatusEnum.IN_PREVIEW)) { |
| | | List<AuctionBidRecord> list = auctionBidRecordService.lambdaQuery() |
| | | .eq(AuctionBidRecord::getGoodsSkuId, auctionGoods.getGoodsSkuId()) |
| | | .orderByDesc(AuctionBidRecord::getLastBidAmount).list(); |
| | | if (StringUtils.isNotEmpty(list)) { |
| | | Set<Long> collect = list.stream().map(AuctionBidRecord::getMemberId) |
| | | .collect(Collectors.toSet()); |
| | | List<Member> memberList = memberClient. |
| | | getMemberListByIds(collect, SecurityConstants.INNER).getData(); |
| | | Map<Long, String> memberMap = memberList.stream() |
| | | .collect(Collectors.toMap(Member::getId, Member::getNickname)); |
| | | List<AuctionBidRecordVO> auctionBidRecordVOS = BeanUtils.copyList(list, |
| | | AuctionBidRecordVO.class); |
| | | for (AuctionBidRecordVO vo : auctionBidRecordVOS) { |
| | | String memberName = memberMap.get(vo.getMemberId()); |
| | | vo.setMemberName(StringUtils.isNotEmpty(memberName) ? memberName : ""); |
| | | } |
| | | auctionGoodsVO.setRecordList(auctionBidRecordVOS); |
| | | } |
| | | } |
| | | return auctionGoodsVO; |
| | | } |
| | | |
| | | /** |
| | | * 立即结束 |
| | | * |
| | | * @param id 拍卖商品id |
| | | */ |
| | | @Override |
| | | public void stopAuctionGoods(Long id) { |
| | | AuctionGoods auctionGoods = this.lambdaQuery().eq(AuctionGoods::getId, id) |
| | | .eq(AuctionGoods::getStartStatus, AuctionStartStatusEnum.IN_AUCTION).one(); |
| | | if (StringUtils.isNull(auctionGoods)) { |
| | | throw new ServiceException("拍卖商品不存在"); |
| | | } |
| | | |
| | | } |
| | | } |