<?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.ruoyi.system.mapper.PostCollectionsSystemMapper">

    <resultMap type="PostCollectionsSystem" id="PostCollectionsSystemResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="name"    column="name"    />
        <result property="createTime"    column="create_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
        <result property="image"    column="image"    />
        <result property="giftExchange"    column="gift_exchange"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectPostCollectionsSystemVo">
        select id, type, name, create_time, start_time, end_time, status, image, gift_exchange, update_time, create_by, update_by, del_flag from post_collections_system
    </sql>

    <select id="selectPostCollectionsSystemList" parameterType="PostCollectionsSystem" resultMap="PostCollectionsSystemResult">
        <include refid="selectPostCollectionsSystemVo"/>
        <where>
            <if test="type != null "> and type = #{type}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="image != null  and image != ''"> and image = #{image}</if>
            <if test="giftExchange != null "> and gift_exchange = #{giftExchange}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="updateBy != null  and updateBy != ''"> and update_by = #{updateBy}</if>
        and del_flag = '0'
        </where>
    </select>

    <select id="selectPostCollectionsSystemById" parameterType="Long" resultMap="PostCollectionsSystemResult">
        <include refid="selectPostCollectionsSystemVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <insert id="insertPostCollectionsSystem" parameterType="PostCollectionsSystem" useGeneratedKeys="true" keyProperty="id">
        insert into post_collections_system
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">type,</if>
            <if test="name != null">name,</if>
            <if test="createTime != null">create_time,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
            <if test="image != null">image,</if>
            <if test="giftExchange != null">gift_exchange,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="name != null">#{name},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="image != null">#{image},</if>
            <if test="giftExchange != null">#{giftExchange},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updatePostCollectionsSystem" parameterType="PostCollectionsSystem">
        update post_collections_system
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="name != null">name = #{name},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="image != null">image = #{image},</if>
            <if test="giftExchange != null">gift_exchange = #{giftExchange},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePostCollectionsSystemById" parameterType="Long">
        UPDATE  post_collections_system SET del_flag = '1'  WHERE del_flag = '0' AND (id = #{id})
    </delete>

    <delete id="deletePostCollectionsSystemByIds" parameterType="String">
        UPDATE post_collections_system SET del_flag = '1'  WHERE del_flag = '0' AND id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!--套系搜索功能-->
    <select id="selectPostListByTitleOrTime" resultType="PostCollectionsSystem" resultMap="PostCollectionsSystemResult">
        <include refid="selectPostCollectionsSystemVo"/>
        <where>
            <if test="title != null  and title != ''"> and name LIKE concat('%', #{title}, '%')</if>
            <if test="timeLeft != null and timeRight != null "> and create_time between #{timeLeft} AND #{timeRight}</if>
            and del_flag = '0'
        </where>
    </select>

    <!--获取套系 所对应藏品种类 数量-->
    <select id="getCopiesById" parameterType="Long" resultType="Integer">
       SELECT COUNt(system_id) FROM post_collections where system_id = #{id}
    </select>

    <!--用藏品名称进行查询数量-->
    <select id="selectPostCollectionsSystemByName" parameterType="PostCollectionsSystem" resultType="int">
        SELECT COUNT(name) FROM post_collections_system where name =  #{name} and del_flag = '0'
    </select>
    <!--查询该套系状态status-->
    <select id="selectPostCollectionsSystemByStatus" parameterType="Long" resultType="String">
        select status from post_collections_system  where id = #{systemId}
    </select>


    <!--通过ids 查询套系-->
    <select id="selectPostCollectionsSystemByIds" parameterType="String" resultMap="PostCollectionsSystemResult">
        <include refid="selectPostCollectionsSystemVo"/>
        <where>
            id IN
            <foreach item="id" collection="list" open="(" separator="," close=")">
                #{id}
            </foreach>
            and del_flag = '0'
        </where>
    </select>

    <update id="updateBatchById" parameterType="list">
        <foreach collection="list" item="item" separator=";">
            update
            post_collections_system
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.type != null">type = #{item.type},</if>
                <if test="item.name != null">name = #{item.name},</if>
                <if test="item.createTime != null">create_time = #{item.createTime},</if>
                <if test="item.startTime != null">start_time = #{item.startTime},</if>
                <if test="item.endTime != null">end_time = #{item.endTime},</if>
                <if test="item.status != null">status = #{item.status},</if>
                <if test="item.image != null">image = #{item.image},</if>
                <if test="item.giftExchange != null">gift_exchange = #{item.giftExchange},</if>
                <if test="item.updateTime != null">update_time = #{item.updateTime},</if>
                <if test="item.createBy != null">create_by = #{item.createBy},</if>
                <if test="item.updateBy != null">update_by = #{item.updateBy},</if>
                <if test="item.delFlag != null">del_flag = #{item.delFlag},</if>
            </trim>
            where
            id = #{item.id}  and del_flag = '0'

        </foreach>
    </update>

    <update id="shelvesUpdatePostCollectionsSystem" parameterType="PostCollectionsSystem">
        update post_collections_system
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="image != null">image = #{image},</if>
            <if test="giftExchange != null">gift_exchange = #{giftExchange},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}  and del_flag = '0'
    </update>

    <!--获取套系id 所对应截至时间-->
    <select id="getEndTime" parameterType="Long" resultType="Date">
        SELECT end_time FROM post_collections_system where id= #{id}  and del_flag = '0'
    </select>

    <!--获取已上架的套系-->
    <select id="selectListEqStatus" parameterType="String" resultMap="PostCollectionsSystemResult">
        <include refid="selectPostCollectionsSystemVo"/>
        <where>
            id IN
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
            and status='1'
            and del_flag = '0'
        </where>
    </select>
    <!--查询多个套系-->
    <select id="selectPostCollectionsSystemByIdList" parameterType="Long" resultMap="PostCollectionsSystemResult">
        <include refid="selectPostCollectionsSystemVo"/>
        where del_flag = '0' AND id in
        <foreach collection="list" open="(" close=")" separator="," item="id">
        #{id}
        </foreach>
    </select>

</mapper>