package com.ruoyi.generator.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.generator.domain.Community.CommunityTag;
import com.ruoyi.generator.domain.Community.CommunityTagBlock;
import com.ruoyi.generator.mapper.community.CommunityTagBlockMapper;
import com.ruoyi.generator.mapper.community.CommunityTagMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommunityTagServiceImpl extends ServiceImpl<CommunityTagMapper, CommunityTag> implements ICommunityTagService {

    @Autowired
    private CommunityTagBlockMapper communityTagBlockMapper;

    /**
     * 拉黑标签
     *
     * @param communityTagBlock 拉黑标签记录
     */
    @Override
    public CommunityTagBlock blockTag(CommunityTagBlock communityTagBlock) {
        CommunityTagBlock tagBlock = communityTagBlockMapper.selectOne(new QueryWrapper<CommunityTagBlock>()
                .eq("user_id", communityTagBlock.getUserId())
                .eq("tag_id", communityTagBlock.getTagId()));

        if (tagBlock != null) {
            //是否拉黑
            if (communityTagBlock.isBlock() == tagBlock.isBlock()) {
                return tagBlock;
            }
            tagBlock.setBlock(communityTagBlock.isBlock());
            tagBlock.setUpdateBy(communityTagBlock.getUserId());
            tagBlock.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
            communityTagBlockMapper.updateById(tagBlock);
            return tagBlock;
        }

        //新增记录
        tagBlock = new CommunityTagBlock();
        BeanUtils.copyProperties(communityTagBlock, tagBlock);
        tagBlock.setCreateBy(communityTagBlock.getUserId());
        tagBlock.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
        communityTagBlockMapper.insert(tagBlock);
        return tagBlock;
    }
}
