<?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.AdvertisingPlanDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingPlan">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="is_delete" property="isDelete" jdbcType="BOOLEAN"/>
        <result column="create_user" property="createUser" jdbcType="BIGINT"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_user" property="updateUser" jdbcType="BIGINT"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, is_delete, create_user, create_time, update_user, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from advertising_plan
        where is_delete=0 and id = #{id}
    </select>

    <update id="deleteById" parameterType="java.lang.Long">
        update advertising_plan
        set is_delete=1
        where id = #{id}
    </update>
    
    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingPlan"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <!--查询列表-->
    <select id="getAdvertisingPlanList" parameterType="map" resultMap="BaseResultMap">
        SELECT
        t1.id,
        t1.is_delete,
        t1.create_user,
        t1.create_time,
        t1.update_user,
        t1.update_time
        FROM
        advertising_plan t1
        <if test="keyword!=null">
            LEFT JOIN advertising_put t2 ON t1.id = t2.advertising_plan_id
            LEFT JOIN advertising_plan_group t3 ON t1.id = t3.advertising_plan_id
            LEFT JOIN advertising_space t4 ON t2.advertising_space_id = t4.id
            LEFT JOIN book t5 ON t3.book_id = t5.BOOK_ID
            LEFT JOIN book_group_qrcode t6 ON qrcode_id = t6.id
        </if>
        WHERE
        t1.is_delete = 0
        AND t1.create_user=#{partyId}
        <if test="keyword!=null">
            AND (
            t5.BOOK_NAME LIKE concat('%', #{keyword}, '%')
            OR t6.group_name LIKE concat('%', #{keyword}, '%')
            OR t4.ad_name LIKE concat('%', #{keyword}, '%')
            )
        </if>
        GROUP BY t1.id
        order by t1.create_time desc
    </select>
</mapper>