<?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.position.dao.IPositionUserDao">

    <cache flushInterval="3600000"/>

    <!-- 建表 -->
    <update id="createTable">
        CREATE TABLE IF NOT EXISTS `sys_position_user` (
            `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            `position_id` char(36) NOT NULL,
            `user_id` char(36) NOT NULL,
            PRIMARY KEY (`id`),
            KEY `user_id_idx` (`user_id`) USING BTREE,
            KEY `position_id_idx` (`position_id`) USING BTREE
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>

    <!-- 新增 -->
    <insert id="save" parameterType="map" flushCache="true">
        INSERT INTO sys_position_user(
            position_id,
            user_id
        ) VALUES(
            #{positionId},
            #{userId}
        )
    </insert>

    <!-- 删除 -->
    <delete id="delete" parameterType="map" flushCache="true">
        DELETE FROM
            sys_position_user
        WHERE
            1 = 1
        <if test="positionId != null and positionId != ''">
        AND
            position_id = #{positionId}
        </if>
        <if test="userId != null and userId != ''">
        AND
            user_id = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
        AND
            user_id IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
    </delete>

    <!-- 用户ID列表 -->
    <select id="listUserId" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            user_id
        FROM
            sys_position_user
        <where>
        <if test="positionId != null and positionId != ''">
            position_id = #{positionId}
        </if>
        <if test="positionIds != null and positionIds.size > 0">
        AND
            position_id IN (
            <foreach collection="positionIds" index="index" open="(" separator="," close=")">
                #{positionIds[${index}]}
            </foreach>
            )
        </if>
        <if test="userIds != null and userIds.size > 0">
        AND
            user_id IN
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
        </if>
        </where>
    </select>

    <!-- 用户ID列表 -->
    <select id="listGroupUserId" parameterType="map" resultType="java.lang.String" useCache="true">
        SELECT
            user_id
        FROM
            sys_position_user
        WHERE
            1 = 1
        GROUP BY
            user_id
    </select>

    <!-- 职位ID列表 -->
    <select id="listPositionId" parameterType="map" resultType="java.lang.String">
        SELECT
            position_id
        FROM
            sys_position_user
        WHERE
        <if test="userId != null and userId != ''">
            user_id = #{userId}
        </if>
        <if test="userIds != null and userIds.size > 0">
            user_id IN (
            <foreach collection="userIds" index="index" open="(" separator="," close=")">
                #{userIds[${index}]}
            </foreach>
            )
        </if>
    </select>

    <!-- 用户列表 -->
    <select id="listUser" parameterType="map" resultMap="ink.wgink.service.user.dao.IUserDao.userDTO" useCache="false">
        SELECT
            user_id,
            user_username,
            user_name,
            user_phone,
            user_email,
            user_type,
            user_state
        FROM
            sys_user t1
        WHERE
            t1.is_delete = 0
        AND
            t1.user_username != 'admin'
        <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>
        <if test="userType != null">
        AND
            t1.user_type = #{userType}
        </if>
        <if test="userState != null">
        AND
            t1.user_state = #{userState}
        </if>
        <if test="excludeUserType != null and excludeUserType != ''">
        AND
            t1.user_type != #{excludeUserType}
        </if>
        <if test="positionId != null and positionId != ''">
        AND
            t1.user_id IN (
                SELECT
                    user_id
                FROM
                    sys_position_user st1
                WHERE
                    st1.position_id = #{positionId}
            )
        </if>
        <if test="positionIds != null and positionIds.size > 0">
        AND
            t1.user_id IN (
                SELECT
                    user_id
                FROM
                    sys_position_user st1
                WHERE
                    st1.position_id IN
                <foreach collection="positionIds" index="index" open="(" separator="," close=")">
                    #{positionIds[${index}]}
                </foreach>
            )
        </if>
        <if test="excludePositionId != null and excludePositionId != ''">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_position_user st1
                WHERE
                    st1.position_id = #{excludePositionId}
            )
        </if>
        <if test="excludePositionIds != null and excludePositionIds.size > 0">
        AND
            t1.user_id NOT IN (
                SELECT
                    user_id
                FROM
                    sys_position_user st1
                WHERE
                    st1.position_id IN
                <foreach collection="excludePositionIds" index="index" open="(" separator="," close=")">
                    #{excludePositionIds[${index}]}
                </foreach>
            )
        </if>
    </select>

</mapper>