package cn.ey.biz.module.system.api.permission;

import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.api.permission.dto.CodeBlockDataPermissionRespDTO;
import cn.ey.biz.module.system.api.permission.dto.DeptDataPermissionRespDTO;
import cn.ey.biz.module.system.api.user.dto.AdminUserRespDTO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.service.permission.PermissionService;
import cn.ey.biz.module.system.service.user.AdminUserService;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import static cn.ey.biz.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.module.system.enums.ApiConstants.VERSION;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@Slf4j
public class PermissionApiImpl implements PermissionApi {

    @Resource
    private PermissionService permissionService;
    @Resource
    private AdminUserService adminUserService;

    @Override
    public R<Set<Long>> getUserRoleIdListByRoleIds(Collection<Long> roleIds) {
        return success(permissionService.getUserRoleIdListByRoleIds(roleIds));
    }

    @Override
    public R<Boolean> hasAnyPermissions(Long userId, String... permissions) {
        return success(permissionService.hasAnyPermissions(userId, permissions));
    }

    @Override
    public R<Boolean> hasAnyPermissions(Long userId, String app, String... permissions) {
        return success(permissionService.hasAnyPermissions(userId, app, permissions));
    }

    @Override
    public R<Boolean> hasAnyRoles(Long userId, String... roles) {
        return success(permissionService.hasAnyRoles(userId, roles));
    }

    @Override
    public R<DeptDataPermissionRespDTO> getDeptDataPermission(Long userId) {
        return success(permissionService.getDeptDataPermission(userId));
    }

    @Override
    public R<CodeBlockDataPermissionRespDTO> getCodeBlockDataPermission(Long userId) {
        return success(permissionService.getCodeBlockDataPermission(userId));
    }

    @Override
    public R<CodeBlockDataPermissionRespDTO> getCodeBlockDataPermission(Long userId, String gpn) {
        if(userId == null && StrUtil.isEmpty(gpn)){
            throw exception(BAD_REQUEST);
        }
        if(userId == null){
            AdminUserDO adminUserDO = adminUserService.selectByUserCode(gpn);
            if(adminUserDO == null){
                throw exception(USER_NOT_EXISTS);
            }
            userId = adminUserDO.getId();
        }
        return success(permissionService.getCodeBlockDataPermission(userId));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUsersByRole(Long roleId) {
        return success(permissionService.getUsersByRole(roleId));
    }

    @Override
    public R<List<AdminUserRespDTO>> getUsersByRoleCode(String code) {
        return success(permissionService.getUsersByRoleCode(code));
    }

}
