Compare commits
7 Commits
cursor/upg
...
cursor/che
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c796623148 | ||
![]() |
e6fecd8efe | ||
![]() |
af2a0c22cb | ||
![]() |
f781b29f3d | ||
![]() |
141bd22df6 | ||
![]() |
47f450fcf9 | ||
![]() |
3b2a3dd0ea |
5
pom.xml
5
pom.xml
@@ -40,6 +40,9 @@
|
|||||||
<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>3.2.2</maven-surefire-plugin.version>
|
||||||
<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.14.0</maven-compiler-plugin.version>
|
||||||
<flatten-maven-plugin.version>1.6.0</flatten-maven-plugin.version>
|
<flatten-maven-plugin.version>1.6.0</flatten-maven-plugin.version>
|
||||||
|
<!-- maven-surefire-plugin 暂时无法通过 bom 的依赖读取(兼容老版本 IDEA 2024 及以前版本) -->
|
||||||
|
<lombok.version>1.18.38</lombok.version>
|
||||||
|
<spring.boot.version>3.4.5</spring.boot.version>
|
||||||
<mapstruct.version>1.6.3</mapstruct.version>
|
<mapstruct.version>1.6.3</mapstruct.version>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
@@ -77,10 +80,12 @@
|
|||||||
<path>
|
<path>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
</path>
|
</path>
|
||||||
<path>
|
<path>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>${lombok.version}</version>
|
||||||
</path>
|
</path>
|
||||||
<path>
|
<path>
|
||||||
<groupId>org.mapstruct</groupId>
|
<groupId>org.mapstruct</groupId>
|
||||||
|
@@ -50,7 +50,7 @@
|
|||||||
<!-- 工具类相关 -->
|
<!-- 工具类相关 -->
|
||||||
<anji-plus-captcha.version>1.4.0</anji-plus-captcha.version>
|
<anji-plus-captcha.version>1.4.0</anji-plus-captcha.version>
|
||||||
<jsoup.version>1.18.3</jsoup.version>
|
<jsoup.version>1.18.3</jsoup.version>
|
||||||
<lombok.version>1.18.36</lombok.version>
|
<lombok.version>1.18.38</lombok.version>
|
||||||
<mapstruct.version>1.6.3</mapstruct.version>
|
<mapstruct.version>1.6.3</mapstruct.version>
|
||||||
<hutool-5.version>5.8.35</hutool-5.version>
|
<hutool-5.version>5.8.35</hutool-5.version>
|
||||||
<hutool-6.version>6.0.0-M19</hutool-6.version>
|
<hutool-6.version>6.0.0-M19</hutool-6.version>
|
||||||
|
@@ -10,7 +10,9 @@
|
|||||||
<if test="endTime != null">
|
<if test="endTime != null">
|
||||||
AND in_time < #{endTime}
|
AND in_time < #{endTime}
|
||||||
</if>
|
</if>
|
||||||
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getRequiredTenantId()}
|
<if test="@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId() != null">
|
||||||
|
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId()}
|
||||||
|
</if>
|
||||||
AND deleted = 0) -
|
AND deleted = 0) -
|
||||||
(SELECT IFNULL(SUM(total_price), 0)
|
(SELECT IFNULL(SUM(total_price), 0)
|
||||||
FROM erp_purchase_return
|
FROM erp_purchase_return
|
||||||
@@ -18,7 +20,9 @@
|
|||||||
<if test="endTime != null">
|
<if test="endTime != null">
|
||||||
AND return_time < #{endTime}
|
AND return_time < #{endTime}
|
||||||
</if>
|
</if>
|
||||||
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getRequiredTenantId()}
|
<if test="@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId() != null">
|
||||||
|
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId()}
|
||||||
|
</if>
|
||||||
AND deleted = 0)
|
AND deleted = 0)
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
@@ -10,7 +10,9 @@
|
|||||||
<if test="endTime != null">
|
<if test="endTime != null">
|
||||||
AND out_time < #{endTime}
|
AND out_time < #{endTime}
|
||||||
</if>
|
</if>
|
||||||
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getRequiredTenantId()}
|
<if test="@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId() != null">
|
||||||
|
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId()}
|
||||||
|
</if>
|
||||||
AND deleted = 0) -
|
AND deleted = 0) -
|
||||||
(SELECT IFNULL(SUM(total_price), 0)
|
(SELECT IFNULL(SUM(total_price), 0)
|
||||||
FROM erp_sale_return
|
FROM erp_sale_return
|
||||||
@@ -18,7 +20,9 @@
|
|||||||
<if test="endTime != null">
|
<if test="endTime != null">
|
||||||
AND return_time < #{endTime}
|
AND return_time < #{endTime}
|
||||||
</if>
|
</if>
|
||||||
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getRequiredTenantId()}
|
<if test="@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId() != null">
|
||||||
|
AND tenant_id = ${@cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder@getTenantId()}
|
||||||
|
</if>
|
||||||
AND deleted = 0)
|
AND deleted = 0)
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
@@ -0,0 +1,155 @@
|
|||||||
|
package cn.iocoder.yudao.module.erp.service.statistics;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||||
|
import cn.iocoder.yudao.module.erp.dal.mysql.statistics.ErpPurchaseStatisticsMapper;
|
||||||
|
import cn.iocoder.yudao.module.erp.dal.mysql.statistics.ErpSaleStatisticsMapper;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ERP 统计服务测试类
|
||||||
|
* 主要测试在多租户关闭情况下,统计查询是否能正常工作
|
||||||
|
*
|
||||||
|
* @author 芋道源码
|
||||||
|
*/
|
||||||
|
@SpringBootTest
|
||||||
|
@ActiveProfiles("unit-test")
|
||||||
|
public class ErpStatisticsServiceTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ErpSaleStatisticsService saleStatisticsService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ErpPurchaseStatisticsService purchaseStatisticsService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private ErpSaleStatisticsMapper saleStatisticsMapper;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private ErpPurchaseStatisticsMapper purchaseStatisticsMapper;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
// 清理租户上下文
|
||||||
|
TenantContextHolder.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
// 清理租户上下文
|
||||||
|
TenantContextHolder.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSaleStatisticsWithoutTenant() {
|
||||||
|
// 准备参数
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(2024, 1, 31, 23, 59, 59);
|
||||||
|
BigDecimal expectedPrice = new BigDecimal("1000.00");
|
||||||
|
|
||||||
|
// Mock 返回值
|
||||||
|
when(saleStatisticsMapper.getSalePrice(any(LocalDateTime.class), any(LocalDateTime.class)))
|
||||||
|
.thenReturn(expectedPrice);
|
||||||
|
|
||||||
|
// 测试:在没有租户ID的情况下调用销售统计
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
BigDecimal result = saleStatisticsService.getSalePrice(beginTime, endTime);
|
||||||
|
assertEquals(expectedPrice, result);
|
||||||
|
}, "在多租户关闭时,销售统计查询应该能正常工作");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPurchaseStatisticsWithoutTenant() {
|
||||||
|
// 准备参数
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(2024, 1, 31, 23, 59, 59);
|
||||||
|
BigDecimal expectedPrice = new BigDecimal("800.00");
|
||||||
|
|
||||||
|
// Mock 返回值
|
||||||
|
when(purchaseStatisticsMapper.getPurchasePrice(any(LocalDateTime.class), any(LocalDateTime.class)))
|
||||||
|
.thenReturn(expectedPrice);
|
||||||
|
|
||||||
|
// 测试:在没有租户ID的情况下调用采购统计
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
BigDecimal result = purchaseStatisticsService.getPurchasePrice(beginTime, endTime);
|
||||||
|
assertEquals(expectedPrice, result);
|
||||||
|
}, "在多租户关闭时,采购统计查询应该能正常工作");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSaleStatisticsWithTenant() {
|
||||||
|
// 设置租户ID
|
||||||
|
Long tenantId = 1L;
|
||||||
|
TenantContextHolder.setTenantId(tenantId);
|
||||||
|
|
||||||
|
// 准备参数
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(2024, 1, 31, 23, 59, 59);
|
||||||
|
BigDecimal expectedPrice = new BigDecimal("1500.00");
|
||||||
|
|
||||||
|
// Mock 返回值
|
||||||
|
when(saleStatisticsMapper.getSalePrice(any(LocalDateTime.class), any(LocalDateTime.class)))
|
||||||
|
.thenReturn(expectedPrice);
|
||||||
|
|
||||||
|
// 测试:在有租户ID的情况下调用销售统计
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
BigDecimal result = saleStatisticsService.getSalePrice(beginTime, endTime);
|
||||||
|
assertEquals(expectedPrice, result);
|
||||||
|
}, "在多租户开启时,销售统计查询应该能正常工作");
|
||||||
|
|
||||||
|
// 验证租户ID是否正确设置
|
||||||
|
assertEquals(tenantId, TenantContextHolder.getTenantId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPurchaseStatisticsWithTenant() {
|
||||||
|
// 设置租户ID
|
||||||
|
Long tenantId = 2L;
|
||||||
|
TenantContextHolder.setTenantId(tenantId);
|
||||||
|
|
||||||
|
// 准备参数
|
||||||
|
LocalDateTime beginTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0);
|
||||||
|
LocalDateTime endTime = LocalDateTime.of(2024, 1, 31, 23, 59, 59);
|
||||||
|
BigDecimal expectedPrice = new BigDecimal("1200.00");
|
||||||
|
|
||||||
|
// Mock 返回值
|
||||||
|
when(purchaseStatisticsMapper.getPurchasePrice(any(LocalDateTime.class), any(LocalDateTime.class)))
|
||||||
|
.thenReturn(expectedPrice);
|
||||||
|
|
||||||
|
// 测试:在有租户ID的情况下调用采购统计
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
BigDecimal result = purchaseStatisticsService.getPurchasePrice(beginTime, endTime);
|
||||||
|
assertEquals(expectedPrice, result);
|
||||||
|
}, "在多租户开启时,采购统计查询应该能正常工作");
|
||||||
|
|
||||||
|
// 验证租户ID是否正确设置
|
||||||
|
assertEquals(tenantId, TenantContextHolder.getTenantId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTenantContextHolderMethods() {
|
||||||
|
// 测试 getTenantId() 在没有设置租户时返回 null
|
||||||
|
assertNull(TenantContextHolder.getTenantId(), "未设置租户时应该返回 null");
|
||||||
|
|
||||||
|
// 设置租户ID
|
||||||
|
Long tenantId = 3L;
|
||||||
|
TenantContextHolder.setTenantId(tenantId);
|
||||||
|
assertEquals(tenantId, TenantContextHolder.getTenantId(), "设置租户后应该能正确获取");
|
||||||
|
|
||||||
|
// 清理租户上下文
|
||||||
|
TenantContextHolder.clear();
|
||||||
|
assertNull(TenantContextHolder.getTenantId(), "清理后应该返回 null");
|
||||||
|
}
|
||||||
|
}
|
@@ -40,10 +40,18 @@ public interface CouponTemplateMapper extends BaseMapperX<CouponTemplateDO> {
|
|||||||
.orderByDesc(CouponTemplateDO::getId));
|
.orderByDesc(CouponTemplateDO::getId));
|
||||||
}
|
}
|
||||||
|
|
||||||
default void updateTakeCount(Long id, Integer incrCount) {
|
default int updateTakeCount(Long id, Integer incrCount) {
|
||||||
update(null, new LambdaUpdateWrapper<CouponTemplateDO>()
|
LambdaUpdateWrapper<CouponTemplateDO> wrapper = new LambdaUpdateWrapper<CouponTemplateDO>()
|
||||||
.eq(CouponTemplateDO::getId, id)
|
.eq(CouponTemplateDO::getId, id);
|
||||||
.setSql("take_count = take_count + " + incrCount));
|
|
||||||
|
// 只在增加数量时检查库存(incrCount > 0)
|
||||||
|
if (incrCount > 0) {
|
||||||
|
// 添加库存判断:剩余数量 >= 领取的数量,或者总数量为-1(无限库存)
|
||||||
|
wrapper.and(w -> w.apply("total_count = -1 OR (total_count - take_count) >= {0}", incrCount));
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper.setSql("take_count = take_count + " + incrCount);
|
||||||
|
return update(null, wrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
default List<CouponTemplateDO> selectListByTakeType(Integer takeType) {
|
default List<CouponTemplateDO> selectListByTakeType(Integer takeType) {
|
||||||
|
@@ -279,12 +279,8 @@ public class CouponServiceImpl implements CouponService {
|
|||||||
if (ObjUtil.notEqual(couponTemplate.getTakeType(), takeType.getType())) {
|
if (ObjUtil.notEqual(couponTemplate.getTakeType(), takeType.getType())) {
|
||||||
throw exception(COUPON_TEMPLATE_CANNOT_TAKE);
|
throw exception(COUPON_TEMPLATE_CANNOT_TAKE);
|
||||||
}
|
}
|
||||||
// 校验发放数量不能过小(仅在 CouponTakeTypeEnum.USER 用户领取时)
|
// 注意:库存检查现在在数据库层面的 updateCouponTemplateTakeCount 方法中进行
|
||||||
if (CouponTakeTypeEnum.isUser(couponTemplate.getTakeType())
|
// 如果库存不足,该方法会抛出 COUPON_TEMPLATE_NOT_ENOUGH 异常
|
||||||
&& ObjUtil.notEqual(couponTemplate.getTakeLimitCount(), CouponTemplateDO.TIME_LIMIT_COUNT_MAX) // 非不限制
|
|
||||||
&& couponTemplate.getTakeCount() + userIds.size() > couponTemplate.getTotalCount()) {
|
|
||||||
throw exception(COUPON_TEMPLATE_NOT_ENOUGH);
|
|
||||||
}
|
|
||||||
// 校验"固定日期"的有效期类型是否过期
|
// 校验"固定日期"的有效期类型是否过期
|
||||||
if (CouponTemplateValidityTypeEnum.DATE.getType().equals(couponTemplate.getValidityType())) {
|
if (CouponTemplateValidityTypeEnum.DATE.getType().equals(couponTemplate.getValidityType())) {
|
||||||
if (LocalDateTimeUtils.beforeNow(couponTemplate.getValidEndTime())) {
|
if (LocalDateTimeUtils.beforeNow(couponTemplate.getValidEndTime())) {
|
||||||
|
@@ -23,6 +23,7 @@ import java.util.Objects;
|
|||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_NOT_EXISTS;
|
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_NOT_EXISTS;
|
||||||
|
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_NOT_ENOUGH;
|
||||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_TOTAL_COUNT_TOO_SMALL;
|
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_TEMPLATE_TOTAL_COUNT_TOO_SMALL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,7 +117,11 @@ public class CouponTemplateServiceImpl implements CouponTemplateService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateCouponTemplateTakeCount(Long id, int incrCount) {
|
public void updateCouponTemplateTakeCount(Long id, int incrCount) {
|
||||||
couponTemplateMapper.updateTakeCount(id, incrCount);
|
int updateCount = couponTemplateMapper.updateTakeCount(id, incrCount);
|
||||||
|
// 只在增加数量且更新失败时,说明库存不足,抛出异常
|
||||||
|
if (incrCount > 0 && updateCount == 0) {
|
||||||
|
throw exception(COUPON_TEMPLATE_NOT_ENOUGH);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -138,6 +138,7 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>${spring.boot.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<goals>
|
<goals>
|
||||||
|
Reference in New Issue
Block a user