修复流水明细时间金额筛选SQL问题」}{
This commit is contained in:
@@ -156,7 +156,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
AND IFNULL(bs.AMOUNT_DR, 0) > 0
|
AND IFNULL(bs.AMOUNT_DR, 0) > 0
|
||||||
</if>
|
</if>
|
||||||
<if test="query.transactionStartTime != null and query.transactionStartTime != ''">
|
<if test="query.transactionStartTime != null and query.transactionStartTime != ''">
|
||||||
AND <include refid="parsedTrxDateExpr"/> <![CDATA[ >= ]]>
|
AND (<include refid="parsedTrxDateExpr"/>) <![CDATA[ >= ]]>
|
||||||
CASE
|
CASE
|
||||||
WHEN LENGTH(TRIM(#{query.transactionStartTime})) = 10
|
WHEN LENGTH(TRIM(#{query.transactionStartTime})) = 10
|
||||||
THEN STR_TO_DATE(CONCAT(TRIM(#{query.transactionStartTime}), ' 00:00:00'), '%Y-%m-%d %H:%i:%s')
|
THEN STR_TO_DATE(CONCAT(TRIM(#{query.transactionStartTime}), ' 00:00:00'), '%Y-%m-%d %H:%i:%s')
|
||||||
@@ -164,7 +164,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
END
|
END
|
||||||
</if>
|
</if>
|
||||||
<if test="query.transactionEndTime != null and query.transactionEndTime != ''">
|
<if test="query.transactionEndTime != null and query.transactionEndTime != ''">
|
||||||
AND <include refid="parsedTrxDateExpr"/> <![CDATA[ <= ]]>
|
AND (<include refid="parsedTrxDateExpr"/>) <![CDATA[ <= ]]>
|
||||||
CASE
|
CASE
|
||||||
WHEN LENGTH(TRIM(#{query.transactionEndTime})) = 10
|
WHEN LENGTH(TRIM(#{query.transactionEndTime})) = 10
|
||||||
THEN STR_TO_DATE(CONCAT(TRIM(#{query.transactionEndTime}), ' 23:59:59'), '%Y-%m-%d %H:%i:%s')
|
THEN STR_TO_DATE(CONCAT(TRIM(#{query.transactionEndTime}), ' 23:59:59'), '%Y-%m-%d %H:%i:%s')
|
||||||
@@ -216,10 +216,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
</foreach>
|
</foreach>
|
||||||
</if>
|
</if>
|
||||||
<if test="query.amountMin != null">
|
<if test="query.amountMin != null">
|
||||||
AND <include refid="absoluteAmountExpr"/> <![CDATA[ >= ]]> #{query.amountMin}
|
AND (<include refid="absoluteAmountExpr"/>) <![CDATA[ >= ]]> #{query.amountMin}
|
||||||
</if>
|
</if>
|
||||||
<if test="query.amountMax != null">
|
<if test="query.amountMax != null">
|
||||||
AND <include refid="absoluteAmountExpr"/> <![CDATA[ <= ]]> #{query.amountMax}
|
AND (<include refid="absoluteAmountExpr"/>) <![CDATA[ <= ]]> #{query.amountMax}
|
||||||
</if>
|
</if>
|
||||||
<if test="(query.counterpartyAccount != null and query.counterpartyAccount != '') or query.counterpartyAccountEmpty">
|
<if test="(query.counterpartyAccount != null and query.counterpartyAccount != '') or query.counterpartyAccountEmpty">
|
||||||
AND (
|
AND (
|
||||||
|
|||||||
@@ -69,6 +69,74 @@ class CcdiBankStatementMapperXmlTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void statementFilterWhere_shouldWrapIncludedCaseExpressionsToAvoidAndCaseCollision() throws Exception {
|
||||||
|
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE)) {
|
||||||
|
String xml = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
assertTrue(xml.contains("AND (<include refid=\"parsedTrxDateExpr\"/>) <![CDATA[ >= ]]>"), xml);
|
||||||
|
assertTrue(xml.contains("AND (<include refid=\"parsedTrxDateExpr\"/>) <![CDATA[ <= ]]>"), xml);
|
||||||
|
assertTrue(xml.contains("AND (<include refid=\"absoluteAmountExpr\"/>) <![CDATA[ >= ]]>"), xml);
|
||||||
|
assertTrue(xml.contains("AND (<include refid=\"absoluteAmountExpr\"/>) <![CDATA[ <= ]]>"), xml);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void selectStatementPage_shouldKeepWhitespaceBeforeDateRangeExpressions() throws Exception {
|
||||||
|
MappedStatement mappedStatement = loadMappedStatement(
|
||||||
|
"com.ruoyi.ccdi.project.mapper.CcdiBankStatementMapper.selectStatementPage");
|
||||||
|
CcdiBankStatementQueryDTO queryDTO = new CcdiBankStatementQueryDTO();
|
||||||
|
queryDTO.setProjectId(33L);
|
||||||
|
queryDTO.setTransactionStartTime("2024-01-01 00:00:00");
|
||||||
|
queryDTO.setTransactionEndTime("2024-01-31 23:59:59");
|
||||||
|
|
||||||
|
String sql = renderSql(mappedStatement, queryDTO);
|
||||||
|
|
||||||
|
assertFalse(sql.contains("ANDCASE"), sql);
|
||||||
|
assertTrue(sql.contains("AND ( CASE WHEN bs.TRX_DATE"), sql);
|
||||||
|
assertTrue(sql.contains("END ) >="), sql);
|
||||||
|
assertTrue(sql.contains("END ) <="), sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void selectStatementPage_shouldKeepWhitespaceBeforeAmountRangeExpressions() throws Exception {
|
||||||
|
MappedStatement mappedStatement = loadMappedStatement(
|
||||||
|
"com.ruoyi.ccdi.project.mapper.CcdiBankStatementMapper.selectStatementPage");
|
||||||
|
CcdiBankStatementQueryDTO queryDTO = new CcdiBankStatementQueryDTO();
|
||||||
|
queryDTO.setProjectId(33L);
|
||||||
|
queryDTO.setAmountMin(java.math.BigDecimal.ONE);
|
||||||
|
queryDTO.setAmountMax(new java.math.BigDecimal("100"));
|
||||||
|
|
||||||
|
String sql = renderSql(mappedStatement, queryDTO);
|
||||||
|
|
||||||
|
assertFalse(sql.contains("ANDCASE"), sql);
|
||||||
|
assertTrue(sql.contains("AND ( CASE WHEN IFNULL(bs.AMOUNT_CR, 0) > 0"), sql);
|
||||||
|
assertTrue(sql.contains("END ) >= ?"), sql);
|
||||||
|
assertTrue(sql.contains("END ) <= ?"), sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MappedStatement loadMappedStatement(String statementId) throws Exception {
|
||||||
|
Configuration configuration = new Configuration();
|
||||||
|
configuration.setEnvironment(new Environment("test", new JdbcTransactionFactory(), new NoOpDataSource()));
|
||||||
|
registerTypeAliases(configuration.getTypeAliasRegistry());
|
||||||
|
configuration.getLanguageRegistry().register(XMLLanguageDriver.class);
|
||||||
|
configuration.addMapper(CcdiBankStatementMapper.class);
|
||||||
|
|
||||||
|
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(RESOURCE)) {
|
||||||
|
XMLMapperBuilder xmlMapperBuilder =
|
||||||
|
new XMLMapperBuilder(inputStream, configuration, RESOURCE, configuration.getSqlFragments());
|
||||||
|
xmlMapperBuilder.parse();
|
||||||
|
}
|
||||||
|
return configuration.getMappedStatement(statementId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String renderSql(MappedStatement mappedStatement, CcdiBankStatementQueryDTO queryDTO) {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("query", queryDTO);
|
||||||
|
BoundSql boundSql = mappedStatement.getBoundSql(params);
|
||||||
|
return boundSql.getSql().replaceAll("\\s+", " ").trim();
|
||||||
|
}
|
||||||
|
|
||||||
private void registerTypeAliases(TypeAliasRegistry typeAliasRegistry) {
|
private void registerTypeAliases(TypeAliasRegistry typeAliasRegistry) {
|
||||||
typeAliasRegistry.registerAlias("map", Map.class);
|
typeAliasRegistry.registerAlias("map", Map.class);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user