售票查询按性别统计数量 增加身份证非空判断

This commit is contained in:
punchhhh 2025-01-17 14:38:56 +08:00
parent c672fb03fd
commit 019a3f5be7

View File

@ -36,12 +36,43 @@ public interface SaleDataTodayRepository extends MongoRepository<SaleDataToday,S
@Aggregation(pipeline = {
"{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
"{$project: {certLength: {$strLenCP: '$certificateno'}, secondLastChar: {$substrCP: ['$certificateno', {$subtract: [{$strLenCP: '$certificateno'}, 2]}, 1]}}}",
"{$group: {_id: null, maleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}, then: 1, else: 0}}}, femaleCount: {$sum: {$cond: {if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}, then: 1, else: 0}}}, totalCount: {$sum: 1}}}",
"{$project: {_id: 0, maleCount: 1, femaleCount: 1, maleRatio: {$divide: ['$maleCount', '$totalCount']}}}"
"{$project: {" +
// 使用 $ifNull 处理 certficateno null 或空字符串的情况
" certLength: {$strLenCP: {$ifNull: ['$certificateno', '']}}, " +
// 获取倒数第二个字符并处理 certficateno null 或长度小于 2 的情况
" secondLastChar: {$cond: {" +
" if: {$gte: [{$strLenCP: {$ifNull: ['$certificateno', '']}}, 2]}," +
" then: {$substrCP: [" +
" {$ifNull: ['$certificateno', '']}," +
" {$subtract: [{$strLenCP: {$ifNull: ['$certificateno', '']}}, 2]}, 1" +
" ]}, " +
" else: '0'" + // 如果 certficateno 长度小于 2返回 '0'
" }}" +
"}}",
"{$group: {" +
" _id: null," +
// 统计男性人数倒数第二个字符是奇数
" maleCount: {$sum: {$cond: {" +
" if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 1]}," +
" then: 1, else: 0" +
" }}}," +
// 统计女性人数倒数第二个字符是偶数
" femaleCount: {$sum: {$cond: {" +
" if: {$eq: [{$mod: [{$toInt: '$secondLastChar'}, 2]}, 0]}," +
" then: 1, else: 0" +
" }}}," +
" totalCount: {$sum: 1}" + // 总数
"}}",
"{$project: {" +
" _id: 0," +
" maleCount: 1," +
" femaleCount: 1," +
" maleRatio: {$divide: ['$maleCount', '$totalCount']}" + // 计算男性比例
"}}"
})
AggregationVO findAllByCertificatenoSDTime(String starTime, String endTime);
@Aggregation(pipeline = {
"{$match: {sddate: { $gte: ?0, $lte: ?1 }}}",
"{$match: {certificateno: {$regex: '^.{18}$'}}}",