<?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.push.dao.impl.PushPlanDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.push.entity.PushPlan">
        <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 push_plan
        where is_delete=0 and id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="java.lang.Long">
        update push_plan set is_delete = 1
        where id = #{id,jdbcType=BIGINT} and is_delete = 0
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.push.entity.PushPlan" useGeneratedKeys="true" keyProperty="id">
        insert into push_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>

    <update id="update" parameterType="com.pcloud.book.push.entity.PushPlan">
        update push_plan
        <set>
            <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,jdbcType=BIGINT}
    </update>

    <!--查询计划集合-->
    <select id="getPushPlanList" parameterType="map" resultType="com.pcloud.book.push.dto.PushPlanDTO">
        SELECT
        t.id pushPlanId,
        t.is_delete isDelete,
        t.create_user createUser,
        t.create_time createTime,
        t.update_user updateUser,
        t.update_time updateTime
        FROM
        push_plan t
        LEFT JOIN push t1 ON t.id = t1.push_plan_id
        <if test="keyword!=null">
            LEFT JOIN push_group t2 ON t1.id = t2.push_id
            LEFT JOIN book_group_classify t3 ON t2.classify_id = t3.id
            LEFT JOIN book_group t4 ON t3.book_group_id = t4.id
            LEFT JOIN book t5 ON t3.book_id = t5.BOOK_ID
        </if>
        WHERE
        t.is_delete = 0
        AND t.create_user=#{partyId}
        AND t1.is_delete = 0
        <if test="keyword!=null">
            AND t2.is_delete = 0
            AND t3.is_delete = 0
            AND t4.is_delete = 0
            AND t5.IS_DELETE = 0
            AND (
            t5.BOOK_NAME LIKE concat('%', #{keyword}, '%')
            OR t4.group_qrcode_name LIKE concat('%', #{keyword}, '%')
            OR t3.classify LIKE concat('%', #{keyword}, '%')
            )
        </if>
        <if test="day!=null and weekDay!=null">
            AND (
                t1.push_type = 2 AND DATE_FORMAT(t1.push_time, '%Y-%m-%d') = #{day}
                OR
                t1.push_type = 3 AND #{day} BETWEEN t1.start_time AND t1.end_time
                OR
                t1.push_type = 4 AND #{day} BETWEEN t1.start_time AND t1.end_time AND t1.week_day = #{weekDay}
            )
        </if>
        GROUP BY t.id
        order by t.create_time desc
    </select>
</mapper>