<?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.adnews.dao.impl.AdNewsGroupRecordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNewsGroupRecord">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="message_statistic_id" property="messageStatisticId" jdbcType="BIGINT"/>
        <result column="qrcode_id" property="qrcodeId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="ad_news_id" property="adNewsId" jdbcType="BIGINT"/>
        <result column="ad_news_set_id" property="adNewsSetId" 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"/>
        <result column="custom_content" property="customContent" jdbcType="VARCHAR"/>
        <result column="content_type" property="contentType" jdbcType="INTEGER"/>
        <result column="content_from" property="contentFrom" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, message_statistic_id,qrcode_id, classify_id, book_group_id, ad_news_id, ad_news_set_id, create_user, create_time, update_user, update_time, custom_content,content_type
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from ad_news_group_record
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="getAdNewsGroupRecord" resultType="com.pcloud.book.adnews.vo.AdNewsGroupRecordVO" parameterType="java.lang.Long">
        SELECT
            a.id,
            a.ad_news_id adNewsId,
            d.wechat_name wechatName,
            c.title,
            c.short_url shortUrl,
            a.custom_content customContent,
            a.content_type contentType
        FROM
            ad_news_group_record a
            INNER JOIN ad_news_message_statistic b ON a.message_statistic_id=b.id
            LEFT JOIN ad_news c ON a.ad_news_id=c.id
            LEFT JOIN ad_news_wechat d ON d.news_from=c.news_from AND ((a.content_from=2 AND d.adviser_id=#{partyId,jdbcType=BIGINT}) OR a.content_from <![CDATA[<>]]> 2)
        WHERE a.message_statistic_id=#{messageStatisticId,jdbcType=BIGINT}
            AND a.create_user=#{partyId,jdbcType=BIGINT}
    </select>

    <insert id="insert" parameterType="com.pcloud.book.adnews.entity.AdNewsGroupRecord" useGeneratedKeys="true" keyProperty="id">
        insert into ad_news_group_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            message_statistic_id,
            qrcode_id,
            classify_id,
            book_group_id,
            ad_news_id,
            ad_news_set_id,
            create_user,
            create_time,
            update_user,
            update_time,
            custom_content,
            content_type,
            content_from
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{messageStatisticId,jdbcType=BIGINT},
            #{qrcodeId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{adNewsId,jdbcType=BIGINT},
            #{adNewsSetId,jdbcType=BIGINT},
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
            #{customContent,jdbcType=VARCHAR},
            #{contentType,jdbcType=INTEGER},
            #{contentFrom,jdbcType=INTEGER}
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.adnews.entity.AdNewsGroupRecord">
        update ad_news_group_record
        <set>
            <if test="messageStatisticId != null">
                message_statistic_id = #{messageStatisticId,jdbcType=BIGINT},
            </if>
            <if test="qrcodeId != null">
                qrcode_id = #{qrcodeId,jdbcType=BIGINT},
            </if>
            <if test="classifyId != null">
                classify_id = #{classifyId,jdbcType=BIGINT},
            </if>
            <if test="bookGroupId != null">
                book_group_id = #{bookGroupId,jdbcType=BIGINT},
            </if>
            <if test="adNewsId != null">
                ad_news_id = #{adNewsId,jdbcType=BIGINT},
            </if>
            <if test="adNewsSetId != null">
                ad_news_set_id = #{adNewsSetId,jdbcType=BIGINT},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=VARCHAR},
            </if>
            <if test="customContent != null">
                custom_content = #{customContent,jdbcType=VARCHAR},
            </if>
            <if test="contentType != null">
                content_type = #{contentType,jdbcType=INTEGER},
            </if>
            update_time=now()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        insert into ad_news_group_record (
            message_statistic_id,
            qrcode_id,
            classify_id,
            book_group_id,
            ad_news_id,
            ad_news_set_id,
            create_user,
            create_time,
            update_user,
            update_time,
            custom_content,
            content_type,
            content_from
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.messageStatisticId,jdbcType=BIGINT},
            #{item.qrcodeId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.adNewsId,jdbcType=BIGINT},
            #{item.adNewsSetId,jdbcType=BIGINT},
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW(),
            #{item.customContent,jdbcType=VARCHAR},
            #{item.contentType,jdbcType=INTEGER},
            #{item.contentFrom,jdbcType=INTEGER}
            )
        </foreach>
    </insert>

</mapper>