<?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.simuwang.base.mapper.EmailTemplateMappingMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.EmailTemplateMappingDO">
        <id column="id" property="id"/>
        <result column="template_id" property="templateId"/>
        <result column="title" property="title"/>
        <result column="email" property="email"/>
        <result column="company_name" property="companyName"/>
        <result column="company_id" property="companyId"/>
        <result column="status" property="status"/>
        <result column="isvalid" property="isvalid"/>
        <result column="creatorid" property="creatorId"/>
        <result column="createtime" property="createTime"/>
        <result column="updaterid" property="updaterId"/>
        <result column="updatetime" property="updateTime"/>
        <result column="template_name" property="templateName"/>
    </resultMap>
    <insert id="saveEmailTemplateMappingDO">
        insert into PPW_EMAIL.email_template_mapping(template_id,title,email,company_name,company_id,status,isvalid,creatorid,createtime,updaterid,updatetime)
        values
            (#{templateId},#{title},#{email},#{companyName},#{companyId},#{status},#{isvalid},#{creatorId},#{createTime},#{updaterId},#{updateTime})
    </insert>
    <update id="deleteTemplateSetting">
        update  PPW_EMAIL.email_template_mapping set isvalid=0,updatetime=sysdate(),updaterid=#{userId}
        where isvalid=1 and id in
        <foreach item="id" collection="idList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="updateEmailTemplateMappingDO">
        update PPW_EMAIL.email_template_mapping set template_id=#{templateId},title=#{title},email=#{email},company_name=#{companyName},company_id=#{companyId},
                                                    status=#{status},updatetime=#{updateTime},updaterid=#{updaterId}
        where isvalid=1 and id=#{id}
    </update>

    <select id="queryByEmail" resultType="com.simuwang.base.pojo.dos.EmailTemplateInfoDO">
        select t1.template_id as id,
               t2.direction as direction,
               t2.type as type,
               t2.start_index as startIndex,
               t2.end_index as endIndex
        from PPW_EMAIL.email_template_mapping t1
            join PPW_EMAIL.email_template_info t2 on t2.id = t1.template_id and t2.isvalid = 1 and t2.status = 1
        where t1.email = #{email}
          and t2.type = #{type}
          and t1.isvalid = 1
          and t2.status = 1
    </select>
    <select id="searchTemplateSettingList" resultMap="BaseResultMap">
        select mapping.id,mapping.template_id,mapping.title,mapping.email,mapping.company_name,mapping.company_id,mapping.status,
        t.name as template_name
        from PPW_EMAIL.email_template_mapping mapping
            join PPW_EMAIL.email_template_info t on t.id = mapping.template_id and t.isvalid = 1 and t.status = 1
        where mapping.isvalid=1
        <if test="title != null and title != ''">
            and mapping.title like concat('%',#{title},'%')
        </if>
        <if test="email != null and email != ''">
            and mapping.email like concat('%',#{email},'%')
        </if>
        <if test="companyName != null and companyName != ''">
            and mapping.company_name like concat('%',#{companyName},'%')
        </if>
        <if test="templateName != null and templateName != ''">
            and t.name like concat('%',#{templateName},'%')
        </if>
        <if test="status != null and status !=''">
            and mapping.status=#{status}
        </if>
        limit #{offset},#{pageSize}
    </select>
    <select id="countTemplateSettingList" resultType="java.lang.Long">
        select count(mapping.id)
        from PPW_EMAIL.email_template_mapping mapping
        join PPW_EMAIL.email_template_info t on t.id = mapping.template_id and t.isvalid = 1 and t.status = 1
        where mapping.isvalid=1
        <if test="title != null and title != ''">
            and mapping.title like concat('%',#{title},'%')
        </if>
        <if test="email != null and email != ''">
            and mapping.email like concat('%',#{email},'%')
        </if>
        <if test="companyName != null and companyName != ''">
            and mapping.company_name like concat('%',#{companyName},'%')
        </if>
        <if test="templateName != null and templateName != ''">
            and t.name like concat('%',#{templateName},'%')
        </if>
        <if test="status != null and status !=''">
            and mapping.status=#{status}
        </if>
    </select>
    <select id="searchTemplateById" resultMap="BaseResultMap">
        select mapping.id,mapping.template_id,mapping.title,mapping.email,mapping.company_name,mapping.company_id,mapping.status,
               t.name as template_name
        from PPW_EMAIL.email_template_mapping mapping
                 join PPW_EMAIL.email_template_info t on t.id = mapping.template_id and t.isvalid = 1 and t.status = 1
        where mapping.isvalid=1 and mapping.id=#{id}
    </select>

</mapper>