<?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="selectAdmpEnquiryList" parameterType="org.jeecg.modules.adweb.enquiry.dto.param.AdmpEnquirySearchDto"
            resultType="org.jeecg.modules.adweb.enquiry.dto.result.AdmpEnquiryListDto">
        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.read_status,
        t1.visit_id,
        t1.special_field,
        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 #{ctimeEnd} > 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>


</mapper>