<?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.pcloud.book.group.mapper.WeworkTeacherMapper">
  <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.WeworkTeacher">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="qrcode" jdbcType="VARCHAR" property="qrcode" />
    <result column="real_name" jdbcType="VARCHAR" property="realName" />
    <result column="nickname" jdbcType="VARCHAR" property="nickname" />
    <result column="avatar" jdbcType="VARCHAR" property="avatar" />
    <result column="desc" jdbcType="VARCHAR" property="desc" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="teacher_type" jdbcType="INTEGER" property="teacherType" />
    <result column="is_open" jdbcType="INTEGER" property="isOpen" />
    <result column="material_url" jdbcType="VARCHAR" property="materialUrl" />
    <result column="media_id" jdbcType="VARCHAR" property="mediaId" />
    <result column="account_id" jdbcType="BIGINT" property="accountId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>


  <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.group.dto.WxWorkTeacherDTO" extends="BaseResultMap">

  </resultMap>
  <sql id="Base_Column_List">
    id, qrcode, real_name, nickname, avatar, `desc`, title, teacher_type, is_open, material_url, media_id, account_id, create_time,
    update_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from wework_teacher
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from wework_teacher
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.group.entity.WeworkTeacher" useGeneratedKeys="true">
    insert into wework_teacher (qrcode, real_name, nickname, avatar, `desc`, title,
      teacher_type, is_open, material_url, media_id,
      account_id, create_time, update_time
      )
    values (#{qrcode,jdbcType=VARCHAR}, #{realName,jdbcType=VARCHAR}, #{nickname,jdbcType=VARCHAR}, #{avatar,jdbcType=VARCHAR}, #{desc,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR},
      #{teacherType,jdbcType=INTEGER}, #{isOpen,jdbcType=INTEGER}, #{materialUrl,jdbcType=VARCHAR}, #{mediaId,jdbcType=VARCHAR},
      #{accountId,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.pcloud.book.group.entity.WeworkTeacher" useGeneratedKeys="true">
    insert into wework_teacher
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="qrcode != null">
        qrcode,
      </if>
      <if test="realName != null">
        real_name,
      </if>
      <if test="nickname != null">
        nickname,
      </if>
      <if test="avatar != null">
        avatar,
      </if>
      <if test="desc != null">
        `desc`,
      </if>
      <if test="title != null">
        title,
      </if>
      <if test="teacherType != null">
        teacher_type,
      </if>
      <if test="isOpen != null">
        is_open,
      </if>
      <if test="materialUrl != null">
        material_url,
      </if>
      <if test="mediaId != null">
        media_id,
      </if>
      <if test="accountId != null">
        account_id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="qrcode != null">
        #{qrcode,jdbcType=VARCHAR},
      </if>
      <if test="realName != null">
        #{realName,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="avatar != null">
        #{avatar,jdbcType=VARCHAR},
      </if>
      <if test="desc != null">
        #{desc,jdbcType=VARCHAR},
      </if>
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="teacherType != null">
        #{teacherType,jdbcType=INTEGER},
      </if>
      <if test="isOpen != null">
        #{isOpen,jdbcType=INTEGER},
      </if>
      <if test="materialUrl != null">
        #{materialUrl,jdbcType=VARCHAR},
      </if>
      <if test="mediaId != null">
        #{mediaId,jdbcType=VARCHAR},
      </if>
      <if test="accountId != null">
        #{accountId,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.pcloud.book.group.entity.WeworkTeacher">
    update wework_teacher
    <set>
      <if test="qrcode != null">
        qrcode = #{qrcode,jdbcType=VARCHAR},
      </if>
      <if test="realName != null">
        real_name = #{realName,jdbcType=VARCHAR},
      </if>
      <if test="nickname != null">
        nickname = #{nickname,jdbcType=VARCHAR},
      </if>
      <if test="avatar != null">
        avatar = #{avatar,jdbcType=VARCHAR},
      </if>
      <if test="desc != null">
        `desc` = #{desc,jdbcType=VARCHAR},
      </if>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="teacherType != null">
        teacher_type = #{teacherType,jdbcType=INTEGER},
      </if>
      <if test="isOpen != null">
        is_open = #{isOpen,jdbcType=INTEGER},
      </if>
      <if test="materialUrl != null">
        material_url = #{materialUrl,jdbcType=VARCHAR},
      </if>
      <if test="mediaId != null">
        media_id = #{mediaId,jdbcType=VARCHAR},
      </if>
      <if test="accountId != null">
        account_id = #{accountId,jdbcType=BIGINT},
      </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.pcloud.book.group.entity.WeworkTeacher">
    update wework_teacher
    set qrcode = #{qrcode,jdbcType=VARCHAR},
      real_name = #{realName,jdbcType=VARCHAR},
      nickname = #{nickname,jdbcType=VARCHAR},
      avatar = #{avatar,jdbcType=VARCHAR},
      `desc` = #{desc,jdbcType=VARCHAR},
      title = #{title,jdbcType=VARCHAR},
      teacher_type = #{teacherType,jdbcType=INTEGER},
      is_open = #{isOpen,jdbcType=INTEGER},
      material_url = #{materialUrl,jdbcType=VARCHAR},
      media_id = #{mediaId,jdbcType=VARCHAR},
      account_id = #{accountId,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="count4WxworkTeacherList" resultType="java.lang.Integer">
    select count(*)
    from wework_teacher
    <where>
      <if test="name != null">
        ( real_name like concat('%', #{name}, '%') OR nickname like concat('%', #{name}, '%') OR `desc` like concat('%', #{name}, '%') )
      </if>
      <if test="isOpen != null">
        and is_open = #{isOpen}
      </if>
      <if test="teacherType != null">
        and teacher_type = #{teacherType}
      </if>
    </where>
  </select>

  <select id="getWxworkTeacherList" resultType="com.pcloud.book.group.vo.WeworkTeacherVO">
    select
      id AS id,
      qrcode AS qrcode,
      real_name AS realName,
      nickname,
      avatar,
      `desc`,
      title AS title,
      teacher_type AS teacherType,
      is_open AS isOpen,
      material_url AS materialUrl,
      create_time AS createTime,
      update_time AS updateTime
    from wework_teacher
    <where>
      <if test="name != null">
        ( real_name like concat('%', #{name}, '%') OR nickname like concat('%', #{name}, '%') OR `desc` like concat('%', #{name}, '%') )
      </if>
      <if test="isOpen != null">
        and is_open = #{isOpen}
      </if>
      <if test="teacherType != null">
        and teacher_type = #{teacherType}
      </if>
    </where>
    limit #{offset},#{limit}
  </select>
  <select id="queryAll" resultType="com.pcloud.book.group.vo.WeworkTeacherVO">
    select
      id AS id,
      teacher_type AS teacherType,
      nickname,
      avatar,
      `desc`,
      material_url AS materialUrl
    from wework_teacher
    where is_open = 1
  </select>

    <select id="listByIds" resultMap="BaseResultMap">
      select <include refid="Base_Column_List"/>
      from wework_teacher
      where id in
      <foreach collection="ids" item="item" close=")" open="(" separator=",">
        ${item}
      </foreach>
    </select>

  <select id="getMaterialByIds" resultType="com.pcloud.book.group.vo.WeworkTeacherVO">
    select
      id AS id,
      teacher_type AS teacherType,
      nickname,
      avatar,
      `desc`,
      material_url AS materialUrl
    from wework_teacher
    where is_open = 1 and id in
    <foreach collection="longs" open="(" separator="," close=")" item="item">
      ${item}
    </foreach>
  </select>

  <select id="getOneTeacherQrcode" resultType="string">
    SELECT
        qrcode
    FROM
        wework_teacher
    WHERE
        is_open = 1
    ORDER BY
        RAND()
    LIMIT 1
  </select>

    <select id="getWxWorkTeacherByIds" parameterType="long" resultMap="BaseResultMap4DTO">
      select <include refid="Base_Column_List"/>
      from wework_teacher
      where id in
      <foreach collection="ids" item="item" close=")" open="(" separator=",">
        #{item}
      </foreach>
    </select>


  <select id="getWxworkTeacherById" parameterType="long" resultType="com.pcloud.book.group.vo.WeworkTeacherVO">
    select
    id AS id,
    qrcode AS qrcode,
    real_name AS realName,
    nickname,
    avatar,
    `desc`,
    title AS title,
    teacher_type AS teacherType,
    is_open AS isOpen,
    material_url AS materialUrl,
    create_time AS createTime,
    update_time AS updateTime
    from wework_teacher
    WHERE id = #{id}
  </select>

  <select id="getTeacherByGradeLabel" parameterType="map" resultType="com.pcloud.book.group.vo.WeworkTeacherVO">
        SELECT
        c.id,
        c.qrcode,
        c.real_name AS realName,
        c.nickname,
        c.avatar,
        c.`desc`,
        c.title AS title,
        c.material_url AS materialUrl
    FROM
        applet_resource_apply a
    LEFT JOIN wework_teacher c ON a.target_id = c.id
    WHERE
        a.crowd_type = #{crowdType}
    AND a.target_type = "TEACHER"
    AND a.target_id > 0
    AND c.is_open = 1
    GROUP BY
        a.target_id
    ORDER BY
        c.create_time DESC
    limit #{offset}, #{limit}
  </select>

  <select id="countTeacherByGradeLabel" parameterType="integer" resultType="integer">
    SELECT
    COUNT(DISTINCT a.target_id)
    FROM
    applet_resource_apply a
    LEFT JOIN wework_teacher c ON a.target_id = c.id
    WHERE
    a.crowd_type = #{crowdType}
    AND a.target_type = "TEACHER"
    AND a.target_id > 0
    AND c.is_open = 1
  </select>

</mapper>
