|
|
|
|
@@ -89,7 +89,7 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
@RateLimiter(value = "inventory:limiter", param = "#userId", max = 1, ttl = 10, timeUnit = TimeUnit.SECONDS)
|
|
|
|
|
public boolean increase(Long userId, Long productId, Integer num, BigDecimal price, Integer giftType, BigDecimal giftAmount) {
|
|
|
|
|
public boolean increase(Long userId, Long productId, Integer num) {
|
|
|
|
|
if (Objects.isNull(userId)) {
|
|
|
|
|
throw new ServiceException("无效用户");
|
|
|
|
|
}
|
|
|
|
|
@@ -135,42 +135,16 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
|
|
|
|
.eq(MerchantInventoryEntity::getStatus, MerchantProductStatus.ON_SHELF.getCode())
|
|
|
|
|
.eq(MerchantInventoryEntity::getIsDeleted, 0)
|
|
|
|
|
.orderByDesc(MerchantInventoryEntity::getId));
|
|
|
|
|
BigDecimal totalBenefitPointAmount = BigDecimal.ZERO;
|
|
|
|
|
if (Objects.nonNull(inventory)) {
|
|
|
|
|
BigDecimal multiply = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(num));
|
|
|
|
|
totalBenefitPointAmount = totalBenefitPointAmount.add(multiply);
|
|
|
|
|
if (Objects.isNull(inventory)) {
|
|
|
|
|
throw new ServiceException("该商品还未上架");
|
|
|
|
|
}
|
|
|
|
|
BigDecimal benefitPointAmount = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(num));
|
|
|
|
|
BigDecimal fee = benefitPointAmount.multiply(BigDecimal.valueOf(0.2));
|
|
|
|
|
BigDecimal totalAmount = benefitPointAmount.add(fee);
|
|
|
|
|
if (baseMapper.increaseInventory(inventory.getId(), num, userId, inventory.getVersion()) < 1) {
|
|
|
|
|
throw new ServiceException("增加库存失败");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 1 || Objects.isNull(giftAmount) || giftAmount.compareTo(BigDecimal.ZERO) < 1) {
|
|
|
|
|
throw new ServiceException("无效参数");
|
|
|
|
|
}
|
|
|
|
|
MerchantInventoryEntity entity = new MerchantInventoryEntity();
|
|
|
|
|
entity.setUserId(userId);
|
|
|
|
|
entity.setUserAccount(user.getAccount());
|
|
|
|
|
entity.setType(currentType.getCode());
|
|
|
|
|
entity.setProductName(catalog.getProductName());
|
|
|
|
|
entity.setProductId(productId);
|
|
|
|
|
MarketInfoEntity info = infoService.getById(userId);
|
|
|
|
|
if (Objects.nonNull(info)) {
|
|
|
|
|
entity.setStoreName(info.getStoreName());
|
|
|
|
|
}
|
|
|
|
|
entity.setThumbImageUrl(catalog.getThumbImageUrl());
|
|
|
|
|
entity.setUnitPrice(price);
|
|
|
|
|
entity.setGiftType(MerchantGiftType.BENEFIT_POINT.getCode());
|
|
|
|
|
entity.setUnitGiftAmount(giftAmount);
|
|
|
|
|
entity.setStock(num);
|
|
|
|
|
entity.setStatus(MerchantProductStatus.ON_SHELF.getCode());
|
|
|
|
|
BigDecimal multiply = giftAmount.multiply(BigDecimal.valueOf(num));
|
|
|
|
|
totalBenefitPointAmount = totalBenefitPointAmount.add(multiply);
|
|
|
|
|
if (!save(entity)) {
|
|
|
|
|
throw new ServiceException("操作失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
BigDecimal fee = totalBenefitPointAmount.multiply(BigDecimal.valueOf(0.2));
|
|
|
|
|
totalBenefitPointAmount = totalBenefitPointAmount.add(fee);
|
|
|
|
|
if (!benefitPointService.deductBenefitPoint(new MarketBenefitPointApiDTO(userId, totalBenefitPointAmount, fee, MarketBusinessModule.MERCHANT_PUBLISH, null, "", String.format("上架商品[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), num)))) {
|
|
|
|
|
if (!benefitPointService.deductBenefitPoint(new MarketBenefitPointApiDTO(userId, totalAmount, fee, MarketBusinessModule.MERCHANT_PUBLISH, null, "", String.format("增加库存[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), num)))) {
|
|
|
|
|
throw new ServiceException("扣除福利积分失败");
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
@@ -221,4 +195,96 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
@RateLimiter(value = "inventory:limiter", param = "#userId", max = 1, ttl = 10, timeUnit = TimeUnit.SECONDS)
|
|
|
|
|
public boolean publish(Long userId, Long productId, Integer num, BigDecimal price, Integer giftType, BigDecimal giftAmount) {
|
|
|
|
|
if (Objects.isNull(userId)) {
|
|
|
|
|
throw new ServiceException("无效用户");
|
|
|
|
|
}
|
|
|
|
|
User user = userService.getById(userId);
|
|
|
|
|
if (Objects.isNull(user)) {
|
|
|
|
|
throw new ServiceException("无效用户");
|
|
|
|
|
}
|
|
|
|
|
if (Objects.isNull(num) || num < 1) {
|
|
|
|
|
throw new IllegalArgumentException("无效数量");
|
|
|
|
|
}
|
|
|
|
|
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 1 || Objects.isNull(giftAmount) || giftAmount.compareTo(BigDecimal.ZERO) < 1) {
|
|
|
|
|
throw new ServiceException("无效参数");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MerchantCatalogEntity catalog = catalogService.getById(productId);
|
|
|
|
|
if (Objects.isNull(catalog)) {
|
|
|
|
|
throw new ServiceException("无效商品");
|
|
|
|
|
}
|
|
|
|
|
Integer merchantType = MarketInfoCache.checkMerchantType(userId);
|
|
|
|
|
MerchantType userType = MerchantType.fromCode(merchantType);
|
|
|
|
|
if (userType.getCode() == MerchantType.UNREVIEWED.getCode()) {
|
|
|
|
|
throw new ServiceException("用户没有该操作权限");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MerchantProductType currentType = MerchantProductType.fromCode(catalog.getType());
|
|
|
|
|
switch (currentType) {
|
|
|
|
|
case PLATFORM -> {
|
|
|
|
|
BigDecimal totalAmount = catalog.getCostPrice().multiply(BigDecimal.valueOf(num));
|
|
|
|
|
if (!cashCouponService.deductCashCoupon(new MarketCashCouponApiDTO(userId, totalAmount, MarketBusinessModule.MERCHANT_PURCHASING, null, "", String.format("采购商品[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), num)))) {
|
|
|
|
|
throw new ServiceException("扣除现金券失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case MERCHANT -> {
|
|
|
|
|
if (userType.getCode() != MerchantType.ENTERPRISE.getCode()) {
|
|
|
|
|
throw new ServiceException("无法添加该类型商品");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(catalog.getUserId(), userId)) {
|
|
|
|
|
throw new ServiceException("无法对该商品操作");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
boolean isNew = false;
|
|
|
|
|
MerchantInventoryEntity inventory = getOne(Wrappers.lambdaQuery(MerchantInventoryEntity.class)
|
|
|
|
|
.eq(MerchantInventoryEntity::getUserId, userId)
|
|
|
|
|
.eq(MerchantInventoryEntity::getType, currentType.getCode())
|
|
|
|
|
.eq(MerchantInventoryEntity::getProductId, productId)
|
|
|
|
|
.eq(MerchantInventoryEntity::getIsDeleted, 0)
|
|
|
|
|
.orderByDesc(MerchantInventoryEntity::getId));
|
|
|
|
|
if (Objects.nonNull(inventory)) {
|
|
|
|
|
MerchantProductStatus status = MerchantProductStatus.fromCode(inventory.getStatus());
|
|
|
|
|
if (status == MerchantProductStatus.ON_SHELF) {
|
|
|
|
|
throw new ServiceException("该商品已上架");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
isNew = true;
|
|
|
|
|
}
|
|
|
|
|
BigDecimal benefitPointAmount = giftAmount.multiply(BigDecimal.valueOf(num));
|
|
|
|
|
BigDecimal fee = benefitPointAmount.multiply(BigDecimal.valueOf(0.2));
|
|
|
|
|
BigDecimal totalAmount = benefitPointAmount.add(fee);
|
|
|
|
|
if (!benefitPointService.deductBenefitPoint(new MarketBenefitPointApiDTO(userId, totalAmount, fee, MarketBusinessModule.MERCHANT_PUBLISH, null, "", String.format("上架商品[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), num)))) {
|
|
|
|
|
throw new ServiceException("扣除福利积分失败");
|
|
|
|
|
}
|
|
|
|
|
if (isNew) {
|
|
|
|
|
inventory = new MerchantInventoryEntity();
|
|
|
|
|
inventory.setUserId(userId);
|
|
|
|
|
inventory.setUserAccount(user.getAccount());
|
|
|
|
|
inventory.setType(currentType.getCode());
|
|
|
|
|
inventory.setProductName(catalog.getProductName());
|
|
|
|
|
inventory.setProductId(productId);
|
|
|
|
|
MarketInfoEntity info = infoService.getById(userId);
|
|
|
|
|
if (Objects.nonNull(info)) {
|
|
|
|
|
inventory.setStoreName(info.getStoreName());
|
|
|
|
|
}
|
|
|
|
|
inventory.setThumbImageUrl(catalog.getThumbImageUrl());
|
|
|
|
|
inventory.setGiftType(MerchantGiftType.BENEFIT_POINT.getCode());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
inventory.setUnitPrice(price);
|
|
|
|
|
inventory.setUnitGiftAmount(giftAmount);
|
|
|
|
|
inventory.setStock(num);
|
|
|
|
|
inventory.setStatus(MerchantProductStatus.ON_SHELF.getCode());
|
|
|
|
|
boolean saveOrUpdate = saveOrUpdate(inventory);
|
|
|
|
|
if (!saveOrUpdate) {
|
|
|
|
|
throw new ServiceException("操作失败");
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|