<?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.adnews.dao.impl.AdNewsMessageStatisticDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.adnews.entity.AdNewsMessageStatistic">
        <id column="id" property="id" 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_set_id" property="adNewsSetId" jdbcType="BIGINT"/>
        <result column="message_count" property="messageCount" jdbcType="INTEGER"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, qrcode_id, classify_id, book_group_id, ad_news_set_id, message_count, create_user, create_time
    </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="getAdNewsMessageStatistic" resultType="com.pcloud.book.adnews.vo.AdNewsMessageStatisticVO" parameterType="map">
        SELECT
        DISTINCT
        a.id,
        d.group_name groupName,
        c.classify classify,
        b.group_qrcode_name groupQrcodeName,
        e.BOOK_NAME bookName,
        b.pro_label_id proLabelId,
        b.dep_label_id depLabelId,
        b.pur_label_id purLabelId,
        a.message_count messageCount,
        a.create_time createTime,
        (select GROUP_CONCAT(distinct content_from SEPARATOR ',') FROM ad_news_group_record WHERE message_statistic_id=a.id) contentFroms,
        (SELECT COUNT(1) FROM ad_news_click_record x WHERE x.message_statistic_id=a.id) clickCount,
        (SELECT COUNT(DISTINCT wechat_user_id) FROM ad_news_click_record x WHERE x.message_statistic_id=a.id) userCount
        FROM
            ad_news_message_statistic a
            INNER JOIN book_group b ON a.book_group_id = b.id
            INNER JOIN book_group_classify c ON a.classify_id = c.id
            INNER JOIN book_group_qrcode d ON a.qrcode_id = d.id
            INNER JOIN book e ON b.book_id = e.BOOK_ID
        WHERE
            a.create_user = #{partyId}
            <if test="title!=null">
                AND (
                  d.group_name like concat('%', #{title}, '%')
                  OR c.classify like concat('%', #{title}, '%')
                  OR b.group_qrcode_name like concat('%', #{title}, '%')
                  OR e.BOOK_NAME like concat('%', #{title}, '%')
                )
            </if>
            <if test="beginTime!=null">
              AND a.create_time <![CDATA[>=]]>#{beginTime}
            </if>
            <if test="endTime!=null">
                AND a.create_time <![CDATA[<=]]>#{endTime}
            </if>
            <if test="proLabelId!=null">
                AND b.pro_label_id=#{proLabelId}
            </if>
            <if test="depLabelId!=null">
                AND b.dep_label_id=#{depLabelId}
            </if>
            <if test="purLabelId!=null">
                AND b.pur_label_id=#{purLabelId}
            </if>
        ORDER BY
            a.id DESC
    </select>

    <insert id="insert" parameterType="com.pcloud.book.adnews.entity.AdNewsMessageStatistic" useGeneratedKeys="true" keyProperty="id">
        insert into ad_news_message_statistic
        <trim prefix="(" suffix=")" suffixOverrides=",">
            qrcode_id,
            classify_id,
            book_group_id,
            ad_news_set_id,
            message_count,
            create_user,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{qrcodeId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{bookGroupId,jdbcType=BIGINT},
            #{adNewsSetId,jdbcType=BIGINT},
            #{messageCount,jdbcType=INTEGER},
            #{createUser,jdbcType=BIGINT},
            NOW()
        </trim>
    </insert>

    <!--批量新增-->
    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        insert into ad_news_message_statistic (
        qrcode_id,
        classify_id,
        book_group_id,
        ad_news_set_id,
        message_count,
        create_user,
        create_time
        )
        values
        <foreach collection="list" item="item" index="index" separator="," >
            (
            #{item.qrcodeId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.adNewsSetId,jdbcType=BIGINT},
            #{item.messageCount,jdbcType=INTEGER},
            #{item.createUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

</mapper>