package com.wecloud.im.service.impl;

import io.geekidea.springbootplus.framework.common.exception.BusinessException;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.shiro.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImClientBlacklist;
import com.wecloud.im.mapper.ImClientBlacklistMapper;
import com.wecloud.im.param.ImClientBlacklistPageParam;
import com.wecloud.im.param.ImClientBlacklistQueryVo;
import com.wecloud.im.param.add.ImClientBlacklistUpdate;
import com.wecloud.im.service.ContextService;
import com.wecloud.im.service.ImClientBlacklistService;
import com.wecloud.im.service.ImClientService;
import com.wecloud.utils.SnowflakeUtil;

/**
 * 黑名单 服务实现类
 *
 * @author wei
 * @since 2021-06-17
 */
@Slf4j
@Service
public class ImClientBlacklistServiceImpl extends BaseServiceImpl<ImClientBlacklistMapper, ImClientBlacklist> implements ImClientBlacklistService {

    @Autowired
    private ImClientBlacklistMapper imClientBlacklistMapper;

    @Autowired
    private ImClientService imClientService;

    @Autowired
    private ContextService contextService;

    @Override
    public boolean isBeBlack(String currentClientId, String toClientId) {
        ImClientBlacklist one = this.getOne(new QueryWrapper<ImClientBlacklist>().lambda()
                .eq(ImClientBlacklist::getClientIdPrevent, currentClientId)
                .eq(ImClientBlacklist::getClientIdBePrevent, toClientId));

        return one != null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addImClientBlacklist(ImClientBlacklistUpdate imClientBlacklistUpdate) {
        ImClient currentClient = contextService.getImClientIfNotNullOrThrow();
        ImClient beBlacker = imClientService.getOne(new QueryWrapper<ImClient>().lambda()
                .eq(ImClient::getFkAppid, currentClient.getFkAppid())
                .eq(ImClient::getClientId, imClientBlacklistUpdate.getClientIdBePrevent()));
        if (beBlacker == null) {
            throw new BusinessException("查无用户信息");
        }
        if (this.isBeBlack(currentClient.getClientId(), imClientBlacklistUpdate.getClientIdBePrevent())) {
            throw new BusinessException("该用户已被拉黑");
        }
        ImClientBlacklist imClientBlacklist = new ImClientBlacklist();
        imClientBlacklist.setId(SnowflakeUtil.getId());
        imClientBlacklist.setCreateTime(new Date());
        imClientBlacklist.setFkAppid(currentClient.getFkAppid());
        imClientBlacklist.setClientIdPrevent(currentClient.getClientId());
        imClientBlacklist.setClientIdBePrevent(beBlacker.getClientId());
        this.save(imClientBlacklist);

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeImClientBlacklist(ImClientBlacklistUpdate imClientBlacklistUpdate) {
        // 操作者
        ImClient currentClient = contextService.getImClientIfNotNullOrThrow();
        // 被拉黑者
        ImClient beBlacker = imClientService.getOne(new QueryWrapper<ImClient>().lambda()
                .eq(ImClient::getFkAppid, currentClient.getFkAppid())
                .eq(ImClient::getClientId, imClientBlacklistUpdate.getClientIdBePrevent()));
        if (beBlacker == null) {
            throw new BusinessException("查无用户信息");
        }
        // 删除
        this.remove(new QueryWrapper<ImClientBlacklist>().lambda()
                .eq(ImClientBlacklist::getClientIdPrevent, currentClient.getClientId())
                .eq(ImClientBlacklist::getClientIdBePrevent, beBlacker.getClientId()));
        return true;
    }

    @Override
    public Paging<ImClientBlacklistQueryVo> getImClientBlacklistPageList(ImClientBlacklistPageParam imClientBlacklistPageParam) {
        ImClient currentClient = contextService.getImClientIfNotNullOrThrow();
        return getImClientBlacklistPageList(currentClient, imClientBlacklistPageParam);
    }

    @Override
    public Paging<ImClientBlacklistQueryVo> getImClientBlacklistPageListSdk(ImClientBlacklistPageParam imClientBlacklistPageParam) {
        Long appId = SecurityUtils.getCurrentAppId();
        ImClient client = imClientService.getCacheImClient(appId, imClientBlacklistPageParam.getClientId());
        if (client == null) {
            throw new BusinessException("客户端不存在");
        }
        return getImClientBlacklistPageList(client, imClientBlacklistPageParam);
    }

    private Paging<ImClientBlacklistQueryVo> getImClientBlacklistPageList(ImClient client,
                                                                          ImClientBlacklistPageParam imClientBlacklistPageParam) {

        Page<ImClientBlacklistQueryVo> page = new PageInfo<>(imClientBlacklistPageParam);
        IPage<ImClientBlacklistQueryVo> imClientBlacklistPageList = imClientBlacklistMapper.getImClientBlacklistPageList(page, client.getClientId());
        Paging<ImClientBlacklistQueryVo> imClientBlacklistQueryVoPaging = new Paging<>(imClientBlacklistPageList);
        return imClientBlacklistQueryVoPaging;
    }
}
