<?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="cn.reghao.tnb.content.app.data.db.mapper.ImagePostMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into image_post
        (`user_id`,`album_id`,`album_name`,`channel_id`,`cover_url`,`total`,`scope`,`created_at`,`id`,`deleted`,`create_time`,`update_time`)
        values
        (#{userId},#{albumId},#{albumName},#{channelId},#{coverUrl},#{total},#{scope},#{createdAt},#{id},#{deleted},#{createTime},#{updateTime})
    </insert>

    <update id="updateAlbumCount">
        update image_post
        set update_time=now(),total=#{total}
        where album_id=#{albumId}
    </update>

    <select id="countByCriteria" resultType="java.lang.Integer">
        select count(*)
        from image_post
        <where>
            <if test="scope != null">
                scope in
                <foreach collection="scope" item="id" index="index" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="userId != null">
                and user_id=#{userId}
            </if>
        </where>
    </select>
    <select id="findImageAlbumByPage" resultType="cn.reghao.tnb.content.app.data.model.po.ImagePost">
        select *
        from image_post
        <where>
            deleted=0
            <if test="imageAlbumQuery.scope != null">
                and scope in
                <foreach collection="imageAlbumQuery.scope" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="imageAlbumQuery.albumId != null">
                and album_id=#{imageAlbumQuery.albumId}
            </if>
            <if test="imageAlbumQuery.userId != null">
                and user_id=#{imageAlbumQuery.userId}
            </if>
        </where>
    </select>

    <resultMap id="imageAlbumCard" type="cn.reghao.tnb.content.app.data.model.vo.ImageAlbumCard">
        <result property="userId" column="user_id"/>
        <result property="albumId" column="album_id"/>
        <result property="albumName" column="album_name"/>
        <collection property="imageFiles" column="album_id" ofType="cn.reghao.tnb.content.app.data.model.po.ImageFile" select="getImages"/>
    </resultMap>
    <select id="findImageAlbumCardByAlbumId" resultMap="imageAlbumCard">
        select *
        from image_post
        where album_id=#{albumId}
    </select>
    <select id="getImages" resultType="cn.reghao.tnb.content.app.data.model.po.ImageFile">
        select *
        from image_file
        where album_id=#{albumId}
    </select>
</mapper>
