<?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.crm.manager.dao.mapper.MarketEmailRecordMapper">

    <insert id="insertSaves">
        INSERT INTO `market_email_record` (`email_id`,
                                           `email_config_id`,
                                           `subject`,
                                           `user`,
                                           `name`,
                                           `country`,
                                           `country_name`,
                                           `country_en_name`,
                                           `content`,
                                           `status`,
                                           `c_id`)
        sELECT market_email.id,
               market_email.email_config_id,
               market_email.`subject`,
               ci.email,
               ci.first_name,
               ci.country,
               ci.country_name,
               ci.country_en_name,
               market_email.content,
               0,
               ci.c_id
        FROM market_email,
             custom_info ci
        where market_email.id = #{emailId}

    </insert>
    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.model.entity.market.record.MarketEmailRecordSearchPageEntity">

        select count(*)
        from market_email_record
        <where>
            <if test="status!=null">
                and `status`=#{status}
            </if>
            <if test="user!=null and user!=''">
                and `user`=#{user}
            </if>
            <if test="emailId!=null">
                and email_id=#{emailId}
            </if>
            <if test="country!=null and country!=''">
                and `country`=#{country}
            </if>
            <if test="addUser!=null">
                and `add_user`=#{addUser}
            </if>
            <if test="startDate != null">
                and add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>

    </select>

    <select id="pageList" resultType="com.crm.rely.backend.model.pojo.table.MarketEmailRecordTable"
            parameterType="com.crm.rely.backend.model.entity.market.record.MarketEmailRecordSearchPageEntity">

        select *
        from market_email_record
        <where>
            <if test="status!=null">
                and `status`=#{status}
            </if>
            <if test="user!=null and user!=''">
                and `user`=#{user}
            </if>
            <if test="emailId!=null">
                and email_id=#{emailId}
            </if>
            <if test="country!=null and country!=''">
                and `country`=#{country}
            </if>
            <if test="addUser!=null">
                and `add_user`=#{addUser}
            </if>
            <if test="startDate != null">
                and add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by add_time desc
        <if test="page!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="statusNumber" resultType="com.crm.rely.backend.model.pojo.view.MarketEmailStatusNumberView"
            parameterType="com.crm.rely.backend.model.entity.market.record.MarketEmailRecordStatusNumberEntity">
        select
        sum(IF(`status` = 1, 1, 0)) processing,
        sum(IF(`status` = 2, 1, 0)) success,
        sum(IF(`status` = 3, 1, 0)) fail,
        sum(IF(`status` = 4, 1, 0)) unsubscribe,
        count(id) total
        from market_email_record
        <where>
            <if test="emailId!=null">
                and email_id=#{emailId}
            </if>
            <if test="addUser!=null">
                and `add_user`=#{addUser}
            </if>
        </where>
    </select>

    <select id="selectLimitByStatus" resultType="com.crm.rely.backend.model.pojo.table.MarketEmailRecordTable">

        select *
        from market_email_record
        where market_email_record.status = #{status} limit #{limit};
    </select>

    <update id="updateStatus">

        update market_email_record
        set market_email_record.status = #{status}
        where market_email_record.id = #{id}
    </update>
    <update id="updateStatusByIdIn">

        update market_email_record
        set market_email_record.status = #{status}
        where market_email_record.id in
        <foreach collection="ids" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

</mapper>