<?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.AdvertisingPutDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.advertising.entity.AdvertisingPut">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="advertising_space_id" property="advertisingSpaceId" jdbcType="BIGINT"/>
        <result column="advertising_plan_id" property="advertisingPlanId" jdbcType="BIGINT"/>
        <result column="put_type" property="putType" jdbcType="INTEGER"/>
        <result column="put_time" property="putTime" jdbcType="VARCHAR"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="week_day" property="weekDay" jdbcType="INTEGER"/>
        <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, advertising_space_id, advertising_plan_id, put_type, put_time, start_time,
        end_time, week_day, 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_put
        where id = #{id}
    </select>

    <update id="deleteById" parameterType="java.lang.Long">
        update advertising_put
        set is_delete=1
        where id = #{id}
    </update>
    
    <insert id="insert" parameterType="com.pcloud.book.advertising.entity.AdvertisingPut"
            useGeneratedKeys="true" keyProperty="id">
        insert into advertising_put
        <trim prefix="(" suffix=")" suffixOverrides=",">
            advertising_space_id,
            advertising_plan_id,
            put_type,
            put_time,
            start_time,
            end_time,
            week_day,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{advertisingSpaceId,jdbcType=BIGINT},
            #{advertisingPlanId,jdbcType=BIGINT},
            #{putType,jdbcType=INTEGER},
            #{putTime,jdbcType=VARCHAR},
            #{startTime,jdbcType=TIMESTAMP},
            #{endTime,jdbcType=TIMESTAMP},
            #{weekDay,jdbcType=INTEGER},
            0,
            #{createUser,jdbcType=BIGINT},
            NOW(),
            #{updateUser,jdbcType=BIGINT},
            NOW(),
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.advertising.entity.AdvertisingPut">
        update advertising_put
        <set>
            <if test="advertisingSpaceId != null">
                advertising_space_id = #{advertisingSpaceId,jdbcType=BIGINT},
            </if>
            <if test="advertisingPlanId != null">
                advertising_plan_id = #{advertisingPlanId,jdbcType=BIGINT},
            </if>
            <if test="putType != null">
                put_type = #{putType,jdbcType=INTEGER},
            </if>
            <if test="putTime != null">
                put_time = #{putTime,jdbcType=VARCHAR},
            </if>
            <if test="startTime != null">
                start_time = #{startTime,jdbcType=TIMESTAMP},
            </if>
            <if test="endTime != null">
                end_time = #{endTime,jdbcType=TIMESTAMP},
            </if>
            <if test="weekDay != null">
                week_day = #{weekDay,jdbcType=INTEGER},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=BOOLEAN},
            </if>
            <if test="updateUser != null">
                update_user = #{updateUser,jdbcType=BIGINT},
            </if>
                update_time=now()
        </set>
        where id = #{id}
    </update>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.advertising.entity.AdvertisingPut" useGeneratedKeys="true" keyProperty="id">
        insert into advertising_put (
            advertising_space_id,
            advertising_plan_id,
            put_type,
            put_time,
            start_time,
            end_time,
            week_day,
            is_delete,
            create_user,
            create_time,
            update_user,
            update_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.advertisingSpaceId,jdbcType=BIGINT},
            #{item.advertisingPlanId,jdbcType=BIGINT},
            #{item.putType,jdbcType=INTEGER},
            #{item.putTime,jdbcType=VARCHAR},
            #{item.startTime,jdbcType=TIMESTAMP},
            #{item.endTime,jdbcType=TIMESTAMP},
            #{item.weekDay,jdbcType=INTEGER},
            0,
            #{item.createUser,jdbcType=BIGINT},
            NOW(),
            #{item.updateUser,jdbcType=BIGINT},
            NOW()
            )
        </foreach>
    </insert>

    <!--根据计划id查询-->
    <select id="getListByPlanId" parameterType="Long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from advertising_put t
        where t.is_delete=0 and advertising_plan_id=#{planId}
    </select>

    <!--批量删除-->
    <update id="deleteByIds" parameterType="Long">
        UPDATE
        advertising_put
        SET is_delete = 1,
        update_user = #{partyId},
        update_time = now()
        WHERE id in
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </update>

    <!--根据计划id集合查询-->
    <select id="getListByPlanIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from advertising_put t
        where t.is_delete=0 and advertising_plan_id in
        <foreach collection="planIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>


    <!--根据发送时间查对应的发送计划-->
    <resultMap id="DistributionMap" type="AdvertisingDistributionDTO">
        <id column="advertising_space_id" property="adId" jdbcType="BIGINT"/>
        <result column="put_type" property="putType" jdbcType="INTEGER"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="put_time" property="putTime" jdbcType="VARCHAR"/>
        <result column="week_day" property="weekDay" jdbcType="INTEGER"/>
    </resultMap>
    <select id="getPlanByTime" parameterType="map" resultMap="DistributionMap">
        SELECT
            p.advertising_space_id,
            p.put_type,
            p.start_time,
            p.end_time,
            p.put_time,
            p.week_day
        FROM
            advertising_put p
        LEFT JOIN advertising_plan_group g ON g.advertising_plan_id = p.advertising_plan_id
        WHERE
            g.qrcode_id = #{qrcodeId}
        AND p.advertising_space_id = #{adId}
        AND (
            TIMEDIFF(#{timeOnly}, put_time) BETWEEN '00:00:00' AND '00:00:05'
            OR
            TIMEDIFF(#{dateTime},put_time) BETWEEN '00:00:00' AND '00:00:05'
        )
        AND
        IF (
            p.put_type = 3, p.week_day = WEEKDAY(#{dateOnly}) + 1, p.put_type > 0
        )
        LIMIT 1
    </select>

</mapper>