<?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.tzld.piaoquan.growth.common.dao.mapper.WeComUserMapper">
  <resultMap id="BaseResultMap" type="com.tzld.piaoquan.growth.common.model.po.WeComUser">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="corp_id" jdbcType="BIGINT" property="corpId" />
    <result column="external_user_id" jdbcType="VARCHAR" property="externalUserId" />
    <result column="union_id" jdbcType="VARCHAR" property="unionId" />
    <result column="external_user_id_3rd_party" jdbcType="VARCHAR" property="externalUserId3rdParty" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="avatar" jdbcType="VARCHAR" property="avatar" />
    <result column="gender" jdbcType="INTEGER" property="gender" />
    <result column="is_delete" jdbcType="INTEGER" property="isDelete" />
    <result column="group_msg_disabled" jdbcType="TINYINT" property="groupMsgDisabled" />
    <result column="created_at" jdbcType="BIGINT" property="createdAt" />
    <result column="updated_at" jdbcType="BIGINT" property="updatedAt" />
    <result column="deleted_at" jdbcType="BIGINT" property="deletedAt" />
    <result column="abtype" jdbcType="VARCHAR" property="abtype" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, corp_id, external_user_id, union_id, external_user_id_3rd_party, `type`, `name`, 
    avatar, gender, is_delete, group_msg_disabled, created_at, updated_at, deleted_at, 
    abtype, create_time, update_time
  </sql>
  <select id="selectByExample" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUserExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from we_com_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
    <if test="page != null">
      limit #{page.offset} , #{page.pageSize}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from we_com_user
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from we_com_user
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <delete id="deleteByExample" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUserExample">
    delete from we_com_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUser" useGeneratedKeys="true" keyProperty="id">
    insert into we_com_user (id, corp_id, external_user_id, 
      union_id, external_user_id_3rd_party, `type`, 
      `name`, avatar, gender, 
      is_delete, group_msg_disabled, created_at, 
      updated_at, deleted_at, abtype, 
      create_time, update_time)
    values (#{id,jdbcType=BIGINT}, #{corpId,jdbcType=BIGINT}, #{externalUserId,jdbcType=VARCHAR}, 
      #{unionId,jdbcType=VARCHAR}, #{externalUserId3rdParty,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, 
      #{name,jdbcType=VARCHAR}, #{avatar,jdbcType=VARCHAR}, #{gender,jdbcType=INTEGER}, 
      #{isDelete,jdbcType=INTEGER}, #{groupMsgDisabled,jdbcType=TINYINT}, #{createdAt,jdbcType=BIGINT}, 
      #{updatedAt,jdbcType=BIGINT}, #{deletedAt,jdbcType=BIGINT}, #{abtype,jdbcType=VARCHAR}, 
      #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
    <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
      select LAST_INSERT_ID()
    </selectKey>
  </insert>
  <insert id="insertSelective" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUser" useGeneratedKeys="true" keyProperty="id">
    insert into we_com_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="corpId != null">
        corp_id,
      </if>
      <if test="externalUserId != null">
        external_user_id,
      </if>
      <if test="unionId != null">
        union_id,
      </if>
      <if test="externalUserId3rdParty != null">
        external_user_id_3rd_party,
      </if>
      <if test="type != null">
        `type`,
      </if>
      <if test="name != null">
        `name`,
      </if>
      <if test="avatar != null">
        avatar,
      </if>
      <if test="gender != null">
        gender,
      </if>
      <if test="isDelete != null">
        is_delete,
      </if>
      <if test="groupMsgDisabled != null">
        group_msg_disabled,
      </if>
      <if test="createdAt != null">
        created_at,
      </if>
      <if test="updatedAt != null">
        updated_at,
      </if>
      <if test="deletedAt != null">
        deleted_at,
      </if>
      <if test="abtype != null">
        abtype,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="corpId != null">
        #{corpId,jdbcType=BIGINT},
      </if>
      <if test="externalUserId != null">
        #{externalUserId,jdbcType=VARCHAR},
      </if>
      <if test="unionId != null">
        #{unionId,jdbcType=VARCHAR},
      </if>
      <if test="externalUserId3rdParty != null">
        #{externalUserId3rdParty,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="avatar != null">
        #{avatar,jdbcType=VARCHAR},
      </if>
      <if test="gender != null">
        #{gender,jdbcType=INTEGER},
      </if>
      <if test="isDelete != null">
        #{isDelete,jdbcType=INTEGER},
      </if>
      <if test="groupMsgDisabled != null">
        #{groupMsgDisabled,jdbcType=TINYINT},
      </if>
      <if test="createdAt != null">
        #{createdAt,jdbcType=BIGINT},
      </if>
      <if test="updatedAt != null">
        #{updatedAt,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        #{deletedAt,jdbcType=BIGINT},
      </if>
      <if test="abtype != null">
        #{abtype,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
    <selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">
      select LAST_INSERT_ID()
    </selectKey>
  </insert>
  <select id="countByExample" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUserExample" resultType="java.lang.Long">
    select count(*) from we_com_user
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update we_com_user
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=BIGINT},
      </if>
      <if test="record.corpId != null">
        corp_id = #{record.corpId,jdbcType=BIGINT},
      </if>
      <if test="record.externalUserId != null">
        external_user_id = #{record.externalUserId,jdbcType=VARCHAR},
      </if>
      <if test="record.unionId != null">
        union_id = #{record.unionId,jdbcType=VARCHAR},
      </if>
      <if test="record.externalUserId3rdParty != null">
        external_user_id_3rd_party = #{record.externalUserId3rdParty,jdbcType=VARCHAR},
      </if>
      <if test="record.type != null">
        `type` = #{record.type,jdbcType=INTEGER},
      </if>
      <if test="record.name != null">
        `name` = #{record.name,jdbcType=VARCHAR},
      </if>
      <if test="record.avatar != null">
        avatar = #{record.avatar,jdbcType=VARCHAR},
      </if>
      <if test="record.gender != null">
        gender = #{record.gender,jdbcType=INTEGER},
      </if>
      <if test="record.isDelete != null">
        is_delete = #{record.isDelete,jdbcType=INTEGER},
      </if>
      <if test="record.groupMsgDisabled != null">
        group_msg_disabled = #{record.groupMsgDisabled,jdbcType=TINYINT},
      </if>
      <if test="record.createdAt != null">
        created_at = #{record.createdAt,jdbcType=BIGINT},
      </if>
      <if test="record.updatedAt != null">
        updated_at = #{record.updatedAt,jdbcType=BIGINT},
      </if>
      <if test="record.deletedAt != null">
        deleted_at = #{record.deletedAt,jdbcType=BIGINT},
      </if>
      <if test="record.abtype != null">
        abtype = #{record.abtype,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update we_com_user
    set id = #{record.id,jdbcType=BIGINT},
      corp_id = #{record.corpId,jdbcType=BIGINT},
      external_user_id = #{record.externalUserId,jdbcType=VARCHAR},
      union_id = #{record.unionId,jdbcType=VARCHAR},
      external_user_id_3rd_party = #{record.externalUserId3rdParty,jdbcType=VARCHAR},
      `type` = #{record.type,jdbcType=INTEGER},
      `name` = #{record.name,jdbcType=VARCHAR},
      avatar = #{record.avatar,jdbcType=VARCHAR},
      gender = #{record.gender,jdbcType=INTEGER},
      is_delete = #{record.isDelete,jdbcType=INTEGER},
      group_msg_disabled = #{record.groupMsgDisabled,jdbcType=TINYINT},
      created_at = #{record.createdAt,jdbcType=BIGINT},
      updated_at = #{record.updatedAt,jdbcType=BIGINT},
      deleted_at = #{record.deletedAt,jdbcType=BIGINT},
      abtype = #{record.abtype,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUser">
    update we_com_user
    <set>
      <if test="corpId != null">
        corp_id = #{corpId,jdbcType=BIGINT},
      </if>
      <if test="externalUserId != null">
        external_user_id = #{externalUserId,jdbcType=VARCHAR},
      </if>
      <if test="unionId != null">
        union_id = #{unionId,jdbcType=VARCHAR},
      </if>
      <if test="externalUserId3rdParty != null">
        external_user_id_3rd_party = #{externalUserId3rdParty,jdbcType=VARCHAR},
      </if>
      <if test="type != null">
        `type` = #{type,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="avatar != null">
        avatar = #{avatar,jdbcType=VARCHAR},
      </if>
      <if test="gender != null">
        gender = #{gender,jdbcType=INTEGER},
      </if>
      <if test="isDelete != null">
        is_delete = #{isDelete,jdbcType=INTEGER},
      </if>
      <if test="groupMsgDisabled != null">
        group_msg_disabled = #{groupMsgDisabled,jdbcType=TINYINT},
      </if>
      <if test="createdAt != null">
        created_at = #{createdAt,jdbcType=BIGINT},
      </if>
      <if test="updatedAt != null">
        updated_at = #{updatedAt,jdbcType=BIGINT},
      </if>
      <if test="deletedAt != null">
        deleted_at = #{deletedAt,jdbcType=BIGINT},
      </if>
      <if test="abtype != null">
        abtype = #{abtype,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.tzld.piaoquan.growth.common.model.po.WeComUser">
    update we_com_user
    set corp_id = #{corpId,jdbcType=BIGINT},
      external_user_id = #{externalUserId,jdbcType=VARCHAR},
      union_id = #{unionId,jdbcType=VARCHAR},
      external_user_id_3rd_party = #{externalUserId3rdParty,jdbcType=VARCHAR},
      `type` = #{type,jdbcType=INTEGER},
      `name` = #{name,jdbcType=VARCHAR},
      avatar = #{avatar,jdbcType=VARCHAR},
      gender = #{gender,jdbcType=INTEGER},
      is_delete = #{isDelete,jdbcType=INTEGER},
      group_msg_disabled = #{groupMsgDisabled,jdbcType=TINYINT},
      created_at = #{createdAt,jdbcType=BIGINT},
      updated_at = #{updatedAt,jdbcType=BIGINT},
      deleted_at = #{deletedAt,jdbcType=BIGINT},
      abtype = #{abtype,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectIdByExternalUserId" parameterType="String" resultType="Long">
    select id
    from we_com_user
    where external_user_id = #{externalUserId}
  </select>

  <insert id="insertList" parameterType="java.util.List">
    insert into we_com_user
    (
    corp_id,
    external_user_id,
    union_id,
    external_user_id_3rd_party,
    `type`,
    `name`,
    avatar,
    gender,
    created_at,
    updated_at,
    deleted_at,
    abtype,
    create_time,
    update_time
    )
    values
    <foreach collection="list" item="item" separator=",">
      (
      #{item.corpId,jdbcType=BIGINT}
      #{item.externalUserId,jdbcType=VARCHAR},
      #{item.unionId,jdbcType=VARCHAR},
      #{item.externalUserId3rdParty,jdbcType=VARCHAR},
      #{item.type,jdbcType=INTEGER},
      #{item.name,jdbcType=VARCHAR},
      #{item.avatar,jdbcType=VARCHAR},
      #{item.gender,jdbcType=INTEGER},
      #{item.createdAt,jdbcType=BIGINT},
      #{item.updatedAt,jdbcType=BIGINT},
      #{item.deletedAt,jdbcType=BIGINT},
      #{item.abtype,jdbcType=VARCHAR},
      #{item.createTime,jdbcType=TIMESTAMP},
      #{item.updateTime,jdbcType=TIMESTAMP}
      )
    </foreach>
  </insert>

  <select id="selectUserList" resultType="com.tzld.piaoquan.growth.common.model.po.WeComUser">
    select t1.*
    from we_com_user t1
    left join we_com_staff_with_user t2 on t1.id = t2.user_id
    where t1.group_msg_disabled = #{groupMsgDisabled,jdbcType=TINYINT}
    and t2.staff_id = #{staffId,jdbcType=BIGINT}
    and t2.is_delete = 0
    <if test="filterTagIdList != null and filterTagIdList.size() > 0">
      and t1.id not in (
      select t2.id
      from we_com_user_with_tag t1
      join we_com_user t2 on t1.user_id = t2.id
      where t1.tag_id in
      <foreach item="item" index="index" collection="filterTagIdList"
               open="(" separator="," close=")">
        #{item}
      </foreach>
      )
    </if>
    limit #{pageNum}, #{pageSize}
  </select>

  <select id="selectByTagUserList" resultType="com.tzld.piaoquan.growth.common.model.po.WeComUser">
    select t1.*
    from we_com_user t1
           join we_com_user_with_tag t2 on t1.id = t2.user_id
    where t2.tag_id = #{tagId,jdbcType=BIGINT}
  </select>

  <select id="selectByGenderUserList" resultType="com.tzld.piaoquan.growth.common.model.po.WeComUser">
    select t1.*
    from we_com_user t1
    left join we_com_staff_with_user t2 on t1.id = t2.user_id
    where t1.group_msg_disabled = #{groupMsgDisabled,jdbcType=TINYINT}
    and t2.staff_id = #{staffId,jdbcType=BIGINT}
    and t2.is_delete = 0
    and t1.gender = #{gender,jdbcType=INTEGER}
    <if test="filterTagIdList != null and filterTagIdList.size() > 0">
      and t1.id not in (
      select t2.id
      from we_com_user_with_tag t1
      join we_com_user t2 on t1.user_id = t2.id
      where t1.tag_id in
      <foreach item="item" index="index" collection="filterTagIdList"
               open="(" separator="," close=")">
        #{item}
      </foreach>
      )
    </if>
  </select>
</mapper>