<?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.nantian.cloud.jljc.mapper.TExpertsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.jljc.entity.TExperts">
        <id column="id" property="id"/>
        <result column="userid" property="userid"/>
        <result column="update_time" property="updateTime"/>
        <result column="name" property="name"/>
        <result column="telephone" property="telephone"/>
        <result column="organization" property="organization"/>
        <result column="idtype" property="idtype"/>
        <result column="idnumber" property="idnumber"/>
        <result column="industry_code" property="industryCode"/>
        <result column="del_flag" property="delFlag"/>
        <result column="remarks" property="remarks"/>
        <result column="state" property="state"/>
        <result column="industry_name" property="industryName"/>
        <result column="type" property="type"/>
        <result column="dept_id" property="deptId"/>
        <result column="sex" property="sex"/>
        <result column="position" property="position"/>
        <result column="profession" property="profession"/>
        <result column="label" property="label"/>
        <result column="birthday" property="birthday"/>
        <result column="job_title" property="jobTitle"/>
        <result column="qualification_name" property="qualificationName"/>
        <result column="subordinate_libary" property="subordinateLibary"/>
    </resultMap>
<!--
            id,
            userid,
            update_time,
            name,
            telephone,
            organization,
            idtype,
            idnumber,
            industry_code,
            del_flag,
            remarks,
            state,
            industry_name,
            type,
            dept_id,
            sex,
            position,
            profession,
            label,
            birthday

-->

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
            id,
            userid,
            update_time,
            name,
            telephone,
            organization,
            idtype,
            idnumber,
            industry_code,
            del_flag,
            remarks,
            state,
            industry_name,
            type,
            dept_id,
            sex,
            position,
            profession,
            label,
            birthday,
            job_title,
            qualification_name,
            subordinate_libary,
            serial_num
    </sql>

    <insert id="addExperts">
        insert into t_experts
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userid != null">
                userid,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="telephone != null">
                telephone,
            </if>
            <if test="organization != null">
                organization,
            </if>
            <if test="idtype != null">
                idtype,
            </if>
            <if test="idnumber != null">
                idnumber,
            </if>
            <if test="industryCode != null">
                industry_code,
            </if>
            <if test="industryName != null">
                industry_name,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
            <if test="remarks != null">
                remarks,
            </if>
            <if test="state != null">
                state,
            </if>
            <if test="type != null">
                type,
            </if>
            <if test="deptId !=null">
                dept_id,
            </if>
            <if test="sex !=null">
                sex,
            </if>
            <if test="position !=null">
                position,
            </if>
            <if test="profession !=null">
                profession,
            </if>
            <if test="label !=null">
                label,
            </if>
            <if test="birthday !=null">
                birthday,
            </if>
            <if test="jobTitle !=null">
                job_title,
            </if>
            <if test="qualificationName !=null">
                qualification_name,
            </if>
            <if test="subordinateLibary !=null">
                subordinate_libary,
            </if>
            <if test="serialNum !=null">
                serial_num,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userid != null">
                #{userid},
            </if>
            <if test="updateTime != null">
                #{updateTime},
            </if>
            <if test="name != null">
                #{name},
            </if>
            <if test="telephone != null">
                #{telephone},
            </if>
            <if test="organization != null">
                #{organization},
            </if>
            <if test="idtype != null">
                #{idtype},
            </if>
            <if test="idnumber != null">
                #{idnumber},
            </if>
            <if test="industryCode != null">
                #{industryCode},
            </if>
            <if test="industryName != null">
                #{industryName},
            </if>
            <if test="delFlag != null">
                #{delFlag},
            </if>
            <if test="remarks != null">
                #{remarks},
            </if>
            <if test="state != null">
                #{state},
            </if>
            <if test="type != null">
                #{type},
            </if>
            <if test="deptId !=null">
                #{deptId},
            </if>
            <if test="sex !=null">
                #{sex},
            </if>
            <if test="position !=null">
                #{position},
            </if>
            <if test="profession !=null">
                #{profession},
            </if>
            <if test="label !=null">
                #{label},
            </if>
            <if test="birthday !=null">
                #{birthday},
            </if>
            <if test="jobTitle !=null">
                #{jobTitle},
            </if>
            <if test="qualificationName !=null">
                #{qualificationName},
            </if>
            <if test="subordinateLibary !=null">
                #{subordinateLibary},
            </if>
            <if test="serialNum !=null">
                #{serialNum},
            </if>
        </trim>
    </insert>

    <update id="updateExpertsById">
        update t_experts
        <set>
            <if test="userid != null">
                userid = #{userid},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="telephone != null">
                telephone = #{telephone},
            </if>
            <if test="organization != null">
                organization = #{organization},
            </if>
            <if test="idtype != null">
                idtype = #{idtype},
            </if>
            <if test="idnumber != null">
                idnumber = #{idnumber},
            </if>
            <if test="industryCode != null">
                industry_code = #{industryCode},
            </if>
            <if test="industryName != null">
                industry_name = #{industryName},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag},
            </if>
            <if test="remarks != null">
                remarks = #{remarks},
            </if>
            <if test="state != null">
                state = #{state},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="deptId !=null">
                dept_id = #{deptId},
            </if>
            <if test="sex !=null">
                sex = #{sex},
            </if>
            <if test="position !=null">
                position = #{position},
            </if>
            <if test="profession !=null">
                profession = #{profession},
            </if>
            <if test="label !=null">
                label = #{label},
            </if>
            <if test="birthday !=null">
                birthday =  #{birthday},
            </if>
            <if test="jobTitle !=null">
               job_title = #{jobTitle},
            </if>
            <if test="qualificationName !=null">
                qualification_name = #{qualificationName},
            </if>
            <if test="subordinateLibary !=null">
                subordinate_libary = #{subordinateLibary},
            </if>
            <if test="serialNum !=null">
                serial_num = #{serialNum},
            </if>
        </set>
        where id = #{id}
    </update>

    <delete id="deleteExpertsById">
        update t_experts
        <set>
            <if test="userid != null">
                userid = #{userid},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag},
            </if>
        </set>
        where id = #{id}
    </delete>

    <select id="getExpertsByPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from t_experts
        where del_flag = "1"
        <if test="query.name!=null and query.name!=''">
            and name like concat("%",#{query.name},"%")
        </if>
        <if test="query.type!=null and query.type!=''">
            and type = #{query.type}
        </if>
        <if test="query.industryCode!=null and query.industryCode!=''">
            and industry_code like concat("%",#{query.industryCode},"%")
        </if>
        <if test="query.industryName!=null and query.industryName!=''">
            and industry_name like concat("%",#{query.industryName},"%")
        </if>
        <if test="query.position!=null and query.position!=''">
            and position like concat("%",#{query.position},"%")
        </if>
        <if test="query.profession!=null and query.profession!=''">
            and profession like concat("%",#{query.profession},"%")
        </if>
        <if test="query.label!=null and query.label!=''">
            and label like concat("%",#{query.label},"%")
        </if>
        <if test="query.jobTitle !=null and query.label!=''">
            and job_title like concat("%",#{query.jobTitle},"%")
        </if>
        <if test="query.organization !=null and query.label!=''">
            and organization like concat("%",#{query.organization},"%")
        </if>
    </select>

    <select id="getExperts" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from t_experts
        where del_flag = "1"
        <if test="type!=null and type!=''">
            and type = #{type}
        </if>
        <if test="industryCode!=null and industryCode!=''">
            and industry_code like concat("%",#{industryCode},"%")
        </if>
    </select>

    <select id="getExpertsById" resultType="com.nantian.cloud.jljc.entity.TExperts">
        select
        <include refid="Base_Column_List"></include>
        from t_experts
        where id = #{id}
    </select>

    <!--<select id="getRandomExperts" resultType="com.nantian.cloud.jljc.entity.TExperts">
        <if test="len=='0'.toString()">
            select <include refid="Base_Column_List"></include>
            from t_experts where del_flag = "1"
            order by rand() limit #{num}
        </if>
        <if test="len=='1'.toString()">
            select <include refid="Base_Column_List"></include>
            from (
                select <include refid="Base_Column_List"></include> ,GROUP_CONCAT(profession,job_title,organization,industry_name) str
                from t_experts
                where del_flag = "1" group by id
            ) a where a.str like concat("%",#{query.position},"%")
            order by rand() limit #{num}
        </if>
        <if test="len=='2'.toString()">
            select DISTINCT <include refid="Base_Column_List"></include>
            from (
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",job_title,"%")
                and #{query.position} like concat("%",profession,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",job_title,"%")
                and #{query.position} like concat("%",organization,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",profession,"%")
                and #{query.position} like concat("%",organization,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",industry_name,"%")
                and #{query.position} like concat("%",job_title,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",industry_name,"%")
                and #{query.position} like concat("%",profession,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",industry_name,"%")
                and #{query.position} like concat("%",organization,"%")
            ) t order by rand() limit #{num}
        </if>
        <if test="len=='3'.toString()">
            select DISTINCT <include refid="Base_Column_List"></include>
            from (
            <if test="query.position != '' and query.position != null ">
                and #{query.position} like concat("%",organization,"%")
                and #{query.position} like concat("%",profession,"%")
                and #{query.position} like concat("%",job_title,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",industry_name,"%")
                and #{query.position} like concat("%",job_title,"%")
                and #{query.position} like concat("%",organization,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",industry_name,"%")
                and #{query.position} like concat("%",job_title,"%")
                and #{query.position} like concat("%",profession,"%")
                UNION
                select <include refid="Base_Column_List"></include>
                from t_experts where del_flag = "1"
                and #{query.position} like concat("%",industry_name,"%")
                and #{query.position} like concat("%",organization,"%")
                and #{query.position} like concat("%",profession,"%")
            </if>
            ) t order by rand() limit #{num}
        </if>
        <if test="len=='4'.toString()">
            select <include refid="Base_Column_List"></include>
            from t_experts
            where del_flag = "1"
          <if test="query.position != '' and query.position != null ">
            and #{query.position} like concat("%",organization,"%")
            and #{query.position} like concat("%",profession,"%")
            and #{query.position} like concat("%",job_title,"%")
            and #{query.position} like concat("%",industry_name,"%")
          </if>
            order by rand() limit #{num}
        </if>
    </select>-->

    <select id="getRandomExperts" resultType="com.nantian.cloud.jljc.entity.TExperts">
        select <include refid="Base_Column_List"></include>
        from t_experts where del_flag = "1"
        <if test="list != null and list.length > 0">
            <foreach collection="list" index="i" item="item">
                and ( industry_name like concat("%",#{item},"%")
                or job_title like concat("%",#{item},"%")
                or profession like concat("%",#{item},"%")
                or organization like concat("%",#{item},"%")  )
            </foreach>
        </if>
        order by rand() limit #{num}
    </select>
</mapper>
