2026-10-12 16:41:42 +08:00
package com.ruoyi.projectdev ;
import java.math.BigDecimal ;
import java.util.* ;
import org.apache.ibatis.session.* ;
import org.mybatis.spring.SqlSessionTemplate ;
import org.mybatis.spring.transaction.SpringManagedTransactionFactory ;
import org.junit.jupiter.api.* ;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty ;
import org.springframework.aop.framework.ProxyFactory ;
import org.springframework.jdbc.datasource.* ;
import org.springframework.test.util.ReflectionTestUtils ;
import org.springframework.transaction.* ;
import org.springframework.transaction.interceptor.* ;
import org.springframework.transaction.support.TransactionTemplate ;
import com.ruoyi.common.exception.ServiceException ;
import com.ruoyi.common.utils.SecurityUtils ;
import com.ruoyi.project.domain.* ;
import com.ruoyi.project.mapper.* ;
import com.ruoyi.project.service.* ;
import com.ruoyi.project.service.impl.ProjectInfoServiceImpl ;
import com.ruoyi.warehouse.domain.* ;
import com.ruoyi.warehouse.mapper.* ;
import com.ruoyi.warehouse.service.impl.InventoryCostJournal ;
import static com.ruoyi.warehouse.service.impl.InventoryCostJournal.event ;
import static org.junit.jupiter.api.Assertions.* ;
import static org.mockito.Mockito.* ;
class ProjectLoanDatabaseTest {
private Configuration config ( ) throws Exception { return AuthorizedDatabaseTestSupport . configuration ( " mapper/project/ProjectLoanMapper.xml " , " mapper/project/ProjectInfoMapper.xml " , " mapper/warehouse/InventoryCostMapper.xml " , " mapper/warehouse/ProductInfoMapper.xml " , " mapper/warehouse/WarehouseAreaMapper.xml " ) ; }
@Test void xmlParses ( ) throws Exception { assertTrue ( config ( ) . hasStatement ( ProjectLoanMapper . class . getName ( ) + " .insertReturn " ) ) ; }
private BigDecimal n ( String s ) { return new BigDecimal ( s ) ; }
private < T > T proxy ( T target , DataSourceTransactionManager tm ) { var attrs = new MatchAlwaysTransactionAttributeSource ( ) ; attrs . setTransactionAttribute ( new DefaultTransactionAttribute ( TransactionDefinition . PROPAGATION_NESTED ) ) ; var p = new ProxyFactory ( target ) ; p . setProxyTargetClass ( true ) ; p . addAdvice ( new TransactionInterceptor ( tm , attrs ) ) ; return ( T ) p . getProxy ( ) ; }
@Test @EnabledIfSystemProperty ( named = " projectDbTests " , matches = " true " ) void borrowingRepaymentAndMultiHopCostCorrectionAreAtomic ( ) throws Exception {
String key = " LOANTEST- " + UUID . randomUUID ( ) ;
try ( var connection = AuthorizedDatabaseTestSupport . connection ( ) ; var security = mockStatic ( SecurityUtils . class ) ) {
security . when ( SecurityUtils : : getUsername ) . thenReturn ( " rollback-test " ) ; var ds = new SingleConnectionDataSource ( connection , true ) ; var tm = new DataSourceTransactionManager ( ds ) ; var config = config ( ) ; config . setEnvironment ( new org . apache . ibatis . mapping . Environment ( " loan-rollback " , new SpringManagedTransactionFactory ( ) , ds ) ) ; var session = new SqlSessionTemplate ( new SqlSessionFactoryBuilder ( ) . build ( config ) ) ;
var pm = session . getMapper ( ProjectInfoMapper . class ) ; var items = session . getMapper ( ProductInfoMapper . class ) ; var areas = session . getMapper ( WarehouseAreaMapper . class ) ; var costs = session . getMapper ( InventoryCostMapper . class ) ; var lm = session . getMapper ( ProjectLoanMapper . class ) ;
var projects = new ProjectInfoServiceImpl ( ) ; ReflectionTestUtils . setField ( projects , " projectInfoMapper " , pm ) ;
var raw = new InventoryCostJournal ( ) ; ReflectionTestUtils . setField ( raw , " mapper " , costs ) ; ReflectionTestUtils . setField ( raw , " projects " , projects ) ; ReflectionTestUtils . setField ( raw , " items " , items ) ; ReflectionTestUtils . setField ( raw , " areas " , areas ) ; var journal = proxy ( raw , tm ) ;
var loanRaw = new ProjectLoanService ( ) ; ReflectionTestUtils . setField ( loanRaw , " mapper " , lm ) ; ReflectionTestUtils . setField ( loanRaw , " journal " , journal ) ; ReflectionTestUtils . setField ( loanRaw , " projects " , projects ) ; var loans = proxy ( loanRaw , tm ) ;
var numbers = AuthorizedDatabaseTestSupport . numbers ( new org . springframework . jdbc . core . JdbcTemplate ( ds ) , tm ) ; ReflectionTestUtils . setField ( raw , " businessNumbers " , numbers ) ; ReflectionTestUtils . setField ( loanRaw , " businessNumbers " , numbers ) ;
try { new TransactionTemplate ( tm ) . executeWithoutResult ( status - > { status . setRollbackOnly ( ) ;
List < Long > ids = new ArrayList < > ( ) ; for ( int x = 0 ; x < 3 ; x + + ) { var p = new ProjectInfo ( ) ; p . setProjectCode ( key + x ) ; p . setProjectName ( " 回滚借还验证 " ) ; p . setProjectType ( " RESEARCH " ) ; p . setProjectStatus ( 2 ) ; p . setTotalAmount ( n ( " 0 " ) ) ; p . setCreateBy ( " rollback-test " ) ; pm . insertProjectInfo ( p ) ; ids . add ( p . getId ( ) ) ; }
var item = new ProductInfo ( ) ; item . setProductName ( key ) ; item . setUnit ( " 件 " ) ; items . insertProductInfo ( item ) ; Long i = item . getId ( ) , a = areas . selectWarehouseAreaAll ( ) . get ( 0 ) . getId ( ) , b = areas . selectWarehouseAreaAll ( ) . get ( 1 ) . getId ( ) , p = ids . get ( 0 ) , q = ids . get ( 1 ) , r = ids . get ( 2 ) ;
var receipt = journal . append ( event ( p , i , a , " RECEIPT " , n ( " 10 " ) , n ( " 100 " ) , null , key ) ) ;
var first = loans . borrow ( p , q , i , a , b , n ( " 4 " ) , key + " -BORROW " , null ) ; assertEquals ( n ( " 4.0000 " ) , first . getOutstandingQuantity ( ) ) ;
assertEquals ( first . getId ( ) , loans . borrow ( p , q , i , a , b , n ( " 4 " ) , key + " -BORROW " , null ) . getId ( ) ) ; assertEquals ( 1 , loans . list ( p , i , false ) . size ( ) ) ;
2026-10-14 09:11:33 +08:00
// The purchase repayment selector applies all three filters together. Exercise every
// combination against MySQL so adjacent dynamic SQL fragments cannot break this entry point.
for ( Long projectFilter : new Long [ ] { null , p } ) for ( Long itemFilter : new Long [ ] { null , i } ) for ( boolean outstanding : new boolean [ ] { false , true } ) {
var filtered = loans . list ( projectFilter , itemFilter , outstanding ) ;
assertTrue ( filtered . stream ( ) . anyMatch ( row - > row . getId ( ) . equals ( first . getId ( ) ) ) , " Missing loan for filters " + projectFilter + " : " + itemFilter + " : " + outstanding ) ;
assertTrue ( filtered . stream ( ) . allMatch ( row - > projectFilter = = null | | projectFilter . equals ( row . getLenderProjectId ( ) ) | | projectFilter . equals ( row . getBorrowerProjectId ( ) ) ) ) ;
assertTrue ( filtered . stream ( ) . allMatch ( row - > itemFilter = = null | | itemFilter . equals ( row . getItemId ( ) ) ) ) ;
assertTrue ( filtered . stream ( ) . allMatch ( row - > ! outstanding | | Boolean . TRUE . equals ( row . getActive ( ) ) & & row . getOutstandingQuantity ( ) . signum ( ) > 0 ) ) ;
}
assertTrue ( loans . list ( r , i , true ) . isEmpty ( ) ) ;
2026-10-12 16:41:42 +08:00
var second = loans . borrow ( q , r , i , b , a , n ( " 2 " ) , key + " -SECOND " , null ) ; var consumed = journal . append ( event ( r , i , a , " ISSUE " , n ( " 2 " ) , null , null , key + " -CONSUME " ) ) ;
assertEquals ( n ( " 20.000000 " ) , consumed . getOriginalCost ( ) ) ; journal . reviseReceipt ( receipt . getId ( ) , n ( " 200 " ) , key + " -SETTLE " , journal . balance ( p , i ) . getVersion ( ) ) ;
assertEquals ( n ( " 40.000000 " ) , journal . balance ( q , i ) . getAmount ( ) ) ; assertEquals ( n ( " 40.000000 " ) , costs . selectEvent ( consumed . getId ( ) ) . getEffectiveCost ( ) ) ; assertEquals ( n ( " 20.000000 " ) , costs . selectEvent ( consumed . getId ( ) ) . getOriginalCost ( ) ) ;
assertThrows ( ServiceException . class , ( ) - > journal . reviseReceipt ( first . getInEventId ( ) , n ( " 0 " ) , key + " -BAD " , journal . balance ( q , i ) . getVersion ( ) ) ) ;
var returned = loans . returnStock ( first . getId ( ) , b , a , n ( " 1 " ) , key + " -RETURN " ) ; assertEquals ( n ( " 1.0000 " ) , returned . getReturnedQuantity ( ) ) ; assertEquals ( n ( " 3.0000 " ) , returned . getOutstandingQuantity ( ) ) ;
assertEquals ( n ( " 1.0000 " ) , loans . returnStock ( first . getId ( ) , b , a , n ( " 1 " ) , key + " -RETURN " ) . getReturnedQuantity ( ) ) ;
assertThrows ( ServiceException . class , ( ) - > loans . returnStock ( first . getId ( ) , b , a , n ( " 4 " ) , key + " -EXCESS " ) ) ; assertEquals ( 1 , loans . returns ( first . getId ( ) ) . size ( ) ) ;
journal . reviseReceipt ( receipt . getId ( ) , n ( " 100 " ) , key + " -CORRECTION " , journal . balance ( p , i ) . getVersion ( ) ) ; assertEquals ( n ( " 70.000000 " ) , journal . balance ( p , i ) . getAmount ( ) ) ; assertEquals ( n ( " 10.000000 " ) , journal . balance ( q , i ) . getAmount ( ) ) ; assertEquals ( n ( " 20.000000 " ) , costs . selectEvent ( consumed . getId ( ) ) . getEffectiveCost ( ) ) ;
assertThrows ( ServiceException . class , ( ) - > loans . revokeLoan ( first . getId ( ) , " 已有下游 " ) ) ; assertTrue ( loans . get ( first . getId ( ) ) . getActive ( ) ) ;
loans . revokeStockReturn ( first . getId ( ) , " LOAN-RETURN: " + key + " -RETURN " , " 归还纠错 " ) ; assertEquals ( n ( " 0.0000 " ) , loans . get ( first . getId ( ) ) . getReturnedQuantity ( ) ) ;
journal . voidLastEvent ( consumed . getId ( ) , " 先撤销消耗 " ) ; loans . revokeLoan ( second . getId ( ) , " 撤销第二层借用 " ) ; loans . revokeLoan ( first . getId ( ) , " 撤销首层借用 " ) ;
2026-10-14 09:11:33 +08:00
assertTrue ( loans . list ( p , i , true ) . isEmpty ( ) ) ; assertEquals ( 1 , loans . list ( p , i , false ) . size ( ) ) ;
2026-10-12 16:41:42 +08:00
assertEquals ( n ( " 10.0000 " ) , journal . balance ( p , i ) . getQuantity ( ) ) ; assertEquals ( n ( " 100.000000 " ) , journal . balance ( p , i ) . getAmount ( ) ) ; assertEquals ( n ( " 0.0000 " ) , journal . balance ( q , i ) . getQuantity ( ) ) ;
} ) ; assertNull ( costs . selectEventByKey ( key ) ) ; } finally { connection . rollback ( ) ; }
}
}
}