<?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.yushu.system.mapper.SysMailConfigMapper">
    
    <resultMap type="SysMailConfig" id="SysMailConfigResult">
        <result property="configId"     column="config_id"     />
        <result property="configName"   column="config_name"   />
        <result property="mailType"     column="mail_type"     />
        <result property="host"         column="host"          />
        <result property="port"         column="port"          />
        <result property="username"     column="username"      />
        <result property="password"     column="password"      />
        <result property="apiKey"       column="api_key"       />
        <result property="apiSecret"    column="api_secret"    />
        <result property="regionId"     column="region_id"     />
        <result property="senderName"   column="sender_name"   />
        <result property="senderEmail"  column="sender_email"  />
        <result property="encryption"   column="encryption"    />
        <result property="imapHost"     column="imap_host"     />
        <result property="imapPort"     column="imap_port"     />
        <result property="imapEncryption" column="imap_encryption" />
        <result property="enableReceive" column="enable_receive" />
        <result property="useApi"       column="use_api"       />
        <result property="isDefault"    column="is_default"    />
        <result property="status"       column="status"        />
        <result property="extraParams"  column="extra_params"  />
        <result property="delFlag"      column="del_flag"      />
        <result property="createBy"     column="create_by"     />
        <result property="createTime"   column="create_time"   />
        <result property="updateBy"     column="update_by"     />
        <result property="updateTime"   column="update_time"   />
        <result property="remark"       column="remark"        />
    </resultMap>

    <sql id="selectSysMailConfigVo">
        select config_id, config_name, mail_type, host, port, username, password, api_key, api_secret, 
               region_id, sender_name, sender_email, encryption, imap_host, imap_port, imap_encryption,
               enable_receive, use_api, is_default, status, extra_params, del_flag, 
               create_by, create_time, update_by, update_time, remark
        from sys_mail_config
    </sql>

    <select id="selectSysMailConfigList" parameterType="SysMailConfig" resultMap="SysMailConfigResult">
        <include refid="selectSysMailConfigVo"/>
        <where>
            del_flag = '0'
            <if test="configName != null and configName != ''">
                AND config_name like concat('%', #{configName}, '%')
            </if>
            <if test="mailType != null and mailType != ''">
                AND mail_type = #{mailType}
            </if>
            <if test="status != null and status != ''">
                AND status = #{status}
            </if>
        </where>
        order by is_default desc, create_time desc
    </select>

    <select id="selectSysMailConfigByConfigId" parameterType="Long" resultMap="SysMailConfigResult">
        <include refid="selectSysMailConfigVo"/>
        where config_id = #{configId} and del_flag = '0'
    </select>

    <select id="selectEnabledConfigList" resultMap="SysMailConfigResult">
        <include refid="selectSysMailConfigVo"/>
        where status = '0' and del_flag = '0'
        order by is_default desc, create_time desc
    </select>

    <select id="selectDefaultConfig" resultMap="SysMailConfigResult">
        <include refid="selectSysMailConfigVo"/>
        where is_default = '1' and status = '0' and del_flag = '0'
        limit 1
    </select>

    <insert id="insertSysMailConfig" parameterType="SysMailConfig" useGeneratedKeys="true" keyProperty="configId">
        insert into sys_mail_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="configName != null and configName != ''">config_name,</if>
            <if test="mailType != null and mailType != ''">mail_type,</if>
            <if test="host != null">host,</if>
            <if test="port != null">port,</if>
            <if test="username != null">username,</if>
            <if test="password != null">password,</if>
            <if test="apiKey != null">api_key,</if>
            <if test="apiSecret != null">api_secret,</if>
            <if test="regionId != null">region_id,</if>
            <if test="senderName != null">sender_name,</if>
            <if test="senderEmail != null and senderEmail != ''">sender_email,</if>
            <if test="encryption != null">encryption,</if>
            <if test="imapHost != null">imap_host,</if>
            <if test="imapPort != null">imap_port,</if>
            <if test="imapEncryption != null">imap_encryption,</if>
            <if test="enableReceive != null">enable_receive,</if>
            <if test="useApi != null">use_api,</if>
            <if test="isDefault != null">is_default,</if>
            <if test="status != null">status,</if>
            <if test="extraParams != null">extra_params,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="configName != null and configName != ''">#{configName},</if>
            <if test="mailType != null and mailType != ''">#{mailType},</if>
            <if test="host != null">#{host},</if>
            <if test="port != null">#{port},</if>
            <if test="username != null">#{username},</if>
            <if test="password != null">#{password},</if>
            <if test="apiKey != null">#{apiKey},</if>
            <if test="apiSecret != null">#{apiSecret},</if>
            <if test="regionId != null">#{regionId},</if>
            <if test="senderName != null">#{senderName},</if>
            <if test="senderEmail != null and senderEmail != ''">#{senderEmail},</if>
            <if test="encryption != null">#{encryption},</if>
            <if test="imapHost != null">#{imapHost},</if>
            <if test="imapPort != null">#{imapPort},</if>
            <if test="imapEncryption != null">#{imapEncryption},</if>
            <if test="enableReceive != null">#{enableReceive},</if>
            <if test="useApi != null">#{useApi},</if>
            <if test="isDefault != null">#{isDefault},</if>
            <if test="status != null">#{status},</if>
            <if test="extraParams != null">#{extraParams},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateSysMailConfig" parameterType="SysMailConfig">
        update sys_mail_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="configName != null and configName != ''">config_name = #{configName},</if>
            <if test="mailType != null and mailType != ''">mail_type = #{mailType},</if>
            <if test="host != null">host = #{host},</if>
            <if test="port != null">port = #{port},</if>
            <if test="username != null">username = #{username},</if>
            <if test="password != null">password = #{password},</if>
            <if test="apiKey != null">api_key = #{apiKey},</if>
            <if test="apiSecret != null">api_secret = #{apiSecret},</if>
            <if test="regionId != null">region_id = #{regionId},</if>
            <if test="senderName != null">sender_name = #{senderName},</if>
            <if test="senderEmail != null">sender_email = #{senderEmail},</if>
            <if test="encryption != null">encryption = #{encryption},</if>
            <if test="imapHost != null">imap_host = #{imapHost},</if>
            <if test="imapPort != null">imap_port = #{imapPort},</if>
            <if test="imapEncryption != null">imap_encryption = #{imapEncryption},</if>
            <if test="enableReceive != null">enable_receive = #{enableReceive},</if>
            <if test="useApi != null">use_api = #{useApi},</if>
            <if test="isDefault != null">is_default = #{isDefault},</if>
            <if test="status != null">status = #{status},</if>
            <if test="extraParams != null">extra_params = #{extraParams},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </trim>
        where config_id = #{configId}
    </update>

    <update id="deleteSysMailConfigByConfigId" parameterType="Long">
        update sys_mail_config set del_flag = '2' where config_id = #{configId}
    </update>

    <update id="deleteSysMailConfigByConfigIds" parameterType="String">
        update sys_mail_config set del_flag = '2' where config_id in
        <foreach item="configId" collection="array" open="(" separator="," close=")">
            #{configId}
        </foreach>
    </update>

    <update id="resetDefaultConfig">
        update sys_mail_config set is_default = '0' where is_default = '1' and del_flag = '0'
    </update>

</mapper>
