package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.Dynamic;
import com.ruoyi.cai.domain.UserFollowDynamic;
import com.ruoyi.cai.dto.app.query.DynamicStarReq;
import com.ruoyi.cai.mapper.UserFollowDynamicMapper;
import com.ruoyi.cai.service.DynamicService;
import com.ruoyi.cai.service.UserFollowDynamicService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 关注动态表Service业务层处理
 *
 * @author 77
 * @date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class UserFollowDynamicServiceImpl extends ServiceImpl<UserFollowDynamicMapper, UserFollowDynamic> implements UserFollowDynamicService {

    @Autowired
    private DynamicService dynamicService;

    @Override
    public void star(DynamicStarReq query) {
        Dynamic dynamic = dynamicService.getById(query.getDynamicId());
        if(dynamic == null){
            return;
        }
        this.remove(Wrappers.lambdaQuery(UserFollowDynamic.class)
                .eq(UserFollowDynamic::getDynamicId,query.getDynamicId())
                .eq(UserFollowDynamic::getUserId,query.getUserId()));
        UserFollowDynamic userFollowDynamic = new UserFollowDynamic();
        userFollowDynamic.setUserId(query.getUserId());
        userFollowDynamic.setFollowUid(dynamic.getUserId());
        userFollowDynamic.setDynamicId(dynamic.getId());
        this.save(userFollowDynamic);
    }

    @Override
    public void unStar(DynamicStarReq query) {
        this.remove(Wrappers.lambdaQuery(UserFollowDynamic.class)
                .eq(UserFollowDynamic::getDynamicId,query.getDynamicId())
                .eq(UserFollowDynamic::getUserId,query.getUserId()));
    }
}
