<?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.persagy.adm.diagram.dao.DiagramMapper">

    <resultMap id="diagramBaseMap" type="com.persagy.adm.diagram.entity.DiagramEntity">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="VARCHAR"/>
        <result column="bt_system" property="btSystem" jdbcType="VARCHAR"/>
        <result column="template_id" property="templateId" jdbcType="VARCHAR"/>
        <result column="project_id" property="projectId" jdbcType="VARCHAR"/>
        <result column="system_id" property="systemId" jdbcType="VARCHAR"/>
        <result column="group_code" property="groupCode" jdbcType="VARCHAR"/>
        <result column="state" property="state" jdbcType="VARCHAR"/>
        <result column="diagram_content" property="diagramContent" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="creation_time" property="creationTime" jdbcType="CHAR"/>
        <result column="modifier" property="modifier" jdbcType="VARCHAR"/>
        <result column="modified_time" property="modifiedTime" jdbcType="CHAR"/>
        <result column="valid" property="valid" jdbcType="TINYINT"/>
        <result column="ts" property="ts" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="baseSql">
        id,name,remark,type,bt_system,template_id,project_id,system_id,group_code,diagram_content,
        creator,creation_time,modifier,modified_time,valid,ts,state
    </sql>

    <insert id="saveDiagram">
        INSERT INTO diagram
        (id,name,remark,type,bt_system,template_id,
        project_id,system_id,group_code,diagram_content,
        creator,creation_time,modifier,modified_time,state)
        VALUES
        (#{id},#{name},#{remark},#{type},#{btSystem},#{templateId},
        #{projectId},#{systemId},#{groupCode},#{diagramContent},
        #{creator}, #{creationTime},#{modifier},#{modifiedTime},#{state})
    </insert>

    <delete id="deleteByDiagramId">
        DELETE FROM diagram
        WHERE id=#{diagramId}
    </delete>

    <select id="getDiagram" resultMap="diagramBaseMap">
        SELECT
        <include refid="baseSql"/>
        FROM diagram
        WHERE id=#{diagramId}
    </select>

    <select id="getDiagrams" resultMap="diagramBaseMap">
        SELECT
        <include refid="baseSql"/>
        FROM diagram
        WHERE valid = 1
        <if test="null != diagramType and diagramType.length > 0">
            AND type=#{diagramType}
        </if>
        <if test="null != projectId and projectId.length > 0">
            AND project_id=#{projectId}
        </if>
        <if test="null != systemId and systemId.length > 0">
            AND system_id=#{systemId}
        </if>
        <if test="null != groupCode and groupCode.length > 0">
            AND group_code=#{groupCode}
        </if>
        <if test="null != name and name.length > 0">
            AND name LIKE CONCAT('%',#{name},'%')
        </if>
        <if test="null != flag and (flag == false) ">
            AND state = 'Publish'
        </if>
    </select>

    <select id="getDiagramsBySystemIds" resultMap="diagramBaseMap">
        SELECT
        <include refid="baseSql"/>
        FROM diagram
        WHERE valid = 1
        <if test="systemIds !=null and systemIds.size()>0">
            <foreach collection="systemIds" open="AND system_id IN(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectByName" resultMap="diagramBaseMap">
        SELECT
        <include refid="baseSql"/>
        FROM diagram
        WHERE project_id=#{projectId} AND name=#{name} AND valid = 1
    </select>

</mapper>


