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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.user.mongo.IMongoLoginUserService;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.pojo.dtos.permission.PermissionDTO;
import ink.wgink.pojo.dtos.role.RolePermissionDTO;
import ink.wgink.service.permission.service.IPermissionService;
import ink.wgink.service.role.dao.IRolePermissionDao;
import ink.wgink.service.role.pojo.vos.RolePermissionVO;
import ink.wgink.service.role.service.IRolePermissionService;
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: RolePermissionServiceImpl
 * @Description: 角色权限
 * @Author: WangGeng
 * @Date: 2021/1/31 21:23
 * @Version: 1.0
 **/
@Service
public class RolePermissionServiceImpl extends DefaultBaseService implements IRolePermissionService {

    @Autowired
    private IRolePermissionDao rolePermissionDao;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IRoleUserService roleUserService;
    @Autowired(required = false)
    private IMongoLoginUserService mongoLoginUserService;

    @Override
    public void update(String roleId, RolePermissionVO rolePermissionVO) {
        // 清空原有内容
        Map<String, Object> params = getHashMap(4);
        params.put("roleId", roleId);
        params.put("permissionType", rolePermissionVO.getType());
        rolePermissionDao.delete(params);
        for (String permissionId : rolePermissionVO.getPermissionIds()) {
            String[] permissionIdArray = permissionId.split("\\_");
            params.put("apiTag", permissionIdArray[0]);
            params.put("permissionId", permissionIdArray[1]);
            rolePermissionDao.save(params);
        }
        updateMongoLoginUser(roleId);
    }

    @Override
    public List<RolePermissionDTO> list(Map<String, Object> params) {
        return rolePermissionDao.list(params);
    }

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

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

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

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

    @Override
    public List<String> listPermissionId(String roleId, String permissionType) {
        Map<String, Object> params = getHashMap(4);
        params.put("roleId", roleId);
        params.put("permissionType", permissionType);
        return listPermissionId(params);
    }

    @Override
    public List<String> listPermissionUrl(String roleId, String permissionType) {
        List<String> permissionIds = listPermissionId(roleId, permissionType);
        if (permissionIds.isEmpty()) {
            return new ArrayList<>();
        }
        List<PermissionDTO> permissionDTOs = permissionService.list(permissionIds);
        List<String> permissionUrls = new ArrayList<>();
        permissionDTOs.forEach(permissionDTO -> {
            permissionUrls.add(permissionDTO.getPermissionUrl());
        });
        return permissionUrls;
    }

    /**
     * 更新 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);
            });
        }
    }

}
