package com.ruoyi.project.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.business.domain.train.TtFileDatabase;
import com.ruoyi.project.business.domain.train.TtFileDirectory;
import com.ruoyi.project.business.domain.vo.train.FileDatabaseReq;
import com.ruoyi.project.business.domain.vo.train.FileDatabaseRes;
import com.ruoyi.project.business.domain.vo.train.FileDatabaseVo;
import com.ruoyi.project.business.mapper.TtFileDatabaseMapper;
import com.ruoyi.project.business.service.train.ITtFileDatabaseService;
import com.ruoyi.project.business.service.train.ITtFileDirectoryService;
import com.ruoyi.project.config.AppConfig;
import com.ruoyi.project.system.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author fangk
 * @description 针对表【tt_file_database】的数据库操作Service实现
 * @createDate 2024-05-20 15:28:25
 */
@Service
@RequiredArgsConstructor
public class TtFileDatabaseServiceImpl extends ServiceImpl<TtFileDatabaseMapper, TtFileDatabase>
        implements ITtFileDatabaseService {

    private final ISysDictDataService dictDataService;
    
    private final ITtFileDirectoryService fileDirectoryService;

    @Override
    @Transactional
    public boolean add(FileDatabaseVo fileDatabaseVo) {
        String fileList = fileDatabaseVo.getFileList();
        String[] files = fileList.split(",");
        List<TtFileDatabase> fileDataList = new ArrayList<>();
        //SysDictData dict = new SysDictData();
        //dict.setDictType("database_file_type");
        //List<SysDictData> dicts = dictDataService.selectDictDataList(dict);
        //Set<String> fileTypeValues = dicts.stream().map(SysDictData::getDictValue).collect(Collectors.toSet());

        for (String file : files) {
            TtFileDatabase fileData = new TtFileDatabase();
            BeanUtils.copyProperties(fileDatabaseVo, fileData);
            // 获取文件名
            String fileName = file.substring(file.lastIndexOf("/") + 1);
            fileData.setFileName(fileName);
            // 获取文件类型
            String fileType = file.substring(file.lastIndexOf(".") + 1);

            //if (!fileTypeValues.contains(fileType)) {
            //    fileType = "other";
            //}

            fileData.setFileType(fileType);
            fileDataList.add(fileData);
        }
        return saveBatch(fileDataList);
    }

    @Override
    public List<FileDatabaseRes> selectTtFileDatabaseList(FileDatabaseReq req) {
        //List<TtFileDatabase> list = list(Wrappers.lambdaQuery(TtFileDatabase.class)
        //        .eq(req.getDirectoryId() != null, TtFileDatabase::getDirectoryId, req.getDirectoryId())
        //        .like(StringUtils.isNotBlank(req.getFileName()), TtFileDatabase::getFileName, req.getFileName())
        //        .eq(StringUtils.isNotBlank(req.getFileType()), TtFileDatabase::getFileType, req.getFileType())
        //        .between(req.getStartTime() != null && req.getEndTime() != null, TtFileDatabase::getCreateTime, req.getStartTime(), req.getEndTime())
        //);

        TtFileDirectory directory = fileDirectoryService.getById(req.getDirectoryId());
        if (directory != null && directory.getDerectoryName().equals(AppConfig.getDataFolder())) {
            req.setDirectoryId(null);
        }

        List<FileDatabaseRes> list = baseMapper.selectTtFileDatabaseList(req);
        return list;

    }

    @Override
    public Set<String> getFileTypes() {
        Wrapper<TtFileDatabase> wrapper = Wrappers.lambdaQuery(TtFileDatabase.class).select(TtFileDatabase::getFileType).groupBy(TtFileDatabase::getFileType);
        List<TtFileDatabase> list = list(wrapper);
        Set<String> collect = list.stream().map(TtFileDatabase::getFileType).collect(Collectors.toSet());
        return collect;
    }
}




