<?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.disk.db.mapper.DiskShareMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into my_disk_share
        (`share_id`,`album_type`,`album_id`,`create_at`,`create_by`)
        values
        (#{shareId},#{albumType},#{albumId},#{createAt},#{createBy})
    </insert>
    <insert id="saveDiskShareTo" useGeneratedKeys="true" keyProperty="id">
        insert into my_disk_share_to
        (`share_id`,`user_id`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.shareId},#{item.userId})
        </foreach>
    </insert>

    <update id="updateSetDelete">
        update my_disk_share
        set update_time=now(),deleted=1
        where share_id=#{shareId}
    </update>
    <delete id="deleteShareToList">
        delete from my_disk_share_to
        where share_id=#{shareId}
    </delete>

    <select id="countDiskShare" resultType="java.lang.Integer">
        select count(*)
        from my_disk_share
        where deleted=0 and create_by=#{createBy}
    </select>
    <select id="findDiskShareByPage" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskShare">
        select *
        from my_disk_share
        where deleted=0 and create_by=#{createBy}
    </select>
    <select id="findDiskShare" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskShare">
        select *
        from my_disk_share
        where deleted=0 and share_id=#{shareId}
    </select>

    <select id="findDiskShareList" resultType="cn.reghao.tnb.content.app.disk.model.po.DiskShare">
        select my_disk_share.*
        from my_disk_share_to
        inner join my_disk_share
        on my_disk_share.deleted=0 and my_disk_share_to.share_id=my_disk_share.share_id
        where my_disk_share.album_type=#{albumType} and my_disk_share_to.user_id=#{shareTo}
    </select>

    <select id="countDiskShareTo" resultType="java.lang.Integer">
        select count(*)
        from my_disk_share_to
        where share_id=#{shareId}
    </select>
    <select id="findDiskShareToList" resultType="java.lang.Long">
        select user_id
        from my_disk_share_to
        where share_id=#{shareId}
    </select>
</mapper>
