package ink.wgink.service.role.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.user.mongo.IMongoLoginUserService;
import ink.wgink.module.menu.service.IMenuService;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.pojo.dtos.menu.MenuDTO;
import ink.wgink.pojo.dtos.role.RoleMenuDTO;
import ink.wgink.service.role.dao.IRoleMenuDao;
import ink.wgink.service.role.pojo.vos.RoleMenuVO;
import ink.wgink.service.role.service.IRoleMenuService;
import ink.wgink.service.role.service.IRoleUserService;
import ink.wgink.service.user.util.UserUtil;
import ink.wgink.util.thread.CachedThreadPoolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RoleMenuServiceImpl
 * @Description: 角色菜单
 * @Author: wanggeng
 * @Date: 2021/2/2 12:37 下午
 * @Version: 1.0
 */
@Service
public class RoleMenuServiceImpl extends DefaultBaseService implements IRoleMenuService {

    @Autowired
    private IRoleMenuDao roleMenuDao;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleUserService roleUserService;
    @Autowired(required = false)
    private IMongoLoginUserService mongoLoginUserService;

    @Override
    public void update(String roleId, RoleMenuVO roleMenuVO) {
        Map<String, Object> params = getHashMap(4);
        params.put("roleId", roleId);
        roleMenuDao.delete(params);
        for (String menuId : roleMenuVO.getMenuIds()) {
            params.put("menuId", menuId);
            roleMenuDao.save(params);
        }
        updateMongoLoginUser(roleId);
    }

    @Override
    public List<RoleMenuDTO> list(String roleId) {
        Map<String, Object> params = getHashMap(2);
        params.put("roleId", roleId);
        return roleMenuDao.list(params);
    }

    @Override
    public List<String> listMenuId(Map<String, Object> params) {
        return roleMenuDao.listMenuId(params);
    }

    @Override
    public List<String> listMenuId(String roleId) {
        Map<String, Object> params = getHashMap(2);
        params.put("roleId", roleId);
        return roleMenuDao.listMenuId(params);
    }

    @Override
    public List<String> listMenuId(List<String> roleIds) {
        Map<String, Object> params = getHashMap(2);
        params.put("roleIds", roleIds);
        return roleMenuDao.listMenuId(params);
    }

    @Override
    public List<String> listMenuUrl(String roleId) {
        List<String> menuIds = listMenuId(roleId);
        if (menuIds.isEmpty()) {
            return new ArrayList<>();
        }
        List<MenuDTO> menuDTOs = menuService.listByIds(menuIds);
        List<String> menuUrls = new ArrayList<>();
        menuDTOs.forEach(menuDTO -> {
            menuUrls.add(menuDTO.getMenuUrl());
        });
        return menuUrls;
    }

    /**
     * 更新 mongo 用户
     * @param roleId
     */
    private void updateMongoLoginUser(String roleId) {
        if (mongoLoginUserService != null) {
            CachedThreadPoolUtil.execute(() -> {
                List<LoginUser> loginUsers = roleUserService.listUserPOByRoleId(roleId).stream().map(UserUtil::createLoginUser).collect(Collectors.toList());
                mongoLoginUserService.update(loginUsers);
            });
        }
    }
}
