<?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="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="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, 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, 
            account_setting_id, 
            create_time, 
            create_date, 
            is_delete,
            link_url
        ) 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}, 
            #{accountSettingId, jdbcType=BIGINT}, 
            now(),
            DATE_FORMAT(now(),"%Y-%m-%d"),
            0,
            #{linkUrl}
        )
    </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,
        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.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.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>

</mapper>