package com.yida.data.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.system.Menu;
import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.common.core.entity.system.SystemUserMenu;
import com.yida.data.common.core.enums.system.MenuShowType;
import com.yida.data.system.dto.MenuForUserDTO;
import com.yida.data.system.mapper.MenuMapper;
import com.yida.data.system.mapper.SystemUserMenuMapper;
import com.yida.data.system.mapper.UserMapper;
import com.yida.data.system.service.IMenuService;
import com.yida.data.system.service.SystemUserMenuService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ccl
 */
@Slf4j
@Service("systemUserMenuService")
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class SystemUserMenuServiceImpl extends ServiceImpl<SystemUserMenuMapper, SystemUserMenu> implements
    SystemUserMenuService {

    @Resource
    private IMenuService menuService;

    private final UserMapper userMapper;
    private final MenuMapper menuMapper;

    @Override
    public void saveUserMenu(MenuForUserDTO dto) {

        // 查询用户信息
        SystemUser systemUser = userMapper.selectById(dto.getUserId());
        // 查询用户角色权限信息
        List<Menu> menuList = menuMapper.findUserPermissions(systemUser.getUsername());
        // 用户角色权限id信息
        List<Long> roleMenuIds = menuList.stream().map(Menu::getMenuId).collect(Collectors.toList());
        Collection<Long> all = CollUtil.unionAll(roleMenuIds, dto.getMenuId());
        // 需要添加的
        List<Long> addMenuIds = all.stream().filter(x -> !roleMenuIds.contains(x)).collect(Collectors.toList());
        // 需要删除的
        List<Long> removeMenuIds = all.stream().filter(x -> !dto.getMenuId().contains(x)).collect(Collectors.toList());
        // 保存数据
        List<SystemUserMenu> menus = new ArrayList<>();
        for (Long menuId : addMenuIds) {
            SystemUserMenu systemUserMenu = new SystemUserMenu();
            systemUserMenu.setMenuId(menuId);
            systemUserMenu.setUserId(dto.getUserId());
            systemUserMenu.setType(MenuShowType.ADD_TYPE.getType());
            menus.add(systemUserMenu);
        }
        for (Long menuId : removeMenuIds) {
            SystemUserMenu systemUserMenu = new SystemUserMenu();
            systemUserMenu.setMenuId(menuId);
            systemUserMenu.setUserId(dto.getUserId());
            systemUserMenu.setType(MenuShowType.REMOVE_TYPE.getType());
            menus.add(systemUserMenu);
        }
        saveBatch(menus);
    }

    @Override
    public void updateUserMenu(MenuForUserDTO dto) {
        remove(Wrappers.lambdaQuery(new SystemUserMenu())
            .eq(SystemUserMenu::getUserId, dto.getUserId()));
        saveUserMenu(dto);
    }

    @Override
    public List<Long> findUserMenu(Long userId) {
        SystemUser systemUser = userMapper.selectById(userId);
        List<Long> menuIds = menuMapper.findUserPermissions(systemUser.getUsername()).stream().map(Menu::getMenuId)
            .collect(Collectors.toList());
        List<SystemUserMenu> userMenuList = list(
            Wrappers.lambdaQuery(new SystemUserMenu()).eq(SystemUserMenu::getUserId, userId));
        for (Long excludeMenuId : userMenuList.stream().filter(x -> x.getType() == 1).map(SystemUserMenu::getMenuId)
            .collect(Collectors.toList())) {
            menuIds.remove(excludeMenuId);
        }
        menuIds.addAll(
            userMenuList.stream().filter(x -> x.getType() == 0).map(SystemUserMenu::getMenuId).collect(Collectors.toList()));
        return menuIds;
    }
}
