<?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="com.jeeplus.modules.ruralprojectrecords.dao.RuralWorkProjectUserDao">


    <insert id="batchInsert" parameterType="java.util.Map">
        INSERT INTO work_project_user
        (user_id , project_id , is_master,del_flag)
        VALUES
        <foreach collection="memberIds" item="str" separator=",">
            (#{str} , #{projectId} , #{isMaster}, '0')
        </foreach>
    </insert>

    <delete id="deleteProjectMembers">
        DELETE FROM work_project_user
        WHERE project_id = #{projectId}
        AND is_master = #{isMaster}
    </delete>

    <delete id="deleteProjectMasterMembers">
        DELETE FROM work_project_user
        WHERE project_id = #{projectId}
        <if test="memberIds != null">
            AND user_id IN <foreach collection="memberIds" item="str" separator="," open="(" close=")">#{str}</foreach>
        </if>
       /* AND is_master = '0';*/
    </delete>

    <update id="deleteUsers">
        UPDATE work_project_user SET del_flag = #{delFlag}
        WHERE project_id = #{id}
        AND user_id = #{userId}
        AND is_master = '0'
        AND del_flag ='0'
    </update>
    <select id="isDelFalg" resultType="com.jeeplus.modules.sys.entity.User">
        SELECT u.id AS "id",
        u.no,
        u.name,
        u.email,
        u.phone,
        u.mobile,
        u.office_id AS "office.id",
        o.top_company AS "office.name",
        a.del_flag AS "delFlag",
        j.name as "basicInfo.jobGrade.name"
        from work_project_user a
        LEFT JOIN sys_user u ON u.id = a.user_id
        LEFT JOIN work_staff_achives w ON w.user_id = a.user_id
        LEFT JOIN work_job_grade j ON j.id = w.job_grade
        LEFT JOIN sys_office o ON o.id = u.office_id
        WHERE /*a.is_master = '0' AND*/ a.project_id = #{projectId}
        <if test="delFlag !=null and delFlag !=''">
            AND a.del_flag = #{delFlag}
        </if>
    </select>

    <insert id="insertUsers">
        INSERT INTO work_project_user( project_id ,user_id , is_master,del_flag)
        <foreach collection="users" item="user" separator=" union all ">
            SELECT #{id}, #{user.id},'0','0' FROM dual
        </foreach>
    </insert>

    <select id="queryProjectUsers" resultType="com.jeeplus.modules.sys.entity.User">
        SELECT u.id,u.name,u.photo from work_project_user a, sys_user u WHERE a.user_id = u.id and a.project_id = #{projectId} and a.is_master = #{isMaster}
    </select>
    <select id="projectUsers" resultType="com.jeeplus.modules.sys.entity.User">
        SELECT u.id,u.name,u.photo from work_project_user a, sys_user u WHERE a.user_id = u.id and a.project_id = #{projectId} and a.del_flag = '0'
    </select>
    <select id="queryAllProjectUsers" resultType="com.jeeplus.modules.sys.entity.User">
        SELECT u.id,u.name,u.photo from work_project_user a, sys_user u WHERE a.user_id = u.id and a.project_id = #{projectId} and a.del_flag = '0'
    </select>

    <update id="reInsertUser" parameterType="RuralProjectRecords">
        update work_project_user
        set del_flag = #{delFlag}
        where project_id = #{id} and user_id = #{userId}
    </update>
</mapper>