<?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.ruoyi.ibs.task.mapper.VisitTaskMapper">

    <select id="getVisitTaskList" parameterType="VisitTaskListDTO" resultType="VisitTaskListVO">
        select
            sc.campaign_name, sc.campaign_id, sc.start_time, sc.end_time, sc.task_type, sc.check_target, sc.visit_type, sc.campaign_degree, sc.cust_type, sc.remark
        from sys_campaign sc
                 left join sys_campaign_group scg on scg.campaign_id = sc.campaign_id
                 left join sys_campaign_group_customer sgc on sgc.campaign_id = sc.campaign_id
                 left join sys_group sg on sg.group_id = scg.group_id
        where sgc.user_id = #{userId}
          and sc.cust_type = #{custType}
          and sgc.cust_claim_status = 1
          and sc.claim_end_time > current_timestamp()
        <if test="campaignName != null and campaignName != ''">
            and sc.campaign_name like concat('%', #{campaignName}, '%')
        </if>
        <if test="visitType != null and visitType != ''">
            and sc.visit_type = #{visitType}
        </if>
        <if test="startTimeMonth != null and startTimeMonth != '' ">
            and sc.start_time like concat(#{startTimeMonth}, '%')
        </if>
        group by sc.campaign_name, sc.campaign_id, sc.start_time, sc.end_time,sc.task_type, sc.check_target,  sc.visit_type, sc.campaign_degree, sc.remark
        order by sc.end_time
    </select>

    <select id="getVisitTargetRetailList" parameterType="VisitTargetDTO" resultType="VisitTargetVO">
        select a.cust_id, a.cust_idc, a.cust_isn, a.cust_gender, a.cust_name, a.cust_phone, b.id as record_id, isnull(
        b.sign_id) as sign_flag, b.feedback_id, b.feedback_type, b.feedback_template_id
        from cust_info_retail a
        left join (
        select id, cust_id , sign_id, feedback_id, feedback_type, feedback_template_id from task_visit_record where
        campaign_id = #{campaignId} and user_id = #{userId}
        ) b on a.cust_id = b.cust_id
        where a.cust_id in (
            select cust_id from sys_campaign_group_customer
                where campaign_id = #{campaignId}
                and user_id = #{userId}
                and cust_claim_status = 1
        )
        <if test="custName != null and custName != ''">
            and a.cust_name like concat('%', #{custName}, '%')
        </if>
    </select>

    <select id="getVisitTargetBusinessList" parameterType="VisitTargetDTO" resultType="VisitTargetVO">
        select a.cust_id, a.cust_idc, a.social_credit_code, a.cust_gender, a.cust_name, a.cust_phone, a.status, b.id as
        record_id, isnull( b.sign_id) as sign_flag , b.feedback_id, b.feedback_type, b.feedback_template_id
        from cust_info_business a
        left join (
        select id, cust_id, sign_id, feedback_id, feedback_type, feedback_template_id from task_visit_record where
        campaign_id = #{campaignId} and user_id = #{userId}
        ) b on a.cust_id = b.cust_id
        where a.cust_id in (
            select cust_id from sys_campaign_group_customer
            where campaign_id = #{campaignId}
              and user_id = #{userId}
              and cust_claim_status = 1
        )
        <if test="custName != null and custName != ''">
            and a.cust_name like concat('%', #{custName}, '%')
        </if>
    </select>

    <select id="getVisitTargetMerchantList" parameterType="VisitTargetDTO" resultType="VisitTargetVO">
        select a.cust_id, a.cust_idc, a.social_credit_code, a.cust_gender, a.cust_name, a.cust_phone, a.status, b.id as
        record_id, isnull( b.sign_id) as sign_flag , b.feedback_id, b.feedback_type, b.feedback_template_id
        from cust_info_merchant a
        left join (
        select id, cust_id , sign_id, feedback_id, feedback_type, feedback_template_id from task_visit_record where
        campaign_id = #{campaignId} and user_id = #{userId}
        ) b on a.cust_id = b.cust_id
        where a.cust_id in (
            select cust_id from sys_campaign_group_customer
            where campaign_id = #{campaignId}
              and user_id = #{userId}
              and cust_claim_status = 1
        )
        <if test="custName != null and custName != ''">
            and a.cust_name like concat('%', #{custName}, '%')
        </if>
    </select>


</mapper>