<?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.BookClockInfoDaoImpl">

    <!-- 微信群打卡实体 -->
    <resultMap id="BaseResultMap" type="com.pcloud.book.clock.entity.BookClockInfo">
        <id column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="origin_id" property="originId" jdbcType="BIGINT"/>
        <result column="origin_type" property="originType" jdbcType="VARCHAR"/>
        <result column="origin_title" property="originTitle" jdbcType="VARCHAR"/>
        <result column="origin_img" property="originImg" jdbcType="VARCHAR"/>
        <result column="unique_number" property="uniqueNumber" jdbcType="VARCHAR"/>
        <result column="is_notice" property="isNotice" jdbcType="TINYINT"/>
        <result column="notice_time" property="noticeTime" jdbcType="VARCHAR"/>
        <result column="notice_content" property="noticeContent" 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>

    <!-- 微信群打卡实体DTO -->
    <resultMap id="BaseResultMapDTO" type="com.pcloud.book.clock.dto.BookClockInfoDTO">
        <id column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="origin_id" property="originId" jdbcType="BIGINT"/>
        <result column="origin_type" property="originType" jdbcType="VARCHAR"/>
        <result column="origin_title" property="originTitle" jdbcType="VARCHAR"/>
        <result column="origin_img" property="originImg" jdbcType="VARCHAR"/>
        <result column="unique_number" property="uniqueNumber" jdbcType="VARCHAR"/>
        <result column="is_notice" property="isNotice" jdbcType="TINYINT"/>
        <result column="notice_time" property="noticeTime" jdbcType="VARCHAR"/>
        <result column="notice_content" property="noticeContent" 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>
    <!-- 微信群打卡列表实体DTO -->
    <resultMap id="BaseResultListMapDTO" type="com.pcloud.book.clock.dto.BookClockListDTO">
        <id column="book_clock_info_id" property="bookClockInfoId" jdbcType="BIGINT"/>
        <result column="origin_id" property="originId" jdbcType="BIGINT"/>
        <result column="origin_type" property="originType" jdbcType="VARCHAR"/>
        <result column="origin_title" property="originTitle" jdbcType="VARCHAR"/>
        <result column="origin_img" property="originImg" jdbcType="VARCHAR"/>
        <result column="unique_number" property="uniqueNumber" jdbcType="VARCHAR"/>
        <result column="bookCount" property="bookCount" jdbcType="INTEGER"/>
        <result column="classifyCount" property="classifyCount" jdbcType="INTEGER"/>
        <collection property="bookGroupIdList" ofType="java.lang.Long">
            <id column="bgi" jdbcType="BIGINT"/>
        </collection>
        <collection property="classifyIdList" ofType="java.lang.Long">
            <id column="ci" jdbcType="BIGINT"/>
        </collection>
    </resultMap>

    <sql id="Base_Column_List">
        book_clock_info_id, origin_id, origin_type, origin_title, origin_img, unique_number, is_notice, notice_time, notice_content,
        create_user, create_time, update_user, update_time
    </sql>


    <!-- 新增书籍和编辑关联信息-->
    <insert id="insert" parameterType="bookClockInfo" useGeneratedKeys="true" keyProperty="bookClockInfoId" flushCache="true">
        INSERT INTO book_clock_info (
        origin_id, origin_type, origin_title, origin_img, unique_number, is_notice, notice_time, notice_content,
        create_user, create_time, update_user, update_time
        )
        VALUES (
        #{originId, jdbcType=BIGINT}, #{originType, jdbcType=VARCHAR}, #{originTitle, jdbcType=VARCHAR}, #{originImg, jdbcType=VARCHAR},
        #{uniqueNumber, jdbcType=VARCHAR}, #{isNotice, jdbcType=TINYINT}, #{noticeTime, jdbcType=VARCHAR}, #{noticeContent, jdbcType=VARCHAR},
        #{createUser, jdbcType=BIGINT}, NOW(), #{updateUser, jdbcType=BIGINT}, NOW()
        )
    </insert>

    <!-- 获取基本信息-->
    <select id="getBaseInfoById" parameterType="long" resultMap="BaseResultMapDTO">
        SELECT <include refid="Base_Column_List"/>
        FROM book_clock_info
        WHERE book_clock_info_id = #{bookClockInfoId}
        LIMIT 1
    </select>

    <!-- 修改作品/应用-->
    <update id="updateBaseInfo4AppType" parameterType="map">
        UPDATE book_clock_info
        <set>
            <if test="newOriginId != null">
                origin_id = #{newOriginId},
            </if>
            <if test="newOriginType != null">
                origin_type = #{newOriginType},
            </if>
            <if test="originTitle != null">
                origin_title = #{originTitle},
            </if>
            <if test="originImg != null">
                origin_img = #{originImg},
            </if>
            unique_number = #{uniqueNumber}
        </set>
        WHERE book_clock_info_id = #{bookClockInfoId}
    </update>

    <!-- 修改基本信息(修改关键词)-->
    <update id="updateBaseInfo4KeywordType" parameterType="map">
        UPDATE book_clock_info
        SET is_notice = #{isNotice},
        notice_time = #{noticeTime},
        notice_content = #{noticeContent}
        WHERE book_clock_info_id = #{bookClockInfoId}
    </update>

    <!-- 获取基本信息-->
    <select id="getBookGroupClockBaseInfo" parameterType="Long" resultMap="BaseResultMapDTO">
        SELECT <include refid="Base_Column_List"/>
        FROM book_clock_info
        WHERE book_clock_info_id = #{bookClockInfoId}
    </select>


    <!-- 获取该编辑已选的超级作者作品及应用-->
    <select id="getOriginIdAndTypeByAdviserId" parameterType="long" resultMap="BaseResultMapDTO">
        SELECT book_clock_info_id,origin_id,origin_type
        FROM book_clock_info
        WHERE create_user = #{adviserId}
    </select>

    <select id="listBookGroupClock" parameterType="map" resultMap="BaseResultListMapDTO">
        select bci.*, bgc.book_group_id as bgi, bgc.classify_id as ci
        from (
            select book_clock_info_id, origin_id, origin_type, origin_title, origin_img, unique_number from book_clock_info where create_user = #{adviserId}
            <if test="name != null">
                and (origin_title like CONCAT('%', #{name}, '%') or unique_number like CONCAT('%', #{name}, '%'))
            </if>
            <if test="startDate != null and endDate != null">
                and create_time between #{startDate} and #{endDate}
            </if>
            order by update_time desc
            limit #{startNum}, #{numPerPage}
        ) bci
        left join book_group_clock bgc on bci.book_clock_info_id = bgc.book_clock_info_id
    </select>

    <select id="countBookGroupClock" resultType="java.lang.Integer">
        select count(1) totalCount from book_clock_info where create_user = #{adviserId}
        <if test="name != null">
            and (origin_title like CONCAT('%', #{name}, '%') or unique_number like CONCAT('%', #{name}, '%'))
        </if>
        <if test="startDate != null and endDate != null">
            and create_time between #{startDate} and #{endDate}
        </if>
        order by update_time desc
    </select>

    <!-- 获取该作品/应用该编辑创建的基本信息-->
    <select id="getBaseInfoByMap" parameterType="map" resultMap="BaseResultMapDTO">
        SELECT <include refid="Base_Column_List"/>
        FROM book_clock_info
        WHERE origin_id = #{originId}
        AND   origin_type = #{originType}
        AND   create_user = #{adviserId}
    </select>

    <!-- 修改作品基本信息-->
    <update id="updateProductInfo" parameterType="map">
        UPDATE book_clock_info
        <set>
            <if test="title != null">
                origin_title = #{title},
            </if>
            <if test="coverImg != null">
                origin_img = #{coverImg},
            </if>
            unique_number = #{uniqueNumber},
        </set>
        WHERE origin_id = #{originId}
        AND origin_type = "PRODUCT"
    </update>

    <!-- 修改应用基本信息-->
    <update id="updateAppInfo" parameterType="map">
        UPDATE book_clock_info
        <set>
            <if test="title != null">
                origin_title = #{title},
            </if>
            <if test="coverImg != null">
                origin_img = #{coverImg},
            </if>
        </set>
        WHERE origin_id = #{originId}
        AND origin_type = "APP"
    </update>

    <!-- 删除关联的基本信息-->
    <delete id="deleteBookClockInfo" parameterType="map">
        DELETE FROM book_clock_info
        WHERE book_clock_info_id = #{bookClockInfoId}
    </delete>

    <select id="getAllClockInfoIds" parameterType="map" resultMap="BaseResultMapDTO">
        select <include refid="Base_Column_List"/>
        from book_clock_info where origin_type = 'APP'
    </select>

    <select id="getAllAttInfoIds" parameterType="map" resultMap="BaseResultMapDTO">
        select <include refid="Base_Column_List"/>
        from book_clock_info where origin_type = 'PRODUCT'
    </select>

</mapper>