package org.jeecg.modules.oroqen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.oroqen.entity.OroqenUserFavorite;
import org.jeecg.modules.oroqen.mapper.OroqenUserFavoriteMapper;
import org.jeecg.modules.oroqen.service.IOroqenUserFavoriteService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 用户收藏
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Service
public class OroqenUserFavoriteServiceImpl extends ServiceImpl<OroqenUserFavoriteMapper, OroqenUserFavorite> implements IOroqenUserFavoriteService {

    @Override
    public List<OroqenUserFavorite> getByUserId(String userId) {
        return baseMapper.selectByUserId(userId);
    }

    @Override
    public List<OroqenUserFavorite> getByUserIdAndType(String userId, Integer favoriteType) {
        return baseMapper.selectByUserIdAndType(userId, favoriteType);
    }

    @Override
    public void addFavorite(String userId, Integer favoriteType, String targetId) {
        // 检查是否已收藏
        if (!isFavorited(userId, favoriteType, targetId)) {
            OroqenUserFavorite favorite = new OroqenUserFavorite();
            favorite.setUserId(userId);
            favorite.setFavoriteType(favoriteType);
            favorite.setTargetId(targetId);
            favorite.setCreateTime(new Date());
            this.save(favorite);
        }
    }

    @Override
    public void removeFavorite(String userId, Integer favoriteType, String targetId) {
        QueryWrapper<OroqenUserFavorite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("favorite_type", favoriteType);
        queryWrapper.eq("target_id", targetId);
        this.remove(queryWrapper);
    }

    @Override
    public boolean isFavorited(String userId, Integer favoriteType, String targetId) {
        OroqenUserFavorite favorite = baseMapper.selectByUserIdAndTarget(userId, favoriteType, targetId);
        return favorite != null;
    }
}