package com.ruoyi.post.frequency.posshield.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.post.frequency.posshield.entity.PostShield;
import com.ruoyi.post.frequency.posshield.mapper.PostShieldMapper;
import com.ruoyi.post.frequency.posshield.service.PostShieldService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.SnowIdUtils;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 论坛屏蔽Service业务层处理
 *
 * @author liwenlong
 * @date 2023-10-26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PostShieldServiceImpl extends ServiceImpl<PostShieldMapper, PostShield>  implements PostShieldService
{
    @Autowired
    private PostShieldMapper postShieldMapper;

    /**
     * 查询论坛屏蔽
     *
     * @param id 论坛屏蔽主键
     * @return 论坛屏蔽
     */
    @Override
    public PostShield selectPostShieldById(Long id)
    {
        return postShieldMapper.selectPostShieldById(id);
    }

    /**
     * 查询论坛屏蔽列表
     *
     * @param postShield 论坛屏蔽
     * @return 论坛屏蔽
     */
    @Override
    public List<PostShield> selectPostShieldList(PostShield postShield)
    {
        return postShieldMapper.selectPostShieldList(postShield);
    }

    /**
     * 新增论坛屏蔽
     *
     * @param postShield 论坛屏蔽
     * @return 结果
     */
    @Override
    public int insertPostShield(PostShield postShield)
    {
        postShield.setCreateTime(DateUtils.getNowDate());
        return postShieldMapper.insertPostShield(postShield);
    }

    /**
     * 修改论坛屏蔽
     *
     * @param postShield 论坛屏蔽
     * @return 结果
     */
    @Override
    public int updatePostShield(PostShield postShield)
    {
        postShield.setUpdateTime(DateUtils.getNowDate());
        return postShieldMapper.updatePostShield(postShield);
    }

    /**
     * 批量删除论坛屏蔽
     *
     * @param ids 需要删除的论坛屏蔽主键
     * @return 结果
     */
    @Override
    public int deletePostShieldByIds(Long[] ids)
    {
        this.lambdaUpdate().set(PostShield::getDelFlag,PublicCommon.删除).in(PostShield::getId,ids).update();
        return ids.length;
    }

    /**
     * 删除论坛屏蔽信息
     *
     * @param id 论坛屏蔽主键
     * @return 结果
     */
    @Override
    public int deletePostShieldById(Long id)
    {
        this.lambdaUpdate().set(PostShield::getDelFlag,PublicCommon.删除).eq(PostShield::getId,id).update();
        return 1;
    }

    @Override
    public ResponseData postShield(Long postId, UserVo userVo) {

        PostShield postShield = new PostShield()
                .setId(SnowIdUtils.uniqueLong())
                .setPostId(postId)
                .setUserType(userVo.getUserType())
                .setUserId(userVo.getUserId());

        postShield.setUpdateData();

        //帖子屏蔽
        postShieldMapper.postShield(postShield,userVo);

        return ResponseData.success();
    }

    @Override
    public List<Long> shieldPostList(UserVo userVo) {

        //查询屏蔽帖子记录
        List<PostShield> list = this.list(new QueryWrapper<PostShield>().lambda()
                .eq(PostShield::getDelFlag, PublicCommon.启用)
                .eq(PostShield::getUserType, userVo.getUserType())
                .eq(PostShield::getUserId, userVo.getUserId()));

        return list.stream().map(PostShield::getPostId).collect(Collectors.toList());
    }
}
