package com.ruoyi.frequency.block.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.BlockPageReq;
import com.ruoyi.controller.req.BlockUserReq;
import com.ruoyi.controller.resp.BlockPageResp;
import com.ruoyi.frequency.block.entity.Block;
import com.ruoyi.frequency.block.mapper.BlockMapper;
import com.ruoyi.frequency.block.service.BlockService;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 拉黑Service业务层处理
 *
 * @author liwenlong
 * @date 2023-07-22
 */
@Service
public class BlockServiceImpl extends ServiceImpl<BlockMapper, Block> implements BlockService {
    @Autowired
    private BlockMapper blockMapper;

    /**
     * 查询拉黑
     *
     * @param id 拉黑主键
     * @return 拉黑
     */
    @Override
    public Block selectBlockById(Long id) {
        return blockMapper.selectBlockById(id);
    }

    /**
     * 查询拉黑列表
     *
     * @param block 拉黑
     * @return 拉黑
     */
    @Override
    public List<Block> selectBlockList(Block block) {
        return blockMapper.selectBlockList(block);
    }

    /**
     * 新增拉黑
     *
     * @param block 拉黑
     * @return 结果
     */
    @Override
    public int insertBlock(Block block) {
        block.setCreateTime(DateUtils.getNowDate());
        return blockMapper.insertBlock(block);
    }

    /**
     * 修改拉黑
     *
     * @param block 拉黑
     * @return 结果
     */
    @Override
    public int updateBlock(Block block) {
        block.setUpdateTime(DateUtils.getNowDate());
        return blockMapper.updateBlock(block);
    }

    /**
     * 批量删除拉黑
     *
     * @param ids 需要删除的拉黑主键
     * @return 结果
     */
    @Override
    public int deleteBlockByIds(Long[] ids) {
        return blockMapper.deleteBlockByIds(ids);
    }

    /**
     * 删除拉黑信息
     *
     * @param id 拉黑主键
     * @return 结果
     */
    @Override
    public int deleteBlockById(Long id) {
        return blockMapper.deleteBlockById(id);
    }


    @Override
    public ResponseData blockUser(BlockUserReq req, UserVo userVo) {

        Block block = this.getOne(new QueryWrapper<Block>().lambda()
                .eq(Block::getUserType, userVo.getUserType())
                .eq(Block::getUserId, userVo.getUserId())
                .eq(Block::getBlockUserType, req.getUserType())
                .eq(Block::getBlockUserId, req.getUserId()));
        boolean isBlock = false;
        if (block == null) {
            block = new Block()
                    .setUserId(userVo.getUserId())
                    .setUserType(userVo.getUserType())
                    .setBlockUserType(req.getUserType())
                    .setBlockUserId(req.getUserId());
            this.save(block);
            //拉黑
            isBlock = true;
        } else {
            //是否拉黑
            isBlock = ObjectUtil.notEqual(PublicCommon.启用, block.getDelFlag());

            this.lambdaUpdate().set(Block::getDelFlag, isBlock ? PublicCommon.启用 : PublicCommon.删除)
                    .eq(Block::getId, block.getId()).update();
        }

        return ResponseData.success();
    }

    @Autowired
    private UserService userService;

    @Override
    public ResponseData blockPage(BlockPageReq req, UserVo userVo) {
        Page<BlockPageResp> page = this.baseMapper.blockPage(new Page<>(req.getPageNo(), req.getPageSize()), req, userVo);

        if (!page.getRecords().isEmpty()) {
            Set<UserVo> userVoSet = page.getRecords().stream().map(resp -> new UserVo(resp.getBlockUserType(), resp.getBlockUserId())).collect(Collectors.toSet());
            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet, userVo);

            page.getRecords().stream().forEach(resp -> {
                resp.setUserInfo(userMap.get(new UserVo(resp.getBlockUserType(), resp.getBlockUserId())));
            });
        }
        return ResponseData.success(new PageResult<>(page));
    }

    @Override
    public ResponseData otherBlockPage(BlockPageReq req, UserVo userVo) {

        Page<BlockPageResp> page = this.baseMapper.blockPage(new Page<>(req.getPageNo(), req.getPageSize()), req, new UserVo(req.getOtherUserType(), req.getOtherUserId()));

        if (!page.getRecords().isEmpty()) {
            Set<UserVo> userVoSet = page.getRecords().stream().map(resp -> new UserVo(resp.getBlockUserType(), resp.getBlockUserId())).collect(Collectors.toSet());
            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet, userVo);

            page.getRecords().stream().forEach(resp -> {
                resp.setUserInfo(userMap.get(new UserVo(resp.getBlockUserType(), resp.getBlockUserId())));
            });
        }
        return ResponseData.success(new PageResult<>(page));
    }

    @Override
    public ResponseData blockUserVersion2(BlockUserReq req, UserVo userVo) {
        Block block = this.getOne(new QueryWrapper<Block>().lambda()
                .eq(Block::getUserType, userVo.getUserType())
                .eq(Block::getUserId, userVo.getUserId())
                .eq(Block::getBlockUserType, req.getUserType())
                .eq(Block::getBlockUserId, req.getUserId()));
        if (block == null) {
            block = new Block()
                    .setUserId(userVo.getUserId())
                    .setUserType(userVo.getUserType())
                    .setBlockUserType(req.getUserType())
                    .setBlockUserId(req.getUserId());
            this.save(block);
        } else {
            this.lambdaUpdate().set(Block::getDelFlag, PublicCommon.启用)
                    .eq(Block::getId, block.getId()).update();
        }

        return ResponseData.success();
    }

    @Override
    public ResponseData cancelBlockUserVersion2(BlockUserReq req, UserVo userVo) {
        Block block = this.getOne(new QueryWrapper<Block>().lambda()
                .eq(Block::getUserType, userVo.getUserType())
                .eq(Block::getUserId, userVo.getUserId())
                .eq(Block::getBlockUserType, req.getUserType())
                .eq(Block::getBlockUserId, req.getUserId())
                .eq(Block::getDelFlag, PublicCommon.启用));
        if (block != null) {
            this.lambdaUpdate().set(Block::getDelFlag, PublicCommon.删除)
                    .eq(Block::getId, block.getId()).update();
        }

        return ResponseData.success();
    }
}
