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

    <resultMap type="com.salpa.subject.domain.MonitorProject" id="MonitorProjectResult">
        <id     property="id"            column="id"             />
        <result property="projectName"          column="project_name"           />
        <result property="projectDescription"   column="project_description"    />
        <result property="subjectId"             column="subject_id"              />
        <result property="deleted"              column="deleted"                />
        <result property="createAt"             column="create_at"              />
    </resultMap>

    <select id="getList" resultType="map">
        SELECT p.id id,p.project_name projectName,p.project_description projectDescription,p.create_at createAt,p.subject_id subjectId,
        d.id dataSourceId,d.database_name databaseName,d.schema_name schemaName,d.database_type databaseType,t.icon,
        r.is_auto_sync isAutoSync,r.auto_sync_cron autoSyncCron
        FROM monitor_project p LEFT JOIN monitor_data_source d ON p.id = d.project_id LEFT JOIN monitor_project_sync_rule r ON p.id = r.project_id LEFT JOIN monitor_database_type t ON d.database_type = t.database_type
        WHERE p.subject_id = #{subjectId} and p.deleted = 0
        <if test="projectName != null and projectName != ''">
            AND p.project_name like concat('%', #{projectName}, '%')
        </if>
        <if test="databaseName != null and databaseName != ''">
            AND d.database_name like concat('%', #{databaseName}, '%')
        </if>
        <if test="schemaName != null and schemaName != ''">
            AND d.schema_name like concat('%', #{schemaName}, '%')
        </if>
        <if test="databaseType != null and databaseType != ''">
            AND d.database_type = #{databaseType}
        </if>
    </select>

    <select id="checkProjectNameUnique" resultType="int">
        SELECT count(1) from monitor_project where project_name = #{projectName} and deleted = 0 limit 1
    </select>

    <select id="checkDatabaseNameUnique" resultType="int">
        SELECT count(1) from monitor_data_source where database_name = #{databaseName} limit 1
    </select>

    <select id="selectById" resultMap="MonitorProjectResult">
        SELECT id,project_name,project_description,create_at,subject_id
        FROM monitor_project WHERE id = #{id}
    </select>

    <insert id="addProject" parameterType="MonitorProject" useGeneratedKeys="true" keyProperty="id">
        insert into monitor_project(
        <if test="subjectId != null">subject_id,</if>
        <if test="projectName != null and projectName != ''">project_name,</if>
        <if test="projectDescription != null and projectDescription != ''">project_description,</if>
        deleted
        )values(
        <if test="subjectId != null">#{subjectId},</if>
        <if test="projectName != null and projectName != ''">#{projectName},</if>
        <if test="projectDescription != null and projectDescription != ''">#{projectDescription},</if>
        0
        )
    </insert>

    <update id="updateProject">
        update monitor_project
        <set>
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            project_description = #{projectDescription},
            <if test="subjectId != null">subject_id = #{subjectId}</if>
        </set>
        where id = #{id}
    </update>

    <update id="deleteProject">
        UPDATE monitor_project SET deleted = 1 WHERE id = #{id}
    </update>
</mapper>