<?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.clock.dao.impl.BookGroupClockDaoImpl">

    <!-- 微信群打卡实体 -->
    <resultMap id="BaseResultMap" type="com.pcloud.book.clock.entity.BookGroupClock">
        <id column="book_group_clock_id" property="bookGroupClockId" jdbcType="BIGINT"/>
        <result column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <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>

    <!-- 微信群打卡实体DTO -->
    <resultMap id="BaseResultMapDTO" type="com.pcloud.book.clock.dto.BookGroupClockDTO">
        <id column="book_group_clock_id" property="bookGroupClockId" jdbcType="BIGINT"/>
        <result column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <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">
        book_group_clock_id, book_clock_info_id, channel_id, book_group_id, classify_id, create_user, create_time, update_user, update_time
    </sql>


    <!-- 新增书籍和编辑关联信息-->
    <insert id="insert" parameterType="bookGroupClock" useGeneratedKeys="true" keyProperty="bookGroupClockId" flushCache="true">
        INSERT INTO book_group_clock (
            book_clock_info_id, book_clock_info_id, classify_id, create_user, create_time, update_user, update_time
        )
        VALUES (
        #{bookClockInfoId, jdbcType=BIGINT}, #{channelId, jdbcType=BIGINT}, #{bookGroupId, jdbcType=BIGINT},
        #{classifyId, jdbcType=BIGINT}, #{createUser, jdbcType=BIGINT}, NOW(), #{updateUser, jdbcType=BIGINT}, NOW()
        )
    </insert>

    <insert id="batchInsert" parameterType="bookGroupClock" useGeneratedKeys="true" keyProperty="bookGroupClockId" flushCache="true">
        insert into book_group_clock (
        book_clock_info_id,
        channel_id,
        book_group_id,
        classify_id,
        create_user,
        create_time,
        update_user,
        update_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.bookClockInfoId,jdbcType=BIGINT},
            #{item.channelId,jdbcType=BIGINT},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>


    <!-- 删除关联的社群书-->
    <delete id="deleteBookGroupId4AppType" parameterType="map">
        DELETE FROM book_group_clock
        WHERE book_clock_info_id = #{bookClockInfoId}
        AND classify_id = 0
    </delete>

    <!-- 删除关联的分类-->
    <delete id="deleteClassifyId4AppType" parameterType="map">
        DELETE FROM book_group_clock
        WHERE book_clock_info_id = #{bookClockInfoId}
        AND book_group_id = 0
    </delete>

    <!-- 获取关联的社群码-->
    <select id="getBookGroupClockIds" parameterType="map" resultMap="BaseResultMapDTO">
        SELECT <include refid="Base_Column_List"/>
        FROM book_group_clock
        WHERE book_clock_info_id = #{bookClockInfoId}
    </select>

    <!-- 删除该作品/应用已关联的书下面的分类-->
    <delete id="deleteClassifyIdByBookGroupIds" parameterType="map">
        DELETE FROM book_group_clock
        WHERE book_clock_info_id = #{bookClockInfoId}
        AND classify_id IN
        <foreach collection="classifyIds" open="(" close=")" separator="," item="item">
            ${item}
        </foreach>
    </delete>

    <!--校验群是否为微信群打卡的群-->
    <select id="getInfoIdByWeixinGroupId" parameterType="map" resultType="long">
        SELECT
        book_clock_info_id
        FROM
        book_group_clock
        WHERE
        create_user = #{adviserId}
        AND (
            book_group_id = #{bookGroupId} OR classify_id = #{classifyId}
        )
        ORDER BY book_clock_info_id DESC
    </select>

    <!--获取微信群打卡基本信息-->
    <select id="getInfoByInfoIds" parameterType="list" resultMap="BaseResultMapDTO">
        SELECT <include refid="Base_Column_List"/>
        FROM
        book_group_clock
        WHERE book_clock_info_id IN
        <foreach collection="list" open="(" item="item" close=")" separator=",">
            ${item}
        </foreach>
    </select>

    <!--取消关联-->
    <delete id="cancelSelected" parameterType="map">
        DELETE FROM book_group_clock
        WHERE book_clock_info_id = #{bookClockInfoId}
        AND book_group_id = #{bookGroupId}
        AND classify_id = #{classifyId}
    </delete>

    <!--删除对应关联的书及分类-->
    <delete id="deleteBookGroupClock" parameterType="map">
        DELETE FROM book_group_clock
        WHERE book_clock_info_id = #{bookClockInfoId}
    </delete>

    <select id="getClassifyIdsByBookGroupId" parameterType="map" resultType="Long">
        SELECT DISTINCT
	      t2.id
        FROM
	      book_group_clock t1
        LEFT JOIN book_group_classify t2 ON t1.book_group_id = t2.book_group_id
        LEFT JOIN weixin_clock_group_classify t3 ON t2.id = t3.book_group_classify_id
        WHERE
	      t2.is_delete = 0
        AND t3.weixin_clock_group_classify_id IS NULL
        AND t1.book_clock_info_id = #{bookClockInfoId}
    </select>
    
    <select id="getClassifyIdsByClassfyId" parameterType="map" resultType="Long">
        SELECT DISTINCT
	      t2.id
        FROM
	      book_group_clock t1
        LEFT JOIN book_group_classify t2 ON t1.classify_id = t2.id
        LEFT JOIN weixin_clock_group_classify t3 ON t2.id = t3.book_group_classify_id
        WHERE
	      t2.is_delete = 0
        AND t3.weixin_clock_group_classify_id IS NULL
        AND t1.book_clock_info_id = #{bookClockInfoId}
    </select>


</mapper>