<?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.ruixuan.jinniu.mapper.KuaishouProjectMemberMapper">

    <resultMap type="com.ruixuan.jinniu.entity.KuaishouProjectMember" id="KuaishouProjectMemberResult">
        <result property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="roleKey" column="role_key"/>
        <result property="roleName" column="role_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectKuaishouProjectMemberVo">
        select id,
               project_id,
               project_name,
               user_id,
               user_name,
               role_key,
               role_name,
               create_time,
               update_time
        from kuaishou_project_member
    </sql>

    <select id="selectKuaishouProjectMemberList" parameterType="com.ruixuan.jinniu.entity.KuaishouProjectMember"
            resultMap="KuaishouProjectMemberResult">
        <include refid="selectKuaishouProjectMemberVo"/>
        <where>
            <if test="projectId != null ">and project_id = #{projectId}</if>
            <if test="projectName != null  and projectName != ''">and project_name like concat('%', #{projectName},
                '%')
            </if>
            <if test="userId != null  and userId != ''">and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="roleKey != null  and roleKey != ''">and role_key = #{roleKey}</if>
            <if test="roleName != null  and roleName != ''">and role_name like concat('%', #{roleName}, '%')</if>
        </where>
    </select>

    <select id="selectKuaishouProjectMemberById" parameterType="Long" resultMap="KuaishouProjectMemberResult">
        <include refid="selectKuaishouProjectMemberVo"/>
        where id = #{id}
    </select>

    <select id="queryProjectUsers" parameterType="Long" resultMap="KuaishouProjectMemberResult">
        <include refid="selectKuaishouProjectMemberVo"/>
        where project_id = #{projectId}
    </select>

    <insert id="insertKuaishouProjectMember" parameterType="com.ruixuan.jinniu.entity.KuaishouProjectMember"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into kuaishou_project_member
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="projectName != null">project_name,</if>
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="roleKey != null and roleKey != ''">role_key,</if>
            <if test="roleName != null and roleName != ''">role_name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="projectName != null">#{projectName},</if>
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="roleKey != null and roleKey != ''">#{roleKey},</if>
            <if test="roleName != null and roleName != ''">#{roleName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <insert id="replaceBatch">
        replace into kuaishou_project_member(
        project_id,
        project_name,
        user_id,
        user_name,
        role_key,
        role_name
        )
        values
        <foreach collection="list" item="member" separator=",">
            (
            #{member.projectId},
            #{member.projectName},
            #{member.userId},
            #{member.userName},
            #{member.roleKey},
            #{member.roleName}
            )
        </foreach>
    </insert>

    <update id="updateKuaishouProjectMember" parameterType="com.ruixuan.jinniu.entity.KuaishouProjectMember">
        update kuaishou_project_member
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="projectName != null">project_name = #{projectName},</if>
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="roleKey != null and roleKey != ''">role_key = #{roleKey},</if>
            <if test="roleName != null and roleName != ''">role_name = #{roleName},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKuaishouProjectMemberById" parameterType="Long">
        delete
        from kuaishou_project_member
        where id = #{id}
    </delete>

    <delete id="deleteProjectUsers">
        delete
        from kuaishou_project_member
        where project_id = #{projectId}
          AND user_id = #{userId}
    </delete>
</mapper>