<?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.example.his.api.db.dao.UserDao">

    <resultMap id="PermissionAll" type="com.example.his.api.resp.PermissionAllResp">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="parentId" column="parent_id" jdbcType="INTEGER"/>
        <result property="permissionName" column="permission_name" jdbcType="VARCHAR"/>
        <result property="moduleName" column="module_name" jdbcType="VARCHAR"/>
        <result property="menuType" column="menu_type" jdbcType="VARCHAR"/>
        <result property="icon" column="icon" jdbcType="VARCHAR"/>
        <result property="path" column="path" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="searchUserPermissions" parameterType="int" resultType="String">
        SELECT DISTINCT p.permission_name
        FROM tb_user u
                 JOIN tb_role r ON JSON_CONTAINS(u.role, CAST(r.id AS CHAR))
                 JOIN tb_permission p ON JSON_CONTAINS(r.permissions, CAST(p.id AS CHAR))
        WHERE u.id = #{userId}
          AND u.`status` = 1
    </select>
    <select id="login" parameterType="Map" resultType="Integer">
        SELECT id
        FROM tb_user
        WHERE username = #{username}
          AND password = #{password}
        LIMIT 1;
    </select>
    <select id="searchUsernameById" parameterType="int" resultType="String">
        SELECT username
        FROM tb_user
        WHERE id = #{userId}
    </select>
    <update id="updatePassword" parameterType="Map">
       UPDATE tb_user
          SET password = #{newPassword}
        WHERE id = #{userId}
          AND password = #{password}
    </update>

    <!-- 用户管理-分页查询 -->
    <select id="searchUserByPage" parameterType="Map" resultType="HashMap">
        SELECT DISTINCT u.id,
        u.name,
        u.sex,
        u.tel,
        u.email,
        d.dept_name AS dept,
        d.id AS deptId,
        u.role AS roleId,
        u.hiredate,
        u.root,
        u.status,
        (
        SELECT GROUP_CONCAT( role_name )
        FROM tb_role
        WHERE JSON_CONTAINS ( u.role, CONVERT (id, CHAR) )
        ) AS roles
        FROM tb_user u
        JOIN tb_role r ON JSON_CONTAINS ( u.role, CONVERT (r.id, CHAR) )
        LEFT JOIN tb_dept d ON u.dept_id = d.id
        <where>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                OR d.dept_name LIKE '%${searchKeyWord}%'
            </if>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                OR u.name LIKE '%${searchKeyWord}%'
            </if>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                OR u.status LIKE '%${searchKeyWord}%'
            </if>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                OR u.sex LIKE '%${searchKeyWord}%'
            </if>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                OR u.tel LIKE '%${searchKeyWord}%'
            </if>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                OR u.email LIKE '%${searchKeyWord}%'
            </if>
        </where>
        ORDER BY u.id ASC
    </select>
    <!-- 用户管理-新增 -->
    <insert id="insertUser">
        insert into tb_user
        SET
        username=#{username},
        password=#{password},
        name=#{name},
        sex=#{sex},
        tel=#{tel},
        email=#{email},
        status=#{status},
        dept_id=#{deptId},
        hiredate=#{hiredate},
        create_time=#{createTime},
        role=#{role}
        <if test="openId!=null">
            ,`openId`=#{openId}
        </if>
        <if test="photo!=null">
            ,`photo`=#{photo}
        </if>
        <if test="root!=null">
            ,`root`=#{root}
        </if>
    </insert>
    <!-- 用户管理-编辑 -->
    <update id="updateUser">
        update tb_user
        SET
        name=#{name},
        sex=#{sex},
        tel=#{tel},
        email=#{email},
        status=#{status},
        dept_id=#{deptId},
        hiredate=#{hiredate},
        create_time=#{createTime},
        role=#{role}
        <if test="username!=null">
            ,`username`=#{username}
        </if>
        <if test="password!=null">
            ,`password`=#{password}
        </if>
        <if test="openId!=null">
            ,`openId`=#{openId}
        </if>
        <if test="photo!=null">
            ,`photo`=#{photo}
        </if>
        <if test="root!=null">
            ,`root`=#{root}
        </if>
        where id = #{id}
    </update>
    <!-- 用户管理-删除 -->
    <delete id="deleteUser">
       delete from tb_user where id = #{id}
    </delete>
    <!-- 用户管理-批量删除 -->
    <delete id="deleteBatchUser">
        delete from tb_user where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <!-- 用户管理-查询全部-->
    <select id="searchUserAll" resultType="com.example.his.api.req.MisUser.UserExportExcel">
        select
            u.username,
            u.password,
            u.name,
            u.tel,
            u.email,
            u.hiredate,
            u.status,
            (
                SELECT GROUP_CONCAT( role_name )
                FROM tb_role
                WHERE JSON_CONTAINS ( u.role, CONVERT (id, CHAR) )
            ) AS roles
            from tb_user u
            JOIN tb_role r ON JSON_CONTAINS ( u.role, CONVERT (r.id, CHAR) )
    </select>

    <!-- 查询当前医生的摘要信息 -->
    <select id="searchDoctorById" parameterType="int" resultType="HashMap">
        SELECT id,
               name,
               sex,
               tel
        FROM tb_user
        WHERE id = #{id}
    </select>


</mapper>
