package cn.iocoder.yudao.module.llm.service.dataset;


import cn.iocoder.yudao.module.llm.controller.admin.dataset.vo.DatasetQuestionRespVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetDO;
import cn.iocoder.yudao.module.llm.dal.mysql.dataset.DatasetMapper;
import cn.iocoder.yudao.module.llm.service.async.AsyncDataSetService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class DatasetTaskSyncService {
    @Resource
    private DatasetQuestionService datasetQuestionService;
    @Resource
    private DatasetMapper datasetMapper;
    @Resource
    private AsyncDataSetService dataSetService;

    @Scheduled(cron ="0/15 * * * * ?")
    public void syncDatasetAigcTask() {
        log.info("[syncDatasetAigcTask][开始同步任务]");
        // 查询已经标注完成的数据集
        LambdaQueryWrapper<DatasetDO> wrapper = new LambdaQueryWrapper<DatasetDO>()
                .eq(DatasetDO::getStatus, 2)
                .and(w->w.isNull(DatasetDO::getJobId)
                        .or().eq(DatasetDO::getJobId, ""));
        List<DatasetDO> datasetList = datasetMapper.selectList(wrapper);
        datasetList.forEach(datasetDO -> {
            List<DatasetQuestionRespVO> datasetQuestionList = datasetQuestionService.getDatasetQuestionList(datasetDO.getId());
            dataSetService.JsonFileWrite(datasetDO,datasetQuestionList);
        });
    }
}
