package com.keyware.shandan.bianmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.keyware.shandan.bianmu.entity.LabelEntity;
import com.keyware.shandan.bianmu.mapper.LabelEntityMapper;
import com.keyware.shandan.bianmu.service.LabelEntityService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author GuoXin
 * @date 2023-01-12
 */
@Service
public class LabelEntityServiceImpl extends ServiceImpl<LabelEntityMapper, LabelEntity> implements LabelEntityService {
    @Override
    public boolean saveOrUpdate(LabelEntity entity) {
        Assert.hasText(entity.getId(), "id不能为空");
        removeByEntityId(entity.getEntityId());
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<LabelEntity> entityList) {
        removeByEntityIds(entityList.stream().map(LabelEntity::getEntityId).collect(Collectors.toSet()));
        return super.saveOrUpdateBatch(entityList);
    }

    @Override
    public boolean removeByEntityId(@NotNull Serializable entityId) {
        QueryWrapper<LabelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ENTITY_ID", entityId);
        return remove(wrapper);
    }

    @Override
    public boolean removeByEntityIds(Collection<? extends Serializable> entityIds) {
        if (!entityIds.isEmpty()) {
            QueryWrapper<LabelEntity> wrapper = new QueryWrapper<>();
            wrapper.in("ENTITY_ID", entityIds);
            return remove(wrapper);
        }
        return false;
    }
}
