<?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.kidgrow.usercenter.mapper.SysUserMapper">
    <sql id="where">
        <where>
            t.is_del=0
            <if test="u.id != null and u.id != ''">
                and t.id = #{u.id}
            </if>
            <if test="u.username != null and u.username != ''">
                and t.username like concat('%', #{u.username}, '%')
            </if>
            <if test="u.nickname != null and u.nickname != ''">
                and t.nickname like concat('%', #{u.nickname}, '%')
            </if>
            <if test="u.enabled != null and u.enabled != ''">
                and t.enabled = #{u.enabled}
            </if>
            <if test="u.type != null and u.type != ''">
                and t.type = #{u.type}
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='user_id'">
                and t.id like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='username'">
                and t.username like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='nick_name'">
                and t.nickname like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='mobile'">
                and t.mobile like concat('%', #{u.searchValue}, '%')
            </if>
        </where>
    </sql>
    <sql id="where_map">
        <where>
            <if test="u.isDel != null">
                and t.is_del = #{u.isDel}
            </if>
            <if test="u.id != null and u.id != ''">
                and t.id like concat('%', #{u.id}, '%')
            </if>
            <if test="u.username != null and u.username != ''">
                and t.username = #{u.username}
            </if>
            <if test="u.nickname != null and u.nickname != ''">
                and t.nickname like concat('%', #{u.nickname}, '%')
            </if>
            <if test="u.enabled != null and u.enabled != ''">
                and t.enabled = #{u.enabled}
            </if>
            <if test="u.type != null and u.type != ''">
                and t.type = #{u.type}
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='user_id'">
                and t.id like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='username'">
                and t.username like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='nick_name'">
                and t.nickname like concat('%', #{u.searchValue}, '%')
            </if>
            <if test="u.searchKey != null and u.searchKey != '' and u.searchKey=='mobile'">
                and t.mobile like concat('%', #{u.searchValue}, '%')
            </if>
        </where>
    </sql>

    <select id="findList" resultType="com.kidgrow.common.model.SysUser">
        select * from sys_user t
        <include refid="where"/>
        order by t.id desc
    </select>
    <select id="selectCountByMap" parameterType="map" resultType="integer">
        select count(*) from sys_user t
        <include refid="where_map"/>
    </select>

    <select id="findAppointUsers" parameterType="integer" resultType="com.kidgrow.common.model.SysUser">
        SELECT
        USERS.id,
        USERS.nickname,
        USERS.mobile
        FROM
        `sys_role_user` RU
        LEFT JOIN sys_user USERS ON RU.user_id = USERS.id
        LEFT JOIN sys_role ROLE ON RU.role_id = ROLE.id
        WHERE
        USERS.is_del = 0
        and USERS.enabled = 1
        <if test="type == 0">
            AND ROLE.`code` IN (
            'salemanager',
            'sale')
        </if>
        <if test="type == 1">
            AND ROLE.`code` IN (
            'oprationmanager',
            'opration')
        </if>
    </select>

    <select id="findDoctorUserAllData" parameterType="long" resultType="com.kidgrow.common.model.DoctorUserAll">
		SELECT sysuser.id,
		sysdoctor.doctor_name,
		sysdoctor.id doctor_Id,
		sysdoctor.doctor_ccie,
		sysdoctor.doctor_tel,
		sysdoctor.doctor_rank,
		sysdoctor.doctor_email,
		sysdoctor.is_answer docanswer,
		sysdoctor.is_signing docsign,
		sysdoctor.is_admin_user,
		sysdoctor.doctor_state,
		sysdoctor.doctor_logo,
		sysdoctor.enabled docenabled,
		sysdoctor.is_del docdel,

		syshos.id hospital_id,
		syshos.hospital_name,
		syshos.hospital_short_name,
		syshos.hospital_type_name,
		syshos.hospital_tel,
		syshos.hospital_province,
		syshos.hospital_city,
		syshos.hospital_area,
		syshos.area_code,
		syshos.hospital_qualified_name,
		syshos.hospital_lit_logo,
		syshos.hospital_logo,
		syshos.is_screen hosisscreen,
		syshos.is_answer hosisanswer,
		syshos.hospital_state,
		syshos.enabled hosenabled,
		syshos.is_del hosdel,

		sysdep.id department_id,
		sysdep.department_name,
		sysdep.server_user_name,
		sysdep.server_user_tel,
		sysdep.sale_user_name,
		sysdep.sale_user_tel,
		sysdep.enabled depenabled,
		sysdep.accounts_count,
		sysdep.is_del depdel
		FROM sys_user sysuser
		left JOIN sys_doctor sysdoctor on sysuser.id=sysdoctor.user_id
		left JOIN sys_hospital syshos on sysdoctor.hospital_id=syshos.id
		left JOIN sys_department sysdep on sysdoctor.department_id=sysdep.id
		where sysuser.id=#{userId}
		and sysuser.is_del=0 and sysuser.enabled=1
		and syshos.is_del=0 and syshos.enabled=1
		and sysdep.is_del=0 and sysdep.enabled=1
	</select>
    <select id="hospitalDoctorList" parameterType="long" resultType="com.kidgrow.usercenter.vo.HospitalDoctorListVo">
        SELECT
        dc.create_time,
        dc.department_name,
        dc.hospital_name,
        dc.doctor_name,
        dc.doctor_rank,
        dc.doctor_tel,
        dc.id,
        dc.user_id,
        dc.is_admin_user,
        dc.doctor_state,
        us.mobile,
        us.username,
        us.default_auth,
        us.enabled,
		roleuser.role_id,
		dc.is_del
        FROM
        sys_doctor dc
        LEFT JOIN sys_user us ON dc.user_id = us.id
        LEFT JOIN sys_role_user roleuser
        ON roleuser.user_id=us.id
        where  dc.is_del=0 and dc.enabled=1  and us.enabled=1
        and dc.hospital_id=#{hospitalId} and dc.department_id=#{departmentId}  and (role_id=#{roleId1} or role_id=#{roleId2})
    </select>
    <select id="hospitalDoctorListAndDel" parameterType="long" resultType="com.kidgrow.usercenter.vo.HospitalDoctorListVo">
        SELECT
        distinct
        dc.create_time,
        dc.department_name,
        dc.hospital_name,
        dc.doctor_name,
        dc.doctor_rank,
        dc.doctor_tel,
        dc.id,
        dc.user_id,
        dc.is_admin_user,
        dc.doctor_state,
        us.mobile,
        us.username,
        us.default_auth,
        us.enabled,
-- 		roleuser.role_id,
		dc.is_del
        FROM
        sys_doctor dc
        LEFT JOIN sys_user us ON dc.user_id = us.id
        LEFT JOIN sys_role_user roleuser
        ON roleuser.user_id=us.id
        where   dc.enabled=1  and us.enabled=1
        and dc.hospital_id=#{hospitalId} and dc.department_id=#{departmentId}  and (role_id=#{roleId1} or role_id=#{roleId2})
    </select>
</mapper>