<?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.saye.hospitalgd.mapper.system.UsersMapper">
    <resultMap id="BaseResultMap" type="com.saye.hospitalgd.model.Users">
        <id column="user_id" property="userId" jdbcType="VARCHAR"/>
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="true_name" property="trueName" jdbcType="VARCHAR"/>
        <result column="depart_id" property="departId" jdbcType="VARCHAR"/>
        <result column="user_name" property="userName" jdbcType="VARCHAR"/>
        <result column="mobile_phone" property="mobilePhone" jdbcType="VARCHAR"/>
        <result column="isactive" property="isactive" jdbcType="VARCHAR"/>
        <result column="modify_user_name" property="modifyUserName" jdbcType="VARCHAR"/>
        <result column="modify_true_name" property="modifyTrueName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="VARCHAR"/>
        <result column="create_date" property="createDate" jdbcType="VARCHAR"/>
        <result column="modify_time" property="modifyTime" jdbcType="VARCHAR"/>
        <result column="lock_time" property="lockTime" jdbcType="VARCHAR"/>
        <result column="role_id" property="roleId" jdbcType="VARCHAR"/>
        <result column="role_name" property="roleName" jdbcType="VARCHAR"/>
        <result column="depart_name" property="departName" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="searchUsers" resultMap="BaseResultMap">
        SELECT u.USER_ID,u.USER_NAME,u.TRUE_NAME,u.MOBILE_PHONE,u.MODIFY_TIME,
        u.PASSWORD,u.DEPART_ID,d.DEPART_NAME,u.ISACTIVE,u.MODIFY_USER_NAME,u.MODIFY_TRUE_NAME,u.CREATE_TIME,
        u.CREATE_DATE,(SELECT GROUP_CONCAT(role_id) FROM user_role ur WHERE ur.user_id = u.user_id) role_id,
        (SELECT GROUP_CONCAT(r.role_name) FROM user_role ur LEFT JOIN role r ON ur.role_id = r.role_id WHERE ur.user_id
        = u.user_id)role_name
        FROM users u
        LEFT JOIN departs d ON u.DEPART_ID = d.DEPART_ID
        <where>
            1=1
            <if test="departId!=null and departId.length>0 ">
                and u.DEPART_ID in
                <foreach collection="departId" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="trueName!=null and trueName!=''">
                and u.TRUE_NAME like concat('%',concat(#{trueName},'%'))
            </if>
            <if test="isactive!=null and isactive!='' and isactive!=-1">
                and u.ISACTIVE =#{isactive}
            </if>
            <if test="roleId!=null and roleId!=''">
                AND EXISTS (SELECT 1 FROM user_role ur WHERE u.USER_ID = ur.USER_ID AND ur.ROLE_ID = #{roleId} )
            </if>
        </where>
        order by u.USER_ID desc
    </select>

    <insert id="insertUser">
        insert into users(USER_ID, USER_NAME, PASSWORD, TRUE_NAME, DEPART_ID, ISACTIVE, MODIFY_USER_NAME,
                          MODIFY_TRUE_NAME, CREATE_TIME, CREATE_DATE, modify_time, lock_time)
        values (#{userId}, #{addUserName}, #{addPassword}, #{addTrueName}, #{departId}, 1, #{userName}, #{userTrueName},
                #{create_time}, #{create_date}, #{create_time}, #{create_time})
    </insert>

    <!--插入excel表用户-->
    <insert id="insertExcelUsers" parameterType="java.util.List">
        insert into users(USER_ID, USER_NAME, PASSWORD, TRUE_NAME, DEPART_ID, ISACTIVE, MODIFY_USER_NAME,
        MODIFY_TRUE_NAME, CREATE_TIME,CREATE_DATE,modify_time,lock_time)
        values
        <foreach collection="list" index="index" item="itm" separator=",">
            (#{itm.user_id},#{itm.user_name},#{itm.password},#{itm.true_name},#{itm.depart_id},#{itm.isactive},#{itm.modify_user_name},#{itm.modify_true_name},#{itm.create_time},#{itm.create_date},#{itm.create_time},#{itm.create_time}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        true_name=values(true_name),
        depart_id=values(depart_id),
        modify_user_name=values(modify_user_name),
        modify_true_name=values(modify_true_name),
        modify_time=values(modify_time),
    </insert>

    <!-- 修改用户信息 -->
    <update id="modifyUsers">
        UPDATE users
        <set>
            <if test="trueName!=null and trueName!=''">
                true_name = #{trueName},
            </if>
            <if test="userName!=null and userName!=''">
                MODIFY_USER_NAME = #{userName},
            </if>
            <if test="userTrueName!=null and userTrueName!=''">
                MODIFY_TRUE_NAME = #{userTrueName},
            </if>
<!--            <if test="createTime!=null and createTime!=''">-->
            <!--                CREATE_TIME =#{createTime},-->
            <!--            </if>-->
            <!--            <if test="createDate!=null and createDate!=''">-->
            <!--                CREATE_DATE =#{createDate},-->
            <!--            </if>-->
            modify_time=#{modifyTime}
        </set>
        where USER_ID =#{userId}


    </update>

    <update id="reSetPassword">
        UPDATE users
        <set>
            <if test="password!=null and password!=''">
                password = #{password},
            </if>
            <if test="modifyUserName!=null and modifyUserName!=''">
                MODIFY_USER_NAME = #{modifyUserName},
            </if>
            <if test="modifyTrueName!=null and modifyTrueName!=''">
                MODIFY_TRUE_NAME = #{modifyTrueName},
            </if>
<!--            <if test="createTime!=null and createTime!=''">-->
            <!--                MODIFY_tIME =#{createTime},-->
            <!--            </if>-->
            <!--            <if test="createDate!=null and createDate!=''">-->
            <!--                CREATE_DATE =#{createDate}-->
            <!--            </if>-->
        </set>
        where USER_ID =#{userId}
    </update>

    <update id="unableUser">
        UPDATE users
        <set>
            <if test="isactive!=null and isactive!=''">
                ISACTIVE =#{isactive},
            </if>
            <if test="modifyUserName!=null and modifyUserName!=''">
                MODIFY_USER_NAME = #{modifyUserName},
            </if>
            <if test="modifyTrueName!=null and modifyTrueName!=''">
                MODIFY_TRUE_NAME = #{modifyTrueName},
            </if>
<!--            <if test="createTime!=null and createTime!=''">-->
            <!--                CREATE_TIME =#{createTime},-->
            <!--            </if>-->
            <!--            <if test="createDate!=null and createDate!=''">-->
            <!--                CREATE_DATE =#{createDate}-->
            <!--            </if>-->
        </set>
        where USER_ID =#{userId}
    </update>

    <!-- 新增时用户名验证 查询用户名 -->
    <select id="searchByName" resultMap="BaseResultMap">
        SELECT u.USER_ID,
               u.USER_NAME,
               u.TRUE_NAME,
               u.MOBILE_PHONE,
               u.MODIFY_TIME,
               u.lock_time,
               u.PASSWORD,
               u.DEPART_ID,
               d.DEPART_NAME,
               u.MODIFY_USER_NAME,
               u.MODIFY_TRUE_NAME,
               u.CREATE_DATE
        FROM users u
                 left join departs d on u.DEPART_ID = d.DEPART_ID
        where u.user_name = #{userName}
    </select>

    <!-- 修改用户密码 -->
    <update id="modifyPassword">
        update
            users
        set PASSWORD=#{password},
            modify_time = #{modifyTime}
        where user_name = #{username}
    </update>

    <!-- 修改锁定时间 -->
    <update id="modifyLockTime">
        update
            users
        set lock_time = #{lockTime}
        where user_name = #{username}
    </update>


    <select id="findUserByDepartIds" parameterType="List" resultType="HashMap">
        select u.USER_ID,u.USER_NAME,u.TRUE_NAME,u.MOBILE_PHONE,u.MODIFY_TIME,u.lock_time,
        u.DEPART_ID,u.MODIFY_USER_NAME,u.MODIFY_TRUE_NAME,u.CREATE_DATE,
        from users u where
        u.DEPART_ID in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="findUserByDepartIdsAndIsactive" parameterType="List" resultType="HashMap">
        select u.USER_ID,u.USER_NAME,u.TRUE_NAME,u.MOBILE_PHONE,u.MODIFY_TIME,u.lock_time,
        u.DEPART_ID,u.MODIFY_USER_NAME,u.MODIFY_TRUE_NAME,u.CREATE_DATE,
        d.depart_name
        from users u
        left join departs d ON U.DEPART_ID = d.DEPART_ID
        where u.ISACTIVE ='1' and
        u.DEPART_ID in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
        and u.USER_NAME != 'admin' and u.USER_NAME != 'system'
        order by u.depart_id
    </select>

    <select id="findUserAndIsactive" resultType="HashMap">
        select u.USER_ID,
               u.USER_NAME,
               u.TRUE_NAME,
               u.MOBILE_PHONE,
               u.MODIFY_TIME,
               u.lock_time,
               u.DEPART_ID,
               u.MODIFY_USER_NAME,
               u.MODIFY_TRUE_NAME,
               u.CREATE_DATE,
               d.depart_name
        from users u
                 left join departs d ON u.DEPART_ID = d.DEPART_ID
        where u.ISACTIVE = '1'
          and u.USER_NAME != 'admin'
          and u.USER_NAME != 'system'
        order by u.depart_id
    </select>
</mapper>