<?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.artfess.mail.persistence.dao.MailSettingDao">
	<resultMap id="MailSetting" type="com.artfess.mail.model.MailSetting">
		<id property="id" column="ID_" jdbcType="VARCHAR"/>
		<result property="userId" column="USER_ID_" jdbcType="VARCHAR"/>
		<result property="sendHost" column="SEND_HOST_" jdbcType="VARCHAR"/>
		<result property="sendPort" column="SEND_PORT_" jdbcType="VARCHAR"/>
		<result property="receiveHost" column="RECEIVE_HOST_" jdbcType="VARCHAR"/>
		<result property="receivePort" column="RECEIVE_PORT_" jdbcType="VARCHAR"/>
		<result property="protocal" column="PROTOCAL_" jdbcType="VARCHAR"/>
		<result property="nickName" column="USER_NAME_" jdbcType="VARCHAR"/>
		<result property="mailAddress" column="MAIL_ADDRESS_" jdbcType="VARCHAR"/>
		<result property="password" column="MAIL_PASS_" jdbcType="VARCHAR"/>
		<result property="smtpHost" column="SMTP_HOST_" jdbcType="VARCHAR"/>
		<result property="smtpPort" column="SMTP_PORT_" jdbcType="VARCHAR"/>
		<result property="popHost" column="POP_HOST_" jdbcType="VARCHAR"/>
		<result property="popPort" column="POP_PORT_" jdbcType="VARCHAR"/>
		<result property="imapHost" column="IMAP_HOST_" jdbcType="VARCHAR"/>
		<result property="imapPort" column="IMAP_PORT_" jdbcType="VARCHAR"/>
		<result property="isDefault" column="IS_DEFAULT_" jdbcType="NUMERIC"/>
		<result property="mailType" column="MAIL_TYPE_" jdbcType="VARCHAR"/>
		<result property="SSL" column="USE_SSL_" jdbcType="NUMERIC"/>
		<result property="validate" column="IS_VALIDATE_" jdbcType="NUMERIC"/>
		<result property="isDeleteRemote" column="IS_DELETE_REMOTE_" jdbcType="NUMERIC"/>
		<result property="isHandleAttach" column="IS_HANDLE_ATTACH_" jdbcType="NUMERIC"/>
		<result property="lastMessageId" column="LAST_MESSAGE_ID_" jdbcType="VARCHAR"/>
		<result property="lastReceiveTime" column="LAST_RECEIVE_TIME_" jdbcType="TIMESTAMP"/>
	</resultMap>
	
	<sql id="columns">
		ID_,USER_ID_,SEND_HOST_,SEND_PORT_,RECEIVE_HOST_,RECEIVE_PORT_,PROTOCAL_,USER_NAME_,MAIL_ADDRESS_,MAIL_PASS_,SMTP_HOST_,SMTP_PORT_,POP_HOST_,POP_PORT_,IMAP_HOST_,IMAP_PORT_,IS_DEFAULT_,MAIL_TYPE_,USE_SSL_,IS_VALIDATE_,IS_DELETE_REMOTE_,IS_HANDLE_ATTACH_,LAST_MESSAGE_ID_,LAST_RECEIVE_TIME_
	</sql>
	
	<sql id="dynamicWhere">
		<where>
			<if test="@com.artfess.base.ognl.Ognl@isNotEmpty(userId)"> AND USER_ID_  =#{userId} </if>
			<if test="@com.artfess.base.ognl.Ognl@isNotEmpty(nickName)"> AND USER_NAME_  LIKE #{nickName}  </if>
			<if test="@com.artfess.base.ognl.Ognl@isNotEmpty(mailAddress)"> AND MAIL_ADDRESS_  LIKE #{mailAddress}  </if>
			<if test="@com.artfess.base.ognl.Ognl@isNotEmpty(isDefault)"> AND IS_DEFAULT_  =#{isDefault} </if>
		</where>
	</sql>
	
	<update id="updateDefault" parameterType="com.artfess.mail.model.MailSetting">
		UPDATE portal_sys_mail_setting SET
		IS_DEFAULT_=#{isDefault,jdbcType=NUMERIC}
		WHERE
		ID_=#{id}
	</update>
	
	<select id="getAllByUserId" resultMap="MailSetting">
		SELECT <include refid="columns"/>
		FROM portal_sys_mail_setting
		<include refid="dynamicWhere" />
		<if test="@com.artfess.base.ognl.Ognl@isNotEmpty(orderField)">
		order by ${orderField} ${orderSeq}
		</if>
		<if test="@com.artfess.base.ognl.Ognl@isEmpty(orderField)">
		order by  ID_ desc
		</if>
	</select>
	
	<select id="getMailByAddress" parameterType="java.lang.String" resultMap="MailSetting">
		SELECT <include refid="columns"/>
		FROM portal_sys_mail_setting
		WHERE
		MAIL_ADDRESS_=#{address}
	</select>
	
	<select id="getMailByUserId" parameterType="java.lang.String" resultMap="MailSetting">
		SELECT <include refid="columns"/>
		FROM portal_sys_mail_setting
		WHERE
		USER_ID_=#{userId}
	</select>
		
	<select id="getByIsDefault" parameterType="java.lang.String" resultMap="MailSetting">
		SELECT <include refid="columns"/>
		FROM portal_sys_mail_setting
		WHERE
		USER_ID_=#{userId} and IS_DEFAULT_=1
	</select>
	
	<select id="getCountByAddress" parameterType="java.lang.String" resultType="int">
		SELECT COUNT(1)
		FROM portal_sys_mail_setting
		WHERE
		MAIL_ADDRESS_=#{address}
	</select>
	
	<select id="getCountByUserId" parameterType="java.lang.String" resultType="int">
		SELECT COUNT(1)
		FROM portal_sys_mail_setting
		WHERE
		USER_ID_=#{userId}
	</select>
	
	<update id="updateLastEnvelop">
		UPDATE portal_sys_mail_setting SET
		LAST_MESSAGE_ID_=#{messageId,jdbcType=VARCHAR},
		LAST_RECEIVE_TIME_=#{receiveTime,jdbcType=TIMESTAMP}
		WHERE
		ID_=#{id}
	</update>
</mapper>