package org.jk.rag.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jk.common.mybatis.core.page.PageQuery;
import org.jk.common.satoken.utils.LoginHelper;
import org.jk.rag.domain.Dataset;
import org.jk.rag.domain.SliceInfoHistory;
import org.jk.rag.domain.dto.ReviseSliceDTO;
import org.jk.rag.domain.entity.JkMediaReplacement;
import org.jk.rag.domain.entity.JkSliceInfo;
import org.jk.rag.domain.entity.SliceInfo;
import org.jk.rag.domain.vo.DatasetVo;
import org.jk.rag.domain.vo.JkReviseDocumentVo;
import org.jk.rag.domain.vo.JkReviseVo;
import org.jk.rag.domain.vo.ReviseHistoryVo;
import org.jk.rag.mapper.JkDatasetMapper;
import org.jk.rag.mapper.JkMediaReplacementMapper;
import org.jk.rag.mapper.JkReviseMapper;
import org.jk.rag.mapper.JkSliceInfoMapper;
import org.jk.rag.service.IJkDatasetService;
import org.jk.rag.service.IJkReviseService;
import org.jk.system.service.ISysOssService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户 业务层处理
 *
 * @author jk
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class JkDatasetServiceImpl implements IJkDatasetService {

    private final JkDatasetMapper datasetMapper;

    private final ISysOssService sysOssService;

    @Override
    public Page<DatasetVo> selectDatasetPageList(String name,String createBy, PageQuery pageQuery) {
        Page<DatasetVo> page = datasetMapper.selectPageList(name,createBy,pageQuery.build());
        return page;
    }

    @Override
    public Long insertDataset(DatasetVo datasetVo) {
        Dataset dataset = new Dataset();
        BeanUtils.copyProperties(datasetVo, dataset);
        datasetMapper.insert(dataset);
        return dataset.getId();
    }

    @Override
    public boolean checkName(String name) {
        QueryWrapper<Dataset> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dataset::getName,name);
        DatasetVo datasetVo = datasetMapper.selectVoOne(wrapper);
        if (null != datasetVo) {
            return false;
        }
        return true;
    }

    @Override
    public void deleteById(Long id) {
        DatasetVo datasetVo = datasetMapper.selectVoById(id);
        if(null == datasetVo) {
            return;
        }
        sysOssService.deleteWithValidByIds(List.of(datasetVo.getOssId()),false);
        datasetMapper.deleteById(id);
    }

    @Override
    public List<Dataset> selectDatasetList(String appId) {
        QueryWrapper<Dataset> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Dataset::getAppId,appId);
        List<Dataset> result = datasetMapper.selectList(wrapper);
        return result;
    }

}
