feat:
1. add inventory unlist
This commit is contained in:
@@ -9,10 +9,7 @@ import lombok.AllArgsConstructor;
|
||||
import org.springblade.core.secure.annotation.PreAuth;
|
||||
import org.springblade.core.secure.utils.AuthUtil;
|
||||
import org.springblade.core.tool.api.R;
|
||||
import org.springblade.modules.api.request.IncreaseInventoryRequest;
|
||||
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.request.*;
|
||||
import org.springblade.modules.api.response.MerchantStatusResponse;
|
||||
import org.springblade.modules.api.vo.MarketMerchantCatalogApiVO;
|
||||
import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO;
|
||||
@@ -111,4 +108,11 @@ public class MarketMerchantController {
|
||||
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()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -16,8 +16,8 @@ public enum MarketBusinessModule {
|
||||
CASH_UPGRADE("现金动态升级", "cash_upgrade"),
|
||||
CASH_ACCELERATION("现金动态加速", "cash_acceleration"),
|
||||
MERCHANT_PURCHASING("自营商城进货", "merchant_purchasing"),
|
||||
MERCHANT_PUBLISH("自营商城上架商品", "merchant_publish")
|
||||
;
|
||||
MERCHANT_PUBLISH("自营商城上架商品", "merchant_publish"),
|
||||
MERCHANT_UNLIST("自营商城下架商品", "merchant_unlist");
|
||||
|
||||
@Getter
|
||||
private final String displayName;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -52,7 +52,9 @@ public interface MerchantInventoryMapper extends BaseMapper<MerchantInventoryEnt
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -41,8 +41,12 @@
|
||||
<update id="increaseInventory">
|
||||
update
|
||||
merchant_product_inventory
|
||||
set stock = stock + #{num} , version = version + 1
|
||||
where id = #{id} and version = #{version}
|
||||
set stock = stock + #{num},
|
||||
version = version + 1
|
||||
where id = #{id}
|
||||
and user_id = #{userId}
|
||||
and status = 1
|
||||
and version = #{version}
|
||||
</update>
|
||||
|
||||
<update id="reduceInventory">
|
||||
@@ -50,8 +54,23 @@
|
||||
merchant_product_inventory
|
||||
set stock = stock - #{num},
|
||||
version = version + 1
|
||||
where id = #{userId}
|
||||
and version = #{version}
|
||||
where id = #{id}
|
||||
and user_id = #{userId}
|
||||
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>
|
||||
</mapper>
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
package org.springblade.modules.market.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import org.springblade.core.mp.base.BaseService;
|
||||
import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO;
|
||||
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 unlist(Long userId, Long productId);
|
||||
}
|
||||
|
||||
@@ -33,10 +33,7 @@ import org.springblade.core.log.exception.ServiceException;
|
||||
import org.springblade.core.mp.base.BaseServiceImpl;
|
||||
import org.springblade.core.redis.ratelimiter.RateLimiter;
|
||||
import org.springblade.modules.api.vo.MarketMerchantInventoryApiVO;
|
||||
import org.springblade.modules.market.eums.MarketBusinessModule;
|
||||
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.eums.*;
|
||||
import org.springblade.modules.market.mapper.MerchantInventoryMapper;
|
||||
import org.springblade.modules.market.pojo.dto.MarketBenefitPointApiDTO;
|
||||
import org.springblade.modules.market.pojo.dto.MarketCashCouponApiDTO;
|
||||
@@ -68,7 +65,7 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
||||
|
||||
private final IUserService userService;
|
||||
|
||||
private final IMerchantCatalogService productService;
|
||||
private final IMerchantCatalogService catalogService;
|
||||
|
||||
private final IMarketCashCouponService cashCouponService;
|
||||
|
||||
@@ -103,35 +100,44 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
||||
if (num < 1) {
|
||||
throw new IllegalArgumentException("无效数量");
|
||||
}
|
||||
Integer merchantType = MarketInfoCache.checkMerchantType(userId);
|
||||
MerchantCatalogEntity product = productService.getById(productId);
|
||||
if (Objects.isNull(product)) {
|
||||
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(product.getType());
|
||||
MerchantProductType currentType = MerchantProductType.fromCode(catalog.getType());
|
||||
switch (currentType) {
|
||||
case PLATFORM -> {
|
||||
BigDecimal totalAmount = product.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)));
|
||||
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("无法对该商品操作");
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
if (Objects.nonNull(inventory)) {
|
||||
if (!Objects.equals(inventory.getStatus(), MerchantProductStatus.ON_SHELF.getCode())) {
|
||||
throw new ServiceException("无法为已下架的商品添加库存");
|
||||
}
|
||||
BigDecimal multiply = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(num));
|
||||
totalBenefitPointAmount = totalBenefitPointAmount.add(multiply);
|
||||
if (baseMapper.increaseInventory(inventory.getId(), num, inventory.getVersion()) < 1) {
|
||||
throw new ServiceException("操作失败");
|
||||
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) {
|
||||
@@ -141,17 +147,18 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
||||
entity.setUserId(userId);
|
||||
entity.setUserAccount(user.getAccount());
|
||||
entity.setType(currentType.getCode());
|
||||
entity.setProductName(product.getProductName());
|
||||
entity.setProductName(catalog.getProductName());
|
||||
entity.setProductId(productId);
|
||||
MarketInfoEntity info = infoService.getById(userId);
|
||||
if (Objects.nonNull(info)) {
|
||||
entity.setStoreName(info.getStoreName());
|
||||
}
|
||||
entity.setThumbImageUrl(product.getThumbImageUrl());
|
||||
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)) {
|
||||
@@ -160,8 +167,51 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
|
||||
}
|
||||
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]", product.getProductName(), product.getId(), num), ""))) {
|
||||
throw new ServiceException("操作失败");
|
||||
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("扣除福利积分失败");
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -302,6 +302,9 @@ blade:
|
||||
- method: POST
|
||||
pattern: /market/merchant/increase-inventory
|
||||
crypto: "sha1"
|
||||
- method: POST
|
||||
pattern: /market/merchant/unlist-inventory
|
||||
crypto: "sha1"
|
||||
#多终端认证配置
|
||||
client:
|
||||
- client-id: web-admin
|
||||
|
||||
Reference in New Issue
Block a user