package com.mx.cneeds.server.datashow.web.system;

import com.mx.cneeds.common.UserUtils;
import com.mx.cneeds.common.dto.UserDto;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.vo.UserInfoVo;
import com.mx.cneeds.server.datashow.client.AuthorizationClient;
import com.mx.cneeds.common.dto.JwtToken;
import com.mx.cneeds.server.datashow.client.UserClient;
import com.netflix.client.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.logging.Logger;

/**
 * @ClassName UserController
 * @Author zzrdark
 * @Date 2020-03-04 18:09
 * @Description TODO
 **/
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private AuthorizationClient authenticatorClient;


    @Autowired
    private UserClient userClient;

    /**
     * 登陆接口
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public R login(String username, String password){
        JwtToken token = authenticatorClient.getToken("password", username, password, "all");
        token.setToken(token.getTokenType()+" "+token.getAccessToken());
        log.info(token.toString());
        return new R().put("data",token);
    }

    @RequestMapping("/info")
    public R getInfo(){
        UserDto userDto = userClient.userInfo(UserUtils.getUserName());
        UserInfoVo userInfoVo = new UserInfoVo();


        if (userDto.getUserId() == 0){
            userInfoVo.setRoles("admin");
        }else {
            StringBuffer sb = new StringBuffer();

            userDto.getFuncPerms().forEach(perms -> {
                sb.append(perms+",");
            });
            String rolesStr = sb.substring(0, sb.length() - 1);
            userInfoVo.setRoles(rolesStr);
        }

        userInfoVo.setName(userDto.getUsername());
        userInfoVo.setIntroduction("manager");
        userInfoVo.setAvatar("avatar");
        return new R().put("data",userInfoVo);
    }


}
