<?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.applet.dao.impl.AppletRecordDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="record_type" property="recordType" jdbcType="TINYINT"/>
        <result column="rights_setting_id" property="rightsSettingId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="from_id" property="fromId" jdbcType="BIGINT"/>
        <result column="from_name" property="fromName" jdbcType="VARCHAR"/>
        <result column="type_code" property="typeCode" jdbcType="VARCHAR"/>
        <result column="account_setting_id" property="accountSettingId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="create_date" property="createDate" jdbcType="DATE"/>
        <result column="is_delete" property="isDelete" jdbcType="TINYINT"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.applet.dto.AppletRecordDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT"/>
        <result column="record_type" property="recordType" jdbcType="TINYINT"/>
        <result column="rights_setting_id" property="rightsSettingId" jdbcType="BIGINT"/>
        <result column="book_id" property="bookId" jdbcType="BIGINT"/>
        <result column="channel_id" property="channelId" jdbcType="BIGINT"/>
        <result column="adviser_id" property="adviserId" jdbcType="BIGINT"/>
        <result column="from_id" property="fromId" jdbcType="BIGINT"/>
        <result column="from_name" property="fromName" jdbcType="VARCHAR"/>
        <result column="type_code" property="typeCode" jdbcType="VARCHAR"/>
        <result column="account_setting_id" property="accountSettingId" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="create_date" property="createDate" jdbcType="DATE"/>
        <result column="is_delete" property="isDelete" jdbcType="TINYINT"/>
        <result column="link_url" property="linkUrl" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, wechat_user_id, record_type, rights_setting_id, book_id, channel_id, adviser_id, from_id, from_name, type_code, account_setting_id,
        create_time, create_date, is_delete, link_url
    </sql>

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

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM applet_record
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO applet_record(
            wechat_user_id, 
            record_type, 
            rights_setting_id, 
            book_id, 
            channel_id, 
            adviser_id, 
            from_id, 
            from_name,
            type_code,
            account_setting_id, 
            create_time, 
            create_date, 
            is_delete,
            link_url,
            type_name
        ) VALUES (
            #{wechatUserId, jdbcType=BIGINT}, 
            #{recordType, jdbcType=BOOLEAN}, 
            #{rightsSettingId, jdbcType=BIGINT}, 
            #{bookId, jdbcType=BIGINT}, 
            #{channelId, jdbcType=BIGINT}, 
            #{adviserId, jdbcType=BIGINT}, 
            #{fromId, jdbcType=BIGINT}, 
            #{fromName, jdbcType=VARCHAR},
            #{typeCode, jdbcType=VARCHAR},
            #{accountSettingId, jdbcType=BIGINT}, 
            now(),
            DATE_FORMAT(now(),"%Y-%m-%d"),
            0,
            #{linkUrl},
            #{typeName}
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO applet_record (
        wechat_user_id,
        record_type,
        rights_setting_id,
        book_id,
        channel_id,
        adviser_id,
        from_id,
        from_name,
        type_code,
        account_setting_id,
        create_time,
        create_date,
        is_delete,
        link_url
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.wechatUserId, jdbcType=BIGINT},
            #{item.recordType, jdbcType=BOOLEAN},
            #{item.rightsSettingId, jdbcType=BIGINT},
            #{item.bookId, jdbcType=BIGINT},
            #{item.channelId, jdbcType=BIGINT},
            #{item.adviserId, jdbcType=BIGINT},
            #{item.fromId, jdbcType=BIGINT},
            #{item.fromName, jdbcType=VARCHAR},
            #{item.typeCode, jdbcType=VARCHAR},
            #{item.accountSettingId, jdbcType=BIGINT},
            now(),
            DATE_FORMAT(now(),"%Y-%m-%d"),
            0,
            #{item.linkUrl}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE applet_record
        <set>
            <if test="wechatUserId != null">
                wechat_user_id = #{wechatUserId},
            </if>
            <if test="recordType != null">
                record_type = #{recordType},
            </if>
            <if test="rightsSettingId != null">
                rights_setting_id = #{rightsSettingId},
            </if>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="channelId != null">
                channel_id = #{channelId},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
            <if test="fromId != null">
                from_id = #{fromId},
            </if>
            <if test="fromName != null and fromName != ''">
                from_name = #{fromName},
            </if>
            <if test="accountSettingId != null">
                account_setting_id = #{accountSettingId},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete},
            </if>
            <if test="linkUrl != null">
                link_url = #{linkUrl},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM applet_record where id = #{id}
    </delete>

    <select id="listAppletRecord" parameterType="map" resultMap="BaseResultMap4DTO">
        select
        <include refid="Base_Column_List"/>
        from
        (
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        `applet_record`
        WHERE
        is_delete = 0 and wechat_user_id = #{wechatUserId}
        <if test="queryName != null">
            and from_name like concat("%",#{queryName},"%")
        </if>
        <choose>
            <when test="date != null">
                and create_date = #{date}
            </when>
            <otherwise>
                and create_date &gt; DATE_SUB(curdate(),INTERVAL 30 day)
            </otherwise>
        </choose>
        <if test="recordTypes != null">
            and record_type in
            <foreach collection="recordTypes" index="index" item="item" open="(" separator="," close=")">
                ${item}
            </foreach>
        </if>
        ORDER BY
        create_time DESC
        LIMIT 9999
        ) a
        <choose>
            <when test="date != null">
                group by from_id
            </when>
            <otherwise>
                group by create_date, from_id
            </otherwise>
        </choose>
        order by create_time desc
    </select>

    <select id="getBookMap" parameterType="list"  resultType="com.pcloud.book.applet.dto.AppletUserBookcaseDTO">
           SELECT
        c.id id,
        c.book_id bookId,
        c.channel_id channelId,
        c.adviser_id adviserId,
        b.BOOK_NAME bookName,
        b.COVER_IMG coverImg,
		b.AUTHOR author,
		b.PUBLISH publish,
        a.third_TEMPLET_ID thirdTempletId,
		a.SECOND_TEMPLET_ID secondTempletId,
		a.TEMPLET_ID templetId,
		d.join_group_type joinGroupType,
		d.id bookGroupId,
		b.ISBN isbn,
		a.GRA_LABEL_ID gradeLabelId,
        a.SUB_LABEL_ID subjectLabelId,
        a.vol_label_id volLabelId,
        IF(d.join_group_type=1 OR d.related_book_group_id>0,1,0) hasGroup,
        r.id rightsSettingId,
        r.count rightsSettingCount
    FROM applet_record c
    LEFT JOIN BOOK_ADVISER a ON c.book_id=a.BOOK_ID AND c.adviser_id=a.ADVISER_ID
    AND c.channel_id=a.CHANNEL_ID
	INNER JOIN BOOK b ON c.BOOK_ID = b.BOOK_ID AND b.IS_DELETE = 0
    LEFT JOIN book_group d ON d.BOOK_ID = a.BOOK_ID
    AND d.create_user = c.adviser_id
    AND d.channel_id = c.channel_id
    AND d.IS_DELETE = 0
	LEFT JOIN rights_setting r ON c.rights_setting_id = r.id AND d.join_group_type=4 and r.show_state = 1  and r.rights_setting_type = 2
    WHERE
    a.IS_DELETE = 0
    and 
    c.is_delete = 0
    and
    c.id in
     <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
        #{item.id}
     </foreach>
    </select>

    <update id="updateDeleteState" parameterType="map">
        update
        applet_record
        set
        is_delete = 1
        where
        from_id = #{fromId}
        and
        record_type = #{recordType}
    </update>

    <select id="listReachableDate" parameterType="map" resultType="Date">
        SELECT
            create_date
        FROM
            applet_record
        WHERE
            wechat_user_id = #{wechatUserId}
            AND create_date >= DATE_SUB( curdate(), INTERVAL 30 DAY )
        GROUP BY
            create_date
    </select>

    <select id="getReadBookWeekStatis" parameterType="map" resultType="com.pcloud.book.applet.dto.ReadBookDayStatisDTO">
        SELECT
            COUNT(book_id) readBookCount,
            create_date createDate
        FROM
            applet_record
        WHERE
            record_type = 7
            AND wechat_user_id = #{wechatUserId}
            AND create_date BETWEEN #{mondayDate, jdbcType=DATE} AND #{sundayDate, jdbcType=DATE}
        GROUP BY
            create_date
    </select>

    <select id="getBooksByWechatUserId" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletRecordBookDTO">
        SELECT
            book_id bookId,
            create_date createDate
        FROM
            applet_record
        WHERE
            record_type = 7
            AND wechat_user_id = #{wechatUserId}
    </select>

    <select id="getStatisUserCount" parameterType="map" resultType="long">
        SELECT COUNT(DISTINCT wechat_user_id)
        FROM
            applet_record
        WHERE
            create_date BETWEEN #{prevMonday, jdbcType=DATE} AND #{prevSunday, jdbcType=DATE}
    </select>

    <select id="getAppOrProductBrowseCount" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletAppOrProductDTO">
        SELECT
	from_id serveId,
	count(1) browseCount
    FROM
	`applet_record`
    WHERE
	record_type = #{recordType}
    and
    from_id in
    <foreach collection="serveIds" index="index" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
    GROUP BY
	record_type,
	from_id
    </select>

    <select id="fixAppletRecordDataSync4CH" parameterType="map" resultMap="BaseResultMap">
        SELECT * FROM applet_record WHERE create_date = #{createDate}
    </select>

    <select id="getHandleAppletRecordTypeCode" resultMap="BaseResultMap4DTO">
        SELECT id, record_type, from_id
        FROM applet_record
        WHERE id > #{lastAppletRecordId}
        <if test="recordTypes != null and recordTypes.size() > 0">
            AND record_type IN
            <foreach collection="recordTypes" item="recordType" open="(" close=")" separator=",">
                #{recordType}
            </foreach>
        </if>
        ORDER BY id
        LIMIT #{offset},#{limit}
    </select>

    <update id="batchUpdateTypeCode" parameterType="list">
        update
        applet_record
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="type_code =case" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.typeCode!=null">
                        when id=#{item.id} then #{item.typeCode}
                    </if>
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="item">
                id = #{item.id}
            </foreach>
        </where>
    </update>

</mapper>
