<?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="cn.stylefeng.guns.modular.system.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.stylefeng.guns.modular.system.entity.User">
        <id column="USER_ID" property="userId"/>
        <result column="AVATAR" property="avatar"/>
        <result column="ACCOUNT" property="account"/>
        <result column="PASSWORD" property="password"/>
        <result column="SALT" property="salt"/>
        <result column="NAME" property="name"/>
        <result column="BIRTHDAY" property="birthday"/>
        <result column="SEX" property="sex"/>
        <result column="EMAIL" property="email"/>
        <result column="PHONE" property="phone"/>
        <result column="ROLE_ID" property="roleId"/>
        <result column="DEPT_ID" property="deptId"/>
        <result column="STATUS" property="status"/>
        <result column="CREATE_TIME" property="createTime"/>
        <result column="CREATE_USER" property="createUser"/>
        <result column="UPDATE_TIME" property="updateTime"/>
        <result column="UPDATE_USER" property="updateUser"/>
        <result column="VERSION" property="version"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        USER_ID AS userId, AVATAR AS avatar, ACCOUNT AS account,
        NAME AS name, BIRTHDAY AS birthday, SEX AS sex, EMAIL AS email, PHONE AS phone,
         ROLE_ID AS roleId, DEPT_ID AS deptId, STATUS AS status, CREATE_TIME AS createTime, CREATE_USER AS createUser,
          UPDATE_TIME AS updateTime, UPDATE_USER AS updateUser, VERSION AS version
    </sql>

    <sql id="Base_Column_List_With_Pwd">
		 USER_ID AS userId, AVATAR AS avatar, ACCOUNT AS account, PASSWORD AS password,
        SALT AS salt, NAME AS name, BIRTHDAY AS birthday, SEX AS sex, EMAIL AS email, PHONE AS phone,
         ROLE_ID AS roleId, DEPT_ID AS deptId, STATUS AS status, CREATE_TIME AS createTime, CREATE_USER AS createUser,
          UPDATE_TIME AS updateTime, UPDATE_USER AS updateUser, VERSION AS version
	</sql>

    <select id="selectUsers" resultType="map">
        select
        <include refid="Base_Column_List"/>
        from sys_user
        where STATUS != 'DELETED'
        <if test="name != null and name != ''">
            and (PHONE like CONCAT('%',#{name},'%')
            or ACCOUNT like CONCAT('%',#{name},'%')
            or NAME like CONCAT('%',#{name},'%'))
        </if>
        <if test="deptId != null and deptId != 0">
            and (DEPT_ID = #{deptId} or DEPT_ID in ( select DEPT_ID from sys_dept where PIDS like CONCAT('%[', #{deptId}, ']%') ))
        </if>
        <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
            and (CREATE_TIME between CONCAT(#{beginTime},' 00:00:00') and CONCAT(#{endTime},' 23:59:59'))
        </if>
    </select>

    <update id="setStatus">
		update sys_user set STATUS = #{status} where USER_ID = #{userId}
	</update>

    <update id="changePwd">
		update sys_user set PASSWORD = #{pwd} where USER_ID = #{userId}
	</update>

    <update id="setRoles">
		update sys_user set ROLE_ID = #{roleIds} where USER_ID =
		#{userId}
	</update>

    <select id="getByAccount" resultType="cn.stylefeng.guns.modular.system.entity.User">
        select
        <include refid="Base_Column_List_With_Pwd"/>
        from sys_user where ACCOUNT = #{account} and STATUS != 'DELETED'
    </select>

</mapper>
