package com.competition.modules.leai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.competition.modules.leai.entity.LeaiWebhookEvent;
import com.competition.modules.leai.mapper.LeaiWebhookEventMapper;
import com.competition.modules.leai.service.ILeaiWebhookEventService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 乐读派 Webhook 事件 Service 实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LeaiWebhookEventServiceImpl extends ServiceImpl<LeaiWebhookEventMapper, LeaiWebhookEvent>
        implements ILeaiWebhookEventService {

    @Override
    public boolean existsByEventId(String eventId) {
        LambdaQueryWrapper<LeaiWebhookEvent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaiWebhookEvent::getEventId, eventId);
        return count(wrapper) > 0;
    }

    @Override
    public void saveEvent(String eventId, String eventType, String remoteWorkId, Object payload) {
        LeaiWebhookEvent event = new LeaiWebhookEvent();
        event.setEventId(eventId);
        event.setEventType(eventType);
        event.setRemoteWorkId(remoteWorkId);
        event.setPayload(payload);
        event.setProcessed(1);
        event.setCreateTime(LocalDateTime.now());
        save(event);
    }
}
