<?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.CompanyEmailSendHistoryMapper">
    <resultMap id="BaseResultMap" type="com.simuwang.base.pojo.vo.CompanyEmailSendHistoryVO">
        <id column="id" property="id"/>
        <result column="company_id" property="companyId"/>
        <result column="company_name" property="companyName"/>
        <result column="email" property="email"/>
        <result column="send_status" property="sendStatus"/>
        <result column="send_time" property="sendTime"/>
        <result column="send_remark" property="sendRemark"/>
    </resultMap>
    <update id="deleteEmailHistory">
        update PPW_EMAIL.company_email_send_history set isvalid =0,updatetime=sysdate() where email=#{email}
    </update>
    <update id="deleteEmailHistoryByIds">
        update PPW_EMAIL.company_email_send_history set isvalid =0,updatetime=sysdate() where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="searchCompanyEmailList" resultMap="BaseResultMap">
        SELECT
            cec.id,
            cec.company_id,
            c.company_name,
            cec.email,
            MAX(cesh.send_time) AS send_time,
            cesh.send_remark,
            cesh.send_status
        FROM
            PPW_EMAIL.company_email_config cec
                JOIN PPW_EMAIL.company_information c
                     ON cec.company_id = c.company_id
                LEFT JOIN PPW_EMAIL.company_email_send_history cesh
                     ON cec.email = cesh.email AND cesh.isvalid = 1
        WHERE cec.isvalid = 1 and c.isvalid=1
        <if test="companyName != null and companyName !=''">
            and (c.company_name like concat('',#{companyName},'') or c.company_short_name like concat('',#{companyName},''))
        </if>
        <if test="email != null and email !=''">
            and cec.email like concat('',#{email},'')
        </if>
        <if test="sendStatus != null and sendStatus !=''">
            and cesh.send_status=#{sendStatus}
        </if>
    </select>
    <select id="searchEmailHistory" resultMap="BaseResultMap">
        SELECT
            cec.id,
            cec.company_id,
            c.company_name,
            cec.email,
            cesh.send_time,
            cesh.send_remark,
            cesh.send_status
        FROM
            PPW_EMAIL.company_email_send_history cesh
            JOIN PPW_EMAIL.company_email_config cec
            JOIN PPW_EMAIL.company_information c
            ON cec.company_id = c.company_id
            ON cec.email = cesh.email
        WHERE cec.isvalid = 1
        AND cesh.isvalid = 1 and c.isvalid =1
        <if test="email != null and email !=''">
            and cec.email=#{email}
        </if>
    </select>
</mapper>