<?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.lantone.mapper.cdss.MappingConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lantone.entity.cdss.MappingConfig">
        <id column="id" property="id" />
        <result column="is_deleted" property="isDeleted" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
        <result column="creator" property="creator" />
        <result column="modifier" property="modifier" />
        <result column="hospital_id" property="hospitalId" />
        <result column="type" property="type" />
        <result column="his_name" property="hisName" />
        <result column="his_code" property="hisCode" />
        <result column="his_detail_name" property="hisDetailName" />
        <result column="concept_id" property="conceptId" />
        <result column="form_concept_id" property="formConceptId" />
        <result column="is_match" property="isMatch" />
        <result column="source" property="source" />
        <result column="remark" property="remark" />
    </resultMap>

    <!-- 分页查询 -->
    <select id="getPage" resultType="com.lantone.entity.cdss.wrapper.MappingConfigWrapper">
        select t1.*
        from
        (select a.*
        from tran_mapping_config a
        where a.is_deleted='N'
        <if test="mappingConfigPageVO.hospitalId!=null">
            and a.hospital_id=#{mappingConfigPageVO.hospitalId}
        </if>
        <if test="mappingConfigPageVO.type!=null">
            and a.type=#{mappingConfigPageVO.type}
        </if>
        <if test="mappingConfigPageVO.hisName!=null and mappingConfigPageVO.hisName!=''">
            and a.his_name like concat("%",#{mappingConfigPageVO.hisName},"%")
        </if>
        <if test="mappingConfigPageVO.hisCode!=null and mappingConfigPageVO.hisCode!=''">
            and a.his_code like concat("%",#{mappingConfigPageVO.hisCode},"%")
        </if>
        <if test="mappingConfigPageVO.hisDetailName!=null and mappingConfigPageVO.hisDetailName!=''">
            and a.his_detail_name like concat("%",#{mappingConfigPageVO.hisDetailName},"%")
        </if>
        <if test="mappingConfigPageVO.conceptIds!=null and mappingConfigPageVO.conceptIds.size()>0">
            and a.concept_id in
            <foreach item="conceptId" collection="mappingConfigPageVO.conceptIds" open="(" separator="," close=")">
                #{conceptId}
            </foreach>
        </if>
        <if test="mappingConfigPageVO.notInConceptIds!=null and mappingConfigPageVO.notInConceptIds.size()>0">
            and a.concept_id is not null
            and a.concept_id not in
            <foreach item="notInConceptId" collection="mappingConfigPageVO.notInConceptIds" open="(" separator="," close=")">
                #{notInConceptId}
            </foreach>
        </if>
        <if test="mappingConfigPageVO.formConceptIds!=null and mappingConfigPageVO.formConceptIds.size()>0">
            and a.form_concept_id in
            <foreach item="formConceptId" collection="mappingConfigPageVO.formConceptIds" open="(" separator=","
                     close=")">
                #{formConceptId}
            </foreach>
        </if>
        <if test="mappingConfigPageVO.notInFormConceptIds!=null and mappingConfigPageVO.notInFormConceptIds.size()>0">
            and a.form_concept_id is not null
            and a.form_concept_id in
            <foreach item="notInFormConceptId" collection="mappingConfigPageVO.notInFormConceptIds" open="(" separator=","
                     close=")">
                #{notInFormConceptId}
            </foreach>
        </if>
        <if test="mappingConfigPageVO.isMatch!=null">
            and a.is_match=#{mappingConfigPageVO.isMatch}
        </if>
        )t1
        <if test="mappingConfigPageVO.isMultiple!=null and mappingConfigPageVO.isMultiple==1">
            ,(select a.his_name,a.his_detail_name,count(*)
            from tran_mapping_config a
            where a.is_deleted='N'
            <if test="mappingConfigPageVO.hospitalId!=null">
                and a.hospital_id=#{mappingConfigPageVO.hospitalId}
            </if>
            <if test="mappingConfigPageVO.type!=null">
                and a.type=#{mappingConfigPageVO.type}
            </if>
            <if test="mappingConfigPageVO.hisName!=null and mappingConfigPageVO.hisName!=''">
                and a.his_name like concat("%",#{mappingConfigPageVO.hisName},"%")
            </if>
            <if test="mappingConfigPageVO.hisCode!=null and mappingConfigPageVO.hisCode!=''">
                and a.his_code like concat("%",#{mappingConfigPageVO.hisCode},"%")
            </if>
            <if test="mappingConfigPageVO.hisDetailName!=null and mappingConfigPageVO.hisDetailName!=''">
                and a.his_detail_name like concat("%",#{mappingConfigPageVO.hisDetailName},"%")
            </if>
            <if test="mappingConfigPageVO.conceptIds!=null and mappingConfigPageVO.conceptIds.size()>0">
                and a.concept_id in
                <foreach item="conceptId" collection="mappingConfigPageVO.conceptIds" open="(" separator="," close=")">
                    #{conceptId}
                </foreach>
            </if>
            <if test="mappingConfigPageVO.notInConceptIds!=null and mappingConfigPageVO.notInConceptIds.size()>0">
                and a.concept_id is not null
                and a.concept_id not in
                <foreach item="notInConceptId" collection="mappingConfigPageVO.notInConceptIds" open="(" separator="," close=")">
                    #{notInConceptId}
                </foreach>
            </if>
            <if test="mappingConfigPageVO.formConceptIds!=null and mappingConfigPageVO.formConceptIds.size()>0">
                and a.form_concept_id in
                <foreach item="formConceptId" collection="mappingConfigPageVO.formConceptIds" open="(" separator=","
                         close=")">
                    #{formConceptId}
                </foreach>
            </if>
            <if test="mappingConfigPageVO.notInFormConceptIds!=null and mappingConfigPageVO.notInFormConceptIds.size()>0">
                and a.form_concept_id is not null
                and a.form_concept_id in
                <foreach item="notInFormConceptId" collection="mappingConfigPageVO.notInFormConceptIds" open="(" separator=","
                         close=")">
                    #{notInFormConceptId}
                </foreach>
            </if>
            group by a.his_name
            <if test="mappingConfigPageVO.type!=null and mappingConfigPageVO.type==1">
                ,a.his_detail_name
            </if>
            having count(*)>1
            ) t2
            where t1.his_name=t2.his_name
            <if test="mappingConfigPageVO.type!=null and mappingConfigPageVO.type==1">
                and t1.his_detail_name=t2.his_detail_name
            </if>
        </if>
        order by
        <if test="mappingConfigPageVO.isMultiple!=null and mappingConfigPageVO.isMultiple==1">
            t1.his_name asc,
            <if test="mappingConfigPageVO.type!=null and mappingConfigPageVO.type==1">
                t1.his_detail_name asc,
            </if>
        </if>
        t1.is_match desc,t1.gmt_modified desc
    </select>

    <select id="hisNameIndex" resultType="com.lantone.entity.cdss.MappingConfig"
            parameterType="com.lantone.vo.cdss.RetrievalVO">
        SELECT DISTINCT u.his_name
        FROM(
        SELECT
        DISTINCT his_name
        FROM
        `tran_mapping_config`
        WHERE
        is_deleted = "N"
        AND (his_detail_name IS NULL OR his_detail_name = '')
        <if test="hospitalId!=null">
            and hospital_id=#{hospitalId}
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="inputStr!=null and inputStr!=''">
            and his_name = #{inputStr}
        </if>
        UNION
        SELECT
        DISTINCT his_name
        FROM
        `tran_mapping_config`
        WHERE
        is_deleted = "N"
        AND (his_detail_name IS NULL OR his_detail_name = '')
        <if test="hospitalId!=null">
            and hospital_id=#{hospitalId}
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="inputStr!=null and inputStr!=''">
            and his_name like concat(#{inputStr},"%")
        </if>
        UNION
        SELECT
        DISTINCT his_name
        FROM
        `tran_mapping_config`
        WHERE
        is_deleted = "N"
        AND (his_detail_name IS NULL OR his_detail_name = '')
        <if test="hospitalId!=null">
            and hospital_id=#{hospitalId}
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="inputStr!=null and inputStr!=''">
            and his_name like concat("%",#{inputStr},"%")
        </if>
        ) u LIMIT 100
    </select>

    <select id="hisDetailNameIndex" resultType="com.lantone.entity.cdss.MappingConfig"
            parameterType="com.lantone.vo.cdss.RetrievalVO">
        SELECT DISTINCT u.his_name,u.his_detail_name,max(u.concept_id) as concept_id
        FROM(
        SELECT
        DISTINCT his_name,his_detail_name,concept_id
        FROM
        `tran_mapping_config`
        WHERE
        is_deleted = "N"
        <if test="hospitalId!=null">
            and hospital_id=#{hospitalId}
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="inputStr!=null and inputStr!=''">
            and his_detail_name = #{inputStr}
        </if>
        UNION
        SELECT
        DISTINCT his_name,his_detail_name,concept_id
        FROM
        `tran_mapping_config`
        WHERE
        is_deleted = "N"
        <if test="hospitalId!=null">
            and hospital_id=#{hospitalId}
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="inputStr!=null and inputStr!=''">
            and his_detail_name like concat(#{inputStr},"%")
        </if>
        UNION
        SELECT
        DISTINCT his_name,his_detail_name,concept_id
        FROM
        `tran_mapping_config`
        WHERE
        is_deleted = "N"
        <if test="hospitalId!=null">
            and hospital_id=#{hospitalId}
        </if>
        <if test="type!=null">
            and type=#{type}
        </if>
        <if test="inputStr!=null and inputStr!=''">
            and his_detail_name like concat("%",#{inputStr},"%")
        </if>
        ) u
        GROUP BY
        u.his_name,
        u.his_detail_name
        LIMIT 100
    </select>
</mapper>
