package com.ycwl.basic.pricing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.dto.resp.SceneCouponConfigResp;
import com.ycwl.basic.pricing.entity.PriceSceneCouponConfig;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 场景优惠券配置Mapper
 */
@Mapper
public interface PriceSceneCouponConfigMapper extends BaseMapper<PriceSceneCouponConfig> {

    /**
     * 查询场景下已启用的优惠券配置（带优惠券详情）
     * 用于前端领取接口
     *
     * @param sceneKey 场景标识
     * @param scenicId 景区ID
     * @return 配置列表（带优惠券信息）
     */
    @Select("SELECT scc.id, scc.scene_key, scc.coupon_id, scc.scenic_id, scc.enabled, " +
            "scc.sort_order, scc.create_time, scc.update_time, " +
            "c.coupon_name, c.coupon_type, c.discount_value, " +
            "c.is_active AS coupon_active, c.valid_from AS coupon_valid_from, c.valid_until AS coupon_valid_until " +
            "FROM price_scene_coupon_config scc " +
            "JOIN price_coupon_config c ON scc.coupon_id = c.id AND c.deleted = 0 " +
            "WHERE scc.scene_key = #{sceneKey} AND scc.scenic_id = #{scenicId} " +
            "AND scc.enabled = 1 AND c.is_active = 1 " +
            "AND (c.valid_from IS NULL OR c.valid_from <= NOW()) " +
            "AND (c.valid_until IS NULL OR c.valid_until > NOW()) " +
            "ORDER BY scc.sort_order ASC, scc.id ASC")
    List<SceneCouponConfigResp> selectEnabledBySceneKeyAndScenicId(
            @Param("sceneKey") String sceneKey,
            @Param("scenicId") Long scenicId);

    /**
     * 检查场景下是否存在已启用的配置（用于判断是否需要回退到默认）
     *
     * @param sceneKey 场景标识
     * @param scenicId 景区ID
     * @return 配置数量
     */
    @Select("SELECT COUNT(*) FROM price_scene_coupon_config " +
            "WHERE scene_key = #{sceneKey} AND scenic_id = #{scenicId} AND enabled = 1")
    int countEnabledBySceneKeyAndScenicId(
            @Param("sceneKey") String sceneKey,
            @Param("scenicId") Long scenicId);

    /**
     * 查询所有已配置的场景列表（去重）
     *
     * @return 场景标识列表
     */
    @Select("SELECT DISTINCT scene_key FROM price_scene_coupon_config ORDER BY scene_key")
    List<String> selectDistinctSceneKeys();

    /**
     * 管理端：带优惠券信息的分页查询
     *
     * @param sceneKey 场景标识（模糊匹配，可为null）
     * @param scenicId 景区ID（精确匹配，可为null）
     * @param couponId 优惠券ID（精确匹配，可为null）
     * @param enabled  启用状态（精确匹配，可为null）
     * @return 配置列表（带优惠券信息）
     */
    @Select("<script>" +
            "SELECT scc.id, scc.scene_key, scc.coupon_id, scc.scenic_id, scc.enabled, " +
            "scc.sort_order, scc.create_time, scc.update_time, " +
            "c.coupon_name, c.coupon_type, c.discount_value, " +
            "c.is_active AS coupon_active, c.valid_from AS coupon_valid_from, c.valid_until AS coupon_valid_until " +
            "FROM price_scene_coupon_config scc " +
            "LEFT JOIN price_coupon_config c ON scc.coupon_id = c.id AND c.deleted = 0 " +
            "<where>" +
            "<if test='sceneKey != null and sceneKey != \"\"'>" +
            "AND scc.scene_key LIKE CONCAT('%', #{sceneKey}, '%') " +
            "</if>" +
            "<if test='scenicId != null'>" +
            "AND scc.scenic_id = #{scenicId} " +
            "</if>" +
            "<if test='couponId != null'>" +
            "AND scc.coupon_id = #{couponId} " +
            "</if>" +
            "<if test='enabled != null'>" +
            "AND scc.enabled = #{enabled} " +
            "</if>" +
            "</where>" +
            "ORDER BY scc.scenic_id DESC, scc.scene_key ASC, scc.sort_order ASC, scc.id DESC" +
            "</script>")
    List<SceneCouponConfigResp> selectPageWithCouponInfo(
            @Param("sceneKey") String sceneKey,
            @Param("scenicId") Long scenicId,
            @Param("couponId") Long couponId,
            @Param("enabled") Integer enabled);
}
