<?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.reading.dao.impl.ReadingGroupDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.reading.entity.ReadingGroup" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR" />
    <result column="wx_group_name" property="wxGroupName" jdbcType="VARCHAR" />
    <result column="activity_id" property="activityId" jdbcType="BIGINT" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, wx_group_id, wx_group_name, activity_id, create_user, create_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.reading.entity.ReadingGroup" useGeneratedKeys="true" keyProperty="id">
    insert into reading_group (wx_group_id, wx_group_name,
      activity_id, create_user, create_time
      )
    values (#{wxGroupId,jdbcType=VARCHAR}, #{wxGroupName,jdbcType=VARCHAR},
      #{activityId,jdbcType=BIGINT}, #{createUser,jdbcType=BIGINT}, NOW()
      )
  </insert>

  <select id="getByActivityId" parameterType="long" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM reading_group
    WHERE activity_id = #{activityId}
    AND wx_group_id IS NOT NULL
  </select>

  <select id="getByWxGroupId" parameterType="string" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM reading_group
    WHERE wx_group_id = #{wxGroupId}
  </select>

  <update id="updateWxGroupIdById" parameterType="map">
    UPDATE reading_group
    SET wx_group_id = #{wxGroupId},
    update_time = NOW()
    WHERE id = #{id}
  </update>

  <select id="getById" parameterType="long" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/>
    FROM reading_group
    WHERE id = #{id}
  </select>

  <select id="getLatestNullGroup" resultMap="BaseResultMap" parameterType="string">
    SELECT
    <include refid="Base_Column_List"/>
    FROM
        reading_group
    WHERE
        wx_group_id IS NULL
    AND id IN (
      SELECT DISTINCT
      group_id
      FROM
      reading_group_robot
      WHERE
      alt_id = #{altId}
      )
    ORDER BY
        id DESC
    LIMIT 1
  </select>

  <select id="getAllByActivityId" parameterType="long" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM reading_group
    WHERE activity_id = #{activityId}
  </select>

  <update id="updateGroupName" parameterType="map">
    UPDATE reading_group
    SET wx_group_name = #{wxGroupName}
    WHERE
        id = #{id}
  </update>

  <select id="getDetailByWxGroupId" parameterType="string" resultMap="com.pcloud.book.reading.dao.impl.ReadingActivityDaoImpl.BaseDTOResultMap">
    select
    <include refid="com.pcloud.book.reading.dao.impl.ReadingActivityDaoImpl.Base_Column_List_ra"/>
    from
    reading_group rg
    left join reading_activity ra
    on rg.activity_id = ra.id
    where rg.wx_group_id = #{wxGroupId}
  </select>

</mapper>