package com.purui.prims.headless.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.purui.prims.common.pojo.enums.ItemType;
import com.purui.prims.common.util.BeanMapper;
import com.purui.prims.headless.api.pojo.request.RoleAuthReq;
import com.purui.prims.headless.api.pojo.response.RoleAuthResp;
import com.purui.prims.headless.server.persistence.dataobject.RoleAuthDO;
import com.purui.prims.headless.server.persistence.mapper.RoleAuthDOMapper;
import com.purui.prims.headless.server.service.RoleAuthService;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author charlie
 * @since 2024-07-05
 */
@Service
public class RoleAuthServiceImpl extends ServiceImpl<RoleAuthDOMapper, RoleAuthDO> implements RoleAuthService {

    @Override
    public List<RoleAuthResp> getMetricRoleAuthList(Integer roleId) {
        return getRoleAuthList(roleId, ItemType.METRIC);
    }

    @Override
    public List<RoleAuthResp> getDatasetRoleAuthList(Integer roleId) {
        return getRoleAuthList(roleId, ItemType.DATASET);
    }

    @Override
    public List<RoleAuthResp> getDimensionRoleAuthList(Integer roleId) {
        return getRoleAuthList(roleId, ItemType.DIMENSION);
    }

    @Override
    public List<RoleAuthResp> getRoleAuthList(Integer roleId) {
        QueryWrapper<RoleAuthDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(RoleAuthDO::getRoleId, roleId);
        return list(queryWrapper).stream().map(this::convert).collect(Collectors.toList());
    }

    private List<RoleAuthResp> getRoleAuthList(Integer roleId, ItemType itemType) {
        QueryWrapper<RoleAuthDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(RoleAuthDO::getDataType, itemType.name())
                    .eq(RoleAuthDO::getRoleId, roleId);
        return list(queryWrapper).stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public List<RoleAuthResp> getDomainRoleAuthList(Integer roleId) {
        return getRoleAuthList(roleId, ItemType.DOMAIN);
    }

    private RoleAuthResp convert(RoleAuthDO roleAuthDO) {
        RoleAuthResp roleAuthResp = new RoleAuthResp();
        BeanMapper.mapper(roleAuthDO, roleAuthResp);
        return roleAuthResp;
    }

    @Override
    public boolean grant(RoleAuthReq roleAuthReq) {
        RoleAuthDO roleAuthDO = new RoleAuthDO();
        BeanMapper.mapper(roleAuthReq, roleAuthDO);
        return save(roleAuthDO);
    }
}
