package com.ai.da.service.impl;

import com.ai.da.mapper.primary.TCollectionElementRelationMapper;
import com.ai.da.mapper.primary.entity.TCollectionElementRelation;
import com.ai.da.service.TCollectionElementRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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


/**
 * <p>
 * collection和element的关联表 服务实现类
 * </p>
 *
 * @author yanglei
 * @since 2023-05-07
 */
@Service
public class TCollectionElementRelationServiceImpl extends ServiceImpl<TCollectionElementRelationMapper, TCollectionElementRelation> implements TCollectionElementRelationService {
    @Resource
    private TCollectionElementRelationMapper tCollectionElementRelationMapper;

    public void deleteByCollectionId(Long collectionId) {
        QueryWrapper<TCollectionElementRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id", collectionId);
        tCollectionElementRelationMapper.delete(queryWrapper);
    }

    public List<Long> getByCollectionId(Long collectionId) {
        QueryWrapper<TCollectionElementRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collection_id", collectionId);
        List<TCollectionElementRelation> tCollectionElementRelations = tCollectionElementRelationMapper.selectList(queryWrapper);
        return CollectionUtils.isEmpty(tCollectionElementRelations) ? null :
                tCollectionElementRelations.stream().map(TCollectionElementRelation::getElementId).collect(Collectors.toList());
    }
}
