1. add inventory unlist
This commit is contained in:
2026-04-25 21:52:21 +08:00
parent 1d6e7f2f0f
commit 49ee6f354c
9 changed files with 155 additions and 34 deletions

View File

@@ -9,10 +9,7 @@ import lombok.AllArgsConstructor;
import org.springblade.core.secure.annotation.PreAuth; import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.modules.api.request.IncreaseInventoryRequest; import org.springblade.modules.api.request.*;
import org.springblade.modules.api.request.SubmitMerchantProductRequest;
import org.springblade.modules.api.request.SubmitPayoutAccountRequest;
import org.springblade.modules.api.request.VerifyMerchantRequest;
import org.springblade.modules.api.response.MerchantStatusResponse; import org.springblade.modules.api.response.MerchantStatusResponse;
import org.springblade.modules.api.vo.MarketMerchantCatalogApiVO; import org.springblade.modules.api.vo.MarketMerchantCatalogApiVO;
import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO; import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO;
@@ -111,4 +108,11 @@ public class MarketMerchantController {
return R.data(payoutAccountService.accountListByUserId(AuthUtil.getUserId())); return R.data(payoutAccountService.accountListByUserId(AuthUtil.getUserId()));
} }
@PostMapping("/unlist-inventory")
@ApiOperationSupport(order = 11)
@Operation(summary = "下架库存")
public R unlistInventory(@RequestBody @Valid UnlistInventoryRequest request) {
return R.status(inventoryService.unlist(AuthUtil.getUserId(), request.getProductId()));
}
} }

View File

@@ -0,0 +1,14 @@
package org.springblade.modules.api.request;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.io.Serializable;
@Data
public class UnlistInventoryRequest implements Serializable {
@NotNull(message = "无效参数")
private Long productId;
}

View File

@@ -16,8 +16,8 @@ public enum MarketBusinessModule {
CASH_UPGRADE("现金动态升级", "cash_upgrade"), CASH_UPGRADE("现金动态升级", "cash_upgrade"),
CASH_ACCELERATION("现金动态加速", "cash_acceleration"), CASH_ACCELERATION("现金动态加速", "cash_acceleration"),
MERCHANT_PURCHASING("自营商城进货", "merchant_purchasing"), MERCHANT_PURCHASING("自营商城进货", "merchant_purchasing"),
MERCHANT_PUBLISH("自营商城上架商品", "merchant_publish") MERCHANT_PUBLISH("自营商城上架商品", "merchant_publish"),
; MERCHANT_UNLIST("自营商城下架商品", "merchant_unlist");
@Getter @Getter
private final String displayName; private final String displayName;

View File

@@ -0,0 +1,27 @@
package org.springblade.modules.market.eums;
import lombok.Getter;
@Getter
public enum MerchantProductStatus {
ON_SHELF(1,"上架"),
OFF_SHELF(2,"下架");
private final Integer code;
private final String desc;
MerchantProductStatus(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public static MerchantProductStatus fromCode(Integer code) {
for (MerchantProductStatus type : MerchantProductStatus.values()) {
if (type.code.equals(code)) {
return type;
}
}
throw new IllegalArgumentException("无效的商品状态 code: " + code);
}
}

View File

@@ -52,7 +52,9 @@ public interface MerchantInventoryMapper extends BaseMapper<MerchantInventoryEnt
List<MarketMerchantInventoryApiVO> selectProductInventoryApiList(Long userId); List<MarketMerchantInventoryApiVO> selectProductInventoryApiList(Long userId);
int increaseInventory(Long id, Integer num, Integer version); int increaseInventory(Long id, Integer num, Long userId, Integer version);
int reduceInventory(Long id, Integer num, Integer version); int reduceInventory(Long id, Integer num, Long userId, Integer version);
int unlistInventory(Long id, Integer num, Long userId, Integer version);
} }

View File

@@ -41,8 +41,12 @@
<update id="increaseInventory"> <update id="increaseInventory">
update update
merchant_product_inventory merchant_product_inventory
set stock = stock + #{num} , version = version + 1 set stock = stock + #{num},
where id = #{id} and version = #{version} version = version + 1
where id = #{id}
and user_id = #{userId}
and status = 1
and version = #{version}
</update> </update>
<update id="reduceInventory"> <update id="reduceInventory">
@@ -50,8 +54,23 @@
merchant_product_inventory merchant_product_inventory
set stock = stock - #{num}, set stock = stock - #{num},
version = version + 1 version = version + 1
where id = #{userId} where id = #{id}
and version = #{version} and user_id = #{userId}
and (stock - #{num}) >= 0 and (stock - #{num}) >= 0
and status = 1
and version = #{version}
</update>
<update id="unlistInventory">
update
merchant_product_inventory
set stock = 0,
status = 2,
version = version + 1
where id = #{id}
and stock = #{num}
and user_id = #{userId}
and status = 1
and version = #{version}
</update> </update>
</mapper> </mapper>

View File

@@ -26,6 +26,7 @@
package org.springblade.modules.market.service; package org.springblade.modules.market.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.validation.constraints.NotNull;
import org.springblade.core.mp.base.BaseService; import org.springblade.core.mp.base.BaseService;
import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO; import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO;
import org.springblade.modules.market.pojo.entity.MerchantInventoryEntity; import org.springblade.modules.market.pojo.entity.MerchantInventoryEntity;
@@ -54,4 +55,5 @@ public interface IMerchantInventoryService extends BaseService<MerchantInventory
boolean increase(Long userId, Long productId, Integer num, BigDecimal price, Integer giftType, BigDecimal giftAmount); boolean increase(Long userId, Long productId, Integer num, BigDecimal price, Integer giftType, BigDecimal giftAmount);
boolean unlist(Long userId, Long productId);
} }

View File

@@ -33,10 +33,7 @@ import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.redis.ratelimiter.RateLimiter; import org.springblade.core.redis.ratelimiter.RateLimiter;
import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO; import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO;
import org.springblade.modules.market.eums.MarketBusinessModule; import org.springblade.modules.market.eums.*;
import org.springblade.modules.market.eums.MerchantGiftType;
import org.springblade.modules.market.eums.MerchantProductType;
import org.springblade.modules.market.eums.MerchantType;
import org.springblade.modules.market.mapper.MerchantInventoryMapper; import org.springblade.modules.market.mapper.MerchantInventoryMapper;
import org.springblade.modules.market.pojo.dto.MarketBenefitPointApiDTO; import org.springblade.modules.market.pojo.dto.MarketBenefitPointApiDTO;
import org.springblade.modules.market.pojo.dto.MarketCashCouponApiDTO; import org.springblade.modules.market.pojo.dto.MarketCashCouponApiDTO;
@@ -68,7 +65,7 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
private final IUserService userService; private final IUserService userService;
private final IMerchantCatalogService productService; private final IMerchantCatalogService catalogService;
private final IMarketCashCouponService cashCouponService; private final IMarketCashCouponService cashCouponService;
@@ -103,35 +100,44 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
if (num < 1) { if (num < 1) {
throw new IllegalArgumentException("无效数量"); throw new IllegalArgumentException("无效数量");
} }
Integer merchantType = MarketInfoCache.checkMerchantType(userId); MerchantCatalogEntity catalog = catalogService.getById(productId);
MerchantCatalogEntity product = productService.getById(productId); if (Objects.isNull(catalog)) {
if (Objects.isNull(product)) {
throw new ServiceException("无效商品"); throw new ServiceException("无效商品");
} }
Integer merchantType = MarketInfoCache.checkMerchantType(userId);
MerchantType userType = MerchantType.fromCode(merchantType); MerchantType userType = MerchantType.fromCode(merchantType);
if (userType.getCode() == MerchantType.UNREVIEWED.getCode()) { if (userType.getCode() == MerchantType.UNREVIEWED.getCode()) {
throw new ServiceException("用户没有该操作权限"); throw new ServiceException("用户没有该操作权限");
} }
MerchantProductType currentType = MerchantProductType.fromCode(product.getType()); MerchantProductType currentType = MerchantProductType.fromCode(catalog.getType());
switch (currentType) { switch (currentType) {
case PLATFORM -> { case PLATFORM -> {
BigDecimal totalAmount = product.getCostPrice().multiply(BigDecimal.valueOf(num)); BigDecimal totalAmount = catalog.getCostPrice().multiply(BigDecimal.valueOf(num));
cashCouponService.deductCashCoupon(new MarketCashCouponApiDTO(userId, totalAmount, MarketBusinessModule.MERCHANT_PURCHASING, null, "", String.format("采购商品[%s],ID[%d],数量[%d]", product.getProductName(), product.getId(), 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 -> { case MERCHANT -> {
if (userType.getCode() != MerchantType.ENTERPRISE.getCode()) { if (userType.getCode() != MerchantType.ENTERPRISE.getCode()) {
throw new ServiceException("无法添加该类型商品"); throw new ServiceException("无法添加该类型商品");
} }
if (!Objects.equals(catalog.getUserId(), userId)) {
throw new ServiceException("无法对该商品操作");
}
} }
} }
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)); 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));
BigDecimal totalBenefitPointAmount = BigDecimal.ZERO; BigDecimal totalBenefitPointAmount = BigDecimal.ZERO;
if (Objects.nonNull(inventory)) { if (Objects.nonNull(inventory)) {
if (!Objects.equals(inventory.getStatus(), MerchantProductStatus.ON_SHELF.getCode())) {
throw new ServiceException("无法为已下架的商品添加库存");
}
BigDecimal multiply = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(num)); BigDecimal multiply = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(num));
totalBenefitPointAmount = totalBenefitPointAmount.add(multiply); totalBenefitPointAmount = totalBenefitPointAmount.add(multiply);
if (baseMapper.increaseInventory(inventory.getId(), num, inventory.getVersion()) < 1) { if (baseMapper.increaseInventory(inventory.getId(), num, userId, inventory.getVersion()) < 1) {
throw new ServiceException("操作失败"); throw new ServiceException("增加库存失败");
} }
} else { } else {
if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 1 || Objects.isNull(giftAmount) || giftAmount.compareTo(BigDecimal.ZERO) < 1) { if (Objects.isNull(price) || price.compareTo(BigDecimal.ZERO) < 1 || Objects.isNull(giftAmount) || giftAmount.compareTo(BigDecimal.ZERO) < 1) {
@@ -141,17 +147,18 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
entity.setUserId(userId); entity.setUserId(userId);
entity.setUserAccount(user.getAccount()); entity.setUserAccount(user.getAccount());
entity.setType(currentType.getCode()); entity.setType(currentType.getCode());
entity.setProductName(product.getProductName()); entity.setProductName(catalog.getProductName());
entity.setProductId(productId); entity.setProductId(productId);
MarketInfoEntity info = infoService.getById(userId); MarketInfoEntity info = infoService.getById(userId);
if (Objects.nonNull(info)) { if (Objects.nonNull(info)) {
entity.setStoreName(info.getStoreName()); entity.setStoreName(info.getStoreName());
} }
entity.setThumbImageUrl(product.getThumbImageUrl()); entity.setThumbImageUrl(catalog.getThumbImageUrl());
entity.setUnitPrice(price); entity.setUnitPrice(price);
entity.setGiftType(MerchantGiftType.BENEFIT_POINT.getCode()); entity.setGiftType(MerchantGiftType.BENEFIT_POINT.getCode());
entity.setUnitGiftAmount(giftAmount); entity.setUnitGiftAmount(giftAmount);
entity.setStock(num); entity.setStock(num);
entity.setStatus(MerchantProductStatus.ON_SHELF.getCode());
BigDecimal multiply = giftAmount.multiply(BigDecimal.valueOf(num)); BigDecimal multiply = giftAmount.multiply(BigDecimal.valueOf(num));
totalBenefitPointAmount = totalBenefitPointAmount.add(multiply); totalBenefitPointAmount = totalBenefitPointAmount.add(multiply);
if (!save(entity)) { if (!save(entity)) {
@@ -160,8 +167,51 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
} }
BigDecimal fee = totalBenefitPointAmount.multiply(BigDecimal.valueOf(0.2)); BigDecimal fee = totalBenefitPointAmount.multiply(BigDecimal.valueOf(0.2));
totalBenefitPointAmount = totalBenefitPointAmount.add(fee); totalBenefitPointAmount = totalBenefitPointAmount.add(fee);
if (!benefitPointService.deductBenefitPoint(new MarketBenefitPointApiDTO(userId, totalBenefitPointAmount, fee, MarketBusinessModule.MERCHANT_PUBLISH, null, String.format("上架商品[%s],ID[%d],数量[%d]", product.getProductName(), product.getId(), num), ""))) { if (!benefitPointService.deductBenefitPoint(new MarketBenefitPointApiDTO(userId, totalBenefitPointAmount, fee, MarketBusinessModule.MERCHANT_PUBLISH, null, "", String.format("上架商品[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), num)))) {
throw new ServiceException("操作失败"); throw new ServiceException("扣除福利积分失败");
}
return true;
}
@Override
@RateLimiter(value = "inventory:limiter", param = "#userId", max = 1, ttl = 10, timeUnit = TimeUnit.SECONDS)
@Transactional(rollbackFor = Exception.class)
public boolean unlist(Long userId, Long productId) {
if (Objects.isNull(userId) || Objects.isNull(productId)) {
throw new ServiceException("无效参数");
}
Integer merchantType = MarketInfoCache.checkMerchantType(userId);
MerchantType userType = MerchantType.fromCode(merchantType);
if (userType == MerchantType.UNREVIEWED) {
throw new ServiceException("用户没有该操作权限");
}
MerchantInventoryEntity inventory = getOne(Wrappers.lambdaQuery(MerchantInventoryEntity.class).eq(MerchantInventoryEntity::getProductId, productId).eq(MerchantInventoryEntity::getUserId, userId).eq(MerchantInventoryEntity::getStatus, MerchantProductStatus.ON_SHELF).eq(MerchantInventoryEntity::getIsDeleted, 0).orderByDesc(MerchantInventoryEntity::getId));
if (Objects.isNull(inventory)) {
throw new ServiceException("商品不存在");
}
if (baseMapper.unlistInventory(inventory.getId(), inventory.getStock(), userId, inventory.getVersion()) < 1) {
throw new ServiceException("下架失败");
}
if (inventory.getStock() > 0) {
MerchantProductType inventoryType = MerchantProductType.fromCode(inventory.getType());
MerchantCatalogEntity catalog = catalogService.getById(inventory.getProductId());
if (Objects.isNull(catalog)) {
throw new ServiceException("未找到商品目录,操作失败");
}
if (inventoryType == MerchantProductType.PLATFORM){
BigDecimal totalCashCouponAmount = catalog.getCostPrice().multiply(BigDecimal.valueOf(inventory.getStock()));
if (!cashCouponService.addCashCoupon(new MarketCashCouponApiDTO(userId, totalCashCouponAmount, MarketBusinessModule.MERCHANT_UNLIST, null, "", String.format("下架商品[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), inventory.getStock())))) {
throw new ServiceException("返还现金券失败");
}
}
BigDecimal totalBenefitPointAmount = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(inventory.getStock()));
BigDecimal fee = totalBenefitPointAmount.multiply(BigDecimal.valueOf(0.2));
totalBenefitPointAmount = totalBenefitPointAmount.add(fee);
if (!benefitPointService.addBenefitPoint(new MarketBenefitPointApiDTO(userId, totalBenefitPointAmount, fee, MarketBusinessModule.MERCHANT_UNLIST, null, "", String.format("下架商品[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), inventory.getStock())))){
throw new ServiceException("返还福利积分失败");
}
} }
return true; return true;
} }

View File

@@ -302,6 +302,9 @@ blade:
- method: POST - method: POST
pattern: /market/merchant/increase-inventory pattern: /market/merchant/increase-inventory
crypto: "sha1" crypto: "sha1"
- method: POST
pattern: /market/merchant/unlist-inventory
crypto: "sha1"
#多终端认证配置 #多终端认证配置
client: client:
- client-id: web-admin - client-id: web-admin