<?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.adweb.enquiry.mapper.AdwebEnquiryMapper">

    <select id="selectEnquiryList" parameterType="org.jeecg.modules.adweb.enquiry.dto.param.EnquirySearchDto"
            resultType="org.jeecg.modules.adweb.enquiry.dto.result.EnquiryListDto">
        SELECT
        t1.id,
        t2.`name` AS siteName,
        t1.`no`,
        t1.no_out AS noOut,
        t1.modular,
        t1.record_ctime AS recordCtime,
        t1.ctime AS ctime,
        t1.from_email AS fromEmail,
        t1.country,
        t1.site_host AS siteHost,
        t1.uid,
        t1.enquiry_file,
        t1.`status`,
        t1.tracker,
        t1.site_code AS siteCode,
        t1.site_id AS siteId,
        t1.phase,
        t1.track_status AS trackStatus,
        t1.replay_status AS replayStatus,
        t1.sys_effective AS sysEffective,
        t1.user_effective AS userEffective,
        t1.sensitive_message AS sensitiveMessage,
        t1.company_id AS companyId,
        t1.contact_ids AS contactIds,
        t1.trans_content AS transContent,
        t1.whats_app AS whatsApp,
        t1.name contact,
        t1.phone,
        t1.company formCompany,
        t1.from_page,
        t1.message context,
        t1.from_page path,
        t1.read_status,
        t1.visit_id,
        t1.special_field,
        t1.verify_num AS verifyNum,
        t1.cart_items as cartItems,
        t1.effective_reason effectiveReason,
        t1.plugin_name pluginName,
        t1.waste_enquiry_type wasteEnquiryType,
        case waste_enquiry when 0 then '否' when 1 then '是' end as wasteEnquiry,
        country_name as countryName,
        country_code as countryCode,
        t1.from_ip fromIp,
        t1.customer_ip customerIp,
        t1.principal_uid principalUid
        FROM
        adweb_enquiry t1,
        adweb_site t2
        <where>
            t1.site_code = t2.`code`
            AND t2.`status` != 0
            AND t1.status != 0
            <if test="codeList != null">
                <choose>
                    <when test="codeList.size() != 0">
                        AND t1.site_code IN
                        <foreach collection="codeList" item="code" open="(" close=")" separator=",">
                            #{code}
                        </foreach>
                    </when>
                    <otherwise>
                        AND t1.id = -1
                    </otherwise>
                </choose>
            </if>
            <if test="null != searchText and '' != searchText">
                and (
                t1.from_email like CONCAT('%', #{searchText}, '%')
                or t1.name like CONCAT('%', #{searchText}, '%')
                or t1.country_name like CONCAT('%', #{searchText}, '%')
                )
            </if>
            <if test="null != searchContent and '' != searchContent">
                and (
                t1.message like CONCAT('%', #{searchContent}, '%')
                )
            </if>
            <if test="null != userEffective and '' != userEffective">
                AND t1.user_effective IN ${userEffective}
            </if>
            <if test="null != wasteEnquiryType and '' != wasteEnquiryType">
                AND t1.waste_enquiry_type = #{wasteEnquiryType}
            </if>
            <if test="null != ctimeBegin">
                and t1.record_ctime >= #{ctimeBegin}
            </if>
            <if test="null != ctimeEnd">
                and DATE_ADD(DATE(#{ctimeEnd}), INTERVAL 1 DAY) > t1.record_ctime
            </if>
            <if test="null != whatsApp and '' != whatsApp">
                and t1.whats_app = #{whatsApp}
            </if>
            <if test="readStatus != null">
                AND t1.read_status = #{readStatus}
            </if>
            <if test="principalUid != null and principalUid != ''">
                AND t1.principal_uid = #{principalUid}
            </if>
            <if test="selfPrincipalUid != null and selfPrincipalUid != ''">
                AND (t1.principal_uid = #{selfPrincipalUid} OR t1.principal_uid = 'ALL')
            </if>
        </where>
        ORDER BY
        t1.record_ctime DESC
    </select>

    <select id="queryWasteEnquiryList" parameterType="org.jeecg.modules.adweb.enquiry.dto.param.EnquirySearchDto"
            resultType="org.jeecg.modules.adweb.enquiry.dto.result.EnquiryListDto">
        SELECT
        t1.id,
        t2.`name` AS siteName,
        t1.`no`,
        t1.no_out AS noOut,
        t1.modular,
        t1.record_ctime AS recordCtime,
        t1.from_email AS fromEmail,
        t1.country,
        t1.site_host AS siteHost,
        t1.uid,
        t1.STATUS,
        t1.tracker,
        t1.site_code AS siteCode,
        t1.phase,
        t1.track_status AS trackStatus,
        t1.replay_status AS replayStatus,
        t1.sys_effective AS sysEffective,
        t1.user_effective AS userEffective,
        t1.sensitive_message AS sensitiveMessage,
        t1.company_id AS companyId,
        t1.contact_ids AS contactIds,
        t1.trans_content AS transContent,
        t1.whats_app AS whatsApp,
        t1.name contact,
        t1.phone,
        t1.company formCompany,
        t1.from_page,
        t1.message context,
        t1.from_page path,
        t1.country_name as countryName,
        t1.country_code as countryCode,
        t1.from_ip fromIp
        FROM
        adweb_enquiry t1,
        adweb_site t2
        <where>
            t1.site_code = t2.`code`
            AND t2.`status` != 0 AND t1.status = 0
            <if test="codeList != null">
                <choose>
                    <when test="codeList.size() != 0">
                        AND t1.site_code IN
                        <foreach collection="codeList" item="code" open="(" close=")" separator=",">
                            #{code}
                        </foreach>
                    </when>
                    <otherwise>
                        AND t1.id = -1
                    </otherwise>
                </choose>
            </if>
            <if test="null != siteId and '' != siteId">
                and t2.id in (${siteId})
            </if>
            <if test="null != searchText and '' != searchText">
                and (t1.from_email like CONCAT('%', #{searchText}, '%')
                or t1.no like CONCAT('%', #{searchText}, '%')
                or t1.country like CONCAT('%', #{searchText}, '%')
                or t1.site_host like CONCAT('%', #{searchText}, '%')
                or t1.no_out like CONCAT('%', #{searchText}, '%')
                or t2.name like CONCAT('%', #{searchText}, '%')
                )
            </if>
            <if test="userEffective != null and userEffective != '' and userEffective != 'undefined'">
                AND t1.user_effective IN ${userEffective}
            </if>
            <if test="null != ctimeBegin">
                and t1.record_ctime >= #{ctimeBegin}
            </if>
            <if test="null != ctimeEnd">
                and DATE_ADD(DATE(#{ctimeEnd}), INTERVAL 1 DAY) > t1.record_ctime
            </if>
            <if test="null != whatsApp and '' != whatsApp">
                and t1.whats_app = #{whatsApp}
            </if>
            <if test="principalUid != null and principalUid != ''">
                AND (t1.principal_uid = #{principalUid} OR t1.principal_uid = 'ALL')
            </if>
        </where>
        ORDER BY
        t1.record_ctime DESC
    </select>

    <select id="getWastedEnquiry" resultType="org.jeecg.modules.adweb.enquiry.dto.result.EnquiryListDto">
        SELECT t1.id,
        t2.`name` AS siteName,
        t1.`no`,
        t1.no_out AS noOut,
        t1.modular,
        t1.record_ctime AS recordCtime,
        t1.from_email AS fromEmail,
        t1.country,
        t1.site_host AS siteHost,
        t1.uid,
        t1.`status`,
        t1.tracker,
        t1.site_code AS siteCode,
        t1.site_id AS siteId,
        t1.phase,
        t1.track_status AS trackStatus,
        t1.replay_status AS replayStatus,
        t1.sys_effective AS sysEffective,
        t1.user_effective AS userEffective,
        t1.sensitive_message AS sensitiveMessage,
        t1.company_id AS companyId,
        t1.contact_ids AS contactIds,
        t1.trans_content AS transContent,
        t1.whats_app AS whatsApp,
        t1.name contact,
        t1.phone,
        t1.company formCompany,
        t1.from_page,
        t1.message context,
        t1.from_page path,
        t1.from_ip fromIp,
        t1.customer_ip customerIp,
        t1.read_status,
        t1.visit_id,
        t1.special_field,
        t1.country_name countryName,
        t1.effective_reason effectiveReason,
        t1.plugin_name pluginName FROM adweb_enquiry t1
        LEFT JOIN adweb_site t2 ON t1.site_code = t2.`code`
        WHERE
        t1.status = 1 AND t1.user_effective = 0 AND t1.sys_effective = 1 AND t1.waste_enquiry_type = #{wasteEnquiryType}
        <if test="ip != null and ip != ''">
            <if test="siteId != null and siteId != ''">
                AND t1.site_id = #{siteId}
            </if>
            AND t1.customer_ip = #{ip}
            <if test="null != searchText and '' != searchText">
                AND (
                t1.from_email like CONCAT('%', #{searchText}, '%')
                OR t1.name like CONCAT('%', #{searchText}, '%')
                )
            </if>
        </if>
        <if test="email != null and email != ''">
            <if test="type == null or type == ''">
                AND t1.from_email = #{email}
            </if>
            <if test="type == 0">
                AND (
                t1.from_email like CONCAT('%', #{email}, '%')
                AND t1.from_email != #{email} and t1.site_id = #{siteId}
                )
            </if>
            <if test="type == 1">
                and t1.from_email = #{email} and t1.site_id = #{siteId}
            </if>
            <if test="null != searchText and '' != searchText">
                and (
                t1.from_email like CONCAT('%', #{searchText}, '%')
                or t1.name like CONCAT('%', #{searchText}, '%')
                )
            </if>

        </if>
        <if test="keyword != null and keyword != ''">
            <if test="siteId != null and siteId != ''">
                AND t1.site_id = #{siteId}
            </if>
            AND SUBSTRING_INDEX(effective_reason,'-',-1) = #{keyword}
            <if test="null != searchText and '' != searchText">
                and (
                t1.from_email like CONCAT('%', #{searchText}, '%')
                or t1.name like CONCAT('%', #{searchText}, '%')
                )
            </if>
        </if>
        ORDER BY t1.record_ctime DESC
    </select>

    <update id="updatePrincipalUidByRegion">
        update
        adweb_enquiry t1,
        adweb_country t2,
        adweb_user_country t3
        set
        t1.principal_uid = t3.uid
        where
        t1.country_code = t2.country_iso_code
        and t2.geoname_id = t3.country_id
        and t1.site_id IN
        <foreach collection="siteIds" item="siteId" open="(" close=")" separator=",">
            #{siteId}
        </foreach>
        and t3.site_id = #{siteId}
        and t1.principal_type != 1
    </update>

    <select id="getEnquiryDailyCounts" resultType="org.apache.commons.lang3.tuple.ImmutableTriple">
        SELECT
        DATE_FORMAT(record_ctime, '%Y-%m-%d') AS date,
        COUNT(id) AS enquires,
        COUNT(CASE WHEN read_status = 0 THEN 1 END) AS unread
        FROM
        adweb_enquiry
        WHERE
        status != 0
        AND user_effective != 0
        AND site_code = #{siteCode}
        <if test="start != null">
            AND record_ctime >= #{start}
        </if>
        <if test="end != null">
            AND record_ctime &lt; DATE_ADD(DATE(#{end}), INTERVAL 1 DAY)
        </if>
        GROUP BY date
    </select>

    <select id="getEnquiryPeriodicCounts" resultType="org.apache.commons.lang3.tuple.ImmutablePair">
        SELECT
        tp.period AS period,
        COALESCE(COUNT(id), 0) AS enquires
        FROM
        (
        SELECT 'today' AS period
        UNION ALL SELECT 'yesterday'
        UNION ALL SELECT 'thisWeek'
        UNION ALL SELECT 'lastWeek'
        UNION ALL SELECT 'thisMonth'
        UNION ALL SELECT 'lastMonth'
        UNION ALL SELECT 'allTime'
        ) tp
        LEFT JOIN
        adweb_enquiry ae
        ON ae.status != 0 AND ae.user_effective != 0
        <if test="siteCode != null">
            AND ae.site_code = #{siteCode}
        </if>
        AND (
        (tp.period = 'today' AND DATE(ae.record_ctime) = CURDATE()) OR
        (tp.period = 'yesterday' AND DATE(ae.record_ctime) = CURDATE() - INTERVAL 1 DAY) OR
        (tp.period = 'thisWeek' AND YEARWEEK(ae.record_ctime, 1) = YEARWEEK(CURDATE(), 1)) OR
        (tp.period = 'lastWeek' AND YEARWEEK(ae.record_ctime, 1) = YEARWEEK(CURDATE() - INTERVAL 1 WEEK, 1)) OR
        (tp.period = 'thisMonth' AND YEAR(ae.record_ctime) = YEAR(CURDATE()) AND MONTH(ae.record_ctime) =
        MONTH(CURDATE())) OR
        (tp.period = 'lastMonth' AND YEAR(ae.record_ctime) = YEAR(CURDATE() - INTERVAL 1 MONTH) AND
        MONTH(ae.record_ctime) =
        MONTH(CURDATE() - INTERVAL 1 MONTH)) OR
        (tp.period = 'allTime')
        )
        GROUP BY
        tp.period;
    </select>

    <select id="getEnquiryCountryStats" resultType="org.jeecg.modules.adweb.dmp.vo.report.EnquiryCountryStatsVO">
        SELECT
        t1.enquires,
        LOWER(t1.country_code) AS country_code,
        t2.country_name_en AS country,
        t2.country_name
        FROM
        (
        SELECT
        country_code,
        COUNT(1) AS enquires
        FROM
        adweb_enquiry
        WHERE
        status != 0
        AND user_effective != 0
        AND site_code = #{siteCode}
        <if test="start != null">
            AND record_ctime >= #{start}
        </if>
        <if test="end != null">
            AND record_ctime &lt; DATE_ADD(DATE(#{end}), INTERVAL 1 DAY)
        </if>
        GROUP BY
        country_code
        ) t1
        LEFT JOIN adweb_country t2 ON t1.country_code = t2.country_iso_code
        ORDER BY
        t1.enquires DESC
    </select>


    <select id="getEnquiryList" resultType="org.jeecg.modules.adweb.enquiry.dto.EnquiryDTO">
        SELECT
        id,
        site_host,
        site_code,
        record_id
        FROM
        adweb_enquiry
        WHERE
        site_code = #{siteCode}
        <if test="startTime != null">
            AND record_ctime &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND record_ctime &lt;= #{endTime}
        </if>
    </select>

    <select id="getEnquiryCount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        principal_uid uid,
        count(1) enquiryCount
        FROM
        adweb_enquiry
        WHERE
        `status` = 1
        AND principal_uid IN
        <foreach collection="uids" item="uid" open="(" close=")" separator=",">
            #{uid}
        </foreach>
        GROUP BY
        principal_uid
    </select>

    <select id="getMaxRecordIdByCode" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT MAX(record_id + 0)
        FROM adweb_enquiry
        WHERE site_code = #{siteCode}
          AND plugin_name = #{plugin}
    </select>

</mapper>