<?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.ycwl.basic.mapper.CouponMapper">
    <update id="updateStatus">
        UPDATE coupon
        SET status = IF(status = 1, 0, 1)
        WHERE id = #{id}
    </update>
    <select id="selectByQuery" resultType="com.ycwl.basic.model.pc.coupon.resp.CouponRespVO">
        SELECT
        c.id, scenic_id AS scenicId,
        c.name AS name, c.description AS description, c.countdown AS countdown, c.broadcast,
        config_ids AS configIds, discount_price AS discountPrice,
        type, discount_type AS discountType,
        c.status, c.create_at
        FROM coupon c
        <where>
            AND c.deleted = 0
            <if test="scenicId != null">AND scenic_id = #{scenicId}</if>
            <if test="name != null and name != ''">AND c.name = concat('%',#{name},'%')</if>
            <if test="type != null">AND type = #{type}</if>
            <if test="discountType != null">AND discount_type = #{discountType}</if>
            <if test="status != null">AND c.status = #{status}</if>
            <if test="createAtStart != null">AND create_at >= #{createAtStart}</if>
            <if test="createAtEnd != null">AND create_at &lt;= #{createAtEnd}
            </if>
        </where>
        ORDER BY create_at DESC
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.coupon.entity.CouponEntity">
        SELECT id, scenic_id, name, description, countdown, broadcast, config_ids, discount_price, type, discount_type, status, create_at, deleted, deleted_at
        FROM coupon
        WHERE id = #{id} AND deleted = 0
    </select>
    
    <insert id="insert" parameterType="com.ycwl.basic.model.pc.coupon.entity.CouponEntity" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO coupon (
            scenic_id, name, description, countdown, broadcast, 
            config_ids, discount_price, type, discount_type, 
            status, create_at, deleted, deleted_at
        ) VALUES (
            #{scenicId}, #{name}, #{description}, #{countdown}, #{broadcast},
            #{configIds}, #{discountPrice}, #{type}, #{discountType},
            #{status}, #{createAt}, #{deleted}, #{deletedAt}
        )
    </insert>
    
    <update id="updateById" parameterType="com.ycwl.basic.model.pc.coupon.entity.CouponEntity">
        UPDATE coupon SET
            scenic_id = #{scenicId},
            name = #{name},
            description = #{description},
            countdown = #{countdown},
            broadcast = #{broadcast},
            config_ids = #{configIds},
            discount_price = #{discountPrice},
            type = #{type},
            discount_type = #{discountType},
            status = #{status},
            create_at = #{createAt},
            deleted = #{deleted},
            deleted_at = #{deletedAt}
        WHERE id = #{id}
    </update>
    
    <delete id="deleteById">
        UPDATE coupon SET deleted = 1, deleted_at = NOW() WHERE id = #{id}
    </delete>
    
    <select id="selectById" resultType="com.ycwl.basic.model.pc.coupon.entity.CouponEntity">
        SELECT id, scenic_id, name, description, countdown, broadcast, config_ids, discount_price, type, discount_type, status, create_at, deleted, deleted_at
        FROM coupon
        WHERE id = #{id}
    </select>
    
    <select id="selectList" resultType="com.ycwl.basic.model.pc.coupon.entity.CouponEntity">
        SELECT id, scenic_id, name, description, countdown, broadcast, config_ids, discount_price, type, discount_type, status, create_at, deleted, deleted_at
        FROM coupon
        WHERE deleted = 0
        ORDER BY create_at DESC
    </select>
    
    <select id="selectByScenicIdAndTypeAndStatus" resultType="com.ycwl.basic.model.pc.coupon.entity.CouponEntity">
        SELECT id, scenic_id, name, description, countdown, broadcast, config_ids, discount_price, type, discount_type, status, create_at, deleted, deleted_at
        FROM coupon
        WHERE scenic_id = #{scenicId} AND type = #{type} AND status = #{status} AND deleted = 0
        LIMIT 1
    </select>
</mapper>