Merge remote-tracking branch 'origin/test' into test

This commit is contained in:
liuminghui
2026-10-27 13:36:11 +08:00
19 changed files with 782 additions and 29 deletions
@@ -143,7 +143,13 @@ public class SysRoleController extends BaseController
{ {
roleService.checkRoleAllowed(role); roleService.checkRoleAllowed(role);
roleService.checkRoleDataScope(role.getRoleId()); roleService.checkRoleDataScope(role.getRoleId());
return toAjax(roleService.authDataScope(role)); int rows = roleService.authDataScope(role);
if (rows > 0)
{
SysRole savedRole = roleService.selectRoleById(role.getRoleId());
tokenService.refreshPermissionByRoleId(role.getRoleId(), permissionService, savedRole.getDataScope());
}
return toAjax(rows);
} }
/** /**
@@ -0,0 +1,378 @@
package com.bms.ar;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.util.ReflectionTestUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.bms.ar.domain.ArBill;
import com.bms.ar.domain.ArBillExport;
import com.bms.ar.domain.ArBillGenerateTask;
import com.bms.ar.domain.ArBillLog;
import com.bms.ar.domain.vo.ArBillGenerateQuery;
import com.bms.ar.domain.vo.ArBillGenerateRequest;
import com.bms.ar.domain.vo.ArBillQuery;
import com.bms.ar.mapper.ArBillDetailFeeMapper;
import com.bms.ar.mapper.ArBillDetailMapper;
import com.bms.ar.mapper.ArBillExportMapper;
import com.bms.ar.mapper.ArBillGenerateTaskMapper;
import com.bms.ar.mapper.ArBillLogMapper;
import com.bms.ar.mapper.ArBillMapper;
import com.bms.ar.service.ArBillDataScopeService;
import com.bms.ar.service.impl.ArBillServiceImpl;
import com.bms.common.core.domain.entity.SysRole;
import com.bms.common.core.domain.entity.SysUser;
import com.bms.common.core.domain.model.LoginUser;
import com.bms.common.exception.ServiceException;
import com.bms.common.utils.file.OssStorage;
import com.bms.framework.aspectj.DataScopeAspect;
import com.bms.price.mapper.PrcOrderBillMapper;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/** 不启动应用或连接业务数据库,验证真实切面、Mapper SQL 和业务入口的越权阻断。 */
class ArBillDataScopeTest {
private ArBillMapper billMapper;
private ArBillDetailMapper detailMapper;
private ArBillDetailFeeMapper feeMapper;
private ArBillExportMapper exportMapper;
private ArBillGenerateTaskMapper taskMapper;
private ArBillLogMapper logMapper;
private PrcOrderBillMapper sourceMapper;
private ArBillDataScopeService scopeService;
private ArBillServiceImpl service;
@BeforeEach
void setUp() {
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(new MockHttpServletRequest()));
billMapper = mock(ArBillMapper.class);
detailMapper = mock(ArBillDetailMapper.class);
feeMapper = mock(ArBillDetailFeeMapper.class);
exportMapper = mock(ArBillExportMapper.class);
taskMapper = mock(ArBillGenerateTaskMapper.class);
logMapper = mock(ArBillLogMapper.class);
sourceMapper = mock(PrcOrderBillMapper.class);
ArBillDataScopeService scopeTarget = new ArBillDataScopeService();
ReflectionTestUtils.setField(scopeTarget, "billMapper", billMapper);
ReflectionTestUtils.setField(scopeTarget, "logMapper", logMapper);
ReflectionTestUtils.setField(scopeTarget, "generateTaskMapper", taskMapper);
scopeService = proxy(scopeTarget);
ArBillServiceImpl target = new ArBillServiceImpl();
ReflectionTestUtils.setField(target, "billMapper", billMapper);
ReflectionTestUtils.setField(target, "detailMapper", detailMapper);
ReflectionTestUtils.setField(target, "detailFeeMapper", feeMapper);
ReflectionTestUtils.setField(target, "exportMapper", exportMapper);
ReflectionTestUtils.setField(target, "generateTaskMapper", taskMapper);
ReflectionTestUtils.setField(target, "logMapper", logMapper);
ReflectionTestUtils.setField(target, "sourceBillMapper", sourceMapper);
ReflectionTestUtils.setField(target, "dataScopeService", scopeService);
service = proxy(target);
login(42L, role(10L, "5", "ar:bill:list"));
}
@AfterEach
void cleanUp() {
SecurityContextHolder.clearContext();
RequestContextHolder.resetRequestAttributes();
PageHelper.clearPage();
}
@ParameterizedTest
@CsvSource({"1, ALL", "2, role_id = 10", "3, ar_scope_user.dept_id = 200",
"4, ancestors", "5, ar_scope_user.user_id = 42"})
void appliesAllFiveRoleScopesAndClearsClientSuppliedSql(String scope, String expected) {
login(42L, role(10L, scope, "ar:bill:list"));
ArBillQuery query = new ArBillQuery();
query.getParams().put("dataScope", " OR 1=1 /* client supplied */");
service.selectArBillList(query);
String sql = scope(query);
if ("ALL".equals(expected)) {
assertEquals("", sql);
} else {
assertTrue(sql.contains(expected), sql);
}
assertFalse(sql.contains("client supplied"));
verify(billMapper).selectArBillList(same(query));
}
@Test
void mergesOnlyRolesWithBillListPermissionAndIgnoresDisabledRoles() {
SysRole disabled = role(13L, "1", "ar:bill:list");
disabled.setStatus("1");
login(42L, role(10L, "5", "ar:bill:list"), role(11L, "3", "ar:bill:list"),
role(12L, "1", "other:list"), disabled);
ArBillQuery query = new ArBillQuery();
service.selectArBillList(query);
assertTrue(scope(query).contains("ar_scope_user.user_id = 42"));
assertTrue(scope(query).contains("ar_scope_user.dept_id = 200"));
assertTrue(scope(query).contains(" OR "));
login(42L, role(10L, "5", "ar:bill:list"), role(11L, "1", "ar:bill:list"));
service.selectArBillList(query);
assertEquals("", scope(query));
}
@Test
void superAdminIsUnrestrictedButNoMatchingRoleGetsNoRows() {
ArBillQuery query = new ArBillQuery();
login(1L);
service.selectArBillList(query);
assertEquals("", scope(query));
login(42L, role(10L, "1", "other:list"));
service.selectArBillList(query);
assertTrue(scope(query).contains("ar_scope_user.dept_id = 0"));
}
@Test
void combinesCustomDepartmentRoles() {
login(42L, role(10L, "2", "ar:bill:list"), role(11L, "2", "ar:bill:list"));
ArBillQuery query = new ArBillQuery();
service.selectArBillList(query);
assertTrue(scope(query).contains("role_id in (10,11)"));
}
@Test
void rejectsBillIdsBeforeReadingDetailsOrRunningActions() {
forbidden(() -> service.selectArBillById(80L));
forbidden(() -> service.selectArBillDetails(80L));
forbidden(() -> service.selectArBillDetailFees(80L, 1L));
forbidden(() -> service.selectExports(80L));
forbidden(() -> service.confirmBill(80L, null, "tester", "confirm"));
forbidden(() -> service.voidBill(80L, null, "tester", "void"));
forbidden(() -> service.exportBill(80L, "tester", "export"));
verifyNoInteractions(detailMapper, feeMapper, exportMapper, logMapper);
verify(billMapper, never()).selectArBillByIdForUpdate(anyLong());
}
@Test
void checksExportAndGenerateTaskOwnershipThroughTheirBill() throws Exception {
ArBillExport export = new ArBillExport();
export.setArBillId(80L);
export.setExportStatus("SUCCESS");
export.setStorageKey("oss://test/bill.xlsx");
when(exportMapper.selectById(9L)).thenReturn(export);
MockHttpServletResponse response = new MockHttpServletResponse();
forbidden(() -> service.selectExport(9L));
forbidden(() -> service.downloadExport(9L, response));
assertEquals(0, response.getContentAsByteArray().length);
assertNull(response.getHeader("Content-Disposition"));
ArBillGenerateTask task = new ArBillGenerateTask();
task.setArBillId(80L);
when(taskMapper.selectById(3L)).thenReturn(task);
forbidden(() -> service.selectGenerateTask(3L));
forbidden(() -> service.cancelGenerateTask(3L, "tester", "cancel"));
forbidden(() -> service.retryGenerateTask(3L, "tester", "retry"));
verify(taskMapper, never()).markCancelRequested(anyLong());
}
@Test
void authorizedBillStillReturnsMaskedAccountAndDownloadsItsExport() throws Exception {
ArBill bill = new ArBill();
bill.setArBillId(80L);
bill.setBankAccountNo("123456780504");
when(billMapper.selectArBillByDataScope(any())).thenReturn(bill);
assertEquals("****0504", service.selectArBillById(80L).getBankAccountNo());
ArBillExport export = new ArBillExport();
export.setArBillId(80L);
export.setExportStatus("SUCCESS");
export.setStorageKey("oss://test/bill.xlsx");
export.setFileName("bill.xlsx");
when(exportMapper.selectById(9L)).thenReturn(export);
OssStorage storage = mock(OssStorage.class);
ReflectionTestUtils.setField(service, "ossStorage", storage);
doAnswer(invocation -> {
((OutputStream) invocation.getArgument(1)).write(new byte[] {1, 2, 3});
return null;
}).when(storage).download(eq("oss://test/bill.xlsx"), any(OutputStream.class));
MockHttpServletResponse response = new MockHttpServletResponse();
service.downloadExport(9L, response);
assertArrayEquals(new byte[] {1, 2, 3}, response.getContentAsByteArray());
assertTrue(response.getHeader("Content-Disposition").contains("bill.xlsx"));
}
@Test
void permissionLookupPreservesDetailPaginationAndClearsItOnDenial() {
Page<Object> page = PageHelper.startPage(3, 20);
page.setOrderBy("detail_no desc");
when(billMapper.selectArBillByDataScope(any())).thenAnswer(invocation -> {
assertNull(PageHelper.getLocalPage(), "权限校验不能消耗明细分页");
assertTrue(scope(invocation.getArgument(0)).contains("ar_scope_user.user_id = 42"));
return new ArBill();
});
when(detailMapper.selectByBillId(80L)).thenAnswer(invocation -> {
assertSame(page, PageHelper.getLocalPage());
assertEquals("detail_no desc", page.getOrderBy());
PageHelper.clearPage();
return Collections.emptyList();
});
service.selectArBillDetails(80L);
assertNull(PageHelper.getLocalPage());
doReturn(null).when(billMapper).selectArBillByDataScope(any());
PageHelper.startPage(3, 20);
forbidden(() -> service.selectArBillDetails(81L));
assertNull(PageHelper.getLocalPage(), "拒绝请求后不能向下一请求遗留分页");
}
@Test
void rejectsCustomerPreviewAndGenerationBeforeReadingSourceOrders() {
ArBillGenerateQuery preview = new ArBillGenerateQuery();
preview.setCustomerId(100L);
preview.setBillPeriodEnd(new Date());
forbidden(() -> service.previewBill(preview));
forbidden(() -> service.sumPreviewBill(preview));
ArBillGenerateRequest request = new ArBillGenerateRequest();
request.setCustomerId(100L);
request.setBillName("test");
request.setBillPeriodEnd(new Date());
forbidden(() -> service.generateBill(request, "tester", "generate"));
verifyNoInteractions(sourceMapper, taskMapper);
}
@Test
void authorizedCustomerPreviewRetainsItsOriginalPagination() {
Page<Object> page = PageHelper.startPage(2, 10);
when(billMapper.selectCustomerIdByDataScope(any())).thenAnswer(invocation -> {
assertNull(PageHelper.getLocalPage());
return 100L;
});
doAnswer(invocation -> {
assertSame(page, PageHelper.getLocalPage());
PageHelper.clearPage();
return Collections.emptyList();
}).when(sourceMapper).selectPrcOrderBillByCustomerPeriod(eq(100L), any(), anyList(), anyList(),
any(), any(), anyList(), any(), any());
ArBillGenerateQuery preview = new ArBillGenerateQuery();
preview.setCustomerId(100L);
preview.setBillPeriodEnd(new Date());
service.previewBill(preview);
assertNull(PageHelper.getLocalPage());
}
@Test
void exportIdempotencyCannotReturnAnExportFromAnotherBill() {
when(billMapper.selectArBillByDataScope(any())).thenReturn(new ArBill());
ArBillLog previous = new ArBillLog();
previous.setObjectId("9");
when(logMapper.selectSuccessByRequestId(anyString(), anyString(), anyString())).thenReturn(previous);
ArBillExport export = new ArBillExport();
export.setArBillId(81L);
when(exportMapper.selectById(9L)).thenReturn(export);
ServiceException denied = assertThrows(ServiceException.class,
() -> service.exportBill(80L, "tester", "reused"));
assertTrue(denied.getMessage().contains("其他账单"));
verify(exportMapper, never()).insert(any());
}
@Test
void logAndActiveTaskQueriesUseTheSameRoleScope() {
scopeService.selectLogs(new ArBillQuery(), new ArBillLog());
scopeService.selectActiveGenerateTasks(new ArBillQuery(), "tester");
verify(logMapper).selectList(any(), argThat(params ->
params.get("dataScope").toString().contains("ar_scope_user.user_id = 42")));
verify(taskMapper).selectActiveByCreateBy(eq("tester"), argThat(params ->
params.get("dataScope").toString().contains("ar_scope_user.user_id = 42")));
}
@Test
void allReadMappersRenderScopedSqlAndKeepNormalFilters() throws Exception {
Configuration configuration = new Configuration();
configuration.getTypeAliasRegistry().registerAlias("ArBill", ArBill.class);
configuration.getTypeAliasRegistry().registerAlias("ArBillLog", ArBillLog.class);
configuration.getTypeAliasRegistry().registerAlias("ArBillGenerateTask", ArBillGenerateTask.class);
for (String name : List.of("ArBillMapper", "ArBillLogMapper", "ArBillGenerateTaskMapper")) {
String resource = "mapper/ar/" + name + ".xml";
try (InputStream input = new ClassPathResource(resource).getInputStream()) {
new XMLMapperBuilder(input, configuration, resource, configuration.getSqlFragments()).parse();
}
}
ArBillQuery query = new ArBillQuery();
query.setArBillId(80L);
query.setCustomerId(100L);
query.setStatus("CONFIRMED");
service.selectArBillList(query);
String listSql = sql(configuration, "ArBillMapper.selectArBillList", query);
assertTrue(listSql.contains("status = ?"));
assertTrue(listSql.contains("customer_id = ?"));
assertTrue(listSql.contains("ar_scope_customer.customer_id = ar_bill.customer_id"));
assertTrue(listSql.contains("ar_scope_id_user.user_id is null"), "历史工号仅在ID不存在时兜底");
assertTrue(listSql.contains("exists ("), "EXISTS 避免业务员关联放大列表和分页总数");
assertFalse(listSql.contains("${"));
for (String statement : List.of("ArBillMapper.selectArBillByDataScope", "ArBillMapper.selectCustomerIdByDataScope")) {
assertTrue(sql(configuration, statement, query).contains("ar_scope_user.user_id = 42"));
}
Map<String, Object> logs = Map.of("query", new ArBillLog(), "params", query.getParams());
assertTrue(sql(configuration, "ArBillLogMapper.selectList", logs).contains("ar_scope_customer.customer_id = b.customer_id"));
Map<String, Object> tasks = Map.of("createBy", "tester", "params", query.getParams());
assertTrue(sql(configuration, "ArBillGenerateTaskMapper.selectActiveByCreateBy", tasks)
.contains("b.ar_bill_id = ar_bill_generate_task.ar_bill_id"));
login(42L, role(10L, "1", "ar:bill:list"));
service.selectArBillList(query);
assertFalse(sql(configuration, "ArBillMapper.selectArBillList", query).contains("ar_scope_customer"));
}
private String sql(Configuration configuration, String statement, Object parameters) {
return configuration.getMappedStatement("com.bms.ar.mapper." + statement).getBoundSql(parameters)
.getSql().replaceAll("\\s+", " ");
}
private String scope(ArBillQuery query) {
return String.valueOf(query.getParams().get("dataScope"));
}
private void forbidden(Executable action) {
assertEquals(403, assertThrows(ServiceException.class, action).getCode());
}
private SysRole role(Long id, String scope, String permission) {
SysRole role = new SysRole(id);
role.setDataScope(scope);
role.setStatus("0");
role.setPermissions(Set.of(permission));
return role;
}
private void login(Long userId, SysRole... roles) {
SysUser user = new SysUser(userId);
user.setDeptId(200L);
user.setUserName("tester");
user.setRoles(List.of(roles));
LoginUser login = new LoginUser(userId, 200L, user, Set.of("ar:bill:list"));
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(login, null, Collections.emptyList()));
}
@SuppressWarnings("unchecked")
private <T> T proxy(T target) {
AspectJProxyFactory factory = new AspectJProxyFactory(target);
factory.setProxyTargetClass(true);
factory.addAspect(new DataScopeAspect());
return (T) factory.getProxy();
}
}
@@ -0,0 +1,68 @@
package com.bms.system;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import com.bms.common.constant.CacheConstants;
import com.bms.common.core.domain.entity.SysRole;
import com.bms.common.core.domain.entity.SysUser;
import com.bms.common.core.domain.model.LoginUser;
import com.bms.common.core.redis.RedisCache;
import com.bms.framework.web.service.SysPermissionService;
import com.bms.framework.web.service.TokenService;
import com.bms.system.service.ISysRoleService;
import com.bms.web.controller.system.SysRoleController;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class SysRoleDataScopeRefreshTest {
@Test
void savingDataScopeRefreshesOnlineUsersWithPersistedScope() {
ISysRoleService roles = mock(ISysRoleService.class);
TokenService tokens = mock(TokenService.class);
SysPermissionService permissions = mock(SysPermissionService.class);
SysRoleController controller = new SysRoleController();
ReflectionTestUtils.setField(controller, "roleService", roles);
ReflectionTestUtils.setField(controller, "tokenService", tokens);
ReflectionTestUtils.setField(controller, "permissionService", permissions);
SysRole submitted = new SysRole(10L);
submitted.setDataScope("5");
SysRole persisted = new SysRole(10L);
persisted.setDataScope("5");
when(roles.authDataScope(submitted)).thenReturn(1);
when(roles.selectRoleById(10L)).thenReturn(persisted);
controller.dataScope(submitted);
verify(tokens).refreshPermissionByRoleId(10L, permissions, "5");
}
@Test
void replacesCachedDataScopeBeforeSavingSessionAndPreservesOtherRoles() {
RedisCache redis = mock(RedisCache.class);
SysPermissionService permissions = mock(SysPermissionService.class);
TokenService tokens = spy(new TokenService());
ReflectionTestUtils.setField(tokens, "redisCache", redis);
doNothing().when(tokens).refreshToken(any());
SysRole target = new SysRole(10L);
target.setDataScope("1");
SysRole other = new SysRole(11L);
other.setDataScope("3");
SysUser user = new SysUser(42L);
user.setRoles(List.of(target, other));
LoginUser login = new LoginUser(42L, 200L, user, Set.of("ar:bill:list"));
String key = CacheConstants.LOGIN_TOKEN_KEY + "test";
when(redis.keys(CacheConstants.LOGIN_TOKEN_KEY + "*")).thenReturn(List.of(key));
when(redis.getCacheObject(key)).thenReturn(login);
when(permissions.getMenuPermission(user)).thenAnswer(invocation -> {
assertEquals("5", target.getDataScope());
return Set.of("ar:bill:list");
});
tokens.refreshPermissionByRoleId(10L, permissions, "5");
assertEquals("5", login.getUser().getRoles().get(0).getDataScope());
assertEquals("3", other.getDataScope());
verify(tokens).refreshToken(login);
}
}
@@ -238,6 +238,12 @@ public class TokenService
* @param permissionService 权限服务 * @param permissionService 权限服务
*/ */
public void refreshPermissionByRoleId(Long roleId, SysPermissionService permissionService) public void refreshPermissionByRoleId(Long roleId, SysPermissionService permissionService)
{
refreshPermissionByRoleId(roleId, permissionService, null);
}
/** 数据范围变更时同步更新在线登录信息中的角色快照。 */
public void refreshPermissionByRoleId(Long roleId, SysPermissionService permissionService, String dataScope)
{ {
// 扫描所有在线 token // 扫描所有在线 token
String pattern = CacheConstants.LOGIN_TOKEN_KEY + "*"; String pattern = CacheConstants.LOGIN_TOKEN_KEY + "*";
@@ -261,6 +267,12 @@ public class TokenService
{ {
continue; continue;
} }
if (dataScope != null)
{
loginUser.getUser().getRoles().stream()
.filter(r -> roleId.equals(r.getRoleId()))
.forEach(r -> r.setDataScope(dataScope));
}
// 刷新权限缓存 // 刷新权限缓存
loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser())); loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
refreshToken(loginUser); refreshToken(loginUser);
@@ -2,6 +2,7 @@ package com.bms.ar.mapper;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import com.bms.ar.domain.ArBillGenerateTask; import com.bms.ar.domain.ArBillGenerateTask;
@@ -14,7 +15,8 @@ public interface ArBillGenerateTaskMapper {
ArBillGenerateTask selectByRequestToken(String requestToken); ArBillGenerateTask selectByRequestToken(String requestToken);
List<ArBillGenerateTask> selectActiveByCreateBy(@Param("createBy") String createBy); List<ArBillGenerateTask> selectActiveByCreateBy(@Param("createBy") String createBy,
@Param("params") Map<String, Object> params);
List<ArBillGenerateTask> selectWaiting(@Param("limit") int limit); List<ArBillGenerateTask> selectWaiting(@Param("limit") int limit);
@@ -1,12 +1,13 @@
package com.bms.ar.mapper; package com.bms.ar.mapper;
import java.util.List; import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import com.bms.ar.domain.ArBillLog; import com.bms.ar.domain.ArBillLog;
public interface ArBillLogMapper public interface ArBillLogMapper
{ {
List<ArBillLog> selectList(ArBillLog query); List<ArBillLog> selectList(@Param("query") ArBillLog query, @Param("params") Map<String, Object> params);
ArBillLog selectSuccessByRequestId(@Param("requestId") String requestId, ArBillLog selectSuccessByRequestId(@Param("requestId") String requestId,
@Param("objectType") String objectType, @Param("objectType") String objectType,
@@ -11,6 +11,10 @@ import com.bms.ar.domain.vo.ArBillQuery;
public interface ArBillMapper { public interface ArBillMapper {
List<ArBill> selectArBillList(ArBillQuery query); List<ArBill> selectArBillList(ArBillQuery query);
ArBill selectArBillByDataScope(ArBillQuery query);
Long selectCustomerIdByDataScope(ArBillQuery query);
ArBill selectArBillById(Long arBillId); ArBill selectArBillById(Long arBillId);
ArBill selectArBillByBillNo(String billNo); ArBill selectArBillByBillNo(String billNo);
@@ -0,0 +1,82 @@
package com.bms.ar.service;
import java.util.List;
import java.util.function.Supplier;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bms.ar.domain.ArBill;
import com.bms.ar.domain.ArBillGenerateTask;
import com.bms.ar.domain.ArBillLog;
import com.bms.ar.domain.vo.ArBillQuery;
import com.bms.ar.mapper.ArBillGenerateTaskMapper;
import com.bms.ar.mapper.ArBillLogMapper;
import com.bms.ar.mapper.ArBillMapper;
import com.bms.common.annotation.DataScope;
import com.bms.common.constant.HttpStatus;
import com.bms.common.exception.ServiceException;
/**
* 按客户归属业务员和其部门校验账单可见范围。
* 独立 Bean 保证内部业务调用也经过 DataScope 切面;范围始终取具有账单列表权限的角色。
*/
@Service
public class ArBillDataScopeService {
@Autowired
private ArBillMapper billMapper;
@Autowired
private ArBillLogMapper logMapper;
@Autowired
private ArBillGenerateTaskMapper generateTaskMapper;
@DataScope(deptAlias = "ar_scope_user", userAlias = "ar_scope_user", permission = "ar:bill:list")
public ArBill requireBill(ArBillQuery query) {
return withoutPagination(() -> {
ArBill bill = query.getArBillId() == null ? null : billMapper.selectArBillByDataScope(query);
if (bill == null) {
throw new ServiceException("应收账单不存在或无权访问", HttpStatus.FORBIDDEN);
}
return bill;
});
}
@DataScope(deptAlias = "ar_scope_user", userAlias = "ar_scope_user", permission = "ar:bill:list")
public void requireCustomer(ArBillQuery query) {
withoutPagination(() -> {
Long customerId = query.getCustomerId() == null || query.getCustomerId() <= 0
? null : billMapper.selectCustomerIdByDataScope(query);
if (customerId == null || customerId <= 0) {
throw new ServiceException("客户不存在或无权访问该客户的应收账单", HttpStatus.FORBIDDEN);
}
return customerId;
});
}
@DataScope(deptAlias = "ar_scope_user", userAlias = "ar_scope_user", permission = "ar:bill:list")
public List<ArBillLog> selectLogs(ArBillQuery scope, ArBillLog query) {
return logMapper.selectList(query, scope.getParams());
}
@DataScope(deptAlias = "ar_scope_user", userAlias = "ar_scope_user", permission = "ar:bill:list")
public List<ArBillGenerateTask> selectActiveGenerateTasks(ArBillQuery scope, String operator) {
return generateTaskMapper.selectActiveByCreateBy(operator, scope.getParams());
}
/** 权限校验 SQL 不消耗调用方为明细/订单预览设置的分页,拒绝访问时清理分页。 */
private <T> T withoutPagination(Supplier<T> lookup) {
Page<?> pendingPage = PageHelper.getLocalPage();
PageHelper.clearPage();
try {
T result = lookup.get();
if (pendingPage != null) {
PageHelper.setLocalPage(pendingPage);
}
return result;
} catch (RuntimeException | Error e) {
PageHelper.clearPage();
throw e;
}
}
}
@@ -13,16 +13,20 @@ import com.bms.ar.enums.ArLogOperationType;
import com.bms.ar.enums.ArLogResult; import com.bms.ar.enums.ArLogResult;
import com.bms.ar.mapper.ArBillLogMapper; import com.bms.ar.mapper.ArBillLogMapper;
import com.bms.ar.service.IArBillLogService; import com.bms.ar.service.IArBillLogService;
import com.bms.ar.service.ArBillDataScopeService;
import com.bms.ar.domain.vo.ArBillQuery;
import com.bms.common.utils.uuid.IdUtils; import com.bms.common.utils.uuid.IdUtils;
@Service @Service
public class ArBillLogServiceImpl implements IArBillLogService { public class ArBillLogServiceImpl implements IArBillLogService {
@Autowired @Autowired
private ArBillLogMapper logMapper; private ArBillLogMapper logMapper;
@Autowired
private ArBillDataScopeService dataScopeService;
@Override @Override
public List<ArBillLog> selectLogList(ArBillLog query) { public List<ArBillLog> selectLogList(ArBillLog query) {
return logMapper.selectList(query); return dataScopeService.selectLogs(new ArBillQuery(), query);
} }
@Override @Override
@@ -75,11 +75,13 @@ import com.bms.ar.mapper.ArBankAccountMapper;
import com.bms.ar.mapper.ArExportTemplateMapper; import com.bms.ar.mapper.ArExportTemplateMapper;
import com.bms.ar.service.IArBillLogService; import com.bms.ar.service.IArBillLogService;
import com.bms.ar.service.IArBillService; import com.bms.ar.service.IArBillService;
import com.bms.ar.service.ArBillDataScopeService;
import com.bms.ar.domain.vo.ArBillActionRequest; import com.bms.ar.domain.vo.ArBillActionRequest;
import com.bms.ar.domain.vo.ArBillGenerateQuery; import com.bms.ar.domain.vo.ArBillGenerateQuery;
import com.bms.ar.domain.vo.ArBillGenerateRequest; import com.bms.ar.domain.vo.ArBillGenerateRequest;
import com.bms.ar.domain.vo.ArBillQuery; import com.bms.ar.domain.vo.ArBillQuery;
import com.bms.common.config.BmsConfig; import com.bms.common.config.BmsConfig;
import com.bms.common.annotation.DataScope;
import com.bms.common.exception.ServiceException; import com.bms.common.exception.ServiceException;
import com.bms.common.utils.DateUtils; import com.bms.common.utils.DateUtils;
import com.bms.common.utils.SecurityUtils; import com.bms.common.utils.SecurityUtils;
@@ -197,6 +199,8 @@ public class ArBillServiceImpl implements IArBillService {
@Autowired @Autowired
private IArBillLogService logService; private IArBillLogService logService;
@Autowired @Autowired
private ArBillDataScopeService dataScopeService;
@Autowired
private ApplicationEventPublisher eventPublisher; private ApplicationEventPublisher eventPublisher;
@Autowired @Autowired
private PrcOrderBillMapper sourceBillMapper; private PrcOrderBillMapper sourceBillMapper;
@@ -211,6 +215,7 @@ public class ArBillServiceImpl implements IArBillService {
* 查询账单列表,并对返回给前端的收款账号做脱敏。 * 查询账单列表,并对返回给前端的收款账号做脱敏。
*/ */
@Override @Override
@DataScope(deptAlias = "ar_scope_user", userAlias = "ar_scope_user", permission = "ar:bill:list")
public List<ArBill> selectArBillList(ArBillQuery query) { public List<ArBill> selectArBillList(ArBillQuery query) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
List<ArBill> bills = billMapper.selectArBillList(query); List<ArBill> bills = billMapper.selectArBillList(query);
@@ -229,10 +234,7 @@ public class ArBillServiceImpl implements IArBillService {
@Override @Override
public ArBill selectArBillById(Long arBillId) { public ArBill selectArBillById(Long arBillId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
ArBill bill = billMapper.selectArBillById(arBillId); ArBill bill = requireBillDataScope(arBillId);
if (bill == null) {
throw new ServiceException("应收账单不存在");
}
bill.setBankAccountNo(maskAccount(bill.getBankAccountNo())); bill.setBankAccountNo(maskAccount(bill.getBankAccountNo()));
bill.setExports(exportMapper.selectByBillId(arBillId)); bill.setExports(exportMapper.selectByBillId(arBillId));
log.debug("应收账单详情查询完成,arBillId={}, status={}, exportCount={}, costMs={}", log.debug("应收账单详情查询完成,arBillId={}, status={}, exportCount={}, costMs={}",
@@ -247,6 +249,7 @@ public class ArBillServiceImpl implements IArBillService {
@Override @Override
public List<ArBillDetail> selectArBillDetails(Long arBillId) { public List<ArBillDetail> selectArBillDetails(Long arBillId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
requireBillDataScope(arBillId);
List<ArBillDetail> details = detailMapper.selectByBillId(arBillId); List<ArBillDetail> details = detailMapper.selectByBillId(arBillId);
log.debug("应收账单明细查询完成,arBillId={}, resultCount={}, costMs={}", log.debug("应收账单明细查询完成,arBillId={}, resultCount={}, costMs={}",
arBillId, details.size(), elapsedMillis(startedAt)); arBillId, details.size(), elapsedMillis(startedAt));
@@ -259,6 +262,7 @@ public class ArBillServiceImpl implements IArBillService {
@Override @Override
public List<ArBillDetailFee> selectArBillDetailFees(Long arBillId, Long detailId) { public List<ArBillDetailFee> selectArBillDetailFees(Long arBillId, Long detailId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
requireBillDataScope(arBillId);
List<ArBillDetailFee> fees = detailFeeMapper.selectByBillIdAndDetailId(arBillId, detailId); List<ArBillDetailFee> fees = detailFeeMapper.selectByBillIdAndDetailId(arBillId, detailId);
log.debug("应收账单费用明细查询完成,arBillId={}, detailId={}, resultCount={}, costMs={}", log.debug("应收账单费用明细查询完成,arBillId={}, detailId={}, resultCount={}, costMs={}",
arBillId, detailId, fees.size(), elapsedMillis(startedAt)); arBillId, detailId, fees.size(), elapsedMillis(startedAt));
@@ -299,6 +303,7 @@ public class ArBillServiceImpl implements IArBillService {
public List<PrcOrderBill> previewBill(ArBillGenerateQuery query) { public List<PrcOrderBill> previewBill(ArBillGenerateQuery query) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
validatePreviewQuery(query); validatePreviewQuery(query);
requireCustomerDataScope(query.getCustomerId());
List<String> orderIdentifiers = normalizeOrderIdentifiers(query.getOrderOrTrackingNo()); List<String> orderIdentifiers = normalizeOrderIdentifiers(query.getOrderOrTrackingNo());
List<String> excludeOrderIdentifiers = normalizeOrderIdentifiers(query.getExcludeOrderOrTrackingNo()); List<String> excludeOrderIdentifiers = normalizeOrderIdentifiers(query.getExcludeOrderOrTrackingNo());
List<PrcOrderBill> result = sourceBillMapper.selectPrcOrderBillByCustomerPeriod(query.getCustomerId(), List<PrcOrderBill> result = sourceBillMapper.selectPrcOrderBillByCustomerPeriod(query.getCustomerId(),
@@ -327,6 +332,7 @@ public class ArBillServiceImpl implements IArBillService {
public BigDecimal sumPreviewBill(ArBillGenerateQuery query) { public BigDecimal sumPreviewBill(ArBillGenerateQuery query) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
validatePreviewQuery(query); validatePreviewQuery(query);
requireCustomerDataScope(query.getCustomerId());
List<String> orderIdentifiers = normalizeOrderIdentifiers(query.getOrderOrTrackingNo()); List<String> orderIdentifiers = normalizeOrderIdentifiers(query.getOrderOrTrackingNo());
List<String> excludeOrderIdentifiers = normalizeOrderIdentifiers(query.getExcludeOrderOrTrackingNo()); List<String> excludeOrderIdentifiers = normalizeOrderIdentifiers(query.getExcludeOrderOrTrackingNo());
BigDecimal total = nvl(sourceBillMapper.sumPrcOrderBillIncomeByCustomerPeriod(query.getCustomerId(), BigDecimal total = nvl(sourceBillMapper.sumPrcOrderBillIncomeByCustomerPeriod(query.getCustomerId(),
@@ -350,8 +356,13 @@ public class ArBillServiceImpl implements IArBillService {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
Long createUserId = SecurityUtils.getUserId(); Long createUserId = SecurityUtils.getUserId();
validatePeriod(request); validatePeriod(request);
requireCustomerDataScope(request.getCustomerId());
ArBillGenerateTask existed = generateTaskMapper.selectByRequestToken(requestId); ArBillGenerateTask existed = generateTaskMapper.selectByRequestToken(requestId);
if (existed != null) { if (existed != null) {
ArBill existedBill = requireBillDataScope(existed.getArBillId());
if (!request.getCustomerId().equals(existedBill.getCustomerId())) {
throw new ServiceException("请求标识已用于其他客户,请重新发起");
}
log.info("应收账单生成请求命中幂等记录,operator={}, requestId={}, taskId={}, status={}, costMs={}", log.info("应收账单生成请求命中幂等记录,operator={}, requestId={}, taskId={}, status={}, costMs={}",
safeLogValue(operator), safeLogValue(requestId), existed.getTaskId(), existed.getStatus(), safeLogValue(operator), safeLogValue(requestId), existed.getTaskId(), existed.getStatus(),
elapsedMillis(startedAt)); elapsedMillis(startedAt));
@@ -471,6 +482,7 @@ public class ArBillServiceImpl implements IArBillService {
if (task == null) { if (task == null) {
throw new ServiceException("账单生成任务不存在"); throw new ServiceException("账单生成任务不存在");
} }
requireBillDataScope(task.getArBillId());
log.debug("应收账单生成任务状态查询,taskId={}, status={}, processedCount={}, totalCount={}, costMs={}", log.debug("应收账单生成任务状态查询,taskId={}, status={}, processedCount={}, totalCount={}, costMs={}",
taskId, task.getStatus(), task.getProcessedCount(), task.getTotalCount(), elapsedMillis(startedAt)); taskId, task.getStatus(), task.getProcessedCount(), task.getTotalCount(), elapsedMillis(startedAt));
return task; return task;
@@ -482,7 +494,7 @@ public class ArBillServiceImpl implements IArBillService {
@Override @Override
public List<ArBillGenerateTask> selectActiveGenerateTasks(String operator) { public List<ArBillGenerateTask> selectActiveGenerateTasks(String operator) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
List<ArBillGenerateTask> tasks = generateTaskMapper.selectActiveByCreateBy(operator); List<ArBillGenerateTask> tasks = dataScopeService.selectActiveGenerateTasks(new ArBillQuery(), operator);
log.debug("当前用户应收账单生成任务查询,operator={}, taskCount={}, costMs={}", log.debug("当前用户应收账单生成任务查询,operator={}, taskCount={}, costMs={}",
safeLogValue(operator), tasks.size(), elapsedMillis(startedAt)); safeLogValue(operator), tasks.size(), elapsedMillis(startedAt));
return tasks; return tasks;
@@ -913,6 +925,7 @@ public class ArBillServiceImpl implements IArBillService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public ArBill confirmBill(Long arBillId, ArBillActionRequest request, String operator, String requestId) { public ArBill confirmBill(Long arBillId, ArBillActionRequest request, String operator, String requestId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
requireBillDataScope(arBillId);
ArBill bill = billMapper.selectArBillByIdForUpdate(arBillId); ArBill bill = billMapper.selectArBillByIdForUpdate(arBillId);
if (bill == null) { if (bill == null) {
throw failure(arBillId, ArLogObjectType.BILL, String.valueOf(arBillId), ArLogOperationType.CONFIRM, throw failure(arBillId, ArLogObjectType.BILL, String.valueOf(arBillId), ArLogOperationType.CONFIRM,
@@ -957,6 +970,7 @@ public class ArBillServiceImpl implements IArBillService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public ArBill voidBill(Long arBillId, ArBillActionRequest request, String operator, String requestId) { public ArBill voidBill(Long arBillId, ArBillActionRequest request, String operator, String requestId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
requireBillDataScope(arBillId);
String remark = request == null ? null : request.getRemark(); String remark = request == null ? null : request.getRemark();
if (StringUtils.isEmpty(remark)) { if (StringUtils.isEmpty(remark)) {
throw failure(arBillId, ArLogObjectType.BILL, String.valueOf(arBillId), ArLogOperationType.VOID, throw failure(arBillId, ArLogObjectType.BILL, String.valueOf(arBillId), ArLogOperationType.VOID,
@@ -1001,23 +1015,22 @@ public class ArBillServiceImpl implements IArBillService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public ArBillExport exportBill(Long arBillId, String operator, String requestId) { public ArBillExport exportBill(Long arBillId, String operator, String requestId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
ArBill bill = requireBillDataScope(arBillId);
Long exportUserId = SecurityUtils.getUserId(); Long exportUserId = SecurityUtils.getUserId();
ArBillLog previous = logMapper.selectSuccessByRequestId(requestId, ArLogObjectType.EXPORT.getCode(), ArBillLog previous = logMapper.selectSuccessByRequestId(requestId, ArLogObjectType.EXPORT.getCode(),
ArLogResult.SUCCESS.getCode()); ArLogResult.SUCCESS.getCode());
if (previous != null && isNumber(previous.getObjectId())) { if (previous != null && isNumber(previous.getObjectId())) {
ArBillExport previousExport = exportMapper.selectById(Long.valueOf(previous.getObjectId())); ArBillExport previousExport = exportMapper.selectById(Long.valueOf(previous.getObjectId()));
if (previousExport != null) { if (previousExport != null) {
if (!arBillId.equals(previousExport.getArBillId())) {
throw new ServiceException("请求标识已用于其他账单,请重新发起");
}
log.info("应收账单导出请求命中幂等记录,operator={}, requestId={}, arBillId={}, exportId={}, status={}, costMs={}", log.info("应收账单导出请求命中幂等记录,operator={}, requestId={}, arBillId={}, exportId={}, status={}, costMs={}",
safeLogValue(operator), safeLogValue(requestId), arBillId, previousExport.getExportId(), safeLogValue(operator), safeLogValue(requestId), arBillId, previousExport.getExportId(),
previousExport.getExportStatus(), elapsedMillis(startedAt)); previousExport.getExportStatus(), elapsedMillis(startedAt));
return previousExport; return previousExport;
} }
} }
ArBill bill = billMapper.selectArBillById(arBillId);
if (bill == null) {
throw failure(arBillId, ArLogObjectType.EXPORT, String.valueOf(arBillId), ArLogOperationType.EXPORT,
null, "应收账单不存在", operator, requestId);
}
if (!ArBillGenerationStatus.READY.equals(bill.getGenerationStatus())) { if (!ArBillGenerationStatus.READY.equals(bill.getGenerationStatus())) {
throw failure(arBillId, ArLogObjectType.EXPORT, String.valueOf(arBillId), ArLogOperationType.EXPORT, throw failure(arBillId, ArLogObjectType.EXPORT, String.valueOf(arBillId), ArLogOperationType.EXPORT,
bill.getStatus(), "账单尚未生成完成,暂不能导出", operator, requestId); bill.getStatus(), "账单尚未生成完成,暂不能导出", operator, requestId);
@@ -1078,6 +1091,7 @@ public class ArBillServiceImpl implements IArBillService {
if (export == null) { if (export == null) {
throw new ServiceException("导出任务不存在"); throw new ServiceException("导出任务不存在");
} }
requireBillDataScope(export.getArBillId());
log.debug("应收账单导出任务状态查询,exportId={}, status={}, costMs={}", log.debug("应收账单导出任务状态查询,exportId={}, status={}, costMs={}",
exportId, export.getExportStatus(), elapsedMillis(startedAt)); exportId, export.getExportStatus(), elapsedMillis(startedAt));
return export; return export;
@@ -1158,6 +1172,7 @@ public class ArBillServiceImpl implements IArBillService {
@Override @Override
public List<ArBillExport> selectExports(Long arBillId) { public List<ArBillExport> selectExports(Long arBillId) {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
requireBillDataScope(arBillId);
List<ArBillExport> exports = exportMapper.selectByBillId(arBillId); List<ArBillExport> exports = exportMapper.selectByBillId(arBillId);
log.debug("应收账单导出记录查询,arBillId={}, exportCount={}, costMs={}", log.debug("应收账单导出记录查询,arBillId={}, exportCount={}, costMs={}",
arBillId, exports.size(), elapsedMillis(startedAt)); arBillId, exports.size(), elapsedMillis(startedAt));
@@ -1167,8 +1182,8 @@ public class ArBillServiceImpl implements IArBillService {
@Override @Override
public void downloadExport(Long exportId, HttpServletResponse response) throws Exception { public void downloadExport(Long exportId, HttpServletResponse response) throws Exception {
long startedAt = System.nanoTime(); long startedAt = System.nanoTime();
ArBillExport export = exportMapper.selectById(exportId); ArBillExport export = selectExport(exportId);
if (export == null || !ArExportStatus.SUCCESS.getCode().equals(export.getExportStatus()) || StringUtils.isEmpty(export.getStorageKey())) { if (!ArExportStatus.SUCCESS.getCode().equals(export.getExportStatus()) || StringUtils.isEmpty(export.getStorageKey())) {
throw new ServiceException("导出文件不存在或尚未生成成功"); throw new ServiceException("导出文件不存在或尚未生成成功");
} }
String resource = export.getStorageKey(); String resource = export.getStorageKey();
@@ -1197,6 +1212,18 @@ public class ArBillServiceImpl implements IArBillService {
return logs; return logs;
} }
private ArBill requireBillDataScope(Long arBillId) {
ArBillQuery query = new ArBillQuery();
query.setArBillId(arBillId);
return dataScopeService.requireBill(query);
}
private void requireCustomerDataScope(Long customerId) {
ArBillQuery query = new ArBillQuery();
query.setCustomerId(customerId);
dataScopeService.requireCustomer(query);
}
private String exportFailureMessage(Exception e) { private String exportFailureMessage(Exception e) {
String message = StringUtils.isEmpty(e.getMessage()) ? "账单导出失败" : e.getMessage(); String message = StringUtils.isEmpty(e.getMessage()) ? "账单导出失败" : e.getMessage();
return message.length() > 500 ? message.substring(0, 500) : message; return message.length() > 500 ? message.substring(0, 500) : message;
@@ -31,6 +31,8 @@ public class BatchAddIncomeBillFeeRequest implements Serializable
private List<String> productCodes; private List<String> productCodes;
private String countryCode; private String countryCode;
private String creditArrearsStatus; private String creditArrearsStatus;
/** Same as list filter order_status IN (...). */
private List<Integer> orderStatuses;
/** Same as list filter billing_fail_status IN (...). */ /** Same as list filter billing_fail_status IN (...). */
private List<Integer> billingFailStatuses; private List<Integer> billingFailStatuses;
@@ -21,6 +21,8 @@ public class ChargeWeightTemplateRequest implements Serializable
private List<String> customerCodes; private List<String> customerCodes;
private List<String> productCodes; private List<String> productCodes;
private String creditArrearsStatus; private String creditArrearsStatus;
/** Same as list filter order_status IN (...). */
private List<Integer> orderStatuses;
private String beginBizDate; private String beginBizDate;
private String endBizDate; private String endBizDate;
} }
@@ -25,6 +25,8 @@ public class RecalcBillPriceRequest implements Serializable
private List<String> productCodes; private List<String> productCodes;
private String countryCode; private String countryCode;
private String creditArrearsStatus; private String creditArrearsStatus;
/** Same as list filter order_status IN (...). */
private List<Integer> orderStatuses;
/** Same as list filter billing_fail_status IN (...). */ /** Same as list filter billing_fail_status IN (...). */
private List<Integer> billingFailStatuses; private List<Integer> billingFailStatuses;
private String beginBizDate; private String beginBizDate;
@@ -675,7 +675,7 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
List<PrcOrderBill> bills = resolveListScopedBills(request.getScopeMode(), request.getBillIds(), List<PrcOrderBill> bills = resolveListScopedBills(request.getScopeMode(), request.getBillIds(),
request.getOrderNos(), request.getTrackingNos(), request.getTransferNos(), request.getOrderIdentifiers(), request.getOrderNos(), request.getTrackingNos(), request.getTransferNos(), request.getOrderIdentifiers(),
request.getCustomerCodes(), request.getProductCodes(), request.getCountryCode(), request.getCustomerCodes(), request.getProductCodes(), request.getCountryCode(),
request.getCreditArrearsStatus(), request.getBillingFailStatuses(), request.getCreditArrearsStatus(), request.getOrderStatuses(), request.getBillingFailStatuses(),
request.getBeginBizDate(), request.getEndBizDate(), request.getBeginBizDate(), request.getEndBizDate(),
"price.bill.recalc.filterRequired", "price.bill.recalc.scopeRequired", "price.bill.recalc.filterRequired", "price.bill.recalc.scopeRequired",
"price.bill.recalc.selectedRequired"); "price.bill.recalc.selectedRequired");
@@ -1251,7 +1251,7 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
return resolveListScopedBills(request.getScopeMode(), request.getBillIds(), return resolveListScopedBills(request.getScopeMode(), request.getBillIds(),
request.getOrderNos(), request.getTrackingNos(), request.getTransferNos(), request.getOrderIdentifiers(), request.getOrderNos(), request.getTrackingNos(), request.getTransferNos(), request.getOrderIdentifiers(),
request.getCustomerCodes(), request.getProductCodes(), request.getCountryCode(), request.getCustomerCodes(), request.getProductCodes(), request.getCountryCode(),
request.getCreditArrearsStatus(), request.getBillingFailStatuses(), request.getCreditArrearsStatus(), request.getOrderStatuses(), request.getBillingFailStatuses(),
request.getBeginBizDate(), request.getEndBizDate(), request.getBeginBizDate(), request.getEndBizDate(),
"price.bill.fee.batch.filterRequired", "price.bill.fee.batch.scopeRequired", "price.bill.fee.batch.filterRequired", "price.bill.fee.batch.scopeRequired",
"price.bill.fee.batch.selectedRequired"); "price.bill.fee.batch.selectedRequired");
@@ -1260,11 +1260,12 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
/** /**
* Shared list-scope resolve for batch fee / recalc price. * Shared list-scope resolve for batch fee / recalc price.
* scopeMode: SELECTED uses billIds; ALL uses filters (at least one required). * scopeMode: SELECTED uses billIds; ALL uses filters (at least one required).
* orderStatuses alone does not count as a meaningful filter (same as export).
*/ */
private List<PrcOrderBill> resolveListScopedBills(String scopeModeRaw, List<String> billIdsRaw, private List<PrcOrderBill> resolveListScopedBills(String scopeModeRaw, List<String> billIdsRaw,
List<String> orderNosRaw, List<String> trackingNosRaw, List<String> transferNosRaw, List<String> orderIdentifiersRaw, List<String> orderNosRaw, List<String> trackingNosRaw, List<String> transferNosRaw, List<String> orderIdentifiersRaw,
List<String> customerCodesRaw, List<String> productCodesRaw, String countryCodeRaw, List<String> customerCodesRaw, List<String> productCodesRaw, String countryCodeRaw,
String creditArrearsStatusRaw, List<Integer> billingFailStatusesRaw, String creditArrearsStatusRaw, List<Integer> orderStatusesRaw, List<Integer> billingFailStatusesRaw,
String beginBizDateRaw, String endBizDateRaw, String beginBizDateRaw, String endBizDateRaw,
String msgFilter, String msgScope, String msgSelected) String msgFilter, String msgScope, String msgSelected)
{ {
@@ -1279,6 +1280,7 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
List<String> orderIdentifiers = normalizeOrderIdentifiers(orderIdentifiersRaw); List<String> orderIdentifiers = normalizeOrderIdentifiers(orderIdentifiersRaw);
List<String> customerCodes = normalizeCodeList(customerCodesRaw); List<String> customerCodes = normalizeCodeList(customerCodesRaw);
List<String> productCodes = normalizeCodeList(productCodesRaw); List<String> productCodes = normalizeCodeList(productCodesRaw);
List<Integer> orderStatuses = normalizeIntegerList(orderStatusesRaw);
List<Integer> billingFailStatuses = normalizeIntegerList(billingFailStatusesRaw); List<Integer> billingFailStatuses = normalizeIntegerList(billingFailStatusesRaw);
String creditArrearsStatus = StringUtils.isEmpty(creditArrearsStatusRaw) ? null String creditArrearsStatus = StringUtils.isEmpty(creditArrearsStatusRaw) ? null
: creditArrearsStatusRaw.trim(); : creditArrearsStatusRaw.trim();
@@ -1320,6 +1322,7 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
query.setCustomerCodes(customerCodes); query.setCustomerCodes(customerCodes);
query.setProductCodes(productCodes); query.setProductCodes(productCodes);
query.setCreditArrearsStatus(creditArrearsStatus); query.setCreditArrearsStatus(creditArrearsStatus);
query.setOrderStatuses(orderStatuses);
query.setBillingFailStatuses(billingFailStatuses); query.setBillingFailStatuses(billingFailStatuses);
query.setCountryCode(countryCode); query.setCountryCode(countryCode);
if (beginBizDate != null) if (beginBizDate != null)
@@ -2713,6 +2716,7 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
List<String> transferNos = normalizeCodeList(request.getTransferNos()); List<String> transferNos = normalizeCodeList(request.getTransferNos());
List<String> customerCodes = normalizeCodeList(request.getCustomerCodes()); List<String> customerCodes = normalizeCodeList(request.getCustomerCodes());
List<String> productCodes = normalizeCodeList(request.getProductCodes()); List<String> productCodes = normalizeCodeList(request.getProductCodes());
List<Integer> orderStatuses = normalizeIntegerList(request.getOrderStatuses());
String creditArrearsStatus = StringUtils.isEmpty(request.getCreditArrearsStatus()) ? null String creditArrearsStatus = StringUtils.isEmpty(request.getCreditArrearsStatus()) ? null
: request.getCreditArrearsStatus().trim(); : request.getCreditArrearsStatus().trim();
String beginBizDate = StringUtils.isEmpty(request.getBeginBizDate()) ? null String beginBizDate = StringUtils.isEmpty(request.getBeginBizDate()) ? null
@@ -2740,6 +2744,7 @@ public class PrcOrderBillServiceImpl implements IPrcOrderBillService
query.setCustomerCodes(customerCodes); query.setCustomerCodes(customerCodes);
query.setProductCodes(productCodes); query.setProductCodes(productCodes);
query.setCreditArrearsStatus(creditArrearsStatus); query.setCreditArrearsStatus(creditArrearsStatus);
query.setOrderStatuses(orderStatuses);
if (beginBizDate != null) if (beginBizDate != null)
{ {
query.getParams().put("beginBizDate", beginBizDate); query.getParams().put("beginBizDate", beginBizDate);
@@ -66,6 +66,12 @@
<include refid="columns"/> <include refid="columns"/>
where create_by = #{createBy} where create_by = #{createBy}
and status in ('WAITING', 'RUNNING', 'CANCEL_REQUESTED') and status in ('WAITING', 'RUNNING', 'CANCEL_REQUESTED')
<if test="params.dataScope != null and params.dataScope != ''">
and exists (
select 1 from ar_bill b where b.ar_bill_id = ar_bill_generate_task.ar_bill_id
<include refid="com.bms.ar.mapper.ArBillMapper.customerDataScope"><property name="customerIdColumn" value="b.customer_id"/></include>
)
</if>
order by create_time desc, task_id desc order by create_time desc, task_id desc
</select> </select>
@@ -26,7 +26,7 @@
<result property="createTime" column="create_time"/> <result property="createTime" column="create_time"/>
</resultMap> </resultMap>
<select id="selectList" parameterType="ArBillLog" resultMap="ArBillLogResult"> <select id="selectList" resultMap="ArBillLogResult">
select l.log_id, l.ar_bill_id, l.object_type, l.object_id, l.operation_type, l.operation_desc, select l.log_id, l.ar_bill_id, l.object_type, l.object_id, l.operation_type, l.operation_desc,
case l.object_type case l.object_type
when 'BILL' then b.bill_no when 'BILL' then b.bill_no
@@ -43,14 +43,15 @@
left join ar_balance_writeoff bw on bw.writeoff_id = case when l.object_type = 'BALANCE_WRITEOFF' then left join ar_balance_writeoff bw on bw.writeoff_id = case when l.object_type = 'BALANCE_WRITEOFF' then
cast(l.object_id as unsigned) end cast(l.object_id as unsigned) end
<where> <where>
<if test="arBillId != null">and l.ar_bill_id = #{arBillId}</if> <if test="query.arBillId != null">and l.ar_bill_id = #{query.arBillId}</if>
<if test="objectType != null and objectType != ''">and l.object_type = #{objectType}</if> <if test="query.objectType != null and query.objectType != ''">and l.object_type = #{query.objectType}</if>
<if test="objectId != null and objectId != ''">and l.object_id = #{objectId}</if> <if test="query.objectId != null and query.objectId != ''">and l.object_id = #{query.objectId}</if>
<if test="operationType != null and operationType != ''">and l.operation_type = #{operationType}</if> <if test="query.operationType != null and query.operationType != ''">and l.operation_type = #{query.operationType}</if>
<if test="operationSource != null and operationSource != ''">and l.operation_source = #{operationSource} <if test="query.operationSource != null and query.operationSource != ''">and l.operation_source = #{query.operationSource}
</if> </if>
<if test="requestId != null and requestId != ''">and l.request_id = #{requestId}</if> <if test="query.requestId != null and query.requestId != ''">and l.request_id = #{query.requestId}</if>
<if test="result != null and result != ''">and l.result = #{result}</if> <if test="query.result != null and query.result != ''">and l.result = #{query.result}</if>
<include refid="com.bms.ar.mapper.ArBillMapper.customerDataScope"><property name="customerIdColumn" value="b.customer_id"/></include>
</where> </where>
order by l.create_time desc, l.log_id desc order by l.create_time desc, l.log_id desc
</select> </select>
@@ -67,6 +67,27 @@
from ar_bill from ar_bill
</sql> </sql>
<!-- 仅受限角色增加 EXISTS,避免业务员历史关联产生重复账单和错误分页。
sales_user_id 优先按用户ID解析;找不到ID时兼容历史工号,防止数字工号与用户ID同时匹配两个人。
customerIdColumn 仅由下面各个 include 的固定属性传入。 -->
<sql id="customerDataScope">
<if test="params.dataScope != null and params.dataScope != ''">
and exists (
select 1 from ctr_customer ar_scope_customer
left join sys_user ar_scope_id_user
on ar_scope_customer.sales_user_id regexp '^[0-9]+$'
and ar_scope_id_user.user_id = ar_scope_customer.sales_user_id + 0
left join sys_user ar_scope_user
on ar_scope_user.del_flag = '0'
and (ar_scope_user.user_id = ar_scope_id_user.user_id
or (ar_scope_id_user.user_id is null
and ar_scope_user.user_name = ar_scope_customer.sales_user_id))
where ar_scope_customer.customer_id = ${customerIdColumn}
${params.dataScope}
)
</if>
</sql>
<select id="selectArBillList" parameterType="com.bms.ar.domain.vo.ArBillQuery" resultMap="ArBillResult"> <select id="selectArBillList" parameterType="com.bms.ar.domain.vo.ArBillQuery" resultMap="ArBillResult">
<include refid="selectArBillColumns"/> <include refid="selectArBillColumns"/>
<where> <where>
@@ -85,10 +106,23 @@
<if test="billDateStart != null">and bill_date &gt;= #{billDateStart}</if> <if test="billDateStart != null">and bill_date &gt;= #{billDateStart}</if>
<if test="billDateEnd != null">and bill_date &lt;= #{billDateEnd}</if> <if test="billDateEnd != null">and bill_date &lt;= #{billDateEnd}</if>
<if test="billPeriodEnd != null">and bill_period_end = #{billPeriodEnd}</if> <if test="billPeriodEnd != null">and bill_period_end = #{billPeriodEnd}</if>
<include refid="customerDataScope"><property name="customerIdColumn" value="ar_bill.customer_id"/></include>
</where> </where>
order by create_time desc, ar_bill_id desc order by create_time desc, ar_bill_id desc
</select> </select>
<select id="selectArBillByDataScope" parameterType="com.bms.ar.domain.vo.ArBillQuery" resultMap="ArBillResult">
<include refid="selectArBillColumns"/>
where ar_bill_id = #{arBillId}
<include refid="customerDataScope"><property name="customerIdColumn" value="ar_bill.customer_id"/></include>
</select>
<select id="selectCustomerIdByDataScope" parameterType="com.bms.ar.domain.vo.ArBillQuery" resultType="long">
select c.customer_id from ctr_customer c where c.customer_id = #{customerId}
<include refid="customerDataScope"><property name="customerIdColumn" value="c.customer_id"/></include>
</select>
<!-- 后台生成/导出任务使用原始查询,前台入口通过 selectArBillByDataScope 校验。 -->
<select id="selectArBillById" parameterType="long" resultMap="ArBillResult"> <select id="selectArBillById" parameterType="long" resultMap="ArBillResult">
<include refid="selectArBillColumns"/> <include refid="selectArBillColumns"/>
where ar_bill_id = #{arBillId} where ar_bill_id = #{arBillId}
+94 -2
View File
@@ -4,6 +4,7 @@
ref="tableRef" ref="tableRef"
:columns="columns" :columns="columns"
:search-columns="searchColumns" :search-columns="searchColumns"
:default-params="defaultParams"
:query="queryList" :query="queryList"
:search-per-row="4" :search-per-row="4"
row-key="billId" row-key="billId"
@@ -54,6 +55,32 @@
</el-option> </el-option>
</el-select> </el-select>
</template> </template>
<template #search-orderStatuses="{ model }">
<el-select
v-model="model.orderStatuses"
multiple
filterable
clearable
collapse-tags
collapse-tags-tooltip
placeholder="请选择"
style="width: 100%"
>
<template #header>
<el-checkbox
:model-value="isAllSelected(model, 'orderStatuses', orderStatusAllValues)"
:indeterminate="isIndeterminate(model, 'orderStatuses', orderStatusAllValues)"
@change="(checked) => toggleAllSelected(model, 'orderStatuses', orderStatusAllValues, checked)"
>全选</el-checkbox>
</template>
<el-option
v-for="opt in orderStatusOptions"
:key="opt.value"
:label="opt.label"
:value="opt.value"
/>
</el-select>
</template>
<template #col-product="{ row }"> <template #col-product="{ row }">
{{ row.productName || '-' }} ({{ row.productCode }}) {{ row.productName || '-' }} ({{ row.productCode }})
</template> </template>
@@ -293,6 +320,56 @@ const exportFieldRef = ref()
const tableHeight = ref(480) const tableHeight = ref(480)
let tableResizeObserver = null let tableResizeObserver = null
const defaultParams = {
orderStatuses: [3, 4, 5, 6, 7, 8, 11],
billingFailStatuses: []
}
const orderStatusMap = {
0: '草稿订单',
1: '确认订单',
2: '已中转',
3: '已收货',
4: '已装箱',
5: '已出库',
6: '转运中',
7: '已上网',
8: '已签收',
9: '已退件',
10: '已取消',
11: '已复核'
}
const orderStatusOptions = Object.keys(orderStatusMap).map(k => ({ label: orderStatusMap[k], value: Number(k) }))
const orderStatusAllValues = computed(() => orderStatusOptions.map(o => o.value))
function selectedValues(model, key) {
const value = model?.[key]
return Array.isArray(value) ? value : []
}
function resolveAllValues(allValues) {
if (Array.isArray(allValues)) return allValues
const value = allValues?.value
return Array.isArray(value) ? value : []
}
function isAllSelected(model, key, allValues) {
const all = resolveAllValues(allValues)
const selected = selectedValues(model, key)
return all.length > 0 && selected.length === all.length
}
function isIndeterminate(model, key, allValues) {
const all = resolveAllValues(allValues)
const selected = selectedValues(model, key)
return selected.length > 0 && selected.length < all.length
}
function toggleAllSelected(model, key, allValues, checked) {
if (!model) return
model[key] = checked ? [...resolveAllValues(allValues)] : []
}
function updateTableHeight() { function updateTableHeight() {
const tableEl = document.querySelector('.billing-bill > .basic-table .el-table, .billing-bill > .basic-table .basic-table-virtual') const tableEl = document.querySelector('.billing-bill > .basic-table .el-table, .billing-bill > .basic-table .basic-table-virtual')
if (!tableEl) return if (!tableEl) return
@@ -393,7 +470,7 @@ const billingFailStatusOptions = [
const searchColumns = [ const searchColumns = [
{ label: '单号', prop: 'orderIdentifiers', placeholder: '订单号/运单号/转单号' }, { label: '单号', prop: 'orderIdentifiers', placeholder: '订单号/运单号/转单号' },
{ label: '客户', prop: 'customerCodes', placeholder: '客户名称/客户代码/对外代码' }, { label: '客户', prop: 'customerCodes', placeholder: '客户名称/客户代码/对外代码' },
{ label: '产品代码', prop: 'productCodes', placeholder: '产品代码' }, // { label: '产品代码', prop: 'productCodes', placeholder: '产品代码' },
{ {
label: '额度欠费', label: '额度欠费',
prop: 'creditArrearsStatus', prop: 'creditArrearsStatus',
@@ -402,6 +479,19 @@ const searchColumns = [
placeholder: '请选择', placeholder: '请选择',
props: { clearable: true } props: { clearable: true }
}, },
{
label: '订单状态',
prop: 'orderStatuses',
component: 'select',
options: orderStatusOptions,
placeholder: '请选择',
props: {
multiple: true,
clearable: true,
collapseTags: true,
collapseTagsTooltip: true
}
},
{ {
label: '\u8ba1\u8d39\u5931\u8d25', label: '\u8ba1\u8d39\u5931\u8d25',
prop: 'billingFailStatuses', prop: 'billingFailStatuses',
@@ -587,6 +677,7 @@ function buildRecalcPayload() {
customerCodes: rest.customerCodes || [], customerCodes: rest.customerCodes || [],
productCodes: rest.productCodes || [], productCodes: rest.productCodes || [],
creditArrearsStatus: rest.creditArrearsStatus || undefined, creditArrearsStatus: rest.creditArrearsStatus || undefined,
orderStatuses: Array.isArray(rest.orderStatuses) ? rest.orderStatuses : [],
billingFailStatuses: Array.isArray(rest.billingFailStatuses) ? rest.billingFailStatuses : [] billingFailStatuses: Array.isArray(rest.billingFailStatuses) ? rest.billingFailStatuses : []
} }
if (Array.isArray(bizDateRange) && (bizDateRange[0] || bizDateRange[1])) { if (Array.isArray(bizDateRange) && (bizDateRange[0] || bizDateRange[1])) {
@@ -678,7 +769,8 @@ function buildChargeWeightTemplateQuery() {
orderIdentifiers: parseOrderIdentifiers(rest.orderIdentifiers), orderIdentifiers: parseOrderIdentifiers(rest.orderIdentifiers),
customerCodes: rest.customerCodes || [], customerCodes: rest.customerCodes || [],
productCodes: rest.productCodes || [], productCodes: rest.productCodes || [],
creditArrearsStatus: rest.creditArrearsStatus || undefined creditArrearsStatus: rest.creditArrearsStatus || undefined,
orderStatuses: Array.isArray(rest.orderStatuses) ? rest.orderStatuses : []
} }
if (Array.isArray(bizDateRange) && (bizDateRange[0] || bizDateRange[1])) { if (Array.isArray(bizDateRange) && (bizDateRange[0] || bizDateRange[1])) {
payload.beginBizDate = bizDateRange[0] || null payload.beginBizDate = bizDateRange[0] || null
@@ -211,6 +211,11 @@ function buildQueryTags(q) {
: (q.creditArrearsStatus === 'NORMAL' ? "正常" : q.creditArrearsStatus) : (q.creditArrearsStatus === 'NORMAL' ? "正常" : q.creditArrearsStatus)
tags.push("额度欠费" + ':' + label) tags.push("额度欠费" + ':' + label)
} }
const orderStatuses = Array.isArray(q.orderStatuses) ? q.orderStatuses.filter(v => v != null) : []
if (orderStatuses.length) {
const labels = orderStatuses.map(v => orderStatusLabel(v))
tags.push("订单状态" + ':' + labels.join(', '))
}
const failStatuses = Array.isArray(q.billingFailStatuses) ? q.billingFailStatuses.filter(v => v != null) : [] const failStatuses = Array.isArray(q.billingFailStatuses) ? q.billingFailStatuses.filter(v => v != null) : []
if (failStatuses.length) { if (failStatuses.length) {
const labels = failStatuses.map(v => billingFailStatusLabel(v)) const labels = failStatuses.map(v => billingFailStatusLabel(v))
@@ -222,6 +227,25 @@ function buildQueryTags(q) {
return tags return tags
} }
function orderStatusLabel(code) {
const map = {
0: '草稿订单',
1: '确认订单',
2: '已中转',
3: '已收货',
4: '已装箱',
5: '已出库',
6: '转运中',
7: '已上网',
8: '已签收',
9: '已退件',
10: '已取消',
11: '已复核'
}
const key = Number(code)
return map[key] != null ? map[key] : String(code)
}
function billingFailStatusLabel(code) { function billingFailStatusLabel(code) {
const map = { const map = {
0: '计费成功', 0: '计费成功',
@@ -278,6 +302,7 @@ function buildPayload() {
customerCodes: q.customerCodes || [], customerCodes: q.customerCodes || [],
productCodes: q.productCodes || [], productCodes: q.productCodes || [],
creditArrearsStatus: q.creditArrearsStatus || undefined, creditArrearsStatus: q.creditArrearsStatus || undefined,
orderStatuses: Array.isArray(q.orderStatuses) ? q.orderStatuses : [],
billingFailStatuses: Array.isArray(q.billingFailStatuses) ? q.billingFailStatuses : [], billingFailStatuses: Array.isArray(q.billingFailStatuses) ? q.billingFailStatuses : [],
beginBizDate: q.beginBizDate || undefined, beginBizDate: q.beginBizDate || undefined,
endBizDate: q.endBizDate || undefined endBizDate: q.endBizDate || undefined