<?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.productLibrary.mapper.PmsProductLibraryRepositoriesMapper">

    <resultMap type="PmsProductLibraryRepositories" id="PmsProductLibraryRepositoriesResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="type" column="type"/>
        <result property="url" column="url"/>
        <result property="deptId" column="dept_id"/>
        <result property="pmsEnterpriseId" column="pms_enterprise_id"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="delFlag" column="del_flag"/>
        <result property="attributes" column="attributes"/>
        <result property="alias" column="alias"/>
        <result property="snapshotName" column="snapshot_name"/>
        <result property="format" column="format"/>
        <result property="pmsEnterpriseIdentifier" column="enterprise_identifier"/>
    </resultMap>

    <sql id="selectPmsProductLibraryRepositoriesAliasVo">
        select pplr.id,
               pplr.name,
               pplr.type,
               pplr.url,
               pplr.dept_id,
               pplr.pms_enterprise_id,
               pplr.update_time,
               pplr.update_by,
               pplr.create_time,
               pplr.create_by,
               pplr.del_flag,
               pplr.attributes,
               pplr.alias,
               pplr.snapshot_name,
               pplr.format,
               pe.enterprise_identifier
        from pms_product_library_repositories pplr
                 left join pms_enterprise as pe on pplr.pms_enterprise_id = pe.id
    </sql>

    <select id="selectPmsProductLibraryRepositoriesList" parameterType="PmsProductLibraryRepositories"
            resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        <where>
            <if test="name != null  and name != ''">and pplr.name like concat('%', #{name}, '%')</if>
            <if test="type != null  and type != ''">and pplr.type = #{type}</if>
            <if test="format != null  and format != ''">and pplr.format = #{format}</if>
            <if test="url != null  and url != ''">and pplr.url = #{url}</if>
            <if test="deptId != null ">and pplr.dept_id = #{deptId}</if>
            <if test="pmsEnterpriseId != null ">and pplr.pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="pmsEnterpriseIdentifier != null ">and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}</if>
            and pplr.format != 'product'
            and pplr.snapshot_name IS NULL
        </where>
    </select>

    <select id="selectPmsProductLibraryList" resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        <where>
            <if test="name != null  and name != ''">and pplr.name like concat('%', #{name}, '%')</if>
            <if test="pmsEnterpriseIdentifier != null ">and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}</if>
            and pplr.format = 'product'
        </where>
    </select>

    <select id="selectPmsProductLibrarySnapshotPageList" resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        <where>
            <if test="snapshotName != null  and snapshotName != ''">and pplr.snapshot_name like concat('%',
                #{snapshotName}, '%')
            </if>
            and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
            and pplr.name = #{repoName}
            and pplr.snapshot_name IS NOT NULL
        </where>
    </select>

    <select id="selectPmsProductLibraryRepositoriesById" parameterType="Long"
            resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        where pplr.id = #{id}
        and pplr.format != 'product'
        and pplr.snapshot_name IS NULL
    </select>
    <select id="selectPmsProductLibraryRepositoriesByNameCount" resultType="java.lang.Integer">
        select count(0)
        from pms_product_library_repositories pplr
                 left join pms_enterprise as pe on pplr.pms_enterprise_id = pe.id
        where pplr.name = #{name}
          and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
        <choose>
            <when test="format != null and format != 'product'">
                and pplr.format != 'product'
                and pplr.snapshot_name IS NULL
            </when>
            <otherwise>
                and pplr.format = 'product'
            </otherwise>
        </choose>
    </select>

    <select id="selectRepositoriesSnapshotBySnapshotNameCount" resultType="java.lang.Integer">
        select count(0)
        from pms_product_library_repositories pplr
                 left join pms_enterprise as pe on pplr.pms_enterprise_id = pe.id
        where pplr.snapshot_name = #{snapshotName}
          and pplr.name = #{repoName}
          and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
          and pplr.snapshot_name IS NOT NULL
    </select>
    <select id="selectPmsProductLibraryRepositoriesByName"
            resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        where pplr.name = #{name}
        and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
        and pplr.format != 'product'
        and pplr.snapshot_name IS NULL
    </select>
    <select id="selectPmsProductLibraryByName" resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        where pplr.name = #{name}
        and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
        and pplr.format = 'product'
    </select>
    <select id="selectPmsProductLibrarySnapshotByName" resultMap="PmsProductLibraryRepositoriesResult">
        <include refid="selectPmsProductLibraryRepositoriesAliasVo"/>
        where pplr.name = #{repoName}
        and pplr.snapshot_name = #{snapshotName}
        and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}
        and pplr.snapshot_name IS NOT NULL
    </select>

    <insert id="insertPmsProductLibraryRepositories" parameterType="PmsProductLibraryRepositories"
            useGeneratedKeys="true" keyProperty="id">
        insert into pms_product_library_repositories
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="type != null">type,</if>
            <if test="url != null">url,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="attributes != null">attributes,</if>
            <if test="alias != null">alias,</if>
            <if test="snapshotName != null">snapshot_name,</if>
            <if test="format != null">format,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="type != null">#{type},</if>
            <if test="url != null">#{url},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="pmsEnterpriseId != null">#{pmsEnterpriseId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="attributes != null">#{attributes},</if>
            <if test="alias != null">#{alias},</if>
            <if test="snapshotName != null">#{snapshotName},</if>
            <if test="format != null">#{format},</if>
        </trim>
    </insert>

    <update id="updatePmsProductLibraryRepositories" parameterType="PmsProductLibraryRepositories">
        update pms_product_library_repositories
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="type != null">type = #{type},</if>
            <if test="url != null">url = #{url},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id = #{pmsEnterpriseId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="attributes != null">attributes = #{attributes},</if>
            <if test="alias != null">alias = #{alias},</if>
            <if test="snapshotName != null">snapshot_name = #{snapshotName},</if>
            <if test="format != null">format = #{format},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProductLibraryRepositoriesById" parameterType="Long">
        delete
        from pms_product_library_repositories
        where id = #{id}
    </delete>

    <delete id="deletePmsProductLibraryRepositoriesByIds" parameterType="String">
        delete from pms_product_library_repositories where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>