<?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="ink.wgink.service.user.dao.IUserAdjustmentDao">

    <cache flushInterval="3600000"/>

    <resultMap id="userLogDTO" type="ink.wgink.service.user.pojo.dtos.UserLogDTO">
        <result column="user_name" property="userName"/>
        <result column="update_type" property="updateType"/>
        <result column="old_value" property="oldValue"/>
        <result column="new_value" property="newValue"/>
        <result column="update_reason" property="updateReason"/>
        <result column="creator_name" property="creatorName"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_user_adjustment` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `user_id` char(36) DEFAULT NULL COMMENT '用户ID',
            `user_name` varchar(255) DEFAULT NULL COMMENT '用户名称',
            `update_type` varchar(255) DEFAULT NULL COMMENT '类型：password，修改密码；username，修改用户名',
            `old_value` varchar(255) DEFAULT NULL COMMENT '旧值',
            `new_value` varchar(255) DEFAULT NULL COMMENT '新值',
            `update_reason` varchar(255) DEFAULT NULL COMMENT '原因',
            `creator` char(36) DEFAULT NULL,
            `creator_name` varchar(255) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户调整记录表';
    </update>

    <!-- 新增修改日志 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_user_adjustment(
            user_id,
            user_name,
            update_type,
            old_value,
            new_value,
            update_reason,
            creator,
            creator_name,
            gmt_create
        ) VALUES(
            #{userId},
            #{userName},
            #{updateType},
            #{oldValue},
            #{newValue},
            #{updateReason},
            #{creator},
            #{creatorName},
            #{gmtCreate}
        )
    </insert>

    <!-- 用户调整列表 -->
    <select id="list" parameterType="map" resultMap="userLogDTO" useCache="true">
        SELECT
            user_id,
            user_name,
            update_type,
            old_value,
            new_value,
            update_reason,
            creator,
            creator_name,
            LEFT(gmt_create, 19) gmt_create
        FROM
            sys_user_adjustment
        WHERE
            1 = 1
        <if test="updateType != null and updateType != ''">
        AND
            update_type = #{updateType}
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            old_value LIKE CONCAT('%', #{keywords}, '%')
            OR
            new_value LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        ORDER BY
            gmt_create DESC, id DESC
    </select>

</mapper>