package com.haha.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.haha.entity.MarketingActivity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MarketingActivityMapper extends BaseMapper<MarketingActivity> {

    @Select("SELECT * FROM t_marketing_activity WHERE deleted = 0 ORDER BY create_time DESC")
    List<MarketingActivity> selectAllActivities();

    @Select("SELECT * FROM t_marketing_activity WHERE status = #{status} AND deleted = 0 AND start_time <= #{now} AND end_time >= #{now}")
    List<MarketingActivity> selectOngoingActivities(@Param("status") Integer status, @Param("now") LocalDateTime now);

    @Update("UPDATE t_marketing_activity SET status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);

    @Update("UPDATE t_marketing_activity SET used_budget = used_budget + #{amount}, update_time = NOW() WHERE id = #{id}")
    int addUsedBudget(@Param("id") Long id, @Param("amount") java.math.BigDecimal amount);

    @Select("SELECT COUNT(*) FROM t_marketing_activity WHERE deleted = 0")
    int countTotal();

    @Select("SELECT COUNT(*) FROM t_marketing_activity WHERE status = #{status} AND deleted = 0")
    int countByStatus(@Param("status") Integer status);
}
