<?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.advertising.dao.impl.AdvertisingMasterDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingMaster">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="master_name" property="masterName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap id="AdvertisingMasterDTO" type="com.pcloud.book.advertising.dto.AdvertisingMasterDTO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="master_name" property="masterName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, master_name, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_master
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="getDTOById" resultMap="AdvertisingMasterDTO" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_master
        where id = #{id,jdbcType=BIGINT}
    </select>

    <select id="checkMasterNameExist" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
            exists (
                select
                    id
                from
                    advertising_master
                where
                    master_name = #{masterName,jdbcType=VARCHAR}
                <if test="masterId != null">
                    and id != #{masterId,jdbcType=BIGINT}
                </if>
            )
    </select>

    <delete id="deleteById" parameterType="java.lang.Long">
        delete from advertising_master
        where id = #{id,jdbcType=BIGINT}
    </delete>
    
    <select id="getMasterList" parameterType="map" resultMap="AdvertisingMasterDTO">
        select
            m.id,
            m.master_name,
            m.create_time,
            m.update_time,
            count(s.id) spaceNum
        from
            advertising_master m
        left join advertising_space s on s.master_id = m.id
        where 1 = 1
        <if test="null != masterName and masterName != ''">
            and m.master_name like concat('%', #{masterName}, '%')
        </if>
        group by
            m.id desc
    </select>

    <select id="getAllMaster" resultMap="AdvertisingMasterDTO">
        select
        <include refid="Base_Column_List"/>
        from advertising_master
    </select>

    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingMaster"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_master
        <trim prefix="(" suffix=")" suffixOverrides=",">
            master_name,
            create_time,
            update_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{masterName,jdbcType=VARCHAR},
            NOW(),
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingMaster">
        update advertising_master
        <set>
            <if test="masterName != null">
                master_name = #{masterName,jdbcType=VARCHAR},
            </if>
                update_time = NOW(),
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
</mapper>