<?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.web.dao.mapper.WebCustomAnalysisMapper">

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.model.entity.web.custom.analysis.WebCustomAnalysisSearchListEntity">

        select count(*)
        from web_custom_analysis
        LEFT JOIN custom_info ON web_custom_analysis.custom_id=custom_info.id
        <where>
            <if test="platformCustomAnalysis == null and customId!=null">
                and web_custom_analysis.`custom_id` =#{customId}
            </if>
            <if test="title!=null and title!=''">
                and web_custom_analysis.`title` like concat("%",#{title},"%")
            </if>
            <if test="email!=null and email!=''">
                and custom_info.`email` like concat("%",#{email},"%")
            </if>
            <if test="cId!=null">
                and custom_info.`c_id`=#{cId}
            </if>
            <if test="releaseStatus !=null">
                <if test="releaseStatus == 0">
                    and web_custom_analysis.`status` in(1,3)
                </if>
                <if test="releaseStatus == 1">
                    and web_custom_analysis.`status` =2
                </if>
            </if>
            <if test="releaseStatus ==null">
                <if test="status!=null">
                    and web_custom_analysis.`status` =#{status}
                </if>
            </if>
            <if test="startDate != null">
                and web_custom_analysis.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and web_custom_analysis.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>

    </select>

    <select id="pageList" resultType="com.crm.rely.backend.model.pojo.view.WebCustomAnalysisView"
            parameterType="com.crm.rely.backend.model.entity.web.custom.analysis.WebCustomAnalysisSearchListEntity">

        select web_custom_analysis.*,custom_info.first_name
        custom_name,custom_info.ib_id,custom_info.c_id,custom_info.email,custom_info.ib_id agent_id
        <if test="platformCustomAnalysis!=null and customId!=null">
            ,web_custom_analysis_interaction_history.agree interaction_agree
            ,web_custom_analysis_interaction_history.veto interaction_veto
        </if>
        from web_custom_analysis
        LEFT JOIN custom_info ON web_custom_analysis.custom_id=custom_info.id
        <if test="platformCustomAnalysis!=null and customId!=null">
            LEFT JOIN web_custom_analysis_interaction_history ON
            web_custom_analysis.id=web_custom_analysis_interaction_history.analysis_id
            and web_custom_analysis_interaction_history.`custom_id` =#{customId}
        </if>

        <where>
            <if test="platformCustomAnalysis == null and customId!=null">
                and web_custom_analysis.`custom_id` =#{customId}
            </if>
            <if test="title!=null and title!=''">
                and web_custom_analysis.`title` like concat("%",#{title},"%")
            </if>
            <if test="email!=null and email!=''">
                and custom_info.`email` like concat("%",#{email},"%")
            </if>
            <if test="cId!=null">
                and custom_info.`c_id`=#{cId}
            </if>
            <if test="releaseStatus !=null">
                <if test="releaseStatus == 0">
                    and web_custom_analysis.`status` in(1,3)
                </if>
                <if test="releaseStatus == 1">
                    and web_custom_analysis.`status` =2
                </if>
            </if>
            <if test="releaseStatus ==null">
                <if test="status!=null">
                    and web_custom_analysis.`status` =#{status}
                </if>
            </if>
            <if test="startDate != null">
                and web_custom_analysis.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and web_custom_analysis.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by topping desc,
        <if test="sortType!=null and sortName!=null and sortName !=''">
            <if test="sortType==1">
                web_custom_analysis.${sortName},
            </if>
            <if test="sortType==2">
                web_custom_analysis.${sortName} desc,
            </if>
        </if>
        web_custom_analysis.add_time desc
        <if test="page!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>
    <select id="getById" resultType="com.crm.rely.backend.model.pojo.view.WebCustomAnalysisView">

        select web_custom_analysis.*,
               custom_info.first_name
                   custom_name,
               custom_info.ib_id,custom_info.c_id,custom_info.email
        from web_custom_analysis
                 LEFT JOIN custom_info ON web_custom_analysis.custom_id = custom_info.id
        where web_custom_analysis.id = #{id} limit 1
    </select>
</mapper>