<?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.PmsProductLibraryComponentMapper">

    <resultMap type="PmsProductLibraryComponent" id="PmsProductLibraryComponentResult">
        <result property="id" column="id"/>
        <result property="identifier" column="identifier"/>
        <result property="path" column="path"/>
        <result property="group" column="group"/>
        <result property="name" column="name"/>
        <result property="repoId" column="repo_id"/>
        <result property="version" column="version"/>
        <result property="fileIdentifier" column="file_identifier"/>
        <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="reservedField1" column="reserved_field_1"/>
        <result property="reservedField2" column="reserved_field_2"/>
        <result property="fromType" column="from_type"/>
    </resultMap>

    <sql id="selectPmsProductLibraryComponentVo">
        select id,
               identifier,
               path,
               `group`,
               name,
               repo_id,
               version,
               file_identifier,
               dept_id,
               pms_enterprise_id,
               update_time,
               update_by,
               create_time,
               create_by,
               del_flag,
               attributes,
               reserved_field_1,
               reserved_field_2,
               from_type
        from pms_product_library_component
    </sql>

    <sql id="selectPmsProductLibraryComponentAliasVo">
        select pplc.id,
               pplc.identifier,
               pplc.path,
               pplc.`group`,
               pplc.name,
               pplc.repo_id,
               pplc.version,
               pplc.file_identifier,
               pplc.dept_id,
               pplc.pms_enterprise_id,
               pplc.update_time,
               pplc.update_by,
               pplc.create_time,
               pplc.create_by,
               pplc.del_flag,
               pplc.attributes,
               pplc.reserved_field_1,
               pplc.reserved_field_2,
               pplc.from_type,
               pe.enterprise_identifier,
               pplr.format
        from pms_product_library_component as pplc
                 left join pms_enterprise as pe on pplc.pms_enterprise_id = pe.id
                 left join pms_product_library_repositories as pplr on pplr.id = pplc.repo_id
    </sql>

    <select id="selectPmsProductLibraryComponentList" parameterType="PmsProductLibraryComponent"
            resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        <where>
            <if test="identifier != null  and identifier != ''">and identifier = #{identifier}</if>
            <if test="path != null  and path != ''">and `path` = #{path}</if>
            <if test="group != null  and group != ''">and `group` = #{group}</if>
            <if test="name != null  and name != ''">and `name` like concat('%', #{name}, '%')</if>
            <if test="repoId != null  and repoId != ''">and repo_id = #{repoId}</if>
            <if test="version != null  and version != ''">and version = #{version}</if>
            <if test="fileIdentifier != null  and fileIdentifier != ''">and file_identifier = #{fileIdentifier}</if>
            <if test="deptId != null ">and dept_id = #{deptId}</if>
            <if test="pmsEnterpriseId != null ">and pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="attributes != null  and attributes != ''">and attributes = #{attributes}</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>
            <if test="fromType != null  and fromType != ''">and from_type = #{fromType}</if>
        </where>
    </select>

    <select id="selectPmsProductLibraryComponentById" parameterType="Long" resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        where id = #{id}
    </select>
    <select id="selectPmsProductLibraryComponentByNameAndRepoId" resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        where name = #{name}
        and repo_id = #{repoId}
        and pms_enterprise_id = #{pmsEnterpriseId}
    </select>
    <select id="selectPmsProductLibraryComponentByPath" resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        where path = #{path}
        and repo_id = #{repoId}
        and pms_enterprise_id = #{pmsEnterpriseId}
    </select>
    <select id="selectPmsProductLibraryComponentUnderThePath"
            resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        where path like concat(#{path}, '/%')
        and repo_id = #{repoId}
        and pms_enterprise_id = #{pmsEnterpriseId}
    </select>
    <select id="selectProductRepoComponentList" resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentAliasVo"/>
        <where>
            <if test="excludeIds != null  and excludeIds != ''">
                and pplc.id not in
                <foreach item="item" index="index" collection="excludeIds.split(',')" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null  and endTime != null">
                and pplc.create_time between #{startTime} and #{endTime}
            </if>
            <if test="productRepoName != null  and productRepoName != ''">and pplr.`name` = #{productRepoName}</if>
            <if test="pmsEnterpriseIdentifier != null ">and pe.enterprise_identifier = #{pmsEnterpriseIdentifier}</if>
            and pplr.format='product'
        </where>
        order by pplc.create_time desc,pplc.update_time desc
    </select>
    <select id="selectPmsProductLibraryComponentByIdentifier" resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        where repo_id = #{repoId}
        and identifier = #{identifier}
    </select>
    <select id="selectComponentListFromPipelineByAssetIdList" resultMap="PmsProductLibraryComponentResult">
        <include refid="selectPmsProductLibraryComponentVo"/>
        where identifier in
        <foreach item="assetId" collection="assetIdList" open="(" separator="," close=")">
            #{assetId}
        </foreach>
        and from_type='pipeline'
    </select>

    <insert id="insertPmsProductLibraryComponent" parameterType="PmsProductLibraryComponent" useGeneratedKeys="true"
            keyProperty="id">
        insert into pms_product_library_component
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="identifier != null and identifier != ''">identifier,</if>
            <if test="path != null and path != ''">`path`,</if>
            <if test="group != null and group != ''">`group`,</if>
            <if test="name != null and name != ''">`name`,</if>
            <if test="repoId != null and repoId != ''">repo_id,</if>
            <if test="version != null">version,</if>
            <if test="fileIdentifier != null">file_identifier,</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="reservedField1 != null">reserved_field_1,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
            <if test="fromType != null">from_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="identifier != null and identifier != ''">#{identifier},</if>
            <if test="path != null and path != ''">#{path},</if>
            <if test="group != null and group != ''">#{group},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="repoId != null and repoId != ''">#{repoId},</if>
            <if test="version != null">#{version},</if>
            <if test="fileIdentifier != null">#{fileIdentifier},</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="reservedField1 != null">#{reservedField1},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
            <if test="fromType != null">#{fromType},</if>
        </trim>
    </insert>

    <update id="updatePmsProductLibraryComponent" parameterType="PmsProductLibraryComponent">
        update pms_product_library_component
        <trim prefix="SET" suffixOverrides=",">
            <if test="identifier != null and identifier != ''">identifier = #{identifier},</if>
            <if test="path != null and path != ''">path = #{path},</if>
            <if test="group != null and group != ''">`group` = #{group},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="repoId != null and repoId != ''">repo_id = #{repoId},</if>
            <if test="version != null">version = #{version},</if>
            <if test="fileIdentifier != null">file_identifier = #{fileIdentifier},</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="reservedField1 != null">reserved_field_1 = #{reservedField1},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
            <if test="fromType != null">from_type = #{fromType},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="batchUpdateAssetFileIdentifier">
        update pms_product_library_component
        set file_identifier = #{fileIdentifier}
        where identifier = #{assetId}
    </update>

    <delete id="deletePmsProductLibraryComponentById" parameterType="Long">
        delete
        from pms_product_library_component
        where id = #{id}
    </delete>

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