<?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.ruoyi.system.mapper.TDoctorMapper">
    
    <resultMap type="TDoctor" id="TDoctorResult">
        <result property="id"    column="id"    />
        <result property="openId"    column="open_id"    />
        <result property="userId"    column="user_id"    />
        <result property="identifier"    column="identifier"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="sex"    column="sex"    />
        <result property="age"    column="age"    />
        <result property="marriage"    column="marriage"    />
        <result property="title"    column="title"    />
        <result property="speciality"    column="speciality"    />
        <result property="introduction"    column="introduction"    />
        <result property="medicalLicense"    column="medical_license"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="hospitalId"    column="hospital_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTDoctorVo">
        select id, open_id, user_id, identifier, name, phone, sex, age, marriage, title, speciality, introduction, medical_license, qr_code, hospital_id, del_flag, create_time, update_time from t_doctor
    </sql>

    <select id="selectTDoctorList" parameterType="TDoctor" resultMap="TDoctorResult">
        <include refid="selectTDoctorVo"/>
        <where>  
            <if test="openId != null  and openId != ''"> and open_id = #{openId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="identifier != null  and identifier != ''"> and identifier = #{identifier}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="marriage != null  and marriage != ''"> and marriage = #{marriage}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="speciality != null  and speciality != ''"> and speciality = #{speciality}</if>
            <if test="introduction != null  and introduction != ''"> and introduction = #{introduction}</if>
            <if test="medicalLicense != null  and medicalLicense != ''"> and medical_license = #{medicalLicense}</if>
            <if test="qrCode != null  and qrCode != ''"> and qr_code = #{qrCode}</if>
            <if test="hospitalId != null "> and hospital_id = #{hospitalId}</if>
        </where>
    </select>
    
    <select id="selectTDoctorById" parameterType="Long" resultMap="TDoctorResult">
        <include refid="selectTDoctorVo"/>
        where id = #{id}
    </select>

    <select id="selectTDoctorByPhone" parameterType="java.lang.String" resultMap="TDoctorResult">
        <include refid="selectTDoctorVo"/>
        where phone = #{phone}
    </select>

    <select id="selectTDoctorByUserId" parameterType="Long" resultMap="TDoctorResult">
        <include refid="selectTDoctorVo"/>
        where user_id = #{userId}
    </select>
        
    <insert id="insertTDoctor" parameterType="TDoctor" useGeneratedKeys="true" keyProperty="id">
        insert into t_doctor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="openId != null">open_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="identifier != null">identifier,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="phone != null">phone,</if>
            <if test="sex != null">sex,</if>
            <if test="age != null">age,</if>
            <if test="marriage != null">marriage,</if>
            <if test="title != null">title,</if>
            <if test="speciality != null">speciality,</if>
            <if test="introduction != null">introduction,</if>
            <if test="medicalLicense != null">medical_license,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="hospitalId != null">hospital_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="openId != null">#{openId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="identifier != null">#{identifier},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="phone != null">#{phone},</if>
            <if test="sex != null">#{sex},</if>
            <if test="age != null">#{age},</if>
            <if test="marriage != null">#{marriage},</if>
            <if test="title != null">#{title},</if>
            <if test="speciality != null">#{speciality},</if>
            <if test="introduction != null">#{introduction},</if>
            <if test="medicalLicense != null">#{medicalLicense},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="hospitalId != null">#{hospitalId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTDoctor" parameterType="TDoctor">
        update t_doctor
        <trim prefix="SET" suffixOverrides=",">
            <if test="openId != null">open_id = #{openId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="identifier != null">identifier = #{identifier},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="age != null">age = #{age},</if>
            <if test="marriage != null">marriage = #{marriage},</if>
            <if test="title != null">title = #{title},</if>
            <if test="speciality != null">speciality = #{speciality},</if>
            <if test="introduction != null">introduction = #{introduction},</if>
            <if test="medicalLicense != null">medical_license = #{medicalLicense},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="hospitalId != null">hospital_id = #{hospitalId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTDoctorById" parameterType="Long">
        update t_doctor set del_flag = '2' where id = #{id}
    </delete>

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

    <select id="queryByPage" resultType="com.ruoyi.system.domain.resp.PcTDoctorQueryByPageResp">
        select
        id,
        user_id as userId,
        identifier,
        name,
        phone,
        sex,
        age,
        marriage,
        title,
        speciality,
        introduction,
        medical_license as medicalLicense,
        qr_code as qrCode,
        hospital_id as hospitalId,
        create_time as createTime,
        update_time as updateTime
        from
        t_doctor
        <where>
            <if test="startTime != null and endTime != null">
                AND create_time between DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%s') and DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="name != null and name != ''">
                AND name like concat('%', #{name}, '%')
            </if>
            <if test="identifier != null and identifier != ''">
                AND identifier like concat('%', #{identifier}, '%')
            </if>
            <if test="hospitalId != null">
                and hospital_id = #{hospitalId}
            </if>
            <if test="delFlag != null">
                and del_flag = #{delFlag}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="queryPatintCountByDoctor" resultType="int">
        select COUNT(*) from t_patient_hospital_doctor where hospital_id = #{hospitalId} and doctor_id = #{doctorId}
    </select>

    <select id="selectAppDoctorDetailById" resultType="com.ruoyi.system.domain.resp.AppDoctorResp">
        SELECT
            td.id,
            user_id AS userId,
            identifier,
            td.`name`,
            th.`id` AS hospitalId,
            th.`name` AS hospitalName,
            phone,
            sex,
            age,
            marriage,
            title,
            speciality,
            introduction,
            medical_license AS medicalLicense,
            qr_code AS qrCode,
            hospital_id AS hospitalId
        FROM
            t_doctor td
                LEFT JOIN t_hospital th ON th.id = td.hospital_id
        WHERE td.id = #{id}
    </select>
</mapper>