<?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.hz.pm.api.expert.mapper.ExpertAdminManageMapper">

    <sql id="Expert_Full_Info_Region">
        SELECT user_id FROM nd_expert_user_full_info
        <where>
            <if test="query.expertName != null and query.expertName !='' ">
                AND expert_name LIKE CONCAT('%',#{query.expertName, jdbcType=BIGINT},'%')
            </if>
            <if test="query.company != null and query.company !='' ">
                AND company LIKE CONCAT('%',#{query.company, jdbcType=BIGINT},'%')
            </if>
            <if test="query.isDingUser != null and query.isDingUser == true ">
                AND is_ding_user = 'Y'
            </if>
            <if test="query.isDingUser != null and query.isDingUser == false ">
                AND is_ding_user = 'N'
            </if>
        </where>
        ORDER BY update_on DESC
    </sql>

    <sql id="Expert_Tag_Relation">
        SELECT
        user_id
        FROM
        (
        SELECT DISTINCT
        et.user_id,
        et.expert_info_field
        FROM
        expert_tag et
        WHERE
        <foreach collection="query.expertTagQueryList" item="expertTagQuery" index="index" separator="OR">
            (et.expert_info_field = #{expertTagQuery.expertInfoField}
            AND et.tag_code IN
            <foreach collection="expertTagQuery.tagCodeList" item="tagCode" index="index" open="(" close=")"
                     separator=",">
                #{tagCode}
            </foreach>)
        </foreach>
        ) temp
        GROUP BY
        user_id
        HAVING
        count( user_id )= ${query.expertTagQueryList.size()}
    </sql>


    <sql id="Expert_Dictionary_Relation">
        SELECT
        user_id
        FROM
        (
        SELECT DISTINCT
        ed.user_id,
        ed.expert_info_field
        FROM
        expert_dictionary ed
        WHERE
        <foreach collection="query.expertDictionaryQueryList" item="expertDictionaryQuery" index="index" separator="OR">
            (ed.expert_info_field = #{expertDictionaryQuery.expertInfoField}
            AND ed.dictionary_code IN
            <foreach collection="expertDictionaryQuery.dictionaryCodeList" item="dictionaryCode" index="index" open="("
                     close=")"
                     separator=",">
                #{dictionaryCode}
            </foreach>)
        </foreach>
        ) temp
        GROUP BY
        user_id
        HAVING
        count( user_id )= ${query.expertDictionaryQueryList.size()}
    </sql>


    <select id="listExpertUserId" resultType="java.lang.Long"
            parameterType="com.hz.pm.api.expert.model.query.ListExpertQuery">
        <include refid="Expert_Full_Info_Region"/>
    </select>

    <select id="listExpertDictionaryUserId" resultType="java.lang.Long"
            parameterType="com.hz.pm.api.expert.model.query.ListExpertQuery">
        <include refid="Expert_Dictionary_Relation"/>
    </select>

    <select id="listExpertTagUserId" resultType="java.lang.Long"
            parameterType="com.hz.pm.api.expert.model.query.ListExpertQuery">
        <include refid="Expert_Tag_Relation"/>
    </select>

</mapper>
