<?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.salpa.subject.mapper.ProjectSyncRuleMapper">

    <resultMap type="com.salpa.subject.domain.MonitorProjectSyncRule" id="MonitorProjectSyncRuleResult">
        <id     property="id"            column="id"             />
        <id     property="projectId"            column="project_id"             />
        <result property="ignoreTableNameRegexArray"          column="ignore_table_name_regex_array"           />
        <result property="ignoreColumnNameRegexArray"   column="ignore_column_name_regex_array"    />
        <result property="autoSync"              column="is_auto_sync"                />
        <result property="autoSyncCron"             column="auto_sync_cron"              />
        <result property="updateAt"             column="update_at"              />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="selectByProjectId" resultMap="MonitorProjectSyncRuleResult">
        SELECT ignore_table_name_regex_array,ignore_column_name_regex_array,is_auto_sync,auto_sync_cron
        FROM monitor_project_sync_rule WHERE project_id = #{id}
    </select>

    <select id="selectByIsAutoSyncAndNotInProjectIds" resultMap="MonitorProjectSyncRuleResult">
        SELECT ignore_table_name_regex_array,ignore_column_name_regex_array,is_auto_sync,auto_sync_cron
        FROM monitor_project_sync_rule WHERE is_auto_sync = #{autoSync}
        <if test="list != null and list.size() > 0">
            AND project_id not in
            <foreach collection="list" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <insert id="addProjectSyncRule">
        insert into monitor_project_sync_rule(
        <if test="projectSyncRule.projectId != null">project_id,</if>
        ignore_table_name_regex_array,
        ignore_column_name_regex_array,
        <if test="projectSyncRule.autoSyncCron != null and projectSyncRule.autoSyncCron != ''">auto_sync_cron,</if>
        is_auto_sync
        )values(
        <if test="projectSyncRule.projectId != null">#{projectSyncRule.projectId},</if>
        #{tableName},
        #{columnName},
        <if test="projectSyncRule.autoSyncCron != null and projectSyncRule.autoSyncCron != ''">#{projectSyncRule.autoSyncCron},</if>
        #{projectSyncRule.autoSync}
        )
    </insert>

    <delete id="deletProjectSyncRule">
        DELETE FROM monitor_project_sync_rule WHERE project_id = #{id}
    </delete>

    <select id="existsByProjectIdAndCron" resultType="boolean">
        SELECT count(*)
        FROM monitor_project_sync_rule WHERE project_id = #{projectId} AND auto_sync_cron = #{cron}
    </select>

    <select id="selectByIsAutoSyncAndProjectIds" resultMap="MonitorProjectSyncRuleResult">
        SELECT ignore_table_name_regex_array,ignore_column_name_regex_array,is_auto_sync,auto_sync_cron
        FROM monitor_project_sync_rule WHERE is_auto_sync = #{autoSync}
        <if test="list != null and list.size() > 0">
            AND project_id in
            <foreach collection="list" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>
</mapper>