package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.scene.entity.FolderScene;
import com.fdkankan.scene.mapper.IFolderSceneMapper;
import com.fdkankan.scene.service.IFolderSceneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.scene.vo.FolderSceneVO;
import java.util.ArrayList;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文件夹和场景关联表 服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class FolderSceneServiceImpl extends ServiceImpl<IFolderSceneMapper, FolderScene> implements IFolderSceneService {

    @Autowired
    private IFolderSceneMapper folderSceneMapper;

    @Override
    public void deleteByFolderId(Long folderId) {
        remove(new QueryWrapper<FolderScene>().eq("folderId", folderId));
    }

    @Override
    public void deleteBySceneId(Long sceneId) {
        remove(new LambdaQueryWrapper<FolderScene>().eq(FolderScene::getSceneId, sceneId));
    }

    @Override
    public FolderScene findByType(Long sceneId, Integer folderType) {
        return folderSceneMapper.findByConditoin(FolderSceneVO.builder().sceneId(sceneId).folderType(folderType).build());
    }

    @Override
    public List<FolderScene> findByFolderId(Long folderId) {
        return folderSceneMapper.selectList(
                new QueryWrapper<FolderScene>()
                        .eq("folderId", folderId)
                        .eq("tbStatus", TbStatus.VALID.code()));
    }

    @Override
    public List<Long> getSceneIdListByFolderId(Long folderId) {

        List<FolderScene> folderSceneList =
            this.list(new LambdaQueryWrapper<FolderScene>()
                .eq(FolderScene::getFolderId, folderId));

        if(CollUtil.isEmpty(folderSceneList))
            return null;

        return folderSceneList.parallelStream().map(FolderScene::getSceneId).collect(Collectors.toList());
    }

}
