<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.group.dao.impl.GroupAnnouncementDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.GroupAnnouncement">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="book_group_classify_id" property="bookGroupClassifyId" jdbcType="BIGINT"/>
        <result column="announcement_content" property="announcementContent" jdbcType="VARCHAR"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="BIGINT"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,book_group_id,book_group_classify_id,announcement_content,create_user,create_time,update_user,update_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.GroupAnnouncement" useGeneratedKeys="true"
            keyProperty="id">
        INSERT INTO group_announcement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            book_group_id,
            book_group_classify_id,
            announcement_content,
            create_user,
            create_time,
            update_user,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{bookGroupId,jdbcType=BIGINT},
            #{bookGroupClassifyId,jdbcType=BIGINT},
            #{announcementContent,jdbcType=VARCHAR},
            #{createUser,jdbcType=BIGINT},
            now(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <!--根据id查询-->
    <select id="getById" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from group_announcement where id=#{id}
    </select>

    <!--更新群公告内容-->
    <update id="updateContentById" parameterType="map">
        update group_announcement set
        announcement_content=#{content},
        update_time=now()
        where id=#{id}
    </update>

    <!--根据社群码id获取已经设置过公告的微信群id集合-->
    <select id="getHasAnnGroupIdsByBookGroupId" parameterType="Long" resultType="String">
        SELECT
        t2.weixin_group_id
        FROM
        group_announcement t
        INNER JOIN book_group_classify t1 ON t.book_group_classify_id = t1.id
        INNER JOIN book_group_qrcode t2 ON t1.id = t2.classify_id
        WHERE
        t1.book_group_id = #{bookGroupId}
    </select>

    <!--根据社群码id获取-->
    <select id="getByGroupBookId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from group_announcement
        where book_group_id=#{bookGroupId}
    </select>

    <!--根据群分类id获取-->
    <select id="getByClassifyId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from group_announcement
        where book_group_classify_id=#{classifyId}
    </select>

</mapper>