<?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.CustomEmailSubscribeRecordMapper">

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.core.entity.custom.email.subscribe.record.CustomEmailSubscribeRecordSearchEntity">
        select count(*) from custom_email_subscribe_record ce
        left join custom_info ci on ce.c_id = ci.c_id
        <where>
            <if test="cId != null and cId > 0">
                and ce.c_id = #{cId}
            </if>
            <if test="email != null and email != ''">
                and ce.email = #{email}
            </if>
            <if test="countrys != null and countrys.size() > 0">
                and ci.country in
                <foreach collection="countrys" index="idx" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="pageList" resultType="com.crm.rely.backend.core.pojo.view.CustomEmailSubscribeRecordView"
            parameterType="com.crm.rely.backend.core.entity.custom.email.subscribe.record.CustomEmailSubscribeRecordSearchEntity">
        select ce.*,ci.first_name,ci.middle,ci.last_name,ci.country,ci.country_name,ci.country_en_name from custom_email_subscribe_record ce
        left join custom_info ci on ce.c_id = ci.c_id
        <where>
            <if test="cId != null and cId > 0">
                and ce.c_id = #{cId}
            </if>
            <if test="email != null and email != ''">
                and ce.email = #{email}
            </if>
            <if test="countrys != null and countrys.size() > 0">
                and ci.country in
                <foreach collection="countrys" index="idx" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by ce.id desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

</mapper>