1. separate api
This commit is contained in:
2026-04-26 19:19:00 +08:00
parent 90f973adad
commit 1a330785e3
7 changed files with 141 additions and 46 deletions

View File

@@ -84,7 +84,7 @@ public class MarketMerchantController {
@ApiOperationSupport(order = 7) @ApiOperationSupport(order = 7)
@Operation(summary = "增加库存") @Operation(summary = "增加库存")
public R increaseInventory(@RequestBody @Valid IncreaseInventoryRequest request) { public R increaseInventory(@RequestBody @Valid IncreaseInventoryRequest request) {
return R.status(inventoryService.increase(AuthUtil.getUserId(), request.getProductId(), request.getNum(), request.getPrice(), request.getGiftType(), request.getGiftAmount())); return R.status(inventoryService.increase(AuthUtil.getUserId(), request.getProductId(), request.getNum()));
} }
@GetMapping("/is-available") @GetMapping("/is-available")
@@ -115,4 +115,11 @@ public class MarketMerchantController {
return R.status(inventoryService.unlist(AuthUtil.getUserId(), request.getProductId())); return R.status(inventoryService.unlist(AuthUtil.getUserId(), request.getProductId()));
} }
@PostMapping("/publish-inventory")
@ApiOperationSupport(order = 7)
@Operation(summary = "上架商品")
public R publish(@RequestBody @Valid PublishInventoryRequest request) {
return R.status(inventoryService.publish(AuthUtil.getUserId(), request.getProductId(), request.getNum(), request.getPrice(), request.getGiftType(), request.getGiftAmount()));
}
} }

View File

@@ -15,10 +15,4 @@ public class IncreaseInventoryRequest implements Serializable {
@NotNull(message = "无效参数") @NotNull(message = "无效参数")
private Integer num; private Integer num;
private BigDecimal price;
private Integer giftType;
private BigDecimal giftAmount;
} }

View File

@@ -0,0 +1,26 @@
package org.springblade.modules.api.request;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
@Data
public class PublishInventoryRequest implements Serializable {
@NotNull(message = "无效参数")
private Long productId;
@NotNull(message = "无效参数")
private Integer num;
@NotNull(message = "无效参数")
private BigDecimal price;
private Integer giftType;
@NotNull(message = "无效参数")
private BigDecimal giftAmount;
}

View File

@@ -25,4 +25,6 @@ public class MarketMerchantInventoryApiVO implements Serializable {
private String remark; private String remark;
private Integer status;
} }

View File

@@ -35,7 +35,6 @@
select * select *
from merchant_product_inventory from merchant_product_inventory
where user_id = #{userId} where user_id = #{userId}
and status = 1
and is_deleted = 0 and is_deleted = 0
</select> </select>

View File

@@ -26,7 +26,6 @@
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;
@@ -53,7 +52,9 @@ public interface IMerchantInventoryService extends BaseService<MerchantInventory
List<MarketMerchantInventoryApiVO> inventoryListByUserId(Long userId); List<MarketMerchantInventoryApiVO> inventoryListByUserId(Long userId);
boolean increase(Long userId, Long productId, Integer num, BigDecimal price, Integer giftType, BigDecimal giftAmount); boolean increase(Long userId, Long productId, Integer num);
boolean unlist(Long userId, Long productId); boolean unlist(Long userId, Long productId);
boolean publish(Long userId, Long productId, Integer num, BigDecimal price, Integer giftType, BigDecimal giftAmount);
} }

View File

@@ -89,7 +89,7 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@RateLimiter(value = "inventory:limiter", param = "#userId", max = 1, ttl = 10, timeUnit = TimeUnit.SECONDS) @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)) { if (Objects.isNull(userId)) {
throw new ServiceException("无效用户"); throw new ServiceException("无效用户");
} }
@@ -135,42 +135,16 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
.eq(MerchantInventoryEntity::getStatus, MerchantProductStatus.ON_SHELF.getCode()) .eq(MerchantInventoryEntity::getStatus, MerchantProductStatus.ON_SHELF.getCode())
.eq(MerchantInventoryEntity::getIsDeleted, 0) .eq(MerchantInventoryEntity::getIsDeleted, 0)
.orderByDesc(MerchantInventoryEntity::getId)); .orderByDesc(MerchantInventoryEntity::getId));
BigDecimal totalBenefitPointAmount = BigDecimal.ZERO; if (Objects.isNull(inventory)) {
if (Objects.nonNull(inventory)) { throw new ServiceException("该商品还未上架");
BigDecimal multiply = inventory.getUnitGiftAmount().multiply(BigDecimal.valueOf(num)); }
totalBenefitPointAmount = totalBenefitPointAmount.add(multiply); 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) { if (baseMapper.increaseInventory(inventory.getId(), num, userId, inventory.getVersion()) < 1) {
throw new ServiceException("增加库存失败"); throw new ServiceException("增加库存失败");
} }
} else { if (!benefitPointService.deductBenefitPoint(new MarketBenefitPointApiDTO(userId, totalAmount, fee, MarketBusinessModule.MERCHANT_PUBLISH, null, "", String.format("增加库存[%s],ID[%d],数量[%d]", catalog.getProductName(), catalog.getId(), num)))) {
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)))) {
throw new ServiceException("扣除福利积分失败"); throw new ServiceException("扣除福利积分失败");
} }
return true; return true;
@@ -221,4 +195,96 @@ public class MerchantInventoryServiceImpl extends BaseServiceImpl<MerchantInvent
return true; 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;
}
} }