<?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.pms.project.mapper.PmsProjectRepositoryMapper">

    <resultMap type="com.microservices.pms.project.domain.PmsProjectRepository" id="PmsProjectRepositoryResult">
        <result property="id"    column="id"    />
        <result property="pmsProjectId"    column="pms_project_id"    />
        <result property="projectRepositoryId"    column="project_repository_id"    />
        <result property="isTop"    column="is_top"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDeleted"    column="is_deleted"    />
        <result property="reservedField1"    column="reserved_field_1"    />
        <result property="reservedField2"    column="reserved_field_2"    />
    </resultMap>

    <sql id="selectPmsProjectRepositoryVo">
        select id, pms_project_id, project_repository_id, is_top, create_time, create_by, update_time, is_deleted, reserved_field_1, reserved_field_2 from pms_project_repository
    </sql>

    <select id="selectPmsProjectRepositoryList" parameterType="PmsProjectRepository" resultMap="PmsProjectRepositoryResult">
        <include refid="selectPmsProjectRepositoryVo"/>
        <where>  
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <if test="projectRepositoryId != null "> and project_repository_id = #{projectRepositoryId}</if>
            <if test="isTop != null "> and is_top = #{isTop}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="updateTime != null "> and update_time = #{updateTime}</if>
            <if test="isDeleted != null "> and is_deleted = #{isDeleted}</if>
            <if test="reservedField1 != null  and reservedField1 != ''"> and reserved_field_1 = #{reservedField1}</if>
            <if test="reservedField2 != null  and reservedField2 != ''"> and reserved_field_2 = #{reservedField2}</if>
        </where>
    </select>
    
    <select id="selectPmsProjectRepositoryById" parameterType="Long" resultMap="PmsProjectRepositoryResult">
        <include refid="selectPmsProjectRepositoryVo"/>
        where id = #{id}
    </select>
    <select id="countRepositoryByProjectId" resultType="java.lang.Integer">
        select count(*)
        from pms_project_repository
        where pms_project_id = #{projectId}
    </select>
    <select id="selectPmsProjectRepoIdsByProjectId" resultType="java.lang.Integer">
        select project_repository_id
        from pms_project_repository
        where pms_project_id = #{pmsProjectId}
    </select>
    <select id="selectPmsProjectRepositoryCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project_repository
        <where>
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <if test="projectRepositoryId != null "> and project_repository_id = #{projectRepositoryId}</if>
            <if test="isTop != null "> and is_top = #{isTop}</if>
        </where>
    </select>
    <select id="selectLinkedRepositoryIdsByEnterpriseId"  resultType="java.lang.Long">
        select DISTINCT(ppr.project_repository_id)
        from pms_project_repository ppr
        left join pms_project pp on ppr.pms_project_id = pp.id
        where pp.pms_enterprise_id = #{enterpriseId}
    </select>
    <select id="countPmsProjectRepositoryByProjectRepositoryId" resultType="java.lang.Integer">
        select count(*)
        from pms_project_repository
        where project_repository_id = #{projectRepositoryId}
    </select>

    <insert id="insertPmsProjectRepository" parameterType="PmsProjectRepository" useGeneratedKeys="true" keyProperty="id">
        insert into pms_project_repository
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pmsProjectId != null">pms_project_id,</if>
            <if test="projectRepositoryId != null">project_repository_id,</if>
            <if test="isTop != null">is_top,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDeleted != null">is_deleted,</if>
            <if test="reservedField1 != null">reserved_field_1,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pmsProjectId != null">#{pmsProjectId},</if>
            <if test="projectRepositoryId != null">#{projectRepositoryId},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDeleted != null">#{isDeleted},</if>
            <if test="reservedField1 != null">#{reservedField1},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
         </trim>
    </insert>

    <update id="updatePmsProjectRepository" parameterType="PmsProjectRepository">
        update pms_project_repository
        <trim prefix="SET" suffixOverrides=",">
            <if test="pmsProjectId != null">pms_project_id = #{pmsProjectId},</if>
            <if test="projectRepositoryId != null">project_repository_id = #{projectRepositoryId},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
            <if test="reservedField1 != null">reserved_field_1 = #{reservedField1},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProjectRepositoryById" parameterType="Long">
        delete from pms_project_repository where id = #{id}
    </delete>

    <delete id="deletePmsProjectRepositoryByIds" parameterType="String">
        delete from pms_project_repository where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsProjectRepositoryByProjectId">
        delete from pms_project_repository where pms_project_id = #{projectId}
    </delete>
</mapper>