<?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.group.dao.impl.ResourcePagePicDaoImpl">

    <resultMap type="com.pcloud.book.group.entity.ResourcePagePic" id="ResourcePagePicMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="picUrl" column="pic_url" jdbcType="VARCHAR"/>
        <result property="isDefault" column="is_default" jdbcType="INTEGER"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="adviserId" column="adviser_id" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="getById" resultMap="ResourcePagePicMap">
        select
          id, pic_url, is_default, type, create_time, adviser_id
        from resource_page_pic
        where id = #{id}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into resource_page_pic(pic_url, is_default, type, create_time, adviser_id)
        values (#{picUrl}, #{isDefault}, #{type}, now(), #{adviserId})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update resource_page_pic
        <set>
            <if test="picUrl != null and picUrl != ''">
                pic_url = #{picUrl},
            </if>
            <if test="isDefault != null">
                is_default = #{isDefault},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="adviserId != null">
                adviser_id = #{adviserId},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from resource_page_pic where id = #{id}
    </delete>

    <select id="getByType" parameterType="map" resultMap="ResourcePagePicMap">
        select
        id, pic_url, is_default, type, create_time
        from resource_page_pic
        WHERE (adviser_id = #{adviserId} OR is_default=1)
        <if test="type != null">
            AND type = #{type}
        </if>
        ORDER BY is_default ASC, id DESC
    </select>

</mapper>