<?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.MemberUnionidRelatedMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.MemberUnionidRelated">
        <id column="member_unionid_related_id" property="memberUnionidRelatedId" jdbcType="VARCHAR"/>
        <result column="member_id" property="memberId" jdbcType="VARCHAR"/>
        <result column="unionid" property="unionid" jdbcType="VARCHAR"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="enterprise_id" property="enterpriseId" jdbcType="VARCHAR"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="external_userid" property="externalUserid" jdbcType="VARCHAR"/>
        <result column="staff_id" property="staffId" jdbcType="VARCHAR"/>
        <result column="self_external_userid" property="selfExternalUserid" jdbcType="VARCHAR"/>
        <result column="add_create_time" property="addCreateTime" jdbcType="VARCHAR"/>
        <result column="external_name" property="externalName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    <sql id="Base_Column_List">
        member_unionid_related_id, member_id, unionid, wx_enterprise_id, status_flag, external_userid,
    staff_id, create_time, update_time,add_create_time,self_external_userid,external_name,enterprise_id
    </sql>
    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.MemberUnionidRelated">
        insert into tab_haoban_member_unionid_related (member_unionid_related_id, member_id,
                                                       unionid, wx_enterprise_id, status_flag,
                                                       external_userid, staff_id, create_time,
                                                       update_time, add_create_time, self_external_userid,
                                                       external_name, enterprise_id)
        values (#{memberUnionidRelatedId,jdbcType=VARCHAR}, #{memberId,jdbcType=VARCHAR},
                #{unionid,jdbcType=VARCHAR}, #{wxEnterpriseId,jdbcType=VARCHAR}, #{statusFlag,jdbcType=INTEGER},
                #{externalUserid,jdbcType=VARCHAR}, #{staffId,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
                #{updateTime,jdbcType=TIMESTAMP}, #{addCreateTime}, #{selfExternalUserid}, #{externalName},
                #{enterpriseId})
    </insert>
    
    <select id="getBySelfExternalUserId" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
       	from tab_haoban_member_unionid_related  where wx_enterprise_id = #{wxEnterpriseId}  and status_flag = 1
           and self_external_userid = #{selfExternalUserId}
        <if test="staffId != null">
         and staff_id = #{staffId}
        </if>
           union 
           select <include refid="Base_Column_List"/>
       	from tab_haoban_member_unionid_related  where wx_enterprise_id = #{wxEnterpriseId}  and status_flag = 1
           and external_userid = #{selfExternalUserId}
        <if test="staffId != null">
         and staff_id = #{staffId}
        </if>
           order by create_time desc limit 1
    </select>
    

    <select id="listByWxEnterpriseIdAndExternalUserId" resultMap="BaseResultMap">
    	select
           external_userid,  self_external_userid from tab_haoban_member_unionid_related
        where wx_enterprise_id = #{wxEnterpriseId}
        and external_userid in 
         <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        and status_flag = 1
    </select>

</mapper>