<?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.IUserDao">

    <cache flushInterval="3600000"/>

    <resultMap id="userPO" type="ink.wgink.service.user.pojo.pos.UserPO">
        <id property="userId" column="user_id"/>
        <result property="userUsername" column="user_username"/>
        <result property="userPassword" column="user_password"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="userEmail" column="user_email"/>
        <result property="userUKey" column="user_ukey"/>
        <result property="userAvatar" column="user_avatar"/>
        <result property="userType" column="user_type"/>
        <result property="userState" column="user_state"/>
        <result property="userExpiredDate" column="user_expired_date"/>
        <result property="lastLoginAddress" column="last_login_address"/>
        <result property="lastLoginTime" column="last_login_time"/>
        <result property="userLongitude" column="user_longitude"/>
        <result property="userLatitude" column="user_latitude"/>
        <result property="loginType" column="login_type"/>
        <result property="gmtPasswordModified" column="gmt_password_modified"/>
        <result property="gmtCreate" column="gmt_create"/>
    </resultMap>

    <resultMap id="userDTO" type="ink.wgink.pojo.dtos.user.UserDTO">
        <id property="userId" column="user_id"/>
        <result property="userUsername" column="user_username"/>
        <result property="userName" column="user_name"/>
        <result property="userPhone" column="user_phone"/>
        <result property="userEmail" column="user_email"/>
        <result property="userUKey" column="user_ukey"/>
        <result property="userAvatar" column="user_avatar"/>
        <result property="userType" column="user_type"/>
        <result property="userState" column="user_state"/>
        <result property="userExpiredDate" column="user_expired_date"/>
        <result property="lastLoginAddress" column="last_login_address"/>
        <result property="lastLoginTime" column="last_login_time"/>
        <result property="userLongitude" column="user_longitude"/>
        <result property="userLatitude" column="user_latitude"/>
        <result property="departmentIds" column="department_ids"/>
        <result property="departmentNames" column="department_names"/>
        <result property="roleIds" column="role_ids"/>
        <result property="roleNames" column="role_names"/>
        <result property="positionIds" column="position_ids"/>
        <result property="positionNames" column="position_names"/>
        <result property="loginType" column="login_type"/>
        <result property="gmtCreate" column="gmt_create"/>
    </resultMap>

    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_user` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `user_id` char(36) NOT NULL,
            `user_username` varchar(255) NOT NULL COMMENT '用户名',
            `user_password` varchar(255) DEFAULT NULL COMMENT '密码',
            `user_name` varchar(255) DEFAULT NULL COMMENT '姓名',
            `user_phone` varchar(20) DEFAULT NULL COMMENT '电话',
            `user_email` varchar(255) DEFAULT NULL COMMENT '邮箱',
            `user_ukey` varchar(32) DEFAULT NULL COMMENT 'UKey',
            `user_ukey_electronic_secret_key` text COMMENT '用户UKey电子秘钥',
            `user_type` int(2) DEFAULT '2' COMMENT '1:系统用户，2:普通用户',
            `user_state` int(2) DEFAULT '0' COMMENT '用户状态',
            `user_expired_date` varchar(20) DEFAULT NULL COMMENT '用户过期时间',
            `user_avatar` char(36) DEFAULT NULL COMMENT '头像',
            `user_longitude` varchar(255) DEFAULT '0' COMMENT '经度',
            `user_latitude` varchar(255) DEFAULT '0' COMMENT '纬度',
            `last_login_address` varchar(255) DEFAULT NULL COMMENT '最后登录地址',
            `last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
            `login_type` int(1) DEFAULT '1' COMMENT '登录类型',
            `gmt_password_modified` datetime DEFAULT NULL COMMENT '密码修改时间',
            `remarks` varchar(255) DEFAULT NULL COMMENT '备注',
            `creator` char(36) DEFAULT NULL,
            `gmt_create` datetime DEFAULT NULL,
            `modifier` char(36) DEFAULT NULL,
            `gmt_modified` datetime DEFAULT NULL,
            `is_delete` int(2) DEFAULT '0',
            PRIMARY KEY (`id`,`user_id`,`user_username`),
            UNIQUE KEY `user_username` (`user_username`) USING BTREE,
            UNIQUE KEY `user_id` (`user_id`) USING BTREE,
            KEY `user_name` (`user_name`) USING BTREE,
            KEY `user_id_2` (`user_id`,`user_username`,`user_name`) USING BTREE,
            KEY `user_id_3` (`user_id`,`user_name`) USING BTREE,
            KEY `user_id_4` (`user_id`,`user_name`,`user_avatar`) USING BTREE,
            KEY `is_delete_idx` (`is_delete`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 保存用户 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_user(
            user_id,
            user_username,
            user_password,
            user_name,
            user_phone,
            user_email,
            user_type,
            user_state,
            user_expired_date,
            user_avatar,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{userId},
            #{userUsername},
            #{userPassword},
            #{userName},
            #{userPhone},
            #{userEmail},
            #{userType},
            #{userState},
            #{userExpiredDate},
            #{userAvatar},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除用户 -->
    <update id="remove" parameterType="map" flushCache="true">
        UPDATE
            sys_user
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            user_id IN
        <foreach collection="userIds" index="index" open="(" separator="," close=")">
            #{userIds[${index}]}
        </foreach>
    </update>

    <!-- 修改菜单 -->
    <update id="update" parameterType="map" flushCache="true">
        UPDATE
            sys_user
        SET
        <if test="userPassword != null and userPassword != ''">
            user_password = #{userPassword},
        </if>
        <if test="userName != null and userName != ''">
            user_name = #{userName},
        </if>
        <if test="userPhone != null and userPhone != ''">
            user_phone = #{userPhone},
        </if>
        <if test="userEmail != null and userEmail != ''">
            user_email = #{userEmail},
        </if>
        <if test="userType != null">
            user_type = #{userType},
        </if>
        <if test="userState != null">
            user_state = #{userState},
        </if>
        <if test="userExpiredDate != null">
            user_expired_date = #{userExpiredDate},
        </if>
        <if test="userAvatar != null and userAvatar != ''">
            user_avatar = #{userAvatar},
        </if>
        <if test="lastLoginAddress != null and lastLoginAddress != ''">
            last_login_address = #{lastLoginAddress},
        </if>
        <if test="lastLoginTime != null and lastLoginTime != ''">
            last_login_time = #{lastLoginTime},
        </if>
        <if test="gmtPasswordModified != null and gmtPasswordModified != ''">
            gmt_password_modified = #{gmtPasswordModified},
        </if>
        <if test="userUKey != null and userUKey != ''">
            user_ukey = #{userUKey},
        </if>
        <if test="userUKeyElectronicSecretKey != null and userUKeyElectronicSecretKey != ''">
            user_ukey_electronic_secret_key = #{userUKeyElectronicSecretKey},
        </if>
        <if test="loginType != null">
            login_type = #{loginType},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            user_id = #{userId}
    </update>

    <!-- 更新用户密码 -->
    <update id="updatePassword" parameterType="map" flushCache="true">
        UPDATE
            sys_user
        SET
            user_password = #{userPassword},
            gmt_password_modified = #{gmtPasswordModified}
        WHERE
            user_id = #{userId}
    </update>

    <!-- 更新用户名 -->
    <update id="updateUsername" parameterType="map" flushCache="true">
        UPDATE
            sys_user
        SET
            user_username = #{userUsername}
        WHERE
            user_id = #{userId}
    </update>

    <!-- 更新登录信息 -->
    <update id="updateLoginInfo" parameterType="map">
        UPDATE
            sys_user
        SET
        <if test="userLongitude != null and userLongitude != ''">
            user_longitude = #{userLongitude},
        </if>
        <if test="userLatitude != null and userLatitude != ''">
            user_latitude = #{userLatitude},
        </if>
        <if test="lastLoginAddress != null and lastLoginAddress != ''">
            last_login_address = #{lastLoginAddress},
        </if>
        <if test="lastLoginTime != null and lastLoginTime != ''">
            last_login_time = #{lastLoginTime},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            is_delete = 0
        AND
            user_id = #{userId}
    </update>

    <!-- 更新过期时间 -->
    <update id="updateExpiredDate" parameterType="map" flushCache="true">
        UPDATE
            sys_user
        SET
            user_expired_date = #{userExpiredDate}
        WHERE
            user_id = #{userId}
    </update>

    <!-- 获取用户 -->
    <select id="getPO" parameterType="map" resultMap="userPO" useCache="true">
        SELECT
            user_id,
            user_password,
            user_username,
            user_name,
            user_phone,
            user_email,
            user_ukey,
            user_ukey_electronic_secret_key,
            user_type,
            user_state,
            user_expired_date,
            user_avatar,
            user_longitude,
            user_latitude,
            last_login_address,
            LEFT(last_login_time, 19) last_login_time,
            login_type,
            LEFT(gmt_password_modified, 19) gmt_password_modified,
            remarks,
            LEFT(gmt_create, 19) gmt_create
        FROM
            sys_user
        WHERE
            is_delete = 0
        <if test="userId != null and userId != ''">
        AND
            user_id = #{userId}
        </if>
        <if test="userUsername != null and userUsername != ''">
        AND
            user_username = #{userUsername}
        </if>
        <if test="userUKey != null and userUKey != ''">
        AND
            user_ukey = #{userUKey}
        </if>
    </select>

    <!-- 获取用户 -->
    <select id="get" parameterType="map" resultMap="userDTO" useCache="true">
        SELECT
            user_id,
            user_password,
            user_username,
            user_name,
            user_phone,
            user_email,
            user_ukey,
            user_ukey_electronic_secret_key,
            user_type,
            user_state,
            user_expired_date,
            user_avatar,
            user_longitude,
            user_latitude,
            last_login_address,
            LEFT(last_login_time, 19) last_login_time,
            login_type,
            gmt_password_modified,
            remarks,
            LEFT(gmt_create, 19) gmt_create
        FROM
            sys_user
        WHERE
            is_delete = 0
        <if test="userId != null and userId != ''">
        AND
            user_id = #{userId}
        </if>
        <if test="userUsername != null and userUsername != ''">
        AND
            user_username = #{userUsername}
        </if>
        <if test="userUKey != null and userUKey != ''">
        AND
            user_ukey = #{userUKey}
        </if>
    </select>

    <!-- 用户列表 -->
    <select id="list" parameterType="map" resultMap="userDTO" useCache="true">
        SELECT
            user_id,
            user_password,
            user_username,
            user_name,
            user_phone,
            user_email,
            user_ukey,
            user_ukey_electronic_secret_key,
            user_type,
            user_state,
            user_expired_date,
            user_avatar,
            user_longitude,
            user_latitude,
            last_login_address,
            LEFT(last_login_time, 19) last_login_time,
            login_type,
            gmt_password_modified,
            remarks,
            LEFT(gmt_create, 19) gmt_create
        FROM
            sys_user
        WHERE
            is_delete = 0
        AND
            user_username != 'admin'
        <if test="userIds != null and userIds.size > 0">
        AND
            user_id IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
        <if test="excludeUserIds != null and excludeUserIds.size > 0">
        AND
            user_id NOT IN
            <foreach collection="excludeUserIds" index="index" open="(" separator="," close=")">
                #{excludeUserIds[${index}]}
            </foreach>
        </if>
        <if test="keywords != null and keywords != ''">
        AND (
            user_username LIKE CONCAT('%', #{keywords}, '%')
            OR
            user_name LIKE CONCAT('%', #{keywords}, '%')
            OR
            user_phone LIKE CONCAT('%', #{keywords}, '%')
            OR
            user_email LIKE CONCAT('%', #{keywords}, '%')
            )
        </if>
        <choose>
            <when test="sort != null and (sort == 'userUsername' or sort == 'userName' or sort == 'userPhone' or sort == 'userEmail' or sort == 'userState' or sort == 'lastLoginAddress' or sort == 'lastLoginTime')">
                ORDER BY
                <if test="sort == 'userUsername'">
                    user_username ${order}
                </if>
                <if test="sort == 'userName'">
                    user_name ${order}
                </if>
                <if test="sort == 'userPhone'">
                    user_phone ${order}
                </if>
                <if test="sort == 'userEmail'">
                    user_email ${order}
                </if>
                <if test="sort == 'userState'">
                    user_state ${order}
                </if>
                <if test="sort == 'lastLoginAddress'">
                    last_login_address ${order}
                </if>
                <if test="sort == 'lastLoginTime'">
                    last_login_time ${order}
                </if>
            </when>
        </choose>
    </select>

    <!-- 统计用户数量 -->
    <select id="count" parameterType="map" resultType="java.lang.Integer" useCache="true">
        SELECT
            count(*)
        FROM
            sys_user
        WHERE
            is_delete = 0
        AND
            user_username != 'admin'
        <if test="startDay != null and startDay != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ >= ]]> #{startDay}
        </if>
        <if test="endDay != null and endDay != ''">
        AND
            LEFT(gmt_create, 10) <![CDATA[ <= ]]> #{endDay}
        </if>
        <if test="today != null and today != ''">
        AND
            LEFT(gmt_create, 10) = #{today}
        </if>
        <if test="userType != null">
        AND
            user_type = #{userType}
        </if>
        <if test="userState != null">
        AND
            user_state = #{userState}
        </if>
        <if test="errorUserType != null">
        AND
            (user_type <![CDATA[<>]]> 1 AND user_type <![CDATA[<>]]> 2 OR user_type IS NULL)
        </if>
        <if test="loginType != null">
        AND
            login_type = #{loginType}
        </if>
        <if test="errorLoginType != null">
        AND
            (login_type <![CDATA[<>]]> 1 AND login_type <![CDATA[<>]]> 2 AND login_type <![CDATA[<>]]> 3 OR login_type IS NULL)
        </if>
    </select>

    <!-- 密码修改时间 -->
    <select id="getGmtPasswordModified" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT
            LEFT(gmt_password_modified, 19)
        FROM
            sys_user
        WHERE
            is_delete = 0
        AND
            user_id = #{userId}
    </select>

</mapper>