<?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.fs.doctor.mapper.DoctorLevelMapper">
    
    <resultMap type="DoctorLevel" id="DoctorLevelResult">
        <result property="id"    column="id"    />
        <result property="doctorId"    column="doctor_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="productGroup"    column="product_group"    />
        <result property="level"    column="level"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deleted"    column="deleted"    />
        <result property="doctorName"  column="doctor_name" />
        <result property="companyName" column="company_name" />
    </resultMap>

    <sql id="selectDoctorLevelVo">
        SELECT
        dl.id,
        dl.doctor_id,
        dl.company_id,
        dl.product_group,
        dl.level,
        dl.create_time,
        dl.update_time,
        dl.deleted,

        d.doctor_name        AS doctor_name,
        c.company_name       AS company_name

        FROM doctor_level dl
        LEFT JOIN doctor d
        ON d.doctor_id = dl.doctor_id
        LEFT JOIN company c
        ON c.company_id = dl.company_id
    </sql>

    <select id="selectDoctorLevelList" parameterType="DoctorLevel" resultMap="DoctorLevelResult">
        <include refid="selectDoctorLevelVo"/>
        <where>
            <if test="doctorId != null and doctorId != ''"> AND dl.doctor_id = #{doctorId}</if>
            <if test="companyId != null"> AND dl.company_id = #{companyId}</if>
            <if test="productGroup != null and productGroup != ''"> AND dl.product_group = #{productGroup}</if>
            <if test="level != null and level != ''"> AND dl.level = #{level}</if>
            <if test="deleted != null"> AND dl.deleted = #{deleted}</if>
        </where>
        order by dl.create_time desc

    </select>
    
    <select id="selectDoctorLevelById" parameterType="Long" resultMap="DoctorLevelResult">
        <include refid="selectDoctorLevelVo"/>
        WHERE dl.id = #{id}
    </select>
        
    <insert id="insertDoctorLevel" parameterType="DoctorLevel" useGeneratedKeys="true" keyProperty="id">
        insert into doctor_level
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">doctor_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="productGroup != null">product_group,</if>
            <if test="level != null">level,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">#{doctorId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="productGroup != null">#{productGroup},</if>
            <if test="level != null">#{level},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateDoctorLevel" parameterType="DoctorLevel">
        update doctor_level
        <trim prefix="SET" suffixOverrides=",">
            <if test="doctorId != null and doctorId != ''">doctor_id = #{doctorId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="productGroup != null">product_group = #{productGroup},</if>
            <if test="level != null">level = #{level},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDoctorLevelById" parameterType="Long">
        delete from doctor_level where id = #{id}
    </delete>

    <delete id="deleteDoctorLevelByIds" parameterType="String">
        delete from doctor_level where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



    <select id="selectDoctorLevelsByDoctorIds" resultType="com.fs.doctor.domain.DoctorLevel">
        select
        id,
        doctor_id,
        level,
        create_time,
        update_time
        from doctor_level
        where doctor_id in
        <foreach item="doctorId" collection="doctorIds" open="(" separator="," close=")">
            #{doctorId}
        </foreach>
    </select>
</mapper>