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

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.model.entity.market.MarketEmailSearchPageEntity">

        select count(*)
        from market_email
        <where>

            <if test="type!=null">
                and `type`=#{type}
            </if>
            <if test="subject!=null and subject!=''">
                and `subject` like concat('%',#{subject},'%')
            </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.MarketEmailTable"
            parameterType="com.crm.rely.backend.model.entity.market.MarketEmailSearchPageEntity">

        select *
        from market_email
        <where>
            <if test="type!=null">
                and `type`=#{type}
            </if>
            <if test="subject!=null and subject!=''">
                and `subject` like concat('%',#{subject},'%')
            </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="existsByEmail" resultType="integer">

        SELECT IF(
                           DATE_ADD(
                                   MAX(market_email.add_time),
                                   INTERVAL sys_config.`value` MINUTE
	) > now(),
                           1,
                           0
                   )
        FROM market_email,
             sys_config
        WHERE sys_config.`code` = 'MARKETS_EMAIL_INTERVAL'
    </select>
</mapper>