package com.common.mapper;

import com.common.entity.WecomNotification;
import org.apache.ibatis.annotations.*;

/**
 * 微信通知 Mapper 接口
 */
@Mapper
public interface WecomNotificationMapper {

    /**
     * 插入单条记录（使用序列生成ID）
     */
    @Insert("INSERT INTO wecom_notification (" +
            "wecom_notification_id, user_id, wecom_notification_name, wecom_notification_ip, " +
            "wecom_notification_type, wecom_notification_level, wecom_notification_content, " +
            "wecom_notification_time, tenant_id, create_dept, create_by, create_time, " +
            "update_by, update_time, remark, wecom_notification_status" +
            ") VALUES (" +
            "nextval('seq_wecom_notification'), #{userId}, #{wecomNotificationName}, #{wecomNotificationIp}, " +
            "#{wecomNotificationType}, #{wecomNotificationLevel}, #{wecomNotificationContent}, " +
            "#{wecomNotificationTime}, #{tenantId}, #{createDept}, #{createBy}, #{createTime}, " +
            "#{updateBy}, #{updateTime}, #{remark}, #{wecomNotificationStatus}" +
            ")")
    @SelectKey(statement = "SELECT currval('seq_wecom_notification')", keyProperty = "wecomNotificationId", resultType = Long.class, before = false)
    int insert(WecomNotification notification);

    /**
     * 根据通知ID查询
     */
    @Select("SELECT * FROM wecom_notification WHERE wecom_notification_id = #{wecomNotificationId}")
    WecomNotification selectById(@Param("wecomNotificationId") Long wecomNotificationId);

    /**
     * 查询未发送的通知列表
     */
    @Select("SELECT * FROM wecom_notification WHERE wecom_notification_status = '1' ORDER BY create_time DESC")
    java.util.List<WecomNotification> selectUnsentNotifications();

    /**
     * 更新通知状态
     */
    @Update("UPDATE wecom_notification SET wecom_notification_status = #{status}, update_time = NOW() " +
            "WHERE wecom_notification_id = #{wecomNotificationId}")
    int updateStatus(@Param("wecomNotificationId") Long wecomNotificationId, @Param("status") String status);
}
