<?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.TabHaobanInteractRecordMapper">

    <resultMap type="com.gic.haoban.manage.service.entity.content.TabHaobanInteractRecord" id="TabHaobanInteractRecordMap">
        <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="traceId" column="trace_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="shareSource" column="share_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="deleteFlag" column="delete_flag" jdbcType="INTEGER"/>
        <result property="extendInfo" column="extend_info" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="lastAccessTime" column="last_access_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="baseSql">
        id,
               enterprise_id,
               member_id,
               union_id,
               clerk_id,
               trace_id,
               biz_id,
               biz_type,
               store_id,
               channel_source,
               share_source,
               event_type,
               duration_time,
               times,
               delete_flag,
               extend_info,
               create_time,
               update_time,
               last_access_time
    </sql>

    <!--查询单个-->
    <select id="queryById" resultMap="TabHaobanInteractRecordMap">
        select id,
               enterprise_id,
               member_id,
               union_id,
               clerk_id,
               trace_id,
               biz_id,
               biz_type,
               store_id,
               channel_source,
               share_source,
               event_type,
               duration_time,
               times,
               delete_flag,
               extend_info,
               create_time,
               update_time,
               last_access_time
        from tab_haoban_interact_record
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="TabHaobanInteractRecordMap">
        select
        id, enterprise_id, member_id, union_id, clerk_id, trace_id, biz_id, biz_type, store_id, channel_source,share_source,
        event_type, duration_time, times, delete_flag, extend_info, create_time, update_time,last_access_time
        from tab_haoban_interact_record
        <where>
            <if test="clerkId != null and clerkId != ''">
                 clerk_id = #{clerkId} or member_id = #{clerkId}
            </if>
        </where>
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from tab_haoban_interact_record
        <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="traceId != null and traceId != ''">
                and trace_id = #{traceId}
            </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="shareSource != null">
                and share_source = #{shareSource}
            </if>
            <if test="eventType != null">
                and event_type = #{eventType}
            </if>
            <if test="durationTime != null">
                and duration_time = #{durationTime}
            </if>
            <if test="times != null">
                and times = #{times}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
            <if test="extendInfo != null and extendInfo != ''">
                and extend_info = #{extendInfo}
            </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_interact_record(id, enterprise_id, member_id, union_id, clerk_id, trace_id, biz_id, biz_type,
                                               store_id, channel_source,share_source, event_type, duration_time, times, delete_flag,
                                               extend_info, create_time, update_time, last_access_time)
        values (#{id}, #{enterpriseId}, #{memberId}, #{unionId}, #{clerkId}, #{traceId}, #{bizId}, #{bizType}, #{storeId},
                #{channelSource}, #{shareSource},#{eventType}, #{durationTime}, #{times}, #{deleteFlag}, #{extendInfo}, #{createTime},
                #{updateTime}, #{lastAccessTime})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into tab_haoban_interact_record(enterprise_id, member_id, union_id, clerk_id, trace_id, biz_id, biz_type,
        store_id, channel_source,share_source, event_type, duration_time, times, delete_flag, extend_info, create_time, update_time, last_access_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.enterpriseId}, #{entity.memberId}, #{entity.unionId}, #{entity.clerkId}, #{entity.traceId},
            #{entity.bizId}, #{entity.bizType}, #{entity.storeId}, #{entity.channelSource}, #{entity.shareSource},#{entity.eventType},
            #{entity.durationTime}, #{entity.times}, #{entity.deleteFlag}, #{entity.extendInfo}, #{entity.createTime},
            #{entity.updateTime}, #{entity.lastAccessTime})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into tab_haoban_interact_record(enterprise_id, member_id, union_id, clerk_id, trace_id, biz_id, biz_type,
        store_id, channel_source, share_source,event_type, duration_time, times, delete_flag, extend_info, create_time, update_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.enterpriseId}, #{entity.memberId}, #{entity.unionId}, #{entity.clerkId}, #{entity.traceId},
            #{entity.bizId}, #{entity.bizType}, #{entity.storeId}, #{entity.channelSource},#{entity.shareSource}, #{entity.eventType},
            #{entity.durationTime}, #{entity.times}, #{entity.deleteFlag}, #{entity.extendInfo}, #{entity.createTime},
            #{entity.updateTime})
        </foreach>
        on duplicate key update
        enterprise_id = values(enterprise_id),
        member_id = values(member_id),
        union_id = values(union_id),
        clerk_id = values(clerk_id),
        trace_id = values(trace_id),
        biz_id = values(biz_id),
        biz_type = values(biz_type),
        store_id = values(store_id),
        channel_source = values(channel_source),
        share_source = values(share_source),
        event_type = values(event_type),
        duration_time = values(duration_time),
        times = values(times),
        delete_flag = values(delete_flag),
        extend_info = values(extend_info),
        create_time = values(create_time),
        update_time = values(update_time)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update tab_haoban_interact_record
        <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="traceId != null and traceId != ''">
                trace_id = #{traceId},
            </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="shareSource != null">
                share_source = #{shareSource},
            </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="deleteFlag != null">
                delete_flag = #{deleteFlag},
            </if>
            <if test="extendInfo != null and extendInfo != ''">
                extend_info = #{extendInfo},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="lastAccessTime != null">
                last_access_time = #{lastAccessTime}
            </if>
        </set>
        where id = #{id}
    </update>

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

    <select id="queryWithEnterpriseIdAndTraceId" resultMap="TabHaobanInteractRecordMap">
        select <include refid="baseSql"/>
            from tab_haoban_interact_record
        where enterprise_id = #{enterpriseId} and trace_id = #{traceId}
    </select>

    <select id="queryInteractRecordList" resultMap="TabHaobanInteractRecordMap">
        select <include refid="baseSql"/>
            from tab_haoban_interact_record
        <where>
            delete_flag = 0
            <if test="enterpriseId != null and enterpriseId != ''">
                and enterprise_id = #{enterpriseId}
            </if>
            <if test="clerkId != null and clerkId != ''">
                and clerk_id = #{clerkId}
            </if>
            <if test="memberId != null and memberId != ''">
                and member_id = #{memberId}
            </if>
            <if test="startTime != null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and create_time <![CDATA[ <= ]]>  #{endTime}
            </if>
            <if test="unionId != null and unionId != '' ">
                and union_id = #{unionId}
            </if>
            <if test="bizId != null and bizId != ''">
                and biz_id = #{bizId}
            </if>
        </where>
        order by last_access_time desc, id desc
    </select>

    <select id="staticsClerkInteractRecordNew" resultType="com.gic.haoban.manage.service.pojo.bo.content.PotentialCustomerStaticsBO">
        select clerk_id clerkId, count(DISTINCT member_id) num
            from tab_haoban_interact_record
        where delete_flag = 0 and member_id is not null and member_id <![CDATA[ <> ]]> ''
        <if test="clerkId != null and clerkId != ''">
            and clerk_id = #{clerkId}
        </if>
        <if test="enterpriseId != null and enterpriseId != ''">
            and enterprise_id = #{enterpriseId}
        </if>
        <if test="startTime != null">
            and create_time >= #{startTime}
        </if>
        <if test="endTime != null">
            and create_time <![CDATA[ <= ]]>  #{endTime}
        </if>
        group by clerk_id
        order by null
    </select>

    <select id="selectLastByMemberIds" resultMap="TabHaobanInteractRecordMap">
        select t1.*
        FROM tab_haoban_interact_record t1
        JOIN (
        SELECT member_id, MAX(last_access_time) AS max_last_access_time
        FROM tab_haoban_interact_record
        WHERE clerk_id = #{clerkId} AND delete_flag = 0 and
        <foreach collection="list" item="item" separator=" OR ">
            (member_id = #{item.memberId} AND last_access_time > #{item.lastAccessTime})
        </foreach>
        GROUP BY member_id
        ) t2
        ON t1.member_id = t2.member_id AND t1.last_access_time = t2.max_last_access_time
        order by t1.last_access_time desc,t1.id desc
    </select>

    <select id="selectMemberIdsByTime" resultType="com.gic.haoban.manage.service.pojo.bo.potential.MemberLastInteractBO">
        select member_id as memberId, max(last_access_time) as lastAccessTime
        FROM tab_haoban_interact_record
        where clerk_id = #{clerkId} AND delete_flag = 0
        and last_access_time >= #{lastAccessTime}
        group by member_id
        order by last_access_time desc,id desc
    </select>

    <update id="mergeMember">
        update tab_haoban_interact_record
            set member_id = #{memberId}
        where member_id = #{mergeMemberId} and delete_flag = 0
    </update>

    <update id="memberAuthorized">
        update tab_haoban_interact_record
        set member_id = #{memberId}
        where union_id = #{unionId} and delete_flag = 0
        and (member_id is null or member_id = '')
    </update>

</mapper>

