<?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="org.jeecg.modules.okki.wechatgroup.mapper.OkkiShopWechatGroupMsgMapper">

    <select id="getSiteWechatMsgList" resultType="org.jeecg.modules.okki.wechatgroup.entity.OkkiShopWechatGroupMsg">
        SELECT
            t1.site_id,
            IFNULL( t2.seq, 0 ) AS seq
        FROM
            okki_site t1
                LEFT JOIN ( SELECT site_id, MAX( seq ) AS seq FROM `okki_shop_wechat_group_msg` GROUP BY site_id ) t2 ON t2.site_id = t1.site_id
        WHERE
            t1.del_flag =0
        <if test="siteId != null and siteId != ''">
            AND t1.site_id = #{siteId}
        </if>
    </select>

    <select id="getWechatNoReplySiteByDays" resultType="org.jeecg.modules.okki.wechatgroup.entity.OkkiShopWechatGroupMsg">
        SELECT
        t1.site_id,
        t1.from_id,
        t1.content,
        t1.client_id,
        t1.seq,
        t1.msg_type,
        t1.msg_time,
        t3.tc_name
        FROM
        `okki_shop_wechat_group_msg` t1
        INNER JOIN ( SELECT site_id, MAX( seq ) AS seq FROM `okki_shop_wechat_group_msg` GROUP BY site_id ) t2 ON t1.site_id = t2.site_id AND t1.seq = t2.seq
        INNER JOIN ( SELECT site_id,tc_name FROM `okki_site` ) t3 ON t1.site_id = t3.site_id
        WHERE
        <if test="siteId != null and siteId != ''">
            t1.site_id = #{siteId} and
        </if>
        <if test="days != null">
           <![CDATA[ t1.msg_time < NOW() - INTERVAL #{days} DAY ]]>
        </if>
    </select>

    <select id="getWechatNoReplySiteByHours" resultType="org.jeecg.modules.okki.wechatgroup.entity.OkkiShopWechatGroupMsg">
        SELECT
        t1.site_id,
        t1.from_id,
        t1.content,
        t1.client_id,
        t1.seq,
        t1.msg_type,
        t1.msg_time,
        t3.tc_name
        FROM
        `okki_shop_wechat_group_msg` t1
        INNER JOIN (
        SELECT site_id, MAX(seq) AS seq
        FROM `okki_shop_wechat_group_msg`
        GROUP BY site_id
        ) t2 ON t1.site_id = t2.site_id AND t1.seq = t2.seq
        INNER JOIN ( SELECT site_id,tc_name FROM `okki_site` ) t3 ON t1.site_id = t3.site_id
        WHERE
        <if test="siteId != null and siteId != ''">
            t1.site_id = #{siteId} and
        </if>
        <if test="days != null">
            <![CDATA[ t1.msg_time < NOW() - INTERVAL #{days} DAY ]]>
        </if>
        <if test="hours != null">
            and <![CDATA[ t1.msg_time < NOW() - INTERVAL #{hours} HOUR ]]>
        </if>
        <if test="enNames != null">
            and LOWER(t1.from_id) NOT IN
            <foreach item="enName" collection="enNames"  open="(" separator="," close=")">
                LOWER(#{enName})
            </foreach>
        </if>
        AND t1.from_id NOT LIKE '%@xiaoman.cn';
    </select>

    <select id="getLatestWechatMsgList" resultType="org.jeecg.modules.okki.wechatgroup.entity.OkkiShopWechatGroupMsg">
        SELECT
            CASE
            WHEN (LOWER(t1.from_id) IN
        <foreach item="enName" collection="enNames"  open="(" separator="," close=")">
            LOWER(#{enName})
        </foreach> or t1.from_id LIKE '%@xiaoman.cn') THEN '我'
            ELSE '客户'
            END AS from_id,
            t1.content,
            t1.msg_time
        FROM
            `okki_shop_wechat_group_msg` t1
        WHERE
        <if test="siteId != null and siteId != ''">
            t1.site_id = #{siteId} and
        </if>
            t1.msg_type = 'text'
        ORDER BY t1.msg_time desc LIMIT 10
    </select>
</mapper>