<?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.DynamicFormInstanceMapper">
    <select id="selectListByCalledId" resultType="com.yeejoin.amos.boot.module.common.api.dto.DynamicFormInstanceDto">
        SELECT v.sequence_nbr      sequenceNbr,
               v.form_column_id    formColumnId,
               v.instance_id       instanceId,
               v.group_code        groupCode,
               v.field_name        fieldName,
               v.field_code        fieldCode,
               v.field_value       fieldValue,
               v.field_value_label fieldValueLabel,
               v.rec_user_name     recUserName,
               v.rec_user_id       recUserId,
               v.rec_date          recDate,
               v.is_delete         isDelete,
               v.block             block,
               f.field_type        fieldType
        FROM cb_dynamic_form_instance v
                 LEFT JOIN cb_dynamic_form_column f ON f.sequence_nbr = v.form_column_id
        WHERE v.instance_id = #{id}
    </select>

    <select id="listBySeqnum" resultType="com.yeejoin.amos.boot.module.common.api.dto.DynamicFormInstanceDto">
        SELECT v.sequence_nbr      sequenceNbr,
               v.form_column_id    formColumnId,
               v.instance_id       instanceId,
               v.group_code        groupCode,
               v.field_name        fieldName,
               v.field_code        fieldCode,
               v.field_value       fieldValue,
               v.field_value_label fieldValueLabel,
               v.rec_user_name     recUserName,
               v.rec_user_id       recUserId,
               v.rec_date          recDate,
               v.is_delete         isDelete,
               v.block             block,
               f.field_type        fieldType
        FROM cb_dynamic_form_instance v
        LEFT JOIN cb_dynamic_form_column f ON f.sequence_nbr = v.form_column_id
        WHERE v.instance_id = #{id} AND v.field_code = 'personImg'
    </select>

    <select id="listOnDutyPersonByAid" resultType="java.util.Map">
        select
        d.*,
        ps.shift_id as shiftId,
        ps.duty_date as dutyDate,
        ds.name as shiftName,
        '在岗' state
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode,
        <foreach collection="fieldCodes" item="value" index="key" separator=",">
            MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key}
        </foreach>
        from
        cb_dynamic_form_instance i
        where i.GROUP_CODE = #{groupCode}
        GROUP by
        i.INSTANCE_ID ) d,
        cb_duty_person_shift ps,
        cb_duty_shift ds
        where
        d.instanceId = ps.instance_id
        and d.deptId in (SELECT
        `cb_org_usr`.`sequence_nbr`
        FROM
        `cb_org_usr`
        WHERE
        (
        ( `cb_org_usr`.`biz_org_code` LIKE concat( ( SELECT `cb_org_usr`.`biz_org_code` FROM `cb_org_usr` WHERE ( `cb_org_usr`.`sequence_nbr` = 1435847345328660481 ) ), '%' ) )
        AND ( `cb_org_usr`.`biz_org_type` = 'DEPARTMENT' )
        ))

        and ps.shift_id = ds.sequence_nbr
        and ps.duty_date = #{dutyDate}
        <if test="shiftId != null and shiftId != ''">
            and ps.shift_id in (#{shiftId})
        </if>
        order by instanceId desc
    </select>

    <select id="listAll" resultType="java.util.Map">
        select
        d.*
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode
        <foreach collection="fieldCodes" item="value" index="key">
            ,MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key}
        </foreach>
        from
        cb_dynamic_form_instance i
        where i.GROUP_CODE = #{groupCode}
        and i.APP_KEY = #{appKey}
        and i.is_delete = 0
        GROUP by
        i.INSTANCE_ID)d
        <if test="params != null and  params.size() > 0">
            where
            1=1
            <foreach collection="params" index="key" item="value" separator="">
                <choose>
                    <when test="fieldCodes[key] == 'like' and value !=null and value !=''">
                        and d.${key} like concat('%',#{value},'%')
                    </when>
                    <when test="fieldCodes[key] == 'eq' and value !=null and value !=''">
                        and d.${key} = #{value}
                    </when>
                    <when test="fieldCodes[key] == 'ge' and value !=null and value !=''">
                        and d.${key} >= #{value}
                    </when>
                    <when test="fieldCodes[key] == 'le' and value !=null and value !=''">
                        and d.${key} <![CDATA[<=]]> #{value}
                    </when>
                </choose>
            </foreach>
        </if>
        order by instanceId desc
    </select>
    <select id="pageList" resultType="java.util.Map">
        select
        d.*
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode
        <foreach collection="fieldCodes" item="value" index="key" >
           , MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key},
            IF(FIND_IN_SET(i.field_type,'radio,select,treeSelect'), MAX(CASE WHEN i.FIELD_CODE = #{key} THEN
            i.FIELD_VALUE_LABEL END), null) as ${key}Label
        </foreach>
        from
        cb_dynamic_form_instance i
        where
        i.GROUP_CODE = #{groupCode}
        and i.is_delete = 0
        <if test="appKey != null and appKey !=''">
            and i.APP_KEY = #{appKey}
        </if>
        <foreach collection="params" index="key" item="value" separator="">
            <if test="key != null and key == 'instanceIds' ">
                and find_in_set(i.instance_id, #{value}) > 0
            </if>
        </foreach>
        GROUP by
        i.INSTANCE_ID) d
        <if test="params != null and params.size() > 0">
            where
            1=1
            <foreach collection="params" index="key" item="value" separator="">
                <choose>
                    <when test="fieldCodes[key] == 'like' and value !=null and value !=''">
                        and d.${key} like concat('%',#{value},'%')
                    </when>
                    <when test="fieldCodes[key] == 'eq' and value !=null and value !=''">
                        and d.${key} = #{value}
                    </when>
                </choose>
            </foreach>
        <if test="params.endDate != null and params.beginDate != null">
            and d.endDate between #{params.beginDate} and #{params.endDate}
        </if>
        </if>
        <if test="params.date != null and params.lift == 'descend'">
        order by endDate desc
        </if>
        <if test="params.date != null and params.lift == 'ascend'">
            order by endDate ASC
        </if>
        <if test="params.date == null ">
            order by instanceId ASC
        </if>
    </select>
    <select id="listOneDayDutyPerson" resultType="java.util.Map">
        select
        d.*,
        ps.shift_id as shiftId,
        ps.duty_date as dutyDate,
        ds.name as shiftName
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode
        <foreach collection="fieldCodes" item="value" index="key" >
          , MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key}
        </foreach>
        from
        cb_dynamic_form_instance i
        where i.GROUP_CODE = #{groupCode}
        and i.APP_KEY = #{appKey}
        GROUP by
        i.INSTANCE_ID ) d,
        cb_duty_person_shift ps,
        cb_duty_shift ds
        where
        d.instanceId = ps.instance_id
        and ps.shift_id = ds.sequence_nbr
        and ps.duty_date = #{dutyDate}
        <if test="shiftId != null and shiftId != ''">
            and ps.shift_id = #{shiftId}
        </if>
        <if test="params != null and  params.size() > 0">
            <foreach collection="params" index="key" item="value" separator="">
                <choose>
                    <when test="fieldCodes[key] == 'like' and value !=null and value !=''">
                        and d.${key} like concat('%',#{value},'%')
                    </when>
                    <when test="fieldCodes[key] == 'eq' and value !=null and value !=''">
                        and d.${key} = #{value}
                    </when>
                </choose>
            </foreach>
        </if>
        order by instanceId desc
    </select>
    <select id="listOnDutyPerson" resultType="java.util.Map">
        select
        d.*,
        ps.shift_id as shiftId,
        ps.duty_date as dutyDate,
        ds.name as shiftName,
        '在岗' state
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode,
        <foreach collection="fieldCodes" item="value" index="key" separator=",">
            MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key}
        </foreach>
        from
        cb_dynamic_form_instance i
        where i.GROUP_CODE = #{groupCode}
        GROUP by
        i.INSTANCE_ID ) d,
        cb_duty_person_shift ps,
        cb_duty_shift ds
        where
        d.instanceId = ps.instance_id
        and d.deptId not in (SELECT
        `cb_org_usr`.`sequence_nbr`
        FROM
        `cb_org_usr`
        WHERE
        (
        ( `cb_org_usr`.`biz_org_code` LIKE concat( ( SELECT `cb_org_usr`.`biz_org_code` FROM `cb_org_usr` WHERE ( `cb_org_usr`.`sequence_nbr` = 1435847345328660481 ) ), '%' ) )
        AND ( `cb_org_usr`.`biz_org_type` = 'DEPARTMENT' )
        ))
        and ps.shift_id = ds.sequence_nbr
        and ps.duty_date = #{dutyDate}
        <if test="shiftId != null and shiftId != ''">
            and ps.shift_id in (#{shiftId})
        </if>
        order by instanceId desc
    </select>
    <select id="getOnDutyPersonList" resultType="java.util.Map">
        select
        d.*,
        ps.shift_id as shiftId,
        ps.duty_date as dutyDate,
        ds.name as shiftName,
        '在岗' state
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode,
        <foreach collection="fieldCodes" item="value" index="key" separator=",">
            MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key}
        </foreach>
        ,
        IFNULL(( SELECT c.field_value FROM cb_dynamic_form_instance c, `cb_org_usr` WHERE field_code = 'telephone' AND c.instance_id = `cb_org_usr`.sequence_nbr AND `cb_org_usr`.sequence_nbr = MAX(CASE WHEN i.FIELD_CODE = 'userId' THEN i.FIELD_VALUE END) ), "" ) AS telephone
        from
        cb_dynamic_form_instance i
        where i.GROUP_CODE = #{groupCode}
        GROUP by
        i.INSTANCE_ID ) d,
        cb_duty_person_shift ps,
        cb_duty_shift ds
        where
        d.instanceId = ps.instance_id
        and d.deptId not in (SELECT
        `cb_org_usr`.`sequence_nbr`
        FROM
        `cb_org_usr`
        WHERE
        (
        ( `cb_org_usr`.`biz_org_code` LIKE concat( ( SELECT `cb_org_usr`.`biz_org_code` FROM `cb_org_usr` WHERE ( `cb_org_usr`.`sequence_nbr` = 1435847345328660481 ) ), '%' ) )
        AND ( `cb_org_usr`.`biz_org_type` = 'DEPARTMENT' )
        ))
        and ps.shift_id = ds.sequence_nbr
        and ps.duty_date = #{dutyDate}
        <if test="idList != null">
            and ps.shift_id in
            <foreach item="item" collection="idList" separator="," open="(" close=")" index="">
                #{item, jdbcType=NUMERIC}
            </foreach>

        </if>
        order by instanceId desc
    </select>
    <select id="getInstanceByCodeAndValue" resultType="com.yeejoin.amos.boot.module.common.api.entity.DynamicFormInstance">
        SELECT
            f.instance_id
        FROM
            `cb_dynamic_form_instance` f
        <where>
            <if test="code != null and code != ''">
                f.field_code = #{code}
            </if>
            <if test="value != null and value != ''">
                AND f.field_value = #{value}
            </if>
        </where>
    </select>


    <!--不存在值班数据则不查找 修改sql 方法去除 by kongfm 2021-09-14-->
    <select id="pageListNew" resultType="java.util.Map">
        select
        d.*
        from
        (
        select
        i.INSTANCE_ID instanceId,
        i.GROUP_CODE groupCode
        <foreach collection="fieldCodes" item="value" index="key" >
            ,MAX(CASE WHEN i.FIELD_CODE = #{key} THEN i.FIELD_VALUE END) as ${key},
            IF(FIND_IN_SET(i.field_type,'radio,select,treeSelect'), MAX(CASE WHEN i.FIELD_CODE = #{key} THEN
            i.FIELD_VALUE_LABEL END), null) as ${key}Label
        </foreach>
        from
        cb_dynamic_form_instance i
        where
        i.GROUP_CODE = #{groupCode}
        and i.is_delete = 0
        <if test="appKey != null and appKey !=''">
            and i.APP_KEY = #{appKey}
        </if>
        <foreach collection="params" index="key" item="value" separator="">
            <if test="key != null and key == 'instanceIds' ">
                and find_in_set(i.instance_id, #{value}) > 0
            </if>
        </foreach>
        GROUP by
        i.INSTANCE_ID) d
        <if test="params != null and params.size() > 0">
            where
            d.instanceId in (
            select tt.instance_id from cb_duty_person_shift tt where tt.duty_date >= #{stratTime}
            and tt.duty_date <![CDATA[<=]]>  #{endTime}
            )
            <foreach collection="params" index="key" item="value" separator="">
                <choose>
                    <when test="fieldCodes[key] == 'like' and value !=null and value !=''">
                        and d.${key} like concat('%',#{value},'%')
                    </when>
                    <when test="fieldCodes[key] == 'eq' and value !=null and value !=''">
                        and d.${key} = #{value}
                    </when>
                    <when test="fieldCodes[key] == 'ge' and value !=null and value !=''">
                        and d.${key} >= #{value}
                    </when>
                    <when test="fieldCodes[key] == 'le' and value !=null and value !=''">
                        and d.${key} <![CDATA[<=]]> #{value}
                    </when>
                </choose>
            </foreach>
        </if>
        order by instanceId desc
    </select>
  <select id="getDutyPersonByTeamIdAndCarId" resultType="java.util.Map">
     SELECT
	cfir.telephone AS mobilePhone,
	hh.userId AS userId,
	cou.biz_org_name AS userName,
	cou.amos_org_id AS amosId
FROM
	(
		SELECT
			userId,
			instance_id
		FROM
			(
				SELECT
					MAX(
						CASE
						WHEN cbd.field_code = 'deptName' THEN
							cbd.field_value
						END
					) AS deptName,
					MAX(
						CASE
						WHEN cbd.field_code = 'userId' THEN
							cbd.field_value
						END
					) AS userId,
					cbd.instance_id
				FROM
					cb_dynamic_form_instance cbd
				LEFT JOIN cb_duty_person_shift cps ON cbd.instance_id = cps.instance_id
				WHERE
					cps.duty_date =  #{dutyDate}
				AND cps.is_delete = FALSE
				AND cbd.group_code = 'dutyPerson'
				GROUP BY
					cbd.instance_id
			) ss
		WHERE
			ss.deptName = #{departmentName}
	) hh
LEFT JOIN cb_org_usr cou ON hh.userId = cou.sequence_nbr
LEFT JOIN (
	SELECT
		MAX(
			CASE
			WHEN cfi.field_code = 'telephone' THEN
				cfi.field_value
			END
		) AS telephone,
		cfi.instance_id
	FROM
		cb_dynamic_form_instance cfi
) cfir ON cfir.instance_id = hh.userId
    </select>
      <select id="getDutyPersonByTeamIdAndAirId" resultType="java.util.Map">
     SELECT
	cfir.telephone AS mobilePhone,
	hh.userId AS userId,
	cou.biz_org_name AS userName,
	cou.amos_org_id AS amosId
FROM
	(
		SELECT
			userId
		FROM
			(
				SELECT
					MAX(
						CASE
						WHEN cbd.field_code = 'userId' THEN
							cbd.field_value
						END
					) AS userId,
					MAX(
						CASE
						WHEN cbd.field_code = 'firstAidId' THEN
							cbd.field_value
						END
					) AS firstAidId,
					cbd.instance_id
				FROM
					cb_dynamic_form_instance cbd
				LEFT JOIN cb_duty_person_shift cps ON cbd.instance_id = cps.instance_id
				WHERE
					cps.duty_date = #{dutyDate}
				AND cps.is_delete = FALSE
				AND cbd.group_code = 'dutyFirstAid'
				GROUP BY
					cbd.instance_id
			) ss
		WHERE
			ss.firstAidId = #{firstAidId}
	) hh
LEFT JOIN cb_org_usr cou ON hh.userId = cou.sequence_nbr
LEFT JOIN (
	SELECT
		MAX(
			CASE
			WHEN cfi.field_code = 'telephone' THEN
				cfi.field_value
			END
		) AS telephone,
		cfi.instance_id
	FROM
		cb_dynamic_form_instance cfi
) cfir ON cfir.instance_id = hh.userId
    </select>
    <select id ="getFireRescueDepartmentStaff" resultType="java.util.Map">
SELECT
	dd.telephone AS mobilePhone,
	cour.sequence_nbr AS userId,
	cour.biz_org_name AS userName,
	cour.amos_org_id AS amosId
FROM
	(
		SELECT
			Max(
				CASE
				WHEN cbd.field_code = 'positionType' THEN
					cbd.field_value
				END
			) AS positionType,
			Max(
				CASE
				WHEN cbd.field_code = 'telephone' THEN
					cbd.field_value
				END
			) AS telephone,
			Max(
				CASE
				WHEN cbd.field_code = 'stateCode' THEN
					cbd.field_value_label
				END
			) AS stateCode,
			cbd.instance_id
		FROM
			cb_dynamic_form_instance cbd
		WHERE
			cbd.instance_id IN (
				SELECT
					cou.sequence_nbr
				FROM
					cb_org_usr cou
				WHERE
					cou.parent_id = (
						SELECT
							sequence_nbr
						FROM
							cb_org_usr
						WHERE
							biz_org_name = #{departmentName} and is_delete = 0
						
					)
				AND cou.biz_org_type = 'PERSON'  and is_delete = 0
			) group by  cbd.instance_id
	) dd
LEFT JOIN cb_org_usr cour ON dd.instance_id = cour.sequence_nbr
WHERE
 		    <if test="positionType == null ">
                 dd.positionType IS NOT NULL
            </if>
            <if test="positionType != null">
                 dd.positionType in
					<foreach collection="positionType"  item="item" separator="," open="(" close=")" index="">
      					 #{item, jdbcType=NUMERIC}
   				  </foreach> 
            </if>
	</select>
    <select id ="getDutyFirePerson" resultType="java.util.Map">
  SELECT
	dd.telephone AS mobilePhone
FROM
	(
		SELECT
			Max(
				CASE
				WHEN cbd.field_code = 'telephone' THEN
					cbd.field_value
				END
			) AS telephone,
			cbd.instance_id
		FROM
			cb_dynamic_form_instance cbd
		WHERE
			cbd.instance_id IN (
				SELECT
					userId
				FROM
					(
						SELECT
							MAX(
								CASE
								WHEN cbd.field_code = 'deptId' THEN
									cbd.field_value
								END
							) AS deptId,
							MAX(
								CASE
								WHEN cbd.field_code = 'userId' THEN
									cbd.field_value
								END
							) AS userId,
							cbd.instance_id
						FROM
							cb_dynamic_form_instance cbd
						LEFT JOIN cb_duty_person_shift cps ON cbd.instance_id = cps.instance_id
						WHERE
							cps.duty_date =  #{dutyDate}
						AND cps.is_delete = FALSE
						AND cbd.group_code = #{groupCode}
						GROUP BY
							cbd.instance_id
					) ss
				WHERE
					ss.deptId IN (
						SELECT
							sequence_nbr
						FROM
							cb_org_usr
						WHERE
							biz_org_name = '消防救援保障部'
						AND biz_org_type = 'COMPANY'
					)
			)
	) dd
    
    </select>
    
    
    
    
    <select id='getCurentCarIsUserPhone'  resultType="java.util.Map">
   
   SELECT
	ss.telephone AS mobilePhone,
	cou.sequence_nbr AS userId,
	cou.biz_org_name AS userName,
	cou.amos_org_id AS amosId
FROM
	(
    SELECT
	field_value as telephone
	,instance_id
FROM
	cb_dynamic_form_instance
WHERE
	group_code = '246'
AND instance_id = (
	SELECT
		sequence_nbr
	FROM
		cb_org_usr
	WHERE
		amos_org_id = (
			SELECT
				amos_user_id
			FROM
				jc_user_car
			WHERE
				car_id = #{carId}
				and is_delete = 0
		)
		and is_delete = 0
) and field_code='telephone'
	) ss
LEFT JOIN cb_org_usr cou ON ss.instance_id = cou.sequence_nbr
    </select>
    
    <select id='getOrgPersonTelphone' resultType="java.util.Map">
    
 SELECT
	dd.telephone AS mobilePhone
FROM
	(
		SELECT
			Max(
				CASE
				WHEN cbd.field_code = 'administrativePositionCode' THEN
					cbd.field_value_label
				END
			) AS administrativePositionCode,
			Max(
				CASE
				WHEN cbd.field_code = 'telephone' THEN
					cbd.field_value
				END
			) AS telephone,
			Max(
				CASE
				WHEN cbd.field_code = 'stateCode' THEN
					cbd.field_value_label
				END
			) AS stateCode,
			cbd.instance_id
		FROM
			cb_dynamic_form_instance cbd
		WHERE
			cbd.instance_id IN (
				SELECT
					cou.sequence_nbr
				FROM
					cb_org_usr cou
				WHERE
					cou.parent_id in  
					<foreach item="item" collection="companyIds" separator="," open="(" close=")" index="">
      					 #{item.departmentId, jdbcType=NUMERIC}
   				  </foreach> 
				AND cou.biz_org_type = 'PERSON'
			)group by  cbd.instance_id
	) dd
WHERE
	dd.administrativePositionCode IS NOT NULL    
    </select>
    <select id="personInfoList" resultType="java.util.Map">
        SELECT
            i.instance_id AS userId,
            MAX( CASE WHEN i.field_code = 'positionType' THEN IFNULL(i.field_value,'') END ) AS positionType,
            MAX( CASE WHEN i.field_code = 'personImg' THEN IFNULL(i.field_value,'') END ) AS personImg,
            MAX( CASE WHEN i.field_code = 'telephone' THEN IFNULL(i.field_value,'') END ) AS telephone
        FROM
            `cb_dynamic_form_instance` i
        <where>
            <if test="groupCode != null and groupCode != ''">
                group_code = #{groupCode}
            </if>
            <if test="userIdSet != null and userIdSet.size() >0">
                AND i.instance_id IN
                <foreach collection="userIdSet" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
            i.instance_id
    </select>

    <select id="getPersonInfoByPage" resultType="java.util.Map">
        SELECT
            ou.sequence_nbr as id,
            ou.biz_org_name as userName,
            IFNULL( MAX( CASE WHEN cfi.field_code = 'telephone' THEN field_value END ), '' ) AS telephone,
            IF(MAX( CASE WHEN cfi.field_code = 'positionType' THEN field_value_label END ) = '', NULL, MAX( CASE WHEN cfi.field_code = 'positionType' THEN field_value_label END )) AS postTypeName,
            IFNULL( MAX( CASE WHEN cfi.field_code = 'personImg' THEN field_value END ), '' ) AS personImg,
            IFNULL( MAX( CASE WHEN cfi.field_code = 'peopleType' THEN field_value END ), '' ) AS peopleType
        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 cb_dynamic_form_instance cfi ON ou.sequence_nbr = cfi.instance_id
            where ou.biz_org_code like concat(#{bizOrgCode}, '%')
        GROUP BY
            a.id
        HAVING
            peopleType = 1602
    </select>

    <select id="dutyCardCount" resultType="java.util.Map">

        SELECT
            'fire' as peopleType,
            count(*) as total
        from
            (SELECT
                 c.sequence_nbr as id,
                 c.biz_org_name as userName,
                 IFNULL( cf.mobile_phone, '' ) telephone,
                 '驻站消防员' as peopleType
             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 ( ), '%' )
             GROUP BY
                 cs.USER_ID
             ORDER BY
                 cs.sign_time DESC) d
        union all
        SELECT
            (CASE b.peopleType
                 WHEN 1602 THEN
                     'run'
                 ELSE
                     'overhaul'
                END) peopleType,
            count( * ) as total
        FROM
            (
                SELECT
                    ou.sequence_nbr AS id,
                    ou.biz_org_name AS userName,
                    IFNULL( MAX( CASE WHEN cfi.field_code = 'telephone' THEN field_value END ), '' ) AS telephone,
                    IF
                        (
                                MAX( CASE WHEN cfi.field_code = 'positionType' THEN field_value_label END ) = '',
                                NULL,
                                MAX( CASE WHEN cfi.field_code = 'positionType' THEN field_value_label END )
                        ) AS postTypeName,
                    IFNULL( MAX( CASE WHEN cfi.field_code = 'personImg' THEN field_value END ), '' ) AS personImg,
                    IFNULL( MAX( CASE WHEN cfi.field_code = 'peopleType' THEN field_value END ), '' ) AS peopleType
                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 cb_dynamic_form_instance cfi ON ou.sequence_nbr = cfi.instance_id
                GROUP BY
                    a.id
            ) b  WHERE  b.peopleType in ('1602','1604')
        GROUP BY
            peopleType
    </select>

    <select id="dutyCardList" resultType="java.util.Map">
        (SELECT
             c.sequence_nbr as id,
             c.biz_org_name as userName,
             IFNULL( cf.mobile_phone, '' ) telephone,
             '1601' as peopleType
         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 ( ), '%' )
         GROUP BY
             cs.USER_ID
         ORDER BY
             cs.sign_time DESC)
        UNION all
        (SELECT
             id,
             userName,
             telephone,
             peopleType
         FROM
             (
                 SELECT
                     ou.sequence_nbr AS id,
                     ou.biz_org_name AS userName,
                     IFNULL( MAX( CASE WHEN cfi.field_code = 'telephone' THEN field_value END ), '' ) AS telephone,
                     IF
                         (
                                 MAX( CASE WHEN cfi.field_code = 'positionType' THEN field_value_label END ) = '',
                                 NULL,
                                 MAX( CASE WHEN cfi.field_code = 'positionType' THEN field_value_label END )
                         ) AS postTypeName,
                     IFNULL( MAX( CASE WHEN cfi.field_code = 'personImg' THEN field_value END ), '' ) AS personImg,
                     IFNULL( MAX( CASE WHEN cfi.field_code = 'peopleType' THEN field_value END ), '' ) AS peopleType
                 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 cb_dynamic_form_instance cfi ON ou.sequence_nbr = cfi.instance_id
                 GROUP BY
                     a.id
             ) b WHERE b.peopleType in ('1602','1604'))
    </select>
</mapper>
