<?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.group.dao.impl.BookGroupAppDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.group.entity.BookGroupApp">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="VARCHAR"/>
        <result column="origin_type" property="originType" jdbcType="INTEGER"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="BaseResultMapDTO" type="com.pcloud.book.group.dto.BookGroupAppDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="serve_id" property="serveId" jdbcType="BIGINT"/>
        <result column="serve_type" property="serveType" jdbcType="VARCHAR"/>
        <result column="origin_type" property="originType" jdbcType="INTEGER"/>
        <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT"/>
        <result column="classify_id" property="classifyId" jdbcType="BIGINT"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, serve_id, serve_type, origin_type, book_group_id, classify_id, create_user, create_time
    </sql>

    <insert id="insert" parameterType="com.pcloud.book.group.entity.BookGroupApp" useGeneratedKeys="true"
            keyProperty="id">
        insert into book_group_app
        <trim prefix="(" suffix=")" suffixOverrides=",">
            serve_id,
            serve_type,
            origin_type,
            book_group_id,
            classify_id,
            create_user,
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{serveId,jdbcType=BIGINT},
            #{serveType,jdbcType=VARCHAR},
            #{originType,jdbcType=INTEGER},
            #{bookGroupId,jdbcType=BIGINT},
            #{classifyId,jdbcType=BIGINT},
            #{createUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.group.entity.BookGroupApp" useGeneratedKeys="true" keyProperty="id">
        insert into book_group_app (
            serve_id,
            serve_type,
            origin_type,
            book_group_id,
            classify_id,
            create_user,
            create_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
         (
            #{item.serveId,jdbcType=BIGINT},
            #{item.serveType,jdbcType=VARCHAR},
            #{item.originType,jdbcType=INTEGER},
            #{item.bookGroupId,jdbcType=BIGINT},
            #{item.classifyId,jdbcType=BIGINT},
            #{item.createUser,jdbcType=BIGINT},
            NOW()
         )
        </foreach>
    </insert>

    <!--根据条件查询-->
    <select id="getByCondition" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from book_group_app t
        where t.serve_type=#{serveType}
        and t.serve_id=#{serveId}
        and t.book_group_id=#{bookGroupId}
        and t.classify_id=#{classifyId}
        and t.origin_type=#{originType}
    </select>

    <!--根据社群码id查询所挂资源统计-->
    <select id="getBookGroupAppDTOSByBookGroupId" parameterType="map" resultMap="BaseResultMapDTO">
        SELECT
        serve_id,
        serve_type,
        book_group_id,
        create_user
        FROM
        book_group_app t
        WHERE
        t.book_group_id = #{bookGroupId}
        GROUP BY
        serve_id,
        serve_type,
        book_group_id
    </select>

    <!--根据社群码Id查询资源数量-->
    <select id="getServerCountByBookGroupIds" parameterType="map" resultType="com.pcloud.book.group.dto.BookGroupIdAndCountDTO">
        SELECT
        t.book_group_id bookGroupId,
        COUNT(1) count
        FROM
        book_group_app t
        WHERE
        t.book_group_id IN
        <foreach collection="bookGroupIds" open="(" separator="," close=")" index="index" item="item">
            #{item}
        </foreach>
        GROUP BY
        t.book_group_id
    </select>

    <!--查询所有社群书id-->
    <select id="getAllBookGroupId" resultType="Long">
        SELECT distinct book_group_id from
        book_group_app
    </select>

    <!--获取应用统计数据-->
    <select id="getAppServeStatistics" parameterType="map" resultType="com.pcloud.book.group.dto.AppStatisticsDTO">
        SELECT
        t.serve_id serveId,
        t.serve_type serveType
        FROM
        book_group_app t
        WHERE 1=1
        <if test="bookGroupId!=null">
            AND t.book_group_id =#{bookGroupId}
        </if>
        GROUP BY
        t.serve_id,t.serve_type
        order by t.create_time desc
    </select>
</mapper>