<?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.CompanyEmailConfigMapper">
    <insert id="saveCompanyEmailConfig">
        insert into PPW_EMAIL.company_email_config(
        <if test="companyId != null and companyId != ''">company_id,</if>
        <if test="companyName != null and companyName != ''">company_name,</if>
        <if test="email != null and email != ''">email,</if>
        <if test="openStatus != null and openStatus != ''">open_status,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="creatorId != null and creatorId != ''">creatorid,</if>
        <if test="updaterId != null and updaterId != ''">updaterId,</if>
        isvalid,
        createtime,
        updatetime
        )values(
        <if test="companyId != null and companyId != ''">#{companyId},</if>
        <if test="companyName != null and companyName != ''">#{companyName},</if>
        <if test="email != null and email != ''">#{email},</if>
        <if test="openStatus != null and openStatus != ''">#{openStatus},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="creatorId != null and creatorId != ''">#{creatorId},</if>
        <if test="updaterId != null and updaterId != ''">#{updaterId},</if>
        1,
        sysdate(),
        sysdate()
        )
    </insert>

    <update id="updateCompanyEmailConfig">
        update PPW_EMAIL.company_email_config
        <set>
            <if test="companyId != null and companyId != ''">company_id=#{companyId},</if>
            <if test="companyName != null and companyName != ''">company_name=#{companyName},</if>
            <if test="email != null and email != ''">email=#{email},</if>
            <if test="openStatus != null">open_status=#{openStatus},</if>
            <if test="remark != null and remark != ''">remark=#{remark},</if>
            <if test="updaterId != null and updaterId != ''">updaterid=#{updaterId},</if>
            <if test="updateTime != null">updatetime=#{updateTime},</if>
        </set>
        where id = #{id} and isvalid=1
    </update>

    <update id="deleteCompanyEmailConfig">
        update PPW_EMAIL.company_email_config set isvalid =0 where company_id=#{companyId}
    </update>
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.dos.CompanyEmailConfigDO">
        <id column="id" property="id"/>
        <result column="company_id" property="companyId"/>
        <result column="company_name" property="companyName"/>
        <result column="email" property="email"/>
        <result column="open_status" property="openStatus"/>
        <result column="remark" property="remark"/>
        <result column="creatorid" property="creatorId"/>
        <result column="updaterid" property="updaterId"/>
        <result column="createtime" property="createTime"/>
        <result column="updatetime" property="updateTime"/>
        <result column="isvalid" property="isvalid"/>
    </resultMap>
    <sql id="selectCompanyEmailConfigDo">
        select id,company_id, company_name, email, open_status,remark, creatorid, createtime, updaterid, updatetime,isvalid
        from PPW_EMAIL.company_email_config
    </sql>
    <!-- 查询条件 -->
    <sql id="sqlwhereSearch">
        <where>
            isvalid=1
            <if test="id !=null">
                and id = #{id}
            </if>
        </where>
    </sql>
    <select id="selectCompanyEmailConfigById" resultMap="BaseResultMap">
        <include refid="selectCompanyEmailConfigDo"/>
        <include refid="sqlwhereSearch"/>
    </select>
    <select id="searchEmailByCompanyId" resultType="java.lang.String" parameterType="java.lang.String">
        select email from PPW_EMAIL.company_email_config where company_id = #{companyId} and isvalid =1
    </select>

    <resultMap id="BaseVOMap" type="com.simuwang.base.pojo.vo.CompanyEmailConfigVO">
        <id column="id" property="id"/>
        <result column="company_id" property="companyId"/>
        <result column="company_name" property="companyName"/>
        <result column="email" property="email"/>
        <result column="open_status" property="openStatus"/>
        <result column="remark" property="sendRemark"/>
    </resultMap>
    <select id="searchEmailConfig" resultMap="BaseVOMap">
        select id,company_id,company_name,email,open_status,remark from
         PPW_EMAIL.company_email_config where isvalid =1 and company_id=#{companyId}
        limit #{offset},#{pageSize}
    </select>
    <select id="countEmailConfig" resultType="java.lang.Long" parameterType="java.lang.String">
        select count(1) from PPW_EMAIL.company_email_config where isvalid =1 and company_id=#{companyId}
    </select>
    <select id="searchEmailConfigByCompanyId" resultMap="BaseVOMap">
        select id,company_id,company_name,email,open_status,remark from
            PPW_EMAIL.company_email_config where isvalid =1 and company_id=#{companyId}
    </select>
    <select id="selectEmailConfig"  resultMap="BaseResultMap">
        select id,company_id,company_name,email,open_status,remark from
            PPW_EMAIL.company_email_config where isvalid =1 and company_id=#{companyId} and email=#{email} limit 1
    </select>
</mapper>