package com.fdkankan.platform.user.controller.sso;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.platform.agent.request.RequestAgent;
import com.fdkankan.platform.agent.service.IAgentService;
import com.fdkankan.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 代理商登录模块
 */
@RestController
@RequestMapping("/service/sso/agent")
public class SsoAgentController {

    @Autowired
    private IAgentService agentService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 代理商登录
     */
    @PostMapping(value = "/login")
    public JSONObject AgentLogin(@RequestBody RequestAgent requestAgent){
        return agentService.login(requestAgent);
    }

    /**
     * 代理商登出
     */
    @PostMapping(value = "/logout")
    public String logout(@RequestHeader String token) {
        if(token == null){
            throw new BusinessException(ErrorCode.USER_NOT_LOGIN);
        }
        String userName = JwtUtil.getUsername(token);
        if(userName == null){
            throw new BusinessException(ErrorCode.TOKEN_ILLEGAL);
        }
        redisUtil.del(userName);
        return ServerCode.SUCCESS.message();
    }
}
