<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper namespace="com.reyun.saas.mob.user.dao.WarningConfigMapper">
    <select id="selectTemplateByUserIds" resultType="com.reyun.saas.mob.user.dto.WarningConfigDTO">
        select
            t0.id,t0.name,t0.remark,t1.actual_name ,t0.create_time
        from warning_config t0
            left join user t1 on t0.user_id=t1.id
        where
            t0.user_id in (${userIds}) and t0.del_flag is false and t0.template is true
            and t0.os = #{os}
            <if test="email!=null and email!=''">
                and t0.create_email = #{email}
            </if>
            <if test="searchText!=null and searchText!=''">
                and t0.name like '%${searchText}%'
            </if>
    </select>
    <select id="selectConfigByUserIds" resultType="com.reyun.saas.mob.user.dto.WarningConfigDTO">
        select
            t0.id,t0.name,t0.appkey,t1.app_name appName,t0.channel_id channelId,t2.channel_alias channelAlias,t0.data_cycle_rule dataCycleRule,t0.eval,t0.isusing,t0.create_time createTime,GROUP_CONCAT(t3.addressee_name) addressNames
        from warning_config t0
            left join app t1 on t0.appkey=t1.appkey
            left join channel t2 on t0.channel_id=t2.id
            left join warning_addressee t3 on t0.id = t3.config_id
        where
            t0.user_id in (${userIds}) and t0.del_flag is false and t0.template is false
            and t0.os = #{os}
            <if test="isusing!=null">
                and t0.isusing = #{isusing}
            </if>
            <if test="appkey!=null and appkey!=''">
                and t0.appkey = #{appkey}
            </if>
            <if test="channelId!=null">
                and t0.channel_id = #{channelId}
            </if>
            <if test="appkeys!='' or channelIds!=''">
                and
            </if>
            <if test="appkeys!=''">
                <if test="channelIds!=''">
                    (
                </if>
                t0.appkey in (${appkeys})
            </if>
            <if test="channelIds!=''">

                <if test="appkeys!=''">
                    or
                </if>
                t0.channel_id in (${channelIds})
                <if test="appkeys!=''">
                    )
                </if>
            </if>
        group by t3.config_id
    </select>

    <select id="checkWarningReportList" resultType="com.reyun.saas.mob.user.dto.WarningConfigDTO">
        select
            t0.id,t0.name,t0.appkey,t1.app_name appName,t4.actual_name,t0.cid,t0.channel_id,t2.channel_alias,t1.os,t0.data_cycle_rule,t0.eval,t0.create_email,GROUP_CONCAT(t3.addressee_name) addressNames
        from warning_config t0
            left join app t1 on t0.appkey=t1.appkey
            left join channel t2 on t0.channel_id=t2.id
            left join warning_addressee t3 on t0.id = t3.config_id
            left join user t4 on t4.id = t0.user_id
        where
            t0.del_flag is false and t0.template is false and t0.isusing = true
        group by t3.config_id
    </select>

    <select id="selectConfigAnalysisByUserIds" resultType="com.reyun.saas.mob.user.dto.WarningConfigDTO">
        select
        t0.id,t0.name,t0.appkey,t1.app_name,t0.channel_id,t2.channel_alias,t0.data_cycle_rule,count(t3.id) historyCount, IFNULL(sum(t3.informed),0) as informedSum
        from warning_config t0
        left join app t1 on t0.appkey=t1.appkey
        left join channel t2 on t0.channel_id=t2.id
        left join warning_data_info t3 on t0.id = t3.config_id
        where
            t0.user_id in (${userIds})
            and t0.del_flag is false
            and t0.template is false
            and t1.os=#{os}
            and t3.ds &gt;= #{startDate}
            and t3.ds &lt;= #{endDate}
        <if test="appkey!=null and appkey!=''">
            and t0.appkey = #{appkey}
        </if>
        <if test="channelId!=null">
            and t0.channel_id = #{channelId}
        </if>
        <if test="appkeys!='' or channelIds!=''">
            and
        </if>
        <if test="appkeys!=''">
            <if test="channelIds!=''">
                (
            </if>
            t0.appkey in (${appkeys})
        </if>
        <if test="channelIds!=''">

            <if test="appkeys!=''">
                or
            </if>
            t0.channel_id in (${channelIds})
            <if test="appkeys!=''">
                )
            </if>
        </if>
        group by t3.config_id having count(t3.id)>0
        order by historyCount desc
    </select>
</mapper>