<?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.ReadingGroupRobotDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.reading.entity.ReadingGroupRobot" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="wx_group_id" property="wxGroupId" jdbcType="VARCHAR" />
    <result column="alt_id" property="altId" jdbcType="VARCHAR" />
    <result column="group_id" property="groupId" jdbcType="BIGINT" />
    <result column="is_master" property="isMaster" jdbcType="BIT" />
    <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, alt_id, group_id, is_master, create_time
  </sql>

  <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
    insert into reading_group_robot (wx_group_id, alt_id,
    group_id, is_master, create_time
    )
    values
    <foreach collection="list" item="item" index="index" separator="," >
      ( #{item.wxGroupId,jdbcType=VARCHAR}, #{item.altId,jdbcType=VARCHAR},
      #{item.groupId}, #{item.isMaster,jdbcType=BIT}, NOW()
      )
    </foreach>
  </insert>

  <update id="updateWxGroupByGroupId" parameterType="map">
    UPDATE reading_group_robot
    SET wx_group_id = #{wxGroupId},
    update_time = NOW()
    WHERE group_id = #{groupId}
  </update>

  <update id="setMaster" parameterType="map">
    UPDATE reading_group_robot
    SET is_master = 1, update_time = NOW()
    WHERE group_id = #{groupId}
    AND alt_id = #{altId}
  </update>

  <select id="getMasterRobotByWxGroupId" parameterType="string" resultType="string">
    SELECT
	    alt_id
    FROM
        reading_group_robot
    WHERE
        wx_group_id = #{wxGroupId}
    AND is_master = 1
    LIMIT 1
  </select>

  <select id="getGroupIdsByCount" parameterType="integer" resultType="long">
    SELECT
        group_id
    FROM
        reading_group_robot
    WHERE
        wx_group_id IS NULL
    GROUP BY
        group_id
    HAVING
        COUNT(1) = #{count}
    ORDER BY
        create_time DESC
  </select>

  <select id="groupExist" parameterType="map" resultType="boolean">
    SELECT EXISTS(
        SELECT
            id
        FROM
            reading_group_robot
        WHERE
            group_id = #{groupId}
        AND wx_group_id IS NULL
        AND alt_id IN
        <foreach collection="altIds" open="(" close=")" item="item" separator=",">
          #{item}
        </foreach>
          )
  </select>

    <select id="getByActivityId" parameterType="long" resultMap="BaseResultMap">
        SELECT
           	r.id, r.wx_group_id, r.alt_id, r.group_id, r.is_master, r.create_time
        FROM
            reading_group_robot r
        LEFT JOIN reading_group g ON r.group_id = g.id
        WHERE
            g.activity_id = #{activityId}
        AND r.wx_group_id IS NOT NULL
    </select>

    <select id="getByGroupId" parameterType="long" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
            reading_group_robot
        WHERE
            group_id = #{groupId}
    </select>
</mapper>