<?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.common.api.mapper.FirefightersMapper">

    <select id="getFirefightersJobTitleCount"
            resultType="com.yeejoin.amos.boot.biz.common.utils.FirefightersTreeDto">
        select COUNT(a.sequence_nbr) num,
               a.job_title_code
                                     jobTitleCode
        from cb_firefighters a
        where a.is_delete = 0
        <if test='bizOrgCode!=null and bizOrgCode!=""'>and a.biz_org_code like concat  ('%',#{bizOrgCode},'%')</if>
        GROUP BY a.job_title_code
    </select>
    <!--消防队员列表按时间倒叙排列add desc 2021-09-08 by kongfm -->
    <select id="getFirefighters"
            resultType="com.yeejoin.amos.boot.module.common.api.dto.FirefightersZhDto">
        select a.*,b.areas_expertise areasExpertise ,b.areas_expertise_code
        areasExpertiseCode from cb_firefighters a LEFT JOIN
        cb_firefighters_post b on
        a.sequence_nbr=b.firefighters_id where
        a.is_delete=0
        <if test='par.bizOrgCode!=null'>and a.biz_org_code like concat ('%',#{par.bizOrgCode},'%')</if>
        <if test='par.postQualification!=null'>and b.post_qualification_code = #{par.postQualification}</if>
        <if test='par.areasExpertise!=null'>and b.areas_expertise_code= #{par.areasExpertise}</if>
        <if test='par.name!=null'>and a.name like concat ('%',#{par.name},'%')</if>
        <if test='par.address!=null'>and a.residence_detail_val like concat ('%',#{par.address},'%')</if>
        <if test='par.state!=null'>and a.state_code= #{par.state}</if>
        <if test='par.company!=null and par.company!= -1'>and a.company= #{par.company}</if>
        <if test='par.fireTeamId!=null'>and a.fire_team_id= #{par.fireTeamId}</if>
        <if test='par.jobTitles!=null'>and a.job_title_code in
            <foreach collection="par.jobTitles" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if
                test='par.areasExpertiseCode!=null and par.areasExpertiseCode!="0"'>and b.areas_expertise_code
            =#{par.areasExpertiseCode}
        </if>
        <if test='par.company!=null and par.company!= -1'>and a.company= #{par.company}</if>


        <if test='par.areasExpertiseCode=="0"'>and b.areas_expertise_code is not null</if>
        ORDER BY a.rec_date desc limit #{pageNum},#{pageSize}
    </select>
    <!--消防队员列表按时间倒叙排列add desc 2021-09-08 by kongfm -->
    <select id="getFirefightersCount" resultType="Map">
        select COUNT(a.sequence_nbr) num from cb_firefighters a LEFT JOIN
        cb_firefighters_post b on
        a.sequence_nbr=b.firefighters_id where
        a.is_delete=0
        <if test='par.bizOrgCode!=null'>and a.biz_org_code like concat ('%',#{par.bizOrgCode},'%')</if>
        <if test='par.postQualification!=null'>and b.post_qualification_code = #{par.postQualification}</if>
        <if test='par.areasExpertise!=null'>and b.areas_expertise_code= #{par.areasExpertise}</if>
        <if test='par.name!=null'>and a.name like concat ('%',#{par.name},'%')</if>
        <if test='par.address!=null'>and a.residence_detail_val like concat ('%',#{par.address},'%')</if>
        <if test='par.state!=null'>and a.state_code= #{par.state}</if>
        <if test='par.company!=null and par.company!= -1'>and a.company= #{par.company}</if>
        <if test='par.fireTeamId!=null'>and a.fire_team_id= #{par.fireTeamId}</if>
        <if test='par.jobTitles!=null'>and a.job_title_code in
            <foreach collection="par.jobTitles" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
        <if
                test='par.areasExpertiseCode!=null and par.areasExpertiseCode!="0"'>and b.areas_expertise_code
            =#{par.areasExpertiseCode}
        </if>
        <if test='par.company!=null and par.company!= -1'>and a.company= #{par.company}</if>


        <if test='par.areasExpertiseCode=="0"'>and b.areas_expertise_code is not null</if>
<!--


        select COUNT(a.sequence_nbr) num from cb_firefighters a LEFT JOIN
        cb_firefighters_post b on a.sequence_nbr=b.firefighters_id where
        a.is_delete=0
        <if test='par.bizOrgCode!=null'>and a.biz_org_code like concat ('%',#{par.bizOrgCode},'%')</if>
        <if test='par.postQualification!=null'>and b.post_qualification_code = #{par.postQualification}</if>
        <if test='par.areasExpertise!=null'>and b.areas_expertise_code= #{par.areasExpertise}</if>
        <if test='par.name!=null'>and a.name like concat ('%',#{par.name},'%')</if>
        <if test='par.address!=null'>and a.residence_detail_val like concat ('%',#{par.address},'%')</if>
        <if test='par.state!=null'>and a.state_code= #{par.state}</if>
        <if test='par.fireTeamId!=null'>and a.fire_team_id= #{par.fireTeamId}</if>
        <if test='par.company!=null and par.company!= -1'>and a.company= #{par.company}</if>
        <if test='par.jobTitle!=null'>and a.job_title_code =#{par.jobTitle}</if>
        <if
                test='par.areasExpertiseCode!=null and par.areasExpertiseCode!="0"'>and b.areas_expertise_code
            =#{par.areasExpertiseCode}
        </if>
        <if test='par.areasExpertiseCode=="0"'>and b.areas_expertise_code is not null</if>

        ORDER BY a.rec_date desc-->
    </select>


    <select id="listToSelectById" resultType="Map">
        SELECT IFNULL(a.personnel_photos, '') personnelPhotos,
               a.sequence_nbr
                                              sequenceNbr,
               IFNULL(a.`name`, '无')          `name`,
               IFNULL(a.`biz_org_name`, '无')          `bizOrgName`,
               IFNULL(a.job_title, '无')
                                              jobTitle,
               IFNULL(b.administrative_position, '无')
                                              administrativePosition,
               IFNULL(c.`name`, '无')          fireTeamName,
               IFNULL(a.state, '无')           state,
               IFNULL(b.employee_hierarchy, '无')
                                              employeeHierarchy,
               IFNULL(b.areas_expertise, '无') areasExpertise,
               IFNULL((SELECT NAME FROM  cb_data_dictionary  WHERE CODE = a.gender AND type = 'XB'), a.gender) gender,
               IFNULL(b.post_qualification, '无')
                                              postQualification, year ( from_days( datediff( now( ),
            a.birthday_time))) age
        FROM cb_firefighters a LEFT JOIN
            cb_firefighters_post b
        ON a.sequence_nbr
            = b.firefighters_id LEFT JOIN
            cb_fire_team c on
            c.sequence_nbr=a.fire_team_id
        WHERE a.is_delete =0
          and
            a.sequence_nbr=#{id}
    </select>


    <!-- BUG3553 BY kongfm 人员关系显示汉字 -->
    <!---陈浩修改导出的数据量 2021-09-13-->
    <select id="exportToExcel"
            resultType="com.yeejoin.amos.boot.module.common.api.dto.FirefightersExcelDto">
        <!-- SELECT f.*, ( SELECT cb_fire_team.NAME FROM cb_fire_team WHERE cb_fire_team.sequence_nbr
            = f.fire_team_id ) fireTeam, emergency_contact, da.NAME AS relationship,
            emergency_contact_phone FROM cb_firefighters f LEFT JOIN cb_firefighters_contacts
            fc ON f.sequence_nbr = fc.firefighters_id left join cb_data_dictionary da
            on da.CODE = fc.relationship where f.is_delete = #{isDelete} -->
        SELECT
        f.*,
        (
        SELECT
        cb_fire_team. NAME
        FROM
        cb_fire_team
        WHERE
        cb_fire_team.sequence_nbr = f.fire_team_id
        ) fireTeam,
        emergency_contact,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fc.relationship
        AND type = 'RJGX'
        ) AS relationship,
        emergency_contact_phone,
        fw.working_hours,
        fw.fire_working_hours,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fe.first_degree
        AND type = 'XLLX'
        ) AS first_degree,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fe.highest_education
        AND type = 'XLLX'
        ) AS highest_education,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fe.academic_degree
        AND type = 'XWLX'
        ) AS academic_degree,
        fe.school,
        fe.professional_name,
        fp.*
        FROM
        cb_firefighters f
        LEFT JOIN cb_firefighters_contacts fc ON f.sequence_nbr = fc.firefighters_id
        LEFT JOIN cb_firefighters_workexperience fw ON f.sequence_nbr = fw.firefighters_id
        LEFT JOIN cb_firefighters_education fe ON f.sequence_nbr = fe.firefighters_id
        LEFT JOIN cb_firefighters_post fp ON f.sequence_nbr = fp.firefighters_id
        WHERE
        f.is_delete = 0
        <if test="name!=null and name !='-1' and name !=''">and f.`name` like concat('%',#{name},'%')</if>
        <if test='postQualification!=null'>and fp.post_qualification_code = #{postQualification}</if>
        <if test='fireTeamId !=null'>and f.fire_team_id = #{fireTeamId}</if>
        <if test='state !=null'>and f.state_code = #{state}</if>
        <if test='areasExpertise!=null'>and fp.areas_expertise_code = #{areasExpertise}</if>
        <if test='jobTitle!=null'>and f.job_title_code = #{jobTitle}</if>
        <if test='bizOrgCode!=null'>and f.biz_org_code  like concat ('%',#{bizOrgCode},'%')  </if>
    </select>

    <select id="getFirefightersName" resultType="Map">
        SELECT cb_firefighters.name                     AS name,
               IFNULL(cb_firefighters.mobile_phone, '') AS phone
        FROM cb_firefighters
        WHERE cb_firefighters.is_delete = 0
          AND cb_firefighters.name IS NOT NULL
          AND cb_firefighters.name != ''
        group by cb_firefighters.name
    </select>

    <select id="queryById" resultType="com.yeejoin.amos.boot.module.common.api.dto.FirefightersDto">
        SELECT
        firefighters.sequence_nbr sequenceNbr ,
        firefighters.name,
        mobile_phone as mobilePhone,
        amos_user_id amosUserId
        FROM
        (select * from cb_firefighters fr  where is_delete = 0)  firefighters
        WHERE
        fire_team_id in ( select sequence_nbr from cb_fire_team cft  where company in (
        select sequence_nbr from cb_org_usr cou where biz_org_name = #{name}) )
        <if test="gw != null">
            And job_title_code In
            <foreach item="item" collection="gw" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </select>
    
     <select id="selectNormalFirePerson"
            resultType="Map">
SELECT
	cff.*,
	 case when cft.sequence_nbr is null then "" else cft. sequence_nbr end as fireTeamId,
		case when cft.NAME is null then "" else cft. NAME end as fireTeamName,
	case when 	cff.age is null then "" else cff.age end as realAge
  from (
		SELECT
			cf.*,
			FLOOR(datediff(CURDATE(),cf.birthday_time)/365.25) AS age
		FROM
			cb_firefighters cf
		WHERE
			(
				cf.state_code IN ('94', '98')
				OR cf.state_code IS NULL
                or LENGTH(cf.state_code) = 0
			)
		AND cf.is_delete = FALSE
         <if test="jobId != null">
             AND find_in_set(#{jobId},cf.job_title_code) > 0
         </if>
         <if test='bizOrgCode!=null and bizOrgCode!=""'>
             and cf.biz_org_code like concat  (#{bizOrgCode},'%')
         </if>
	) cff
LEFT JOIN cb_fire_team cft ON cff.fire_team_id = cft.sequence_nbr

    </select>

    <select id="getPersonByCodes" resultType="com.yeejoin.amos.boot.module.common.api.entity.Firefighters">
        select cf.employee_number, cd.name as gender from cb_firefighters cf
        left join cb_data_dictionary cd on cf.gender = cd.code
        where
        cf.employee_number in
        <foreach  item="item" collection="list" index="index"  open="(" separator="," close=")">
            #{item}
        </foreach>
        and cf.is_delete = 0
    </select>
    <select id="exportToExcelNew" resultType="com.yeejoin.amos.boot.module.common.api.dto.OrgUsrFireExcelDto">
        select
        a.biz_org_name bizOrgName,
        (select c.biz_org_name from cb_org_usr c  where c.sequence_nbr=a.parent_id) parentId,
        (
        SELECT
        cb_fire_team. NAME
        FROM
        cb_fire_team
        WHERE
        cb_fire_team.sequence_nbr = f.fire_team_id
        ) fireTeam,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fe.first_degree
        AND type = 'XLLX'
        ) AS first_degree,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fe.highest_education
        AND type = 'XLLX'
        ) AS highest_education,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fe.academic_degree
        AND type = 'XWLX'
        ) AS academic_degree,
        fe.school,
        fe.professional_name,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fp.employee_hierarchy
        AND type = 'YGCJ'
        ) AS employee_hierarchy,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fp.internal_position_code
        AND type = 'JGNBZW'
        ) AS internal_position_code,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fp.audit_cycle
        AND type = 'SHZQ'
        ) AS audit_cycle,
        fp.administrative_position as administrativePositionCode,
        fp.job_title as positionType,
        fp.post_qualification as postQualification,
        fp.fire_management_post as fireManagementPostCode,
        fp.certificate_type as certificateType,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = fc.relationship
        AND type = 'RJGX'
        ) AS relationship,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = b.gender
        AND type = 'XB'
        ) AS gender,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = b.safetyTraining
        AND type = 'AQPX'
        ) AS safetyTraining,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = b.peopleType
        AND type = 'DWRYLX'
        ) AS peopleType,
        fc.emergency_contact,
        fc.emergency_contact_phone,
        fp.category,
        fp.level,
        fp.areas_expertise,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = b.certificatesTypeCode
        AND type = 'RYZJLX'
        ) AS certificatesTypeCode,
        (
        SELECT
        NAME
        FROM
        cb_data_dictionary
        WHERE
        CODE = b.stateCode
        AND type = 'RYZT'
        ) AS stateCode,
        fp.employee_hierarchy,
        b.*,
        fw.*,
        DATE_FORMAT(b.birthdayTime,'%Y-%m-%d') AS birthdaryTime
        from cb_org_usr a LEFT JOIN
        (SELECT
        instance_id,
        max(case field_code when 'auditCycle' then field_value_label end)                     auditCycle,
        max(case field_code when 'certificatesNumber' then field_value end)                   certificatesNumber,
        max(case field_code when 'certificatesTypeCode' then field_value end)           certificatesTypeCode,
        max(case field_code when 'certificateType' then field_value_label end)                certificateType,
        max(case field_code when 'fireManagementPostCode' then field_value_label end)         fireManagementPostCode,
        max(case field_code when 'gender' then field_value end)                         gender,
        max(case field_code when 'internalPositionCode' then field_value_label end)           internalPositionCode,
        max(case field_code when 'personNumber' then field_value end)                         personNumber,
        max(case field_code when 'positionType' then field_value_label end)                   positionType,
        max(case field_code when 'safetyTraining' then field_value end)                 safetyTraining,
        max(case field_code when 'stateCode' then field_value end)                      stateCode,
        max(case field_code when 'telephone' then field_value end)                            telephone,
        max(case field_code when 'peopleType' then field_value end)                     peopleType,
        max(case field_code when 'nation' then field_value end)                     nation,
        max( CASE field_code WHEN 'birthdayTime' THEN DATE_FORMAT(field_value, '%Y-%m-%d') END ) birthdayTime,
        max(case field_code when 'maritalStatus' then field_value end)                     maritalStatus,
        max(case field_code when 'nativePlace' then field_value end)                     nativePlace,
        max(case field_code when 'nativePlaceVal' then field_value end)                     nativePlaceVal,
        max(case field_code when 'politicalOutlook' then field_value end)                     politicalOutlook,
        max(case field_code when 'residenceDetails' then field_value end)                     residenceDetails,
        max(case field_code when 'residenceDetailVal' then field_value end)                     residenceDetailVal,
        max(case field_code when 'airportAccommodation' then field_value end)                     airportAccommodation
        FROM cb_dynamic_form_instance GROUP BY instance_id) b

        on b.instance_id=a.sequence_nbr
        LEFT JOIN cb_firefighters_contacts fc ON a.sequence_nbr = fc.org_usr_id
        LEFT JOIN cb_firefighters_workexperience fw ON a.sequence_nbr = fw.org_usr_id
        LEFT JOIN cb_firefighters_education fe ON a.sequence_nbr = fe.org_usr_id
        LEFT JOIN cb_firefighters_post fp ON a.sequence_nbr = fp.org_usr_id
        LEFT JOIN  cb_firefighters f ON a.sequence_nbr = f.org_usr_id

        where a.biz_org_name is not null and a.is_delete = 0 and f.is_delete = 0 and a.biz_org_type = 'PERSON'
        <if test="name!=null and name !='-1' and name !=''">and a.`name` like concat('%',#{name},'%')</if>
        <if test='postQualification!=null'>and fp.post_qualification_code = #{postQualification}</if>
        <if test='fireTeamId !=null'>and f.fire_team_id = #{fireTeamId}</if>
        <if test='state !=null'>and f.state_code = #{state}</if>
        <if test='areasExpertise!=null'>and fp.areas_expertise_code = #{areasExpertise}</if>
        <if test='jobTitle!=null'>and f.job_title_code = #{jobTitle}</if>
        <if test='bizOrgCode!=null'>and f.biz_org_code  like concat ('%',#{bizOrgCode},'%')  </if>
    </select>

    <select id="getTeamCountByCode" resultType="java.lang.Integer">
        SELECT
            count( cft.sequence_nbr ) as teamNum
        FROM
            cb_fire_team cft
                LEFT JOIN cb_org_usr ou ON ou.biz_org_code = cft.biz_org_code
        WHERE
            cft.is_delete = 0
        <if test="companyCode != null and companyCode != ''">
            AND ou.`code` = #{companyCode}
        </if>
    </select>

    <select id="getFireForceInfoByCode" resultType="java.util.Map">
        SELECT
            b.*,
            ( b.firefightersNum - b.fireCer ) AS unfireCer,
            ( b.opertNum - b.operCer ) AS unoperCer,
            TRUNCATE ( abs(( b.fireCer ) / IF (( b.firefightersNum ) = 0, 1, b.firefightersNum )) * 100, 2 ) AS fireAbs,
            TRUNCATE ( abs(( b.operCer ) / IF (( b.opertNum ) = 0, 1, b.opertNum )) * 100, 2 ) AS operAbs
        FROM
            (
                SELECT
                    count( a.is_firefighters = 1 OR NULL ) AS firefightersNum,
                    count( a.is_firefighters = 0 OR NULL ) AS opertNum,
                    COUNT(( a.is_firefighters = 1 AND a.post_qualification IS NOT NULL ) OR NULL ) AS fireCer,
                    COUNT(( a.is_firefighters = 0 AND a.post_qualification IS NOT NULL ) OR NULL ) AS operCer
                FROM
                    (
                        SELECT
                            u.sequence_nbr,
                            cfp.fire_management_post,
                            ( CASE WHEN cf.sequence_nbr IS NULL THEN 0 ELSE 1 END ) AS is_firefighters,
                            cfp.post_qualification
                        FROM
                            ( SELECT
                                *
                                FROM	(

                                SELECT
                                *
                                FROM
                                cb_org_usr cb
                                LEFT JOIN (
                                SELECT	v.`instance_id`,
                                max( CASE v.field_code WHEN 'peopleType' THEN v.field_value END ) peopleType
                                FROM
                                `cb_dynamic_form_instance` v
                                WHERE
                                v.group_code = 246
                                GROUP BY
                                v.`instance_id`
                                ) temp ON cb.sequence_nbr = temp.instance_id
                                )	 te where te.peopleType in('1601','1602' ) 	and te.is_delete = 0	 ) u
                                LEFT JOIN cb_firefighters_post cfp ON cfp.org_usr_id = u.sequence_nbr
                                AND cfp.is_delete = 0
                                LEFT JOIN cb_firefighters cf ON cf.org_usr_id = u.sequence_nbr
                                AND cf.is_delete = 0
                        WHERE
                            u.is_delete = 0
                          AND u.biz_org_type = 'PERSON'
                        <if test="bizOrgCode != null and bizOrgCode != ''">
                            AND u.`biz_org_code` like concat(#{bizOrgCode}, '%')
                        </if>
                        GROUP BY
                            u.sequence_nbr
                    ) a
            ) b
    </select>

    <select id="getStationSignInfo" resultType="java.util.Map">
        SELECT
        sum( c.isNormal ) AS qualifiedNum,
        (
        count( 1 ) - sum( c.isNormal )) AS unQualifiedNum,
        (
        TRUNCATE (
        abs(
        sum( c.isNormal ) /
        IF
        (( count( 1 ) = 0 ), 1, count( 1 ) )) * 100,
        2
        )) AS qualifiedAbs
        FROM
        (
        SELECT
        si.`code`,
        CASE

        WHEN count( b.`date` IS NOT NULL OR NULL ) = DATEDIFF(
        DATE_ADD( CURDATE(), INTERVAL 1 DAY ),
        DATE_ADD( curdate(), INTERVAL - DAY ( curdate()) + 1 DAY )) THEN
        1 ELSE 0
        END AS isNormal
        FROM
        idx_biz_station_info si
        LEFT JOIN (
        SELECT
        *
        FROM
        (
        SELECT
        t.date,
        count( DISTINCT cs.user_id ) AS num,
        si.`code`
        FROM
        (
        SELECT
        DATE_ADD( DATE_ADD( CURDATE(), INTERVAL - 1 DAY ), INTERVAL @s DAY ) AS date,
        @s := @s - 1 AS `index`
        FROM
        mysql.help_topic,
        ( SELECT @s := 1 ) temp
        WHERE
        @s > DATEDIFF( date_add( curdate(), INTERVAL - DAY ( curdate()) + 1 DAY ), CURDATE() )) t
        LEFT JOIN cb_sign cs ON cs.date = t.date
        LEFT JOIN cb_org_usr ou ON ou.`biz_org_code` = cs.`biz_org_code`
        AND ou.is_delete = 0
        LEFT JOIN idx_biz_station_info si ON si.`code` = ou.`code`
        GROUP BY
        t.date,
        si.`code`
        ) a
        WHERE
        num >= 8
        ) b ON b.`code` = si.`code`
        LEFT JOIN cb_org_usr ou1 ON ou1.`code` = si.`code`
        AND is_delete = 0
        <where>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                ou1.`biz_org_code` like concat(#{bizOrgCode}, '%')
            </if>
        </where>
        GROUP BY
        si.`code`
        ) c
    </select>

    <select id="getUsrSignInfoByBizOrgCode" resultType="java.util.Map">
        SELECT
        a.qualifiedNum,
        a.allNum - a.qualifiedNum AS unQualifiedNum,
        (
        TRUNCATE ( abs( a.qualifiedNum / IF (( a.allNum = 0 ), 1, a.allNum )) * 100, 2 )) AS qualifiedAbs
        FROM
        (
        SELECT
        ( SELECT count( DISTINCT user_id ) FROM `cb_sign` WHERE `date` = CURRENT_DATE
        <if test="bizOrgCode != null and bizOrgCode != ''">
            AND `biz_org_code` LIKE concat(#{bizOrgCode}, '%')
        </if>
        ) AS qualifiedNum,
        ( SELECT count( 1 ) FROM cb_org_usr WHERE is_delete = 0 AND biz_org_type = 'PERSON'
        <if test="bizOrgCode != null and bizOrgCode != ''">
            AND `biz_org_code` LIKE concat(#{bizOrgCode}, '%')
        </if>
        ) AS allNum
        ) a
    </select>

    <select id="getUserInfoByYJYA" resultType="java.util.Map">
        SELECT
            '运维人员' AS departmentName,
            (
                SELECT
                    count( 1 )
                FROM
                    (
                        SELECT
                            IFNULL( max( CASE WHEN fi.field_code = 'userId' THEN fi.field_value END ), ps.instance_id ) AS id
                        FROM
                            cb_duty_person_shift ps
                                LEFT JOIN cb_dynamic_form_instance fi ON fi.instance_id = ps.instance_id
                        WHERE
                            ps.duty_date = CURRENT_DATE
                        GROUP BY
                            fi.instance_id
                    ) a
                        LEFT JOIN cb_org_usr ou ON ou.sequence_nbr = a.id
                        LEFT JOIN ( SELECT * FROM cb_dynamic_form_instance WHERE field_code = 'peopleType' ) cfi ON ou.sequence_nbr = cfi.instance_id
                WHERE
            		ou.biz_org_code LIKE concat(#{bizOrgCode}, '%')
                    and cfi.field_value = 1602
                  AND ou.biz_org_type = 'PERSON'
                  AND ou.is_delete = 0
            ) onDuty,
            (
                SELECT
                    count( 1 )
                FROM
                    (
                        SELECT
                            ou.*
                        FROM
                            cb_org_usr ou
                                LEFT JOIN ( SELECT * FROM cb_dynamic_form_instance WHERE field_code = 'peopleType' ) cfi ON ou.sequence_nbr = cfi.instance_id
                        WHERE
                            cfi.field_value = 1602
                          AND ou.is_delete = 0
                          AND ou.biz_org_type = 'PERSON'
              			and ou.biz_org_code like CONCAT(#{bizOrgCode}, '%')
                        GROUP BY
                            ou.sequence_nbr
                    ) a
            ) total



        union all



        SELECT
            '驻站消防队员' AS departmentName,
            (
                SELECT
                    count( 1 )
                FROM
                    (
                        SELECT
                            IFNULL( max( CASE WHEN fi.field_code = 'userId' THEN fi.field_value END ), ps.instance_id ) AS id
                        FROM
                            cb_duty_person_shift ps
                                LEFT JOIN cb_dynamic_form_instance fi ON fi.instance_id = ps.instance_id
                        WHERE
                            ps.duty_date = CURRENT_DATE
                        GROUP BY
                            fi.instance_id
                    ) a
                        LEFT JOIN cb_org_usr ou ON ou.sequence_nbr = a.id
                        LEFT JOIN ( SELECT * FROM cb_dynamic_form_instance WHERE field_code = 'peopleType' ) cfi ON ou.sequence_nbr = cfi.instance_id
                WHERE
             		ou.biz_org_code LIKE concat(#{bizOrgCode}, '%')
                   and  cfi.field_value = 1601
                  AND ou.biz_org_type = 'PERSON'
                  AND ou.is_delete = 0
            ) onDuty,
            (
                SELECT
                    count( 1 )
                FROM
                    (
                        SELECT
                            ou.*
                        FROM
                            cb_org_usr ou
                                LEFT JOIN ( SELECT * FROM cb_dynamic_form_instance WHERE field_code = 'peopleType' ) cfi ON ou.sequence_nbr = cfi.instance_id
                        WHERE
                            cfi.field_value = 1601
                          AND ou.is_delete = 0
                          AND ou.biz_org_type = 'PERSON'
                 		  and ou.biz_org_code like CONCAT(#{bizOrgCode}, '%')
                        GROUP BY
                            ou.sequence_nbr
                    ) a
            ) total
    </select>

    <select id="selectSignPersonList" resultType="java.util.Map">
        SELECT c.biz_org_name,
               IFNULL(( SELECT field_value FROM cb_dynamic_form_instance dfi WHERE dfi.field_code = 'personImg' AND dfi.instance_id = c.sequence_nbr ), '') AS personImg,
               IF(cfp.job_title IS NULL OR cfp.job_title = '', NULL, cfp.job_title) job_title,
               IFNULL( cf.mobile_phone, '' ) telephone,
               cs.sign_time AS sign_time
        FROM
            cb_org_usr c
                RIGHT JOIN cb_sign cs ON c.sequence_nbr = cs.USER_ID
                LEFT JOIN cb_firefighters cf ON CONCAT( c.sequence_nbr, '' ) = cf.org_usr_id
                AND cf.is_delete = 0
                LEFT JOIN cb_firefighters_post cfp ON cfp.org_usr_id = c.sequence_nbr
                AND cfp.is_delete = 0
        WHERE
            c.sequence_nbr IS NOT NULL
          AND cs.sign_time LIKE concat( CURRENT_DATE (), '%' )
          AND c.biz_org_code LIKE concat(#{bizOrgCode},'%')
        GROUP BY
            cs.USER_ID
        ORDER BY
            cs.sign_time DESC
    </select>
</mapper>
