package tech.easyflow.chatlog.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import tech.easyflow.chatlog.domain.entity.ChatSyncCheckpoint;
import tech.easyflow.chatlog.mapper.ChatSyncCheckpointMapper;
import tech.easyflow.chatlog.service.ChatSyncCheckpointService;

import java.util.Date;

@Service
public class ChatSyncCheckpointServiceImpl extends ServiceImpl<ChatSyncCheckpointMapper, ChatSyncCheckpoint>
        implements ChatSyncCheckpointService {

    @Override
    public ChatSyncCheckpoint getOrCreate(String syncCode, String shardKey) {
        QueryWrapper wrapper = QueryWrapper.create()
                .eq(ChatSyncCheckpoint::getSyncCode, syncCode)
                .eq(ChatSyncCheckpoint::getShardKey, shardKey);
        ChatSyncCheckpoint checkpoint = getMapper().selectOneByQuery(wrapper);
        if (checkpoint != null) {
            return checkpoint;
        }
        ChatSyncCheckpoint created = new ChatSyncCheckpoint();
        created.setSyncCode(syncCode);
        created.setShardKey(shardKey);
        created.setStatus("READY");
        created.setModified(new Date());
        save(created);
        return created;
    }
}
