<?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.contact.TabContactLogMapper">
    <resultMap type="com.gic.haoban.manage.service.entity.contact.TabContactLog" id="TabContactLogMap">
        <result property="logId" column="log_id" jdbcType="BIGINT"/>
        <result property="memberId" column="member_id" jdbcType="VARCHAR"/>
        <result property="clerkId" column="clerk_id" jdbcType="VARCHAR"/>
        <result property="clerkCode" column="clerk_code" jdbcType="VARCHAR"/>
        <result property="clerkName" column="clerk_name" jdbcType="VARCHAR"/>
        <result property="storeId" column="store_id" jdbcType="VARCHAR"/>
        <result property="storeCode" column="store_code" jdbcType="VARCHAR"/>
        <result property="contactStatus" column="contact_status" jdbcType="INTEGER"/>
        <result property="contactTime" column="contact_time" jdbcType="TIMESTAMP"/>
        <result property="contactCycle" column="contact_cycle" jdbcType="INTEGER"/>
        <result property="contactCycleFirst" column="contact_cycle_first" jdbcType="INTEGER"/>
        <result property="potentialTime" column="potential_time" jdbcType="TIMESTAMP"/>
        <result property="enterpriseId" column="enterprise_id" jdbcType="VARCHAR"/>
        <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>

    <!--查询单个-->
    <select id="queryById" resultMap="TabContactLogMap">
        select *
        from tab_contact_log
        where log_id = #{logId} and delete_flag = 0
    </select>

    <!--查询多个-->
    <select id="queryByIds" resultMap="TabContactLogMap">
        select *
        from tab_contact_log
        where delete_flag = 0 and log_id in
        <foreach collection="logIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        order by create_time desc
    </select>

    <select id="queryLastLog" resultMap="TabContactLogMap">
        select *
        from tab_contact_log
        where delete_flag = 0 and member_id = #{memberId}
        <if test="clerkId!=null and clerkId!=''">
            and clerk_id = #{clerkId}
        </if>
        <if test="beginTime!=null ">
            and contact_time >= #{beginTime}
        </if>
        order by contact_time desc,log_id desc limit 1
    </select>

    <!--查询列表-->
    <select id="listContactLog" resultMap="TabContactLogMap">
        select
        a.*
        from tab_contact_log a
        where a.enterprise_id = #{enterpriseId} and a.delete_flag = 0
        <if test="search.memberId != null and search.memberId != ''">
            and a.member_id = #{search.memberId}
        </if>
        <if test="search.clerkId != null">
            and a.clerk_id = #{search.clerkId}
        </if>
        <if test="search.contactStatus != null">
            and a.contact_status = #{search.contactStatus}
        </if>
        <if test="search.beginDate != null">
            and a.contact_time >= #{search.beginDate}
        </if>
        <if test="search.endDate != null">
            and a.contact_time <![CDATA[<=]]> #{search.endDate}
        </if>
        order by a.create_time desc,a.log_id desc
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from tab_contact_log
        <where>
            <if test="logId != null">
                and log_id = #{logId}
            </if>
            <if test="memberId != null and memberId != ''">
                and member_id = #{memberId}
            </if>
            <if test="clerkId != null">
                and clerk_id = #{clerkId}
            </if>
            <if test="contactStatus != null">
                and contact_status = #{contactStatus}
            </if>
            <if test="contactTime != null">
                and contact_time = #{contactTime}
            </if>
            <if test="contactCycle != null">
                and contact_cycle = #{contactCycle}
            </if>
            <if test="contactCycleFirst != null">
                and contact_cycle_first = #{contactCycleFirst}
            </if>
            <if test="potentialTime != null">
                and potential_time = #{potentialTime}
            </if>
            <if test="enterpriseId != null and enterpriseId != ''">
                and enterprise_id = #{enterpriseId}
            </if>
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="logId" useGeneratedKeys="true">
        insert into tab_contact_log(
            log_id,
            member_id,
            clerk_id,
            clerk_code,
            clerk_name,
            store_id,
            store_code,
            contact_status,
            contact_time,
            contact_cycle,
            contact_cycle_first,
            potential_time,
            enterprise_id
            )
        values (
                    #{logId},
                   #{memberId},
                   #{clerkId},
                   #{clerkCode},
                   #{clerkName},
                   #{storeId},
                   #{storeCode},
                   #{contactStatus},
                   #{contactTime},
                   #{contactCycle},
                   #{contactCycleFirst},
                   #{potentialTime},
                   #{enterpriseId})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update tab_contact_log
        <set>
            <if test="memberId != null and memberId != ''">
                member_id = #{memberId},
            </if>
            <if test="clerkId != null">
                clerk_id = #{clerkId},
            </if>
            <if test="clerkCode != null and clerkCode != ''">
                clerk_code = #{clerkCode},
            </if>
            <if test="clerkName != null and clerkName != ''">
                clerk_name = #{clerkName},
            </if>
            <if test="storeId != null">
                store_id = #{storeId},
            </if>
            <if test="storeCode != null and storeCode != ''">
                store_code = #{storeCode},
            </if>
            <if test="contactStatus != null">
                contact_status = #{contactStatus},
            </if>
            <if test="contactTime != null">
                contact_time = #{contactTime},
            </if>
            <if test="contactCycle != null">
                contact_cycle = #{contactCycle},
            </if>
            <if test="contactCycleFirst != null">
                contact_cycle_first = #{contactCycleFirst},
            </if>
            <if test="potentialTime != null">
                potential_time = #{potentialTime},
            </if>
            <if test="enterpriseId != null and enterpriseId != ''">
                enterprise_id = #{enterpriseId},
            </if>
            <if test="deleteFlag != null">
                delete_flag = #{deleteFlag},
            </if>
        </set>
        where log_id = #{logId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        update tab_contact_log set delete_flag = 1
        where enterprise_id = #{enterpriseId} and log_id = #{logId} and delete_flag = 0
    </delete>
</mapper>