<?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.gic.haoban.manage.service.dao.mapper.content.TabHaobanPotentialCustomerMapper">

    <resultMap type="com.gic.haoban.manage.service.entity.content.TabHaobanPotentialCustomer"
               id="TabHaobanPotentialCustomerMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="enterpriseId" column="enterprise_id" jdbcType="VARCHAR"/>
        <result property="memberId" column="member_id" jdbcType="VARCHAR"/>
        <result property="unionId" column="union_id" jdbcType="VARCHAR"/>
        <result property="clerkId" column="clerk_id" jdbcType="VARCHAR"/>
        <result property="bizId" column="biz_id" jdbcType="VARCHAR"/>
        <result property="bizType" column="biz_type" jdbcType="INTEGER"/>
        <result property="storeId" column="store_id" jdbcType="VARCHAR"/>
        <result property="channelSource" column="channel_source" jdbcType="INTEGER"/>
        <result property="eventType" column="event_type" jdbcType="INTEGER"/>
        <result property="durationTime" column="duration_time" jdbcType="INTEGER"/>
        <result property="times" column="times" jdbcType="INTEGER"/>
        <result property="memberName" column="member_name" jdbcType="VARCHAR"/>
        <result property="memberNickName" column="member_nick_name" jdbcType="VARCHAR"/>
        <result property="memberImageUrl" column="member_image_url" jdbcType="VARCHAR"/>
        <result property="memberPhone" column="member_phone" jdbcType="VARCHAR"/>
        <result property="dealRecordNum" column="deal_record_num" jdbcType="INTEGER"/>
        <result property="starFlag" column="star_flag" jdbcType="INTEGER"/>
        <result property="seeFlag" column="see_flag" jdbcType="INTEGER"/>
        <result property="lastAccessTime" column="last_access_time" jdbcType="TIMESTAMP"/>
        <result property="deleteFlag" column="delete_flag" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="baseSql">
        id
        , enterprise_id,    member_id,
               union_id,
               clerk_id,
               biz_id,
               biz_type,
               store_id,
               channel_source,
               event_type,
               duration_time,
               times,
               member_name,
               member_nick_name,
               member_image_url,
               member_phone,
               deal_record_num,
               star_flag,
               see_flag,
               last_access_time,
               delete_flag,
               create_time,
               update_time
    </sql>

    <!--查询单个-->
    <select id="queryById" resultMap="TabHaobanPotentialCustomerMap">
        select <include refid="baseSql"/>
        from tab_haoban_potential_customer
        where id = #{id} and clerk_id = #{clerkId} and delete_flag = 0
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from tab_haoban_potential_customer
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="enterpriseId != null and enterpriseId != ''">
                and enterprise_id = #{enterpriseId}
            </if>
            <if test="memberId != null and memberId != ''">
                and member_id = #{memberId}
            </if>
            <if test="unionId != null and unionId != ''">
                and union_id = #{unionId}
            </if>
            <if test="clerkId != null and clerkId != ''">
                and clerk_id = #{clerkId}
            </if>
            <if test="bizId != null and bizId != ''">
                and biz_id = #{bizId}
            </if>
            <if test="bizType != null">
                and biz_type = #{bizType}
            </if>
            <if test="storeId != null and storeId != ''">
                and store_id = #{storeId}
            </if>
            <if test="channelSource != null">
                and channel_source = #{channelSource}
            </if>
            <if test="eventType != null">
                and event_type = #{eventType}
            </if>
            <if test="durationTime != null">
                and duration_time = #{durationTime}
            </if>
            <if test="memberName != null and memberName != ''">
                and member_name = #{memberName}
            </if>
            <if test="memberNickName != null and memberNickName != ''">
                and member_nick_name = #{memberNickName}
            </if>
            <if test="memberPhone != null and memberPhone != ''">
                and member_phone = #{memberPhone}
            </if>
            <if test="dealRecordNum != null">
                and deal_record_num = #{dealRecordNum}
            </if>
            <if test="starFlag != null">
                and star_flag = #{starFlag}
            </if>
            <if test="seeFlag != null">
                and see_flag = #{seeFlag}
            </if>
            <if test="lastAccessTime != null">
                and last_access_time = #{lastAccessTime}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
            <if test="createTime != null">
                and create_time = #{createTime}
            </if>
            <if test="updateTime != null">
                and update_time = #{updateTime}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into tab_haoban_potential_customer(id, enterprise_id, member_id, union_id, clerk_id, biz_id, biz_type,
                                                  store_id, channel_source, event_type, duration_time, times, member_name,
                                                  member_nick_name, member_image_url, member_phone, deal_record_num, star_flag, see_flag,
                                                  last_access_time, delete_flag, create_time, update_time)
        values (#{id}, #{enterpriseId}, #{memberId}, #{unionId}, #{clerkId}, #{bizId}, #{bizType}, #{storeId},
                #{channelSource}, #{eventType}, #{durationTime}, #{times},#{memberName}, #{memberNickName}, #{memberImageUrl}, #{memberPhone},
                #{dealRecordNum}, #{starFlag}, #{seeFlag}, #{lastAccessTime}, #{deleteFlag}, #{createTime},
                #{updateTime})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into tab_haoban_potential_customer(id, enterprise_id, member_id, union_id, clerk_id, biz_id, biz_type,
        store_id, channel_source, event_type, duration_time, times, member_name, member_nick_name, member_image_url, member_phone,
        deal_record_num, star_flag, see_flag, last_access_time, delete_flag, create_time, update_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id}, #{entity.enterpriseId}, #{entity.memberId}, #{entity.unionId}, #{entity.clerkId}, #{entity.bizId},
            #{entity.bizType}, #{entity.storeId}, #{entity.channelSource}, #{entity.eventType}, #{entity.durationTime},#{entity.times},
            #{entity.memberName}, #{entity.memberNickName}, #{entity.memberImageUrl}, #{entity.memberPhone}, #{entity.dealRecordNum},
            #{entity.starFlag}, #{entity.seeFlag}, #{entity.lastAccessTime}, #{entity.deleteFlag}, #{entity.createTime},
            #{entity.updateTime})
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update tab_haoban_potential_customer
        <set>
            <if test="enterpriseId != null and enterpriseId != ''">
                enterprise_id = #{enterpriseId},
            </if>
            <if test="memberId != null and memberId != ''">
                member_id = #{memberId},
            </if>
            <if test="unionId != null and unionId != ''">
                union_id = #{unionId},
            </if>
            <if test="clerkId != null and clerkId != ''">
                clerk_id = #{clerkId},
            </if>
            <if test="bizId != null and bizId != ''">
                biz_id = #{bizId},
            </if>
            <if test="bizType != null">
                biz_type = #{bizType},
            </if>
            <if test="storeId != null and storeId != ''">
                store_id = #{storeId},
            </if>
            <if test="channelSource != null">
                channel_source = #{channelSource},
            </if>
            <if test="eventType != null">
                event_type = #{eventType},
            </if>
            <if test="durationTime != null">
                duration_time = #{durationTime},
            </if>
            <if test="times != null">
                times = #{times},
            </if>
            <if test="memberName != null and memberName != ''">
                member_name = #{memberName},
            </if>
            <if test="memberNickName != null and memberNickName != ''">
                member_nick_name = #{memberNickName},
            </if>
            <if test="memberPhone != null and memberPhone != ''">
                member_phone = #{memberPhone},
            </if>
            <if test="dealRecordNum != null">
                deal_record_num = #{dealRecordNum},
            </if>
            <if test="starFlag != null">
                star_flag = #{starFlag},
            </if>
            <if test="seeFlag != null">
                see_flag = #{seeFlag},
            </if>
            <if test="lastAccessTime != null">
                last_access_time = #{lastAccessTime},
            </if>
            <if test="deleteFlag != null">
                delete_flag = #{deleteFlag},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from tab_haoban_potential_customer
        where id = #{id}
    </delete>

    <select id="queryPotentialCustomerList" resultMap="TabHaobanPotentialCustomerMap">
        select customer.*
        from tab_haoban_potential_customer customer
        <if test="hasMemberRelation != null">
            left join tab_haoban_external_clerk_related related on related.staff_id = #{staffId} and related.status_flag in (1, 3, 4) and customer.member_id = related.member_id
        </if>
        <if test="bizIds != null and bizIds.size > 0">
            <!-- 按素材 -->
            INNER JOIN (
                SELECT `enterprise_id`,`clerk_id`,`member_id`
                FROM tab_haoban_interact_record
                <where>
                    clerk_id = #{clerkId} and delete_flag = 0 and biz_type = 1
                    <if test="enterpriseId != null and enterpriseId != ''">
                        and enterprise_id = #{enterpriseId}
                    </if>
                    and biz_id in
                    <foreach collection="bizIds" separator="," item="item" open="(" close=")">
                        #{item}
                    </foreach>
                </where>
                GROUP BY `enterprise_id`,`clerk_id`,`member_id`
                ORDER BY NULL
            ) temp
            on customer.enterprise_id = temp.enterprise_id and customer.clerk_id = temp.clerk_id and customer.member_id = temp.member_id
        </if>
        <where>
            customer.clerk_id = #{clerkId} and customer.delete_flag = 0
            <if test="enterpriseId != null and enterpriseId != ''">
                and customer.enterprise_id = #{enterpriseId}
            </if>
            <if test="search != null and search != '' and searchType != null and searchType == 1">
                <!-- 姓名、昵称、手机号 -->
                and ( customer.member_name like concat('%', #{search}, '%') or customer.member_nick_name like concat('%', #{search}, '%') or customer.member_phone like concat('%', #{search}, '%') )
            </if>
            <if test="seeFlag != null">
                and customer.see_flag = #{seeFlag}
            </if>
            <if test="starFlag != null">
                and customer.star_flag = #{starFlag}
            </if>
            <if test="hasMemberRelation != null">
                <!-- 企微好友 -->
                and related.clerk_id is not null
            </if>
            <if test="dealRecord != null">
                and customer.deal_record_num > 0
            </if>
            <if test="hasMemberPhone != null">
                and customer.member_phone is not null
            </if>
        </where>
        order by customer.last_access_time desc, customer.id desc
    </select>

    <select id="queryWithClerkIdAndMemberId" resultMap="TabHaobanPotentialCustomerMap">
        select <include refid="baseSql"/>
            from tab_haoban_potential_customer
        where clerk_id = #{clerkId} and delete_flag = 0 and member_id = #{memberId}
    </select>

    <update id="mergeMember">
        update tab_haoban_potential_customer
            set member_id = #{memberId}
            <if test="memberPhone != null and memberPhone != ''">
                , member_phone = #{memberPhone}
            </if>
        where member_id = #{mergeMemberId} and delete_flag = 0
    </update>

    <update id="updateMemberDealRecordNum">
        update tab_haoban_potential_customer
            set deal_record_num =1
        where member_id = #{memberId} and enterprise_id = #{enterpriseId} and delete_flag = 0
    </update>

</mapper>

