<?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="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 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="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 = 'administrativePositionCode' THEN
					cbd.field_value
				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 = (
						SELECT
							sequence_nbr
						FROM
							cb_org_usr
						WHERE
							biz_org_name = #{departmentName} and is_delete = 0
						
					)
				AND cou.biz_org_type = 'PERSON'
			) 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.administrativePositionCode IS NOT NULL
            </if>
            <if test="positionType != null">
                 dd.administrativePositionCode 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 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>
    
</mapper>
