package com.ruoyi.frequency.shield.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.ShieldPageReq;
import com.ruoyi.controller.req.ShieldUserReq;
import com.ruoyi.controller.resp.ShieldPageResp;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.shield.entity.Shield;
import com.ruoyi.frequency.shield.mapper.ShieldMapper;
import com.ruoyi.frequency.shield.service.ShieldService;
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 ShieldServiceImpl extends ServiceImpl<ShieldMapper, Shield> implements ShieldService {
    @Autowired
    private ShieldMapper shieldMapper;

    /**
     * 查询屏蔽用户
     *
     * @param id 屏蔽用户主键
     * @return 屏蔽用户
     */
    @Override
    public Shield selectShieldById(Long id) {
        return shieldMapper.selectShieldById(id);
    }

    /**
     * 查询屏蔽用户列表
     *
     * @param shield 屏蔽用户
     * @return 屏蔽用户
     */
    @Override
    public List<Shield> selectShieldList(Shield shield) {
        return shieldMapper.selectShieldList(shield);
    }

    /**
     * 新增屏蔽用户
     *
     * @param shield 屏蔽用户
     * @return 结果
     */
    @Override
    public int insertShield(Shield shield) {
        shield.setCreateTime(DateUtils.getNowDate());
        return shieldMapper.insertShield(shield);
    }

    /**
     * 修改屏蔽用户
     *
     * @param shield 屏蔽用户
     * @return 结果
     */
    @Override
    public int updateShield(Shield shield) {
        shield.setUpdateTime(DateUtils.getNowDate());
        return shieldMapper.updateShield(shield);
    }

    /**
     * 批量删除屏蔽用户
     *
     * @param ids 需要删除的屏蔽用户主键
     * @return 结果
     */
    @Override
    public int deleteShieldByIds(Long[] ids) {
        return shieldMapper.deleteShieldByIds(ids);
    }

    /**
     * 删除屏蔽用户信息
     *
     * @param id 屏蔽用户主键
     * @return 结果
     */
    @Override
    public int deleteShieldById(Long id) {
        return shieldMapper.deleteShieldById(id);
    }

    @Override
    public ResponseData shieldUser(ShieldUserReq req, UserVo userVo) {
        Shield shield = this.getOne(new QueryWrapper<Shield>().lambda()
                .eq(Shield::getUserType, userVo.getUserType())
                .eq(Shield::getUserId, userVo.getUserId())
                .eq(Shield::getShieldUserType, req.getUserType())
                .eq(Shield::getShieldUserId, req.getUserId()));
        boolean isShield = false;
        if (shield == null) {
            shield = new Shield()
                    .setUserId(userVo.getUserId())
                    .setUserType(userVo.getUserType())
                    .setShieldUserType(req.getUserType())
                    .setShieldUserId(req.getUserId());
            //屏蔽
            isShield = true;

            this.save(shield);
        } else {
            //是否屏蔽
            isShield = ObjectUtil.notEqual(PublicCommon.启用, shield.getDelFlag());

            this.lambdaUpdate().set(Shield::getDelFlag, isShield ? PublicCommon.启用 : PublicCommon.删除)
                    .eq(Shield::getId, shield.getId()).update();
        }

        return ResponseData.success();
    }

    @Autowired
    private UserService userService;

    @Override
    public ResponseData shieldPage(ShieldPageReq req, UserVo userVo) {
        Page<ShieldPageResp> page = this.baseMapper.shieldPage(new Page<>(req.getPageNo(), req.getPageSize()), req, userVo);

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

            page.getRecords().stream().forEach(resp -> {
                resp.setUserInfo(userMap.get(new UserVo(resp.getShieldUserType(), resp.getShieldUserId())));
            });
        }

        return ResponseData.success(new PageResult<>(page));
    }

    @Override
    public ResponseData otherShieldPage(ShieldPageReq req, UserVo userVo) {
        Page<ShieldPageResp> page = this.baseMapper.shieldPage(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.getShieldUserType(), resp.getShieldUserId())).collect(Collectors.toSet());
            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet, userVo);

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

    @Override
    public ResponseData shieldUserVersion2(ShieldUserReq req, UserVo userVo) {
        Shield shield = this.getOne(new QueryWrapper<Shield>().lambda()
                .eq(Shield::getUserType, userVo.getUserType())
                .eq(Shield::getUserId, userVo.getUserId())
                .eq(Shield::getShieldUserType, req.getUserType())
                .eq(Shield::getShieldUserId, req.getUserId()));
        if (shield == null) {
            shield = new Shield()
                    .setUserId(userVo.getUserId())
                    .setUserType(userVo.getUserType())
                    .setShieldUserType(req.getUserType())
                    .setShieldUserId(req.getUserId());
            this.save(shield);
        } else {
            this.lambdaUpdate().set(Shield::getDelFlag, PublicCommon.启用)
                    .eq(Shield::getId, shield.getId()).update();
        }

        return ResponseData.success();
    }

    @Override
    public ResponseData cancelShieldUserVersion2(ShieldUserReq req, UserVo userVo) {
        Shield shield = this.getOne(new QueryWrapper<Shield>().lambda()
                .eq(Shield::getUserType, userVo.getUserType())
                .eq(Shield::getUserId, userVo.getUserId())
                .eq(Shield::getShieldUserType, req.getUserType())
                .eq(Shield::getShieldUserId, req.getUserId()));
        if (shield != null) {
            this.lambdaUpdate().set(Shield::getDelFlag, PublicCommon.删除)
                    .eq(Shield::getId, shield.getId()).update();
        }

        return ResponseData.success();
    }
}
