package com.lightinit.hsdatagateway.modules.foreign.service;

import com.lightinit.hsdatagateway.common.exceptions.PermissionException;
import com.lightinit.hsdatagateway.common.utils.CacheUtils;
import com.lightinit.hsdatagateway.modules.foreign.model.RequestDto;
import com.lightinit.hsdatagateway.modules.gateway.entity.Auth;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizRoute;
import com.lightinit.hsdatagateway.modules.gateway.entity.BizSign;
import com.lightinit.hsdatagateway.modules.gateway.entity.CallLog;
import com.lightinit.hsdatagateway.modules.gateway.service.AuthService;
import com.lightinit.hsdatagateway.modules.gateway.service.BizRouteService;
import com.lightinit.hsdatagateway.modules.gateway.service.BizSignService;
import com.lightinit.hsdatagateway.modules.gateway.service.CallLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @Author: yangrui
 * @Date: 2019/3/5 12:27
 */
@Component
@Slf4j
public class PermissionVerifyService{
    private RequestDto requestDto;

    private AuthService authService;
    private BizRouteService bizRouteService;

    public PermissionVerifyService(AuthService authService, BizRouteService bizRouteService) {
        this.authService = authService;
        this.bizRouteService = bizRouteService;
    }

    public PermissionVerifyService setRequestDto(RequestDto requestDto) {
        this.requestDto = requestDto;
        return this;
    }

    public RequestDto call(){
        requestDto.setTime(System.currentTimeMillis());
        //获取路由
        BizRoute byContextPath = bizRouteService.getByContextPath(requestDto.getPath());
        //获取授权
        Auth byUserAndAppId = authService.getByUserAndAppId(requestDto.getAppId(), requestDto.getUserId(), byContextPath.getRouteId());
        if (Objects.isNull(byUserAndAppId)){
            throw new PermissionException("该接口没有权限");
        }
        requestDto.setRouteId(byContextPath.getRouteId());
        requestDto.setUrl(byContextPath.getMappingUrl());

        return requestDto;
    }
}
