<?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.pipeline.mapper.PmsCiPipelinesMapper">
    
    <resultMap type="PmsCiPipelines" id="PmsCiPipelinesResult">
        <result property="id"    column="id"    />
        <result property="pipelineName"    column="pipeline_name"    />
        <result property="pipelineStatus"    column="pipeline_status"    />
        <result property="fileName"    column="file_name"    />
        <result property="filePath"    column="file_path"    />
        <result property="login"    column="login"    />
        <result property="sync"    column="sync"    />
        <result property="repoName"    column="repo_name"    />
        <result property="repoIdentifier"    column="repo_identifier"    />
        <result property="branch"    column="branch"    />
        <result property="pipelineType"    column="pipeline_type"    />
        <result property="event"    column="event"    />
        <result property="sha"    column="sha"    />
        <result property="owner"    column="owner"    />
        <result property="deptId"    column="dept_id"    />
        <result property="pmsEnterpriseId"    column="pms_enterprise_id"    />
        <result property="pmsProjectId"    column="pms_project_id"    />
        <result property="pipelineTemplateJson"    column="pipeline_template_json"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
    </resultMap>

    <sql id="selectPmsCiPipelinesVo">
        select id, pipeline_name, pipeline_status, file_name, file_path, login, sync, repo_name, repo_identifier, branch, pipeline_type, event, sha, owner, dept_id, pms_enterprise_id, pms_project_id, pipeline_template_json, update_time, update_by, create_time, create_by from pms_ci_pipelines
    </sql>

    <select id="selectPmsCiPipelinesList" parameterType="PmsCiPipelines" resultMap="PmsCiPipelinesResult">
        <include refid="selectPmsCiPipelinesVo"/>
        <where>  
            <if test="pipelineName != null  and pipelineName != ''"> and pipeline_name like concat('%', #{pipelineName}, '%')</if>
            <if test="pipelineStatus != null  and pipelineStatus != ''"> and pipeline_status = #{pipelineStatus}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="login != null  and login != ''"> and login = #{login}</if>
            <if test="sync != null "> and sync = #{sync}</if>
            <if test="repoName != null  and repoName != ''"> and repo_name = #{repoName}</if>
            <if test="repoIdentifier != null  and repoIdentifier != ''"> and repo_identifier = #{repoIdentifier}</if>
            <if test="branch != null  and branch != ''"> and branch = #{branch}</if>
            <if test="pipelineType != null"> and pipeline_type = #{pipelineType}</if>
            <if test="event != null  and event != ''"> and event = #{event}</if>
            <if test="sha != null  and sha != ''"> and sha = #{sha}</if>
            <if test="owner != null  and owner != ''"> and owner = #{owner}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="pmsEnterpriseId != null "> and pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>

    <select id="selectPmsCiPipelinesCardList" parameterType="PmsCiPipelines" resultMap="PmsCiPipelinesResult">
        select owner,repo_identifier, repo_identifier as repo_name, pms_enterprise_id, GROUP_CONCAT(file_name) file_name, max(update_time) as update_time from pms_ci_pipelines
        <where>
            <if test="pipelineName != null  and pipelineName != ''"> and pipeline_name like concat('%', #{pipelineName}, '%')</if>
            <if test="pipelineStatus != null  and pipelineStatus != ''"> and pipeline_status = #{pipelineStatus}</if>
            <if test="fileName != null  and fileName != ''"> and file_name like concat('%', #{fileName}, '%')</if>
            <if test="login != null  and login != ''"> and login = #{login}</if>
            <if test="sync != null "> and sync = #{sync}</if>
            <if test="repoName != null  and repoName != ''"> and repo_name = #{repoName}</if>
            <if test="repoIdentifier != null  and repoIdentifier != ''"> and repo_identifier = #{repoIdentifier}</if>
            <if test="branch != null  and branch != ''"> and branch = #{branch}</if>
            <if test="pipelineType != null"> and pipeline_type = #{pipelineType}</if>
            <if test="event != null  and event != ''"> and event = #{event}</if>
            <if test="sha != null  and sha != ''"> and sha = #{sha}</if>
            <if test="owner != null  and owner != ''"> and owner = #{owner}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="pmsEnterpriseId != null "> and pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="pmsProjectId != null "> and pms_project_id = #{pmsProjectId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
        GROUP BY owner,repo_identifier
    </select>
    
    <select id="selectPmsCiPipelinesById" parameterType="Long" resultMap="PmsCiPipelinesResult">
        <include refid="selectPmsCiPipelinesVo"/>
        where id = #{id}
    </select>
    <select id="selectCountByPipelineNameAndRepoInfo" resultType="java.lang.Integer">
        select count(*)
        from pms_ci_pipelines
        where pipeline_name = #{pipelineName}
        and owner = #{owner}
        and repo_identifier = #{repoIdentifier}
        and branch = #{branch}
    </select>
    <select id="selectPmsCiPipelinesByRepoAndFileName" resultMap="PmsCiPipelinesResult">
        <include refid="selectPmsCiPipelinesVo"/>
        where file_name = #{fileName}
        and owner = #{repoOwner}
        and repo_identifier = #{repoIdentifier}
    </select>

    <insert id="insertPmsCiPipelines" parameterType="PmsCiPipelines" useGeneratedKeys="true" keyProperty="id">
        insert into pms_ci_pipelines
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pipelineName != null and pipelineName != ''">pipeline_name,</if>
            <if test="pipelineStatus != null and pipelineStatus != ''">pipeline_status,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="filePath != null and filePath != ''">file_path,</if>
            <if test="login != null">login,</if>
            <if test="sync != null">sync,</if>
            <if test="repoName != null">repo_name,</if>
            <if test="repoIdentifier != null">repo_identifier,</if>
            <if test="branch != null">branch,</if>
            <if test="pipelineType != null">pipeline_type,</if>
            <if test="event != null">event,</if>
            <if test="sha != null">sha,</if>
            <if test="owner != null">owner,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id,</if>
            <if test="pmsProjectId != null">pms_project_id,</if>
            <if test="pipelineTemplateJson != null">pipeline_template_json,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pipelineName != null and pipelineName != ''">#{pipelineName},</if>
            <if test="pipelineStatus != null and pipelineStatus != ''">#{pipelineStatus},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="filePath != null and filePath != ''">#{filePath},</if>
            <if test="login != null">#{login},</if>
            <if test="sync != null">#{sync},</if>
            <if test="repoName != null">#{repoName},</if>
            <if test="repoIdentifier != null">#{repoIdentifier},</if>
            <if test="branch != null">#{branch},</if>
            <if test="pipelineType != null">#{pipelineType},</if>
            <if test="event != null">#{event},</if>
            <if test="sha != null">#{sha},</if>
            <if test="owner != null">#{owner},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="pmsEnterpriseId != null">#{pmsEnterpriseId},</if>
            <if test="pmsProjectId != null">#{pmsProjectId},</if>
            <if test="pipelineTemplateJson != null">#{pipelineTemplateJson},</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>
         </trim>
    </insert>

    <update id="updatePmsCiPipelines" parameterType="PmsCiPipelines">
        update pms_ci_pipelines
        <trim prefix="SET" suffixOverrides=",">
            <if test="pipelineName != null and pipelineName != ''">pipeline_name = #{pipelineName},</if>
            <if test="pipelineStatus != null and pipelineStatus != ''">pipeline_status = #{pipelineStatus},</if>
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="filePath != null and filePath != ''">file_path = #{filePath},</if>
            <if test="login != null">login = #{login},</if>
            <if test="sync != null">sync = #{sync},</if>
            <if test="repoName != null">repo_name = #{repoName},</if>
            <if test="repoIdentifier != null">repo_identifier = #{repoIdentifier},</if>
            <if test="branch != null">branch = #{branch},</if>
            <if test="pipelineType != null">pipeline_type = #{pipelineType},</if>
            <if test="event != null">event = #{event},</if>
            <if test="sha != null">sha = #{sha},</if>
            <if test="owner != null">owner = #{owner},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id = #{pmsEnterpriseId},</if>
            <if test="pmsProjectId != null">pms_project_id = #{pmsProjectId},</if>
            <if test="pipelineTemplateJson != null">pipeline_template_json = #{pipelineTemplateJson},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsCiPipelinesById" parameterType="Long">
        delete from pms_ci_pipelines where id = #{id}
    </delete>

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