<?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.dos.CompanyEmailSendHistoryDO">
        <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"/>
        <result column="isvalid" property="isvalid"/>
        <result column="createtime" property="createTime"/>
        <result column="updatetime" property="updateTime"/>
        <result column="creatorid" property="creatorId"/>
        <result column="updaterid" property="updaterId"/>
    </resultMap>
    <insert id="saveCompanyEmailSendHistory"
            parameterType="com.simuwang.base.pojo.dos.CompanyEmailSendHistoryDO">
        insert into PPW_EMAIL.company_email_send_history(company_id,email,send_status,send_time,send_remark,isvalid,createtime,updatetime,creatorid,updaterid)
        values (#{companyId},#{email},#{sendStatus},#{sendTime},#{sendRemark},#{isvalid},#{createTime},#{updateTime},#{creatorId},#{updaterId})
    </insert>
    <update id="deleteEmailHistory">
        update PPW_EMAIL.company_email_send_history set isvalid =0,updatetime=sysdate() where email in
        <foreach item="email" collection="emailList" open="(" separator="," close=")">
            #{email}
        </foreach>
    </update>
    <update id="deleteEmailHistoryByIds">
        update PPW_EMAIL.company_email_send_history set isvalid =0,updatetime=sysdate(),updaterid=#{userId} 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,
            maxce.send_time as send_time,
            maxce.send_remark,
            ifnull(maxce.send_status,-1) as send_status
        FROM
        PPW_EMAIL.company_email_config cec
        JOIN PPW_EMAIL.pvn_company_info c
        ON cec.company_id = c.company_id
        LEFT JOIN (select me.email as email,max(me.send_time) as send_time,me.company_id,me.send_remark,me.send_status from PPW_EMAIL.company_email_send_history me where isvalid =1 group by me.email,me.company_id) maxce
        ON cec.email = maxce.email and cec.company_id = maxce.company_id
        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},'%') or c.register_number like concat('%',#{companyName},'%'))
        </if>
        <if test="email != null and email !=''">
            and cec.email like concat('%',#{email},'%')
        </if>
        <if test="sendStatus != null and sendStatus != -1">
            and maxce.send_status=#{sendStatus}
        </if>
        <if test="sendStatus == -1">
            and maxce.send_status is null
        </if>
        order by
        CASE
            WHEN send_time IS NULL THEN 1
        ELSE 0
        END,
            send_time desc
        limit #{offset},#{pageSize}
    </select>

    <select id="countCompanyEmailList" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.CompanyEmailPageQuery">
        SELECT
        count(cec.company_id)
        from
        PPW_EMAIL.company_email_config cec
        JOIN PPW_EMAIL.pvn_company_info c
        ON cec.company_id = c.company_id
        LEFT JOIN (select me.email as email,max(me.send_time) as send_time,me.company_id,me.send_remark,me.send_status from PPW_EMAIL.company_email_send_history me where isvalid =1 group by me.email,me.company_id) maxce
        ON cec.email = maxce.email and cec.company_id = maxce.company_id
        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},'%') or c.register_number like concat('%',#{companyName},'%'))
        </if>
        <if test="email != null and email !=''">
            and cec.email like concat('%',#{email},'%')
        </if>
        <if test="sendStatus != null and sendStatus != -1">
            and maxce.send_status=#{sendStatus}
        </if>
        <if test="sendStatus == -1">
            and maxce.send_status is null
        </if>
    </select>

    <select id="searchEmailHistory" resultMap="BaseResultMap">
        SELECT distinct
        cesh.id,
            cec.company_id,
            c.company_name,
            cesh.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
            ON cec.email = cesh.email
            JOIN PPW_EMAIL.pvn_company_info c
            ON cec.company_id = c.company_id
        WHERE cec.isvalid = 1
        AND cesh.isvalid = 1 and c.isvalid =1
        <if test="companyId != null and companyId !=''">
            and c.company_id=#{companyId}
        </if>
        order by cesh.send_time desc
        limit #{offset},#{pageSize}
    </select>
    <select id="countCompanyEmailhistory" resultType="java.lang.Long"
            parameterType="com.simuwang.base.pojo.dto.query.CompanyEmailHistoryPageQuery">
        SELECT
        count(distinct cesh.id)
        FROM
        PPW_EMAIL.company_email_send_history cesh
        JOIN PPW_EMAIL.company_email_config cec
        ON cec.email = cesh.email
        JOIN PPW_EMAIL.pvn_company_info c
        ON cec.company_id = c.company_id
        WHERE cec.isvalid = 1
        AND cesh.isvalid = 1 and c.isvalid =1
        <if test="companyId != null and companyId !=''">
            and c.company_id=#{companyId}
        </if>
    </select>

</mapper>