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

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.common.enums.RoleDataRightEnum;
import ink.wgink.exceptions.SearchException;
import ink.wgink.interfaces.user.mongo.IMongoLoginUserService;
import ink.wgink.pojo.dtos.role.RoleDTO;
import ink.wgink.pojo.dtos.role.RoleDataRightDTO;
import ink.wgink.pojo.dtos.role.RoleDataRightInfoDTO;
import ink.wgink.service.role.dao.IRoleDataRightDao;
import ink.wgink.service.role.pojo.vos.RoleDataRightVO;
import ink.wgink.service.role.service.IRoleDataRightService;
import ink.wgink.service.role.service.IRoleService;
import ink.wgink.util.thread.CachedThreadPoolUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RoleDataRightServiceImpl
 * @Description: 角色数据权限
 * @Author: WangGeng
 * @Date: 2021/2/21 18:32
 * @Version: 1.0
 **/
@Service
public class RoleDataRightServiceImpl extends DefaultBaseService implements IRoleDataRightService {

    @Autowired
    private IRoleDataRightDao roleDataRightDao;
    @Autowired
    private IRoleService roleService;
    @Autowired(required = false)
    private IMongoLoginUserService mongoLoginUserService;

    @Override
    public void update(String roleId, RoleDataRightVO roleDataRightVO) {
        roleService.updateRoleDataRight(roleId, roleDataRightVO.getRoleDataRight());
        // 删除角色数据权限
        Map<String, Object> params = getHashMap(4);
        params.put("roleId", roleId);
        roleDataRightDao.delete(params);
        if (StringUtils.equals(roleDataRightVO.getRoleDataRight(), RoleDataRightEnum.CUSTOM.getValue())) {
            // 更新自定义权限用户列表
            for (String userId : roleDataRightVO.getUserIds()) {
                params.put("userId", userId);
                roleDataRightDao.save(params);
            }
            updateMongoLoginUser(roleDataRightVO.getUserIds());
        }
    }

    @Override
    public RoleDataRightInfoDTO get(String roleId) {
        RoleDTO roleDTO = roleService.get(roleId);
        if (roleDTO == null) {
            throw new SearchException("角色不存在");
        }
        List<String> userIds = new ArrayList<>();
        if (StringUtils.equals(roleDTO.getRoleDataRight(), RoleDataRightEnum.CUSTOM.getValue())) {
            List<RoleDataRightDTO> roleDataRightDTOs = list(roleId);
            for (RoleDataRightDTO roleDataRightDTO : roleDataRightDTOs) {
                userIds.add(roleDataRightDTO.getUserId());
            }
        }
        RoleDataRightInfoDTO roleDataRightInfoDTO = new RoleDataRightInfoDTO();
        roleDataRightInfoDTO.setRoleDataRight(roleDTO.getRoleDataRight());
        roleDataRightInfoDTO.setUserIds(userIds);
        return roleDataRightInfoDTO;
    }

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

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

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

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

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

    private void updateMongoLoginUser(List<String> userIds) {
        if (mongoLoginUserService != null) {
            CachedThreadPoolUtil.execute(() -> {
                mongoLoginUserService.updateByUserIds(userIds);
            });
        }
    }

}
