package me.qwq.doghouse.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import me.qwq.doghouse.cache.CacheConstants;
import me.qwq.doghouse.dao.RelationshipMapper;
import me.qwq.doghouse.entity.Relationship;

import java.util.Collection;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 标签/分类服务实现类
 */
@Service
public class RelationshipService extends BaseServiceImpl<RelationshipMapper, Relationship> {

    /**
     * 根据 postId 清除所有关联信息
     * @param postId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Caching(evict={
            @CacheEvict(value= {
                    CacheConstants.Metas.LIST_META_COUNT,
                    CacheConstants.Posts.CONDITION_PAGE,
            }, allEntries=true)})
    public boolean clearRelationshipByPostId(Long postId) {
        LambdaQueryWrapper<Relationship> qw = new LambdaQueryWrapper<Relationship>()
                .eq(Relationship::getPostId, postId);
        return remove(qw);
    }

    /**
     * 根据 entity 条件，删除记录，暴露于缓存用
     */
    @Caching(evict={
            @CacheEvict(value= {
                    CacheConstants.Metas.LIST_META_COUNT,
                    CacheConstants.Posts.CONDITION_PAGE,
            }, allEntries=true)})
    public boolean remove(Wrapper<Relationship> queryWrapper) {
        return super.remove(queryWrapper);
    }
    
    /**
     * 批量修改插入，暴露于缓存用
     */
    @Caching(evict={
            @CacheEvict(value= {
                    CacheConstants.Metas.LIST_META_COUNT,
                    CacheConstants.Posts.CONDITION_PAGE,
            }, allEntries=true)})
    public boolean saveOrUpdateBatch(Collection<Relationship> entityList) {
        return super.saveOrUpdateBatch(entityList);
    }
}
