<?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.wisenergy.mapper.WorkUserProjectMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.wisenergy.model.app.WorkUserProject">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="project_id" property="projectId" />
    </resultMap>

    <resultMap id="MinUserMap" type="cn.wisenergy.model.dto.UserProjectDto">
        <id column="user_id" property="id"/>
        <result column="name" property="name"/>
        <result column="project_id" property="projectId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, user_id, project_id
    </sql>
    <insert id="insertBatch">
        INSERT INTO work_user_project
        (user_id,project_id)
        VALUES
        <foreach collection ="userIds" item="userId" separator =",">
            ( #{userId}, #{projectId})
        </foreach >
    </insert>
    <delete id="deleteByProjectId">
        DELETE
        FROM work_user_project
        WHERE project_id = #{projectId}
    </delete>
    <select id="getUserByProjectId" resultMap="MinUserMap" >
        SELECT p.user_id,u.name,p.project_id
        FROM work_user_project p JOIN work_user u ON p.user_id = u.id
        WHERE project_id = #{projectId}
    </select>

    <select id="selectUserIdByProjectIds" resultType="java.lang.Integer">
        select distinct user_id
        from work_user_project up join work_project p on up.project_id = p.id
        <where>
            <if test="isConclusion != null">
                AND p.is_conclusion = #{isConclusion}
            </if>
            <if test="deptId != null and managerId == null">
                AND p.dept_id = #{deptId}
            </if>
            <if test="deptId != null and managerId != null">
                AND (p.dept_id = #{deptId} OR p.manager_id = #{managerId})
            </if>
            <if test="managerId != null and deptId == null">
                AND p.manager_id = #{managerId}
            </if>
        </where>
    </select>

</mapper>
