package com.ruoyi.index.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.microsoft.sqlserver.jdbc.StringUtils;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.domain.LoginUserDept;
import com.ruoyi.system.service.ILoginUserDeptService;

/**
 * 展示指标Controller
 * 
 * @author ruoyi
 * @date 2021-12-28
 */
@Controller
@RequestMapping("/auth")
public class AuthController extends BaseController {
	@Value("${auth.url}")
	private String authurl;

	@Value("${auth.clientId}")
	private String clientId;

	@Value("${auth.clientSecret}")
	private String clientSecret;

	@Autowired
	private ServerConfig serverConfig;

	@Autowired
	private ILoginUserDeptService loginUserDeptService;

	@GetMapping("/auths")
	public String auth(String code) {
		if (StringUtils.isEmpty(code)) {
			return "redirect:" + authurl + "/authorize?redirect_uri=" + serverConfig.getUrl()
					+ "/auth&response_type=code&client_id=" + clientId;
		}
		JSONObject result = send(1, "/getToken", "client_id=" + clientId + "&grant_type=authorization_code&code=" + code
				+ "&client_secret=" + clientSecret);
		String accessToken = result.getString("access_token");
		result = send(2, "/getUserInfo", "client_id=" + clientId + "&access_token=" + accessToken);
		String loginName = result.getString("loginName");
		String displayName = result.getString("displayName");
		AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginName, Constants.LOGIN_SUCCESS,
				MessageUtils.message("user.login.success")));
		LoginUserDept loginUserDept = loginUserDeptService.selectLoginUserDeptByLoginName(loginName);
		if (loginUserDept != null) {
			loginUserDept.setDisplayName(displayName);
			loginUserDeptService.updateLoginUserDept(loginUserDept);
		} else {
			loginUserDept = new LoginUserDept();
			loginUserDept.setCreateBy(loginName);
			loginUserDept.setUpdateTime(new Date());
			loginUserDept.setLoginName(loginName);
			loginUserDept.setDisplayName(displayName);
			loginUserDeptService.insertLoginUserDept(loginUserDept);
		}
		getSession().setAttribute("loginName", loginName);
		getSession().setAttribute("displayName", displayName);
		getSession().setAttribute("plats", loginUserDept.getDept());
		return "redirect:/index/v/sum";
	}

	private JSONObject send(int type, String url, String param) {
		String result;
		if (type == 1) {
			result = HttpUtils.sendPost(authurl + url, param);

		} else {
			result = HttpUtils.sendGet(authurl + url, param);
		}
		return JSONObject.parseObject(result);
	}
}
