<?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.yeejoin.amos.boot.module.jcs.api.mapper.FireResourceSupervisionMapper">

    <select id="selectPersonnelStats" resultType="java.util.HashMap">
        SELECT
            IF(dfi.field_value = 1601, 1, IF(dfi.field_value = 1602, 0, -1)) AS peopleType,
            COUNT(DISTINCT u.sequence_nbr) AS totalCount,
            SUM(IF(ISNULL(cfp.post_qualification), 0, 1)) AS certifiedCount
        FROM
            cb_org_usr u
                INNER JOIN cb_dynamic_form_instance dfi ON dfi.instance_id = u.sequence_nbr AND dfi.field_code = 'peopleType'
                LEFT JOIN cb_firefighters_post cfp ON cfp.org_usr_id = u.sequence_nbr AND cfp.is_delete = false
        WHERE
            u.is_delete = false
            AND u.biz_org_code LIKE CONCAT(#{bizOrgCode}, '%')
        GROUP BY
            peopleType
        HAVING peopleType in (0, 1);
    </select>

    <select id="selectTodayAttendance" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT cs.user_id)
        FROM cb_sign cs
        WHERE cs.date = date_format(now(), '%Y-%m-%d')
          AND cs.is_delete = FALSE
          AND cs.org_code like concat(#{bizOrgCode}, '%')
    </select>

    <select id="getZzPersonNum" resultType="Map">
        SELECT
                u.amos_org_id AS zzFireNum
            FROM
                cb_org_usr u
                    LEFT JOIN cb_dynamic_form_instance dfi ON dfi.instance_id = u.sequence_nbr
            WHERE
                u.is_delete = FALSE
              AND u.amos_org_id IS NOT NULL
              AND dfi.field_value = 1601
              AND dfi.field_code = 'peopleType'
    </select>

    <select id="getYwPersonNum" resultType="Map">
        SELECT
            u.amos_org_id AS ywNum
        FROM
            cb_org_usr u
                LEFT JOIN cb_dynamic_form_instance dfi ON dfi.instance_id = u.sequence_nbr
        WHERE
            u.is_delete = FALSE
          AND u.amos_org_id IS NOT NULL
          AND dfi.field_value = 1602
          AND dfi.field_code = 'peopleType'
    </select>

</mapper>
