<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sundot.airport.system.mapper.portrait.AgeIndicatorMapper">

    <sql id="userIdsCondition">
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询个人年龄 -->
    <select id="queryPersonalAge" resultType="java.lang.Integer">
        SELECT 
            CASE 
                WHEN LENGTH(card_number) = 18 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(SUBSTRING(card_number, 7, 8), '%Y%m%d'), CURDATE())
                WHEN LENGTH(card_number) = 15 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(CONCAT('19', SUBSTRING(card_number, 7, 6)), '%y%m%d'), CURDATE())
                ELSE NULL
            END AS age
        FROM sys_user
        WHERE user_id = #{userId}
    </select>

    <!-- 查询组织年龄统计 -->
    <select id="queryOrgAgeStats" resultType="com.sundot.airport.system.domain.portrait.AgeStats">
        SELECT 
            MAX(
                CASE 
                    WHEN card_number IS NOT NULL AND card_number != '' AND (LENGTH(card_number) = 18 OR LENGTH(card_number) = 15) THEN
                        CASE 
                            WHEN LENGTH(card_number) = 18 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(SUBSTRING(card_number, 7, 8), '%Y%m%d'), CURDATE())
                            WHEN LENGTH(card_number) = 15 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(CONCAT('19', SUBSTRING(card_number, 7, 6)), '%y%m%d'), CURDATE())
                            ELSE NULL
                        END
                    ELSE NULL
                END
            ) AS maxAge,
            MIN(
                CASE 
                    WHEN card_number IS NOT NULL AND card_number != '' AND (LENGTH(card_number) = 18 OR LENGTH(card_number) = 15) THEN
                        CASE 
                            WHEN LENGTH(card_number) = 18 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(SUBSTRING(card_number, 7, 8), '%Y%m%d'), CURDATE())
                            WHEN LENGTH(card_number) = 15 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(CONCAT('19', SUBSTRING(card_number, 7, 6)), '%y%m%d'), CURDATE())
                            ELSE NULL
                        END
                    ELSE NULL
                END
            ) AS minAge,
            CASE 
                WHEN COUNT(*) > 0 THEN ROUND(SUM(
                    CASE 
                        WHEN card_number IS NOT NULL AND card_number != '' AND (LENGTH(card_number) = 18 OR LENGTH(card_number) = 15) THEN
                            CASE 
                                WHEN LENGTH(card_number) = 18 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(SUBSTRING(card_number, 7, 8), '%Y%m%d'), CURDATE())
                                WHEN LENGTH(card_number) = 15 THEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(CONCAT('19', SUBSTRING(card_number, 7, 6)), '%y%m%d'), CURDATE())
                                ELSE 0
                            END
                        ELSE 0
                    END
                ) / COUNT(*), 2)
                ELSE 0
            END AS averageAge
        FROM sys_user
        WHERE user_id in <include refid="userIdsCondition" /> AND del_flag='0' AND status='0'
    </select>

</mapper>