package com.yida.data.school.index.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicroPic;
import com.yida.data.school.index.mapper.EduSchoolHomeMicroPicMapper;
import com.yida.data.school.index.service.EduSchoolHomeMicroPicService;

import org.springframework.stereotype.Service;

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

import cn.hutool.core.collection.CollUtil;

@Service
public class EduSchoolHomeMicroPicServiceImpl extends ServiceImpl<EduSchoolHomeMicroPicMapper,
        EduSchoolHomeMicroPic> implements EduSchoolHomeMicroPicService {

    @Override
    public void savePic(List<EduSchoolHomeMicroPic> picList) {
        Long colId = picList.get(0).getColId();
        List<EduSchoolHomeMicroPic> existPic = list(Wrappers.lambdaQuery(new EduSchoolHomeMicroPic())
                .eq(EduSchoolHomeMicroPic::getColId, colId));
        if (CollUtil.isNotEmpty(existPic)) {
            List<Long> ids = picList.stream().filter(x -> x.getId() != null).map(x -> x.getId()).collect(Collectors.toList());
            remove(Wrappers.lambdaQuery(new EduSchoolHomeMicroPic())
                    .eq(EduSchoolHomeMicroPic::getColId, colId)
                    .notIn(EduSchoolHomeMicroPic::getId, ids));
        }
        saveOrUpdateBatch(picList);
    }
}
