<?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.zioinfo.esn.mapper.TemplateMapper">

  <resultMap id="templateMap" type="com.zioinfo.esn.domain.TemplateVo">
    <id property="id"            column="id"/>
    <result property="tenantCode"    column="tenant_code"/>
    <result property="templateCode"  column="template_code"/>
    <result property="templateName"  column="template_name"/>
    <result property="templateType"  column="template_type"/>
    <result property="width"         column="width"/>
    <result property="height"        column="height"/>
    <result property="description"   column="description"/>
    <result property="layoutJson"    column="layout_json"/>
    <result property="active"        column="is_active"/>
    <result property="createdAt"     column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="templateMap">
    SELECT id, tenant_code, template_code, template_name, template_type,
           width, height, description, layout_json, is_active, created_at
    FROM esn_template
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
      <if test="templateType != null and templateType != ''">AND template_type = #{templateType}</if>
    </where>
    ORDER BY template_code
  </select>

  <select id="findById" resultMap="templateMap">
    SELECT id, tenant_code, template_code, template_name, template_type,
           width, height, description, layout_json, is_active, created_at
    FROM esn_template WHERE id = #{id}
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO esn_template (tenant_code, template_code, template_name, template_type,
                               width, height, description, layout_json, is_active)
    VALUES (#{tenantCode}, #{templateCode}, #{templateName}, #{templateType},
            #{width}, #{height}, #{description}, #{layoutJson}, #{active})
  </insert>

  <update id="update">
    UPDATE esn_template SET
      template_name = #{templateName},
      template_type = #{templateType},
      width         = #{width},
      height        = #{height},
      description   = #{description},
      layout_json   = #{layoutJson},
      is_active     = #{active}
    WHERE id = #{id}
  </update>

  <delete id="delete">DELETE FROM esn_template WHERE id = #{id}</delete>

</mapper>
