package cn.axzo.msg.center.dal.mapper;

import cn.axzo.msg.center.MsgCenterApplication;
import cn.axzo.msg.center.dal.MessageRecordDao;
import cn.axzo.msg.center.domain.entity.MessageRecord;
import cn.axzo.msg.center.message.service.todo.mybatis.Replacement;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Iterator;
import java.util.List;

import static cn.axzo.msg.center.inside.notices.utils.Queries.query;
import static java.util.stream.Collectors.toList;

/**
 * @author yanglin
 */
@SpringBootTest(classes = MsgCenterApplication.class)
@RequiredArgsConstructor(onConstructor_ = @Autowired)
class MessageRecordMapperTest {

    private final MessageRecordMapper messageRecordMapper;
    private final MessageRecordDao messageRecordDao;

    @Test
    void tryMigrate() {
        Iterable<Page<MessageRecord>> pages = messageRecordMapper
                .iterateBath(10, query(MessageRecord.class)
                        .orderByAsc(MessageRecord::getId));

        Iterator<Page<MessageRecord>> iterator = pages.iterator();
        // 试试前5页数据，测试一下
        for (int i = 0; i < 5 && iterator.hasNext(); i++) {
            Page<MessageRecord> page = iterator.next();
            Replacement.TO_MESSAGE_RECORD_COLD.run(() -> {
                List<MessageRecord> records = page.getRecords();
                List<Long> ids = records.stream()
                        .map(MessageRecord::getId)
                        .collect(toList());
                messageRecordMapper.deleteRecordsPhysically(ids);
                messageRecordDao.saveBatch(records);
            });
        }
    }

}