package com.mcwl.resource.service.impl;

import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.resource.domain.Collect;
import com.mcwl.resource.mapper.CollectMapper;
import com.mcwl.resource.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 收藏实现层
 * @author DaiZibo
 * @date 2025/3/5
 * @apiNote
 */

@Service
public class CollectServiceImpl implements CollectService {


    @Autowired
    private CollectMapper collectMapper;

    @Override
    public R addCollect(Collect collect) {

        Collect collect1 = collectMapper.selectCollect(SecurityUtils.getUserId(),collect.getProductId(),collect.getProductType());
        collect.setUserId(SecurityUtils.getUserId());
        collect.setCreateTime(new Date());
        if (collect1 == null){
            //执行收藏
            collectMapper.insert(collect);
            return R.ok(0);
        }

        //执行删除/取消收藏
        collectMapper.deleteById(collect1.getId());
        return R.ok(1);

    }

    @Override
    public Collect selectCollectById(Long modelId, Long userIdMax,Integer type) {

        return collectMapper.selectCollectById(modelId,userIdMax,type);
    }
}
