<?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="${domain}.module.wsys.mpr.UserMapper">

    <sql id="table">"SYS_USER"</sql>

    <sql id="entityColumnList">
        "ID","USER_NAME","USER_CODE","USER_ALIAS","USER_PWD","USER_STATUS","DEPT_ID","DEPT_CODE","DEPT_NAME","ROW_VERSION","IS_DELETED","CREATE_BY","CREATE_TIME","LAST_UPDATE_BY","LAST_UPDATE_TIME"
    </sql>

    <resultMap id="user" type="${domain}.module.wsys.ent.User">
        <result column="ID"  jdbcType="BIGINT" property="id"/>
        <result column="USER_NAME"  jdbcType="VARCHAR" property="userName"/>
        <result column="USER_CODE"  jdbcType="VARCHAR" property="userCode"/>
        <result column="USER_ALIAS"  jdbcType="VARCHAR" property="userAlias"/>
        <result column="USER_PWD"  jdbcType="VARCHAR" property="userPwd"/>
        <result column="USER_STATUS"  jdbcType="VARCHAR" property="userStatus"/>
        <result column="DEPT_ID"  jdbcType="BIGINT" property="deptId"/>
        <result column="DEPT_CODE"  jdbcType="VARCHAR" property="deptCode"/>
        <result column="DEPT_NAME"  jdbcType="VARCHAR" property="deptName"/>
        <result column="ROW_VERSION"  jdbcType="BIGINT" property="rowVersion"/>
        <result column="IS_DELETED"  jdbcType="BIT" property="isDeleted"/>
        <result column="CREATE_BY"  jdbcType="BIGINT" property="createBy"/>
        <result column="CREATE_TIME"  jdbcType="TIMESTAMP" property="createTime"/>
        <result column="LAST_UPDATE_BY"  jdbcType="BIGINT" property="lastUpdateBy"/>
        <result column="LAST_UPDATE_TIME"  jdbcType="TIMESTAMP" property="lastUpdateTime"/>
    </resultMap>

    <insert id="insert">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        (
        ${r"#"}{request.id},
        ${r"#"}{request.userName,jdbcType=VARCHAR},
        ${r"#"}{request.userCode,jdbcType=VARCHAR},
        ${r"#"}{request.userAlias,jdbcType=VARCHAR},
        ${r"#"}{request.userPwd,jdbcType=VARCHAR},
        ${r"#"}{request.userStatus,jdbcType=VARCHAR},
        ${r"#"}{request.deptId,jdbcType=BIGINT},
        ${r"#"}{request.deptCode,jdbcType=VARCHAR},
        ${r"#"}{request.deptName,jdbcType=VARCHAR},
        0,
        0,
        ${r"#"}{token.userId,jdbcType=NUMERIC},
        datetime('now','localtime'),
        NULL,
        NULL
        )
    </insert>

    <insert id="insertBatch">
        INSERT INTO
        <include refid="table"/>
        (
        <include refid="entityColumnList"/>
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            ${r"#"}{item.id},
            ${r"#"}{item.userName,jdbcType=VARCHAR},
            ${r"#"}{item.userCode,jdbcType=VARCHAR},
            ${r"#"}{item.userAlias,jdbcType=VARCHAR},
            ${r"#"}{item.userPwd,jdbcType=VARCHAR},
            ${r"#"}{item.userStatus,jdbcType=VARCHAR},
            ${r"#"}{item.deptId,jdbcType=BIGINT},
            ${r"#"}{item.deptCode,jdbcType=VARCHAR},
            ${r"#"}{item.deptName,jdbcType=VARCHAR},
            0,
            0,
            ${r"#"}{token.userId,jdbcType=NUMERIC},
            datetime('now','localtime'),
            NULL,
            NULL
        </foreach >
    </insert>

    <update id="delete">
        UPDATE
        <include refid="table"/>
        SET "IS_DELETED" = 1
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r'#'}{id}
    </update>

    <update id="deleteBatch">
        UPDATE
        <include refid="table"/>
        SET "IS_DELETED" = 1
        WHERE "IS_DELETED" = 0
        AND "ID" IN
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            ${r'#'}{item}
        </foreach>
    </update>

    <update id="update">
        UPDATE
        <include refid="table"/>
        SET
        USER_CODE = ${r"#"}{request.userCode,jdbcType=VARCHAR},
        USER_ALIAS = ${r"#"}{request.userAlias,jdbcType=VARCHAR},
        USER_STATUS = ${r"#"}{request.userStatus,jdbcType=VARCHAR},
        DEPT_ID = ${r"#"}{request.deptId,jdbcType=BIGINT},
        DEPT_CODE = ${r"#"}{request.deptCode,jdbcType=VARCHAR},
        DEPT_NAME = ${r"#"}{request.deptName,jdbcType=VARCHAR},
        "ROW_VERSION" = "ROW_VERSION" + 1,
        "LAST_UPDATE_BY" = ${r"#"}{token.userId},
        "LAST_UPDATE_TIME" = datetime('now','localtime')
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r"#"}{request.id}
        AND "ROW_VERSION" = ${r"#"}{request.rowVersion}
    </update>

    <update id="changePwd">
        UPDATE
        <include refid="table"/>
        SET
        "USER_PWD" = ${r"#"}{request.userPwd,jdbcType=VARCHAR},
        "ROW_VERSION" = "ROW_VERSION" + 1,
        "LAST_UPDATE_BY" = ${r"#"}{token.userId},
        "LAST_UPDATE_TIME" = datetime('now','localtime')
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r"#"}{request.id}
    </update>

    <select id="find" resultMap="user">
        SELECT
        t1.*,t2.ROLE_NAMES, t2.ROLE_CODES
        FROM
        "sys_user" t1,
        (
        SELECT
        t2.user_id, GROUP_CONCAT(t3.NAME) ROLE_NAMES, GROUP_CONCAT(t3.CODE) ROLE_CODES
        FROM
        "sys_user_role" t2,
        "sys_role" t3
        WHERE
        t2.ROLE_ID = t3.ID
        AND t2."IS_DELETED" = 0
        AND t3."IS_DELETED" = 0
        GROUP BY t2.USER_ID
        ) t2
        WHERE t1."IS_DELETED" = 0 and t1.ID = t2.user_id
        <if test="request.userName != null and request.userName != ''">
            AND t1."USER_NAME" = ${r'#'}{request.userName}
        </if>
        <if test="request.userCode != null and request.userCode != ''">
            AND t1."USER_CODE" = ${r'#'}{request.userCode}
        </if>
        <if test="request.userAlias != null and request.userAlias != ''">
            AND t1."USER_ALIAS" = ${r'#'}{request.userAlias}
        </if>
        <if test="request.userStatus != null and request.userStatus != ''">
            AND t1."USER_STATUS" = ${r'#'}{request.userStatus}
        </if>
        <if test="request.deptId != null and request.deptId != 0">
            AND t1."DEPT_ID" = ${r'#'}{request.deptId}
        </if>
        <if test="request.deptCode != null and request.deptCode != ''">
            AND t1."DEPT_CODE" = ${r'#'}{request.deptCode}
        </if>
        <if test="request.deptName != null and request.deptName != ''">
            AND t1."DEPT_NAME" = ${r'#'}{request.deptName}
        </if>
        <if test="request.startDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &gt;= ${r'#'}{request.startDate}
        </if>
        <if test="request.endDate != null">
            AND strftime('%s',"CREATE_TIME", 'utc')*1000 &lt;= ${r'#'}{request.endDate}
        </if>
    </select>

    <select id="search" resultMap="user">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
    </select>

    <select id="getById" resultMap="user">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE "IS_DELETED" = 0
        AND "ID" = ${r'#'}{id}
    </select>

    <select id="getByIds" resultMap="user">
        SELECT
        <include refid="entityColumnList"/>
        FROM
        <include refid="table"/>
        WHERE
        IS_DELETED = 0
        AND "ID" IN
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            ${r'#'}{item}
        </foreach>
    </select>
</mapper>