<?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.microservices.wiki.mapper.team_projects.TeamProjectsMapper">
    <resultMap id="BaseResultMap" type="com.microservices.wiki.domain.team_projects.TeamProjects">
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="organization_id" property="organizationId" jdbcType="BIGINT" />
    <result column="project_id" property="projectId" jdbcType="BIGINT" />
    <result column="team_id" property="teamId" jdbcType="BIGINT" />
    <result column="created_at" property="createdAt" jdbcType="TIMESTAMP" />
    <result column="updated_at" property="updatedAt" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, organization_id, project_id, team_id, created_at, updated_at
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select 
    <include refid="Base_Column_List" />
    from team_projects
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
    delete from team_projects
    where id = #{id,jdbcType=BIGINT}
  </delete>
    <insert id="insert" parameterType="com.microservices.wiki.domain.team_projects.TeamProjects">
    insert into team_projects (id, organization_id, project_id, 
      team_id, created_at, updated_at
      )
    values (#{id,jdbcType=BIGINT}, #{organizationId,jdbcType=BIGINT}, #{projectId,jdbcType=BIGINT}, 
      #{teamId,jdbcType=BIGINT}, #{createdAt,jdbcType=TIMESTAMP}, #{updatedAt,jdbcType=TIMESTAMP}
      )
  </insert>
    <insert id="insertSelective" parameterType="com.microservices.wiki.domain.team_projects.TeamProjects">
    insert into team_projects
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="organizationId != null" >
        organization_id,
      </if>
      <if test="projectId != null" >
        project_id,
      </if>
      <if test="teamId != null" >
        team_id,
      </if>
      <if test="createdAt != null" >
        created_at,
      </if>
      <if test="updatedAt != null" >
        updated_at,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="organizationId != null" >
        #{organizationId,jdbcType=BIGINT},
      </if>
      <if test="projectId != null" >
        #{projectId,jdbcType=BIGINT},
      </if>
      <if test="teamId != null" >
        #{teamId,jdbcType=BIGINT},
      </if>
      <if test="createdAt != null" >
        #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="updatedAt != null" >
        #{updatedAt,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.microservices.wiki.domain.team_projects.TeamProjects">
    update team_projects
    <set >
      <if test="organizationId != null" >
        organization_id = #{organizationId,jdbcType=BIGINT},
      </if>
      <if test="projectId != null" >
        project_id = #{projectId,jdbcType=BIGINT},
      </if>
      <if test="teamId != null" >
        team_id = #{teamId,jdbcType=BIGINT},
      </if>
      <if test="createdAt != null" >
        created_at = #{createdAt,jdbcType=TIMESTAMP},
      </if>
      <if test="updatedAt != null" >
        updated_at = #{updatedAt,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
    <update id="updateByPrimaryKey" parameterType="com.microservices.wiki.domain.team_projects.TeamProjects">
    update team_projects
    set organization_id = #{organizationId,jdbcType=BIGINT},
      project_id = #{projectId,jdbcType=BIGINT},
      team_id = #{teamId,jdbcType=BIGINT},
      created_at = #{createdAt,jdbcType=TIMESTAMP},
      updated_at = #{updatedAt,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>
  <select id="doesMemberExist" resultType="java.lang.Integer">
    SELECT count(*)
    FROM `team_projects`
    INNER JOIN `teams` ON `teams`.`id` = `team_projects`.`team_id`
    INNER JOIN `team_users` ON `team_users`.`team_id` = `teams`.`id`
    WHERE `team_projects`.`project_id` = #{projectId} AND `team_users`.`user_id` = #{userId}
  </select>
</mapper>