<?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.hm.WxUserAddLogMapper">
    <resultMap type="com.gic.haoban.manage.service.entity.hm.TabWxUserAddLog"
               id="result-map-tabHaobanWxUserAddLog">
        <result column="log_id" property="logId"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId"/>
        <result column="enterprise_id" property="enterpriseId"/>
        <result column="external_userid" property="externalUserid"/>
        <result column="staff_id" property="staffId"/>
        <result column="staff_name" property="staffName"/>
        <result column="status_flag" property="statusFlag"/>
        <result column="add_channel" property="addChannel"/>
        <result column="hm_id" property="hmId"/>
        <result column="hm_name" property="hmName"/>
        <result column="link_id" property="linkId"/>
        <result column="link_name" property="linkName"/>
        <result column="member_id" property="memberId"/>
        <result column="member_name" property="memberName"/>
        <result column="member_nick" property="memberNick"/>
        <result column="member_image" property="memberImage"/>
        <result column="member_phone" property="memberPhone"/>
        <result column="member_grade_id" property="memberGradeId"/>
        <result column="member_grade_name" property="memberGradeName"/>
        <result column="member_card_no" property="memberCardNo"/>
        <result column="clerk_id" property="clerkId"/>
        <result column="clerk_code" property="clerkCode"/>
        <result column="clerk_name" property="clerkName"/>
        <result column="store_id" property="storeId"/>
        <result column="store_name" property="storeName"/>
        <result column="store_code" property="storeCode"/>
        <result column="member_label_id" property="memberLabelId"/>
        <result column="member_label_name" property="memberLabelName"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="all_del_flag" property="allDelFlag"/>
        <result column="qw_add_time" property="qwAddTime" />
        <result column="friend_clerk_id" property="friendClerkId" />
        <result column="friend_store_id" property="friendStoreId" />
    </resultMap>
    <sql id="Base_Column_List">
        log_id ,
		wx_enterprise_id,
		enterprise_id,
		external_userid,
		staff_id,
		staff_name,
		status_flag,
		add_channel,
		hm_id,
		hm_name,
		link_id,
		link_name,
		member_id,
		member_name,
		member_nick,
		member_image,
		member_phone,
		member_grade_id,
		member_grade_name,
		member_card_no,
		clerk_id,
		clerk_code,
		clerk_name,
		store_id,
		store_name,
		store_code,
		member_label_id,
		member_label_name,
		create_time,
		update_time,
		all_del_flag , qw_add_time , friend_clerk_id , friend_store_id
    </sql>
    <!-- ===================== 新增 ======================== -->
    <insert id="insert"
            parameterType="com.gic.haoban.manage.service.entity.hm.TabWxUserAddLog">
  <![CDATA[
        INSERT INTO tab_haoban_wx_user_add_log(log_id,
                                               wx_enterprise_id,
                                               enterprise_id,
                                               external_userid,
                                               staff_id,
                                               staff_name,
                                               status_flag,
                                               add_channel,
                                               hm_id,
                                               hm_name,
                                               link_id,
                                               link_name,
                                               member_id,
                                               member_name,
                                               member_nick,
                                               member_image,
                                               member_phone,
                                               member_grade_id,
                                               member_grade_name,
                                               member_card_no,
                                               clerk_id,
                                               clerk_code,
                                               clerk_name,
                                               store_id,
                                               store_name,
                                               store_code,
                                               member_label_id,
                                               member_label_name,
                                               create_time,
                                               update_time,
                                               add_time,
                                               all_del_flag , qw_add_time , friend_clerk_id , friend_store_id , del_type , del_is_single)
        VALUES (#{logId,jdbcType=BIGINT},
                #{wxEnterpriseId,jdbcType=VARCHAR},
                #{enterpriseId,jdbcType=CHAR},
                #{externalUserid,jdbcType=VARCHAR},
                #{staffId,jdbcType=VARCHAR},
                #{staffName,jdbcType=VARCHAR},
                #{statusFlag,jdbcType=INTEGER},
                #{addChannel,jdbcType=INTEGER},
                #{hmId,jdbcType=BIGINT},
                #{hmName,jdbcType=VARCHAR},
                #{linkId,jdbcType=BIGINT},
                #{linkName,jdbcType=VARCHAR},
                #{memberId,jdbcType=VARCHAR},
                #{memberName,jdbcType=VARCHAR},
                #{memberNick,jdbcType=VARCHAR},
                #{memberImage,jdbcType=VARCHAR},
                #{memberPhone,jdbcType=VARCHAR},
                #{memberGradeId,jdbcType=VARCHAR},
                #{memberGradeName,jdbcType=VARCHAR},
                #{memberCardNo,jdbcType=VARCHAR},
                #{clerkId,jdbcType=VARCHAR},
                #{clerkCode,jdbcType=VARCHAR},
                #{clerkName,jdbcType=VARCHAR},
                #{storeId,jdbcType=VARCHAR},
                #{storeName,jdbcType=VARCHAR},
                #{storeCode,jdbcType=VARCHAR},
                #{memberLabelId,jdbcType=VARCHAR},
                #{memberLabelName,jdbcType=VARCHAR},
                now(),
                now(),
                now(),
                #{allDelFlag,jdbcType=INTEGER} , #{qwAddTime} , #{friendClerkId} , #{friendStoreId} , #{delType} , #{delIsSingle})
        ]]>
	</insert>


    <update id="updateMemberInfo" parameterType="com.gic.haoban.manage.service.entity.hm.TabWxUserAddLog">
        UPDATE tab_haoban_wx_user_add_log SET
        member_id=#{memberId,jdbcType=VARCHAR},
        member_name=#{memberName,jdbcType=VARCHAR},
        member_nick=#{memberNick,jdbcType=VARCHAR},
        member_image=#{memberImage,jdbcType=VARCHAR},
        member_phone=#{memberPhone,jdbcType=VARCHAR},
        member_grade_id=#{memberGradeId,jdbcType=VARCHAR},
        member_grade_name=#{memberGradeName,jdbcType=VARCHAR},
        member_card_no=#{memberCardNo,jdbcType=VARCHAR},
        <if test="null != createTime">
            create_time = #{createTime} ,
        </if>
        update_time=now() WHERE
        external_userid=#{externalUserid} and staff_id=#{staffId} and member_id is null
    </update>


    <select id="listAllForDel"
            parameterType="com.gic.haoban.manage.service.entity.hm.TabWxUserAddLog"
            resultMap="result-map-tabHaobanWxUserAddLog">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_user_add_log where
        external_userid=#{externalUserid} and status_flag != 0 order by
        create_time asc
    </select>

    <select id="getForTask"
            resultMap="result-map-tabHaobanWxUserAddLog">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_user_add_log where
        external_userid=#{externalUserid} and status_flag = 2 and add_channel = 4  order by
        create_time desc limit 1
    </select>

    <select id="selectNewLog" resultMap="result-map-tabHaobanWxUserAddLog">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_user_add_log where
        external_userid=#{externalUserid} and staff_id =#{staffId} and
        status_flag != 0 order by create_time desc limit 1
    </select>

    <select id="listNewLog" resultMap="result-map-tabHaobanWxUserAddLog">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_user_add_log where
        external_userid=#{externalUserid} and staff_id =#{staffId} and
        status_flag != 0 and friend_clerk_id = #{friendClerkId} order by create_time desc limit 1
    </select>

    <select id="listForPage" parameterType="com.gic.haoban.manage.api.dto.qdto.hm.WxUserAddLogSearchQDTO"
            resultMap="result-map-tabHaobanWxUserAddLog">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_user_add_log where wx_enterprise_id = #{wxEnterpriseId}
        <if test="null != enterpriseId">
            and enterprise_id = #{enterpriseId}
        </if>
        <if test="null != linkId">
            and link_id = #{linkId}
        </if>
        <if test="null != hmId">
            and hm_id = #{hmId}
        </if>
        <if test="null != clerkId">
            and clerk_id = #{clerkId}
        </if>
        <if test="null != startTime">
            and create_time <![CDATA[>=]]> #{startTime}
        </if>
        <if test="null != endTime">
            and create_time <![CDATA[<=]]> #{endTime}
        </if>
        <if test="null != addChannel">
            and add_channel = #{addChannel}
        </if>
        and status_flag = #{statusFlag} order by create_time ${createTimeOrder}
    </select>


    <select id="listHalfDelTime" resultMap="result-map-tabHaobanWxUserAddLog">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_wx_user_add_log where  status_flag = 3 and  external_userid in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by create_time desc
    </select>
</mapper> 